1 |
/* |
2 |
* Copyright 2009 Google Inc. All Rights Reserved. |
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
5 |
* This code is free software; you can redistribute it and/or modify it |
6 |
* under the terms of the GNU General Public License version 2 only, as |
7 |
* published by the Free Software Foundation. |
8 |
* |
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
13 |
* accompanied this code). |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License version |
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
* |
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
21 |
* questions. |
22 |
*/ |
23 |
|
24 |
import java.util.Random; |
25 |
import java.math.BigInteger; |
26 |
|
27 |
public enum ArrayBuilder { |
28 |
|
29 |
// These seven are from Tim's paper (listsort.txt) |
30 |
|
31 |
RANDOM_INT { |
32 |
public Object[] build(int len) { |
33 |
Integer[] result = new Integer[len]; |
34 |
for (int i = 0; i < len; i++) |
35 |
result[i] = rnd.nextInt(); |
36 |
return result; |
37 |
} |
38 |
}, |
39 |
|
40 |
DESCENDING_INT { |
41 |
public Object[] build(int len) { |
42 |
Integer[] result = new Integer[len]; |
43 |
for (int i = 0; i < len; i++) |
44 |
result[i] = len - i; |
45 |
return result; |
46 |
} |
47 |
}, |
48 |
|
49 |
ASCENDING_INT { |
50 |
public Object[] build(int len) { |
51 |
Integer[] result = new Integer[len]; |
52 |
for (int i = 0; i < len; i++) |
53 |
result[i] = i; |
54 |
return result; |
55 |
} |
56 |
}, |
57 |
|
58 |
ASCENDING_3_RND_EXCH_INT { |
59 |
public Object[] build(int len) { |
60 |
Integer[] result = new Integer[len]; |
61 |
for (int i = 0; i < len; i++) |
62 |
result[i] = i; |
63 |
for (int i = 0; i < 3; i++) |
64 |
swap(result, rnd.nextInt(result.length), |
65 |
rnd.nextInt(result.length)); |
66 |
return result; |
67 |
} |
68 |
}, |
69 |
|
70 |
ASCENDING_10_RND_AT_END_INT { |
71 |
public Object[] build(int len) { |
72 |
Integer[] result = new Integer[len]; |
73 |
int endStart = len - 10; |
74 |
for (int i = 0; i < endStart; i++) |
75 |
result[i] = i; |
76 |
for (int i = endStart; i < len; i++) |
77 |
result[i] = rnd.nextInt(endStart + 10); |
78 |
return result; |
79 |
} |
80 |
}, |
81 |
|
82 |
ALL_EQUAL_INT { |
83 |
public Object[] build(int len) { |
84 |
Integer[] result = new Integer[len]; |
85 |
for (int i = 0; i < len; i++) |
86 |
result[i] = 666; |
87 |
return result; |
88 |
} |
89 |
}, |
90 |
|
91 |
DUPS_GALORE_INT { |
92 |
public Object[] build(int len) { |
93 |
Integer[] result = new Integer[len]; |
94 |
for (int i = 0; i < len; i++) |
95 |
result[i] = rnd.nextInt(4); |
96 |
return result; |
97 |
} |
98 |
}, |
99 |
|
100 |
RANDOM_WITH_DUPS_INT { |
101 |
public Object[] build(int len) { |
102 |
Integer[] result = new Integer[len]; |
103 |
for (int i = 0; i < len; i++) |
104 |
result[i] = rnd.nextInt(len); |
105 |
return result; |
106 |
} |
107 |
}, |
108 |
|
109 |
PSEUDO_ASCENDING_STRING { |
110 |
public String[] build(int len) { |
111 |
String[] result = new String[len]; |
112 |
for (int i = 0; i < len; i++) |
113 |
result[i] = Integer.toString(i); |
114 |
return result; |
115 |
} |
116 |
}, |
117 |
|
118 |
RANDOM_BIGINT { |
119 |
public BigInteger[] build(int len) { |
120 |
BigInteger[] result = new BigInteger[len]; |
121 |
for (int i = 0; i < len; i++) |
122 |
result[i] = HUGE.add(BigInteger.valueOf(rnd.nextInt(len))); |
123 |
return result; |
124 |
} |
125 |
}; |
126 |
|
127 |
public abstract Object[] build(int len); |
128 |
|
129 |
public void resetRandom() { |
130 |
rnd = new Random(666); |
131 |
} |
132 |
|
133 |
private static Random rnd = new Random(666); |
134 |
|
135 |
private static void swap(Object[] a, int i, int j) { |
136 |
Object t = a[i]; |
137 |
a[i] = a[j]; |
138 |
a[j] = t; |
139 |
} |
140 |
|
141 |
private static BigInteger HUGE = BigInteger.ONE.shiftLeft(100); |
142 |
} |