Repository navigation
Expand file tree
/
Copy pathApp.java
More file actions
217 lines (189 loc) · 7.08 KB
/
Copy pathApp.java
File metadata and controls
217 lines (189 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.Random;
import java.util.Collections;
public class App {
static long RandomSeed = 1;
static Random RandomGenerator = new Random(RandomSeed);
private static ArrayList<Integer> deDuplicateAndScramble(ArrayList<Integer> list) {
TreeSet<Integer> deDuped = new TreeSet<Integer>(list);
ArrayList<Integer> outList = new ArrayList<Integer>(deDuped);
// Collections.shuffle(outList);
return outList;
}
private static ArrayList<Integer> generateIntArrayList(int howMany) {
ArrayList<Integer> list = new ArrayList<Integer>(howMany);
for (int i = 0; i < howMany; i++) {
list.add(Integer.valueOf(RandomGenerator.nextInt(1000000000)));
}
list = deDuplicateAndScramble(list);
return list;
}
private static ArrayList<Integer> generateStrikeList(ArrayList<Integer> fromList, int howMany) {
ArrayList<Integer> strikeList = new ArrayList<Integer>(howMany);
int fromLast = fromList.size() - 1;
for (int i = 0; i < howMany; i++) {
strikeList.add(fromList.get(RandomGenerator.nextInt(fromLast)));
}
strikeList = deDuplicateAndScramble(strikeList);
return strikeList;
}
private static ArrayList<Integer> generateRemoveList(ArrayList<Integer> fromList) {
ArrayList<Integer> removeList = new ArrayList<Integer>(fromList.size() / 2);
for (int i = 0; i < fromList.size() / 2; i++) {
removeList.add(fromList.get(i));
}
removeList = deDuplicateAndScramble(removeList);
return removeList;
}
private static <T> int executeFinds(TwoFourTree coll, ArrayList<Integer> strikes) {
boolean sentinel;
int failures = 0;
for (Integer e : strikes) {
sentinel = coll.hasValue(e.intValue());
if (sentinel == false) {
// System.out.printf("\nFailed to find %d", e.intValue());
failures++;
}
}
if (failures > 0) {
System.out.printf("(%,9d missing) ", failures);
}
return 0;
}
private static <T> int executeComparisonFinds(TreeSet<Integer> coll, ArrayList<Integer> strikes) {
boolean sentinel;
int failures = 0;
for (Integer e : strikes) {
sentinel = coll.contains(e);
if (sentinel == false) {
// System.out.printf("\nFailed to find %d", e.intValue());
failures++;
}
}
if (failures > 0) {
System.out.printf("(%,9d missing) ", failures);
}
return 0;
}
public static void executeIntCase(int listSize, int strikeSize, boolean includeRemoves) {
System.out.printf(
"CASE: %,8d integers, %,8d finds, %,8d removals. Generating...\n",
listSize, strikeSize, strikeSize / 2
);
ArrayList<Integer> intlist = generateIntArrayList(listSize);
ArrayList<Integer> strikes = generateStrikeList(intlist, strikeSize);
ArrayList<Integer> removeList = generateRemoveList(strikes);
long start;
long end;
long ms;
TwoFourTree theTree = new TwoFourTree();
System.out.printf(" TwoFourTree ");
start = System.currentTimeMillis();
for (Integer e : intlist) {
theTree.addValue(e.intValue());
}
end = System.currentTimeMillis();
ms = end - start;
System.out.printf("add: %,7dms ", ms);
start = System.currentTimeMillis();
executeFinds(theTree, strikes);
end = System.currentTimeMillis();
ms = end - start;
System.out.printf("find: %,7dms ", ms);
if (includeRemoves) {
start = System.currentTimeMillis();
for (Integer e : removeList) {
theTree.deleteValue(e.intValue());
if (theTree.hasValue(e.intValue())) {
System.out.printf("Failed to delete %d\n", e.intValue());
}
}
end = System.currentTimeMillis();
ms = end - start;
System.out.printf("del: %,7dms ", ms);
start = System.currentTimeMillis();
executeFinds(theTree, strikes);
end = System.currentTimeMillis();
ms = end - start;
System.out.printf("find: %,6dms ", ms);
System.out.printf("(Should be %,9d missing) ", removeList.size());
}
System.out.printf("\n");
TreeSet<Integer> theComparison = new TreeSet<Integer>();
System.out.printf(" TreeSet ");
start = System.currentTimeMillis();
for (Integer e : intlist) {
theComparison.add(e);
}
end = System.currentTimeMillis();
ms = end - start;
System.out.printf("add: %,7dms ", ms);
start = System.currentTimeMillis();
executeComparisonFinds(theComparison, strikes);
end = System.currentTimeMillis();
ms = end - start;
System.out.printf("find: %,7dms ", ms);
if (includeRemoves) {
start = System.currentTimeMillis();
for (Integer e : removeList) {
theComparison.remove(e);
}
end = System.currentTimeMillis();
ms = end - start;
System.out.printf("del: %,7dms ", ms);
start = System.currentTimeMillis();
executeComparisonFinds(theComparison, strikes);
end = System.currentTimeMillis();
ms = end - start;
System.out.printf("find: %,6dms ", ms);
System.out.printf("(Should be %,9d missing) ", removeList.size());
}
System.out.printf("\n");
System.gc();
}
public static void main(String[] args) throws Exception {
TwoFourTree tft = new TwoFourTree();
tft.addValue(2);
tft.addValue(3);
tft.addValue(5);
tft.addValue(7);
tft.addValue(11);
tft.addValue(13);
tft.addValue(17);
tft.addValue(19);
tft.addValue(23);
tft.addValue(29);
tft.addValue(31);
tft.addValue(37);
tft.addValue(41);
tft.addValue(43);
tft.addValue(47);
tft.addValue(53);
tft.addValue(59);
tft.addValue(67);
tft.addValue(71);
tft.addValue(73);
tft.addValue(79);
tft.addValue(83);
tft.addValue(89);
tft.addValue(97);
System.out.println("Static test: first few prime numbers:");
tft.printInOrder();
tft.deleteValue(37);
System.out.println("\nWithout 37:");
tft.printInOrder();
tft.deleteValue(73);
System.out.println("\nWithout 73:");
tft.printInOrder();
tft.deleteValue(97);
System.out.println("\nWithout 97:");
tft.printInOrder();
executeIntCase(100, 20, true);
executeIntCase(1000, 200, true);
executeIntCase(10000, 2000, true);
executeIntCase(100000, 20000, true);
executeIntCase(1000000, 200000, true);
executeIntCase(10000000, 2000000, true);
}
}