Debian Codegraph @

current/structures/IntHashSet.java

raw symbols SCIP
1 package structures;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashSet;
6 import java.util.Random;
7
8 import shared.KillSwitch;
9 import shared.Primes;
10 import shared.Shared;
11 import shared.Timer;
12 import shared.Tools;
13
14 /**
15 * @author Brian Bushnell
16 * @date June 7, 2017
17 *
18 */
19 public class IntHashSet{
20
21 public static void main(String[] args){
22 Random randy2=Shared.threadLocalRandom();
23 IntHashSet set=new IntHashSet(20, 0.7f);
24 HashSet<Integer> set2=new HashSet<Integer>(20, 0.7f);
25 ArrayList<Integer> list=new ArrayList<Integer>();
26 ArrayList<Integer> list2=new ArrayList<Integer>();
27 for(int i=0; i<1000; i++){
28 assert(!set.contains(i));
29 assert(!set2.contains(i));
30 list.add(Integer.valueOf(i));
31 }
32 for(int i=0; i<1000; i++){
33 int r=randy2.nextInt();
34 list2.add(r);
35 }
36
37 for(int x : list){
38 set.add(x);
39 set2.add(x);
40 assert(set.contains(x));
41 assert(set2.contains(x));
42 }
43
44 for(int x : list){
45 assert(set.contains(x));
46 assert(set2.contains(x));
47 set.remove(x);
48 set2.remove(x);
49 assert(!set.contains(x));
50 assert(!set2.contains(x));
51 }
52 assert(set.isEmpty());
53 assert(set2.isEmpty());
54
55 for(int x : list2){
56 set.add(x);
57 set2.add(x);
58 assert(set.contains(x));
59 assert(set2.contains(x));
60 }
61
62 for(int x : list2){
63 assert(set.contains(x));
64 assert(set2.contains(x));
65 set.remove(x);
66 set2.remove(x);
67 assert(!set.contains(x));
68 assert(!set2.contains(x));
69 }
70 assert(set.isEmpty());
71 assert(set2.isEmpty());
72
73 int count=4000000;
74 int runs=32;
75 IntList ll=new IntList(count);
76 for(int i=0; i<count; i++){ll.add(randy2.nextInt());}
77
78 Shared.printMemory();
79 Timer t=new Timer();
80 for(int k=0; k<2; k++){
81 System.err.println("IntHashSet:");
82 t.start();
83 for(int i=0; i<runs; i++){
84 // for(int x : ll.array){
85 // set.add(x);
86 // }
87 final int[] y=ll.array;
88 for(int z=0; z<count; z++){
89 final int value=y[z];
90 set.add(value);
91 set.contains(value);
92 set.remove(value);
93 set.add(value);
94 }
95 // for(int x : ll.array){
96 // set.remove(x);
97 // }
98 // set.clear();
99 // assert(set.isEmpty());
100 // System.err.println("Finished run "+i);
101 }
102 t.stop();
103 System.err.println(t);
104 Shared.printMemory();
105
106 // System.err.println("HashSet:");
107 // t.start();
108 // for(int i=0; i<runs; i++){
109 // for(int x : ll.array){
110 // set2.add(x);
111 // }
112 // for(int x : ll.array){
113 // set2.remove(x);
114 // }
115 // assert(set2.isEmpty());
116 //// System.err.println("Finished run "+i);
117 // }
118 // t.stop();
119 // System.err.println(t);
120 // Shared.printMemory();
121 }
122 t.stop();
123 }
124
125 /*--------------------------------------------------------------*/
126 /*---------------- Initialization ----------------*/
127 /*--------------------------------------------------------------*/
128
129 public IntHashSet(){
130 this(256);
131 }
132
133 public IntHashSet(int initialSize){
134 this(initialSize, 0.7f);
135 }
136
137 public IntHashSet(int initialSize, float loadFactor_){
138 invalid=randy.nextInt()|MINMASK;
139 assert(invalid<0);
140 assert(initialSize>0);
141 assert(loadFactor_>0 && loadFactor_<1);
142 loadFactor=Tools.mid(0.25f, loadFactor_, 0.90f);
143 resize(initialSize);
144 }
145
146 /*--------------------------------------------------------------*/
147 /*---------------- Public Methods ----------------*/
148 /*--------------------------------------------------------------*/
149
150 public void clear(){
151 if(size<1){return;}
152 Arrays.fill(array, invalid);
153 size=0;
154 }
155
156 public boolean contains(int value){
157 return value==invalid ? false : findCell(value)>=0;
158 }
159
160 /**
161 * Add this value to the set.
162 * @param value
163 * @return true if the value was added, false if it was already contained.
164 */
165 public boolean add(int value){
166 if(value==invalid){resetInvalid();}
167 int cell=findCellOrEmpty(value);
168 if(array[cell]==invalid){
169 array[cell]=value;
170 size++;
171 if(size>sizeLimit){resize();}
172 return true;
173 }
174 assert(array[cell]==value);
175 return false;
176 }
177
178 /**
179 * Add this list to the set.
180 * @param list
181 * @return Number of elements added.
182 */
183 public int addAll(IntList list){
184 int added=0;
185 for(int i=0; i<list.size; i++){
186 boolean b=add(list.get(i));
187 if(b){added++;}
188 }
189 return added;
190 }
191
192 //Does not get added to the list in an IntHashSetList. For rehashing. Cannot be overriden.
193 protected final boolean addSpecial(int value){
194 if(value==invalid){resetInvalid();}
195 int cell=findCellOrEmpty(value);
196 if(array[cell]==invalid){
197 array[cell]=value;
198 size++;
199 if(size>sizeLimit){resize();}
200 return true;
201 }
202 assert(array[cell]==value);
203 return false;
204 }
205
206 /**
207 * Remove this value from the set.
208 * @param value
209 * @return true if the value was removed, false if it was not present.
210 */
211 public boolean remove(int value){
212 if(value==invalid){return false;}
213 final int cell=findCell(value);
214 if(cell<0){return false;}
215 assert(array[cell]==value);
216 array[cell]=invalid;
217 size--;
218
219 rehashFrom(cell);
220 return true;
221 }
222
223 public int size(){return size;}
224 public int sizeLimit(){return sizeLimit;}
225
226 public boolean isEmpty(){return size==0;}
227
228 /*--------------------------------------------------------------*/
229 /*---------------- String Methods ----------------*/
230 /*--------------------------------------------------------------*/
231
232 @Override
233 public String toString(){
234 return toStringListView();
235 }
236
237 public String toStringSetView(){
238 StringBuilder sb=new StringBuilder();
239 sb.append('[');
240 String comma="";
241 for(int i=0; i<array.length; i++){
242 if(array[i]!=invalid){
243 sb.append(comma+"("+i+", "+array[i]+")");
244 comma=", ";
245 }
246 }
247 sb.append(']');
248 return sb.toString();
249 }
250
251 public String toStringListView(){
252 StringBuilder sb=new StringBuilder();
253 sb.append('[');
254 String comma="";
255 for(int i=0; i<array.length; i++){
256 if(array[i]!=invalid){
257 sb.append(comma+array[i]);
258 comma=", ";
259 }
260 }
261 sb.append(']');
262 return sb.toString();
263 }
264
265 public int[] toArray(){
266 int[] x=new int[size];
267 int i=0;
268 for(int v : array){
269 if(v!=invalid){
270 x[i]=v;
271 i++;
272 }
273 }
274 return x;
275 }
276
277 /*--------------------------------------------------------------*/
278 /*---------------- Private Methods ----------------*/
279 /*--------------------------------------------------------------*/
280
281 public boolean verify(){
282 int numValues=0;
283 int numFound=0;
284 for(int i=0; i<array.length; i++){
285 final int value=array[i];
286 if(value!=invalid){
287 numValues++;
288 final int cell=findCell(i);
289 if(i==cell){
290 numFound++;
291 }else{
292 return false;
293 }
294 }
295 }
296 return numValues==numFound && numValues==size;
297 }
298
299 private void rehashFrom(int initial){
300 if(size<1){return;}
301 final int limit=array.length;
302 for(int cell=initial+1; cell<limit; cell++){
303 final int x=array[cell];
304 if(x==invalid){return;}
305 rehashCell(cell);
306 }
307 for(int cell=0; cell<initial; cell++){
308 final int x=array[cell];
309 if(x==invalid){return;}
310 rehashCell(cell);
311 }
312 }
313
314 private boolean rehashCell(final int cell){
315 final int value=array[cell];
316 assert(value!=invalid);
317 if(value==invalid){resetInvalid();}
318 final int dest=findCellOrEmpty(value);
319 if(cell==dest){return false;}
320 assert(array[dest]==invalid);
321 array[cell]=invalid;
322 array[dest]=value;
323 return true;
324 }
325
326 private void resetInvalid(){
327 final int old=invalid;
328 int x=invalid;
329 while(x==old || contains(x)){x=randy.nextInt()|MINMASK;}
330 assert(x<0);
331 invalid=x;
332 for(int i=0; i<array.length; i++){
333 if(array[i]==old){array[i]=invalid;}
334 }
335 }
336
337 private int findCell(final int value){
338 if(value==invalid){return -1;}
339
340 final int limit=array.length, initial=(int)((value&MASK)%modulus);
341 for(int cell=initial; cell<limit; cell++){
342 final int x=array[cell];
343 if(x==value){return cell;}
344 if(x==invalid){return -1;}
345 }
346 for(int cell=0; cell<initial; cell++){
347 final int x=array[cell];
348 if(x==value){return cell;}
349 if(x==invalid){return -1;}
350 }
351 return -1;
352 }
353
354 private int findCellOrEmpty(final int value){
355 assert(value!=invalid) : "Collision - this should have been intercepted.";
356
357 final int limit=array.length, initial=(int)((value&MASK)%modulus);
358 for(int cell=initial; cell<limit; cell++){
359 final int x=array[cell];
360 if(x==value || x==invalid){return cell;}
361 }
362 for(int cell=0; cell<initial; cell++){
363 final int x=array[cell];
364 if(x==value || x==invalid){return cell;}
365 }
366 throw new RuntimeException("No empty cells - size="+size+", limit="+limit);
367 }
368
369 private final void resize(){
370 assert(size>=sizeLimit);
371 resize(array.length*2L+1);
372 }
373
374 private final void resize(final long size2){
375 assert(size2>size) : size+", "+size2;
376 long newPrime=Primes.primeAtLeast(size2);
377 if(newPrime+extra>Integer.MAX_VALUE){
378 newPrime=Primes.primeAtMost(Integer.MAX_VALUE-extra);
379 }
380 assert(newPrime>modulus) : "Overflow: "+size+", "+size2+", "+modulus+", "+newPrime;
381 modulus=(int)newPrime;
382
383 final int size3=(int)(newPrime+extra);
384 sizeLimit=(int)(modulus*loadFactor);
385 final int[] old=array;
386 array=KillSwitch.allocInt1D(size3);
387 Arrays.fill(array, invalid);
388
389 // System.err.println("Resizing "+(old==null ? "null" : ""+old.length)+" to "+size3);
390
391 if(size<1){return;}
392
393 size=0;
394 for(int value : old){
395 if(value!=invalid){
396 addSpecial(value);
397 }
398 }
399 }
400
401 /*--------------------------------------------------------------*/
402 /*---------------- Fields ----------------*/
403 /*--------------------------------------------------------------*/
404
405 private int[] array;
406 private int size=0;
407 /** Value for empty cells */
408 private int invalid;
409 private int modulus;
410 private int sizeLimit;
411 private final float loadFactor;
412
413 private static final Random randy=new Random(1);
414 private static final int MASK=Integer.MAX_VALUE;
415 private static final int MINMASK=Integer.MIN_VALUE;
416
417 private static final int extra=10;
418
419 }