KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > benchmark > DataSetShuffledCompact


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2003 Søren Bak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj.benchmark;
20
21 import bak.pcj.list.IntList;
22
23 /**
24  * This class represents a standard data set for benchmarks.
25  * The data set contains three disjoint lists each containing
26  * a range of consecutive int values in shuffled order:
27  * <pre>
28  * shuffle([0 - (size-1)])
29  * shuffle([(size) - (2*size-1)])
30  * shuffle([(2*size) - (3*size-1)])
31  * <pre>
32  *
33  * @author S&oslash;ren Bak
34  * @version 1.0 2003/5/1
35  * @since 1.0
36  */

37 public class DataSetShuffledCompact extends DataSet {
38
39     /**
40      * Creates a new shuffled compact data set of a specified
41      * size.
42      *
43      * @param size
44      * the size of the data set to create.
45      *
46      * @throws IllegalArgumentException
47      * if <tt>size</tt> is not positive.
48      */

49     public DataSetShuffledCompact(int size) {
50         super("Shuffled/Compact/"+size, size);
51     }
52
53     protected int[] createList(int n, int size) {
54         int[] s = new int[size];
55         int seed;
56         switch (n) {
57         case 0:
58             for (int i = 0; i < size; i++) s[i] = i;
59             seed = 107;
60             break;
61         case 1:
62             for (int i = 0; i < size; i++) s[i] = i+size;
63             seed = 938361;
64             break;
65         case 2:
66             for (int i = 0; i < size; i++) s[i] = i+2*size;
67             seed = 53925253;
68             break;
69         default:
70             throw new IllegalArgumentException JavaDoc();
71         }
72         shuffle(s, seed);
73         return s;
74     }
75
76     /**
77      * Shuffles an int list with a specified random seed value.
78      *
79      * @param list
80      * the list to shuffle.
81      *
82      * @param seed
83      * the random seed to use when shuffling.
84      *
85      * @throws NullPointerException
86      * if <tt>list</tt> is <tt>null</tt>.
87      */

88     private static void shuffle(int[] list, int seed) {
89         java.util.Random JavaDoc rnd = new java.util.Random JavaDoc(seed);
90         int size = list.length;
91         for (int i = 0; i < size; i++) {
92             int idx = rnd.nextInt(size);
93             int tmp = list[i];
94             list[i] = list[idx];
95             list[idx] = tmp;
96         }
97     }
98
99 }
Popular Tags