Debian Codegraph @

current/structures/LongHashSet.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 July 6, 2016
17 *
18 */
19 public final class LongHashSet{
20
21 public static void main(String[] args){
22 Random randy2=Shared.threadLocalRandom();
23 LongHashSet set=new LongHashSet(20, 0.7f);
24 HashSet<Long> set2=new HashSet<Long>(20, 0.7f);
25 ArrayList<Long> list=new ArrayList<Long>();
26 ArrayList<Long> list2=new ArrayList<Long>();
27 for(long i=0; i<1000; i++){
28 assert(!set.contains(i));
29 assert(!set2.contains(i));
30 list.add(Long.valueOf(i));
31 }
32 for(int i=0; i<1000; i++){
33 long r=randy2.nextLong();
34 list2.add(r);
35 }
36
37 for(long x : list){
38 set.add(x);
39 set2.add(x);
40 assert(set.contains(x));
41 assert(set2.contains(x));
42 }
43
44 for(long 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(long x : list2){
56 set.add(x);
57 set2.add(x);
58 assert(set.contains(x));
59 assert(set2.contains(x));
60 }
61
62 for(long 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 LongList ll=new LongList(count);
76 for(int i=0; i<count; i++){ll.add(randy2.nextLong());}
77
78 Shared.printMemory();
79 Timer t=new Timer();
80 for(int k=0; k<2; k++){
81 System.err.println("LongHashSet:");
82 t.start();
83 for(int i=0; i<runs; i++){
84 // for(long x : ll.array){
85 // set.add(x);
86 // }
87 final long[] y=ll.array;
88 for(int z=0; z<count; z++){
89 final long value=y[z];
90 set.add(value);
91 set.contains(value);
92 set.remove(value);
93 set.add(value);
94 }
95 // for(long 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(long x : ll.array){
110 // set2.add(x);
111 // }
112 // for(long 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 LongHashSet(){
130 this(256);
131 }
132
133 public LongHashSet(int initialSize){
134 this(initialSize, 0.7f);
135 }
136
137 public LongHashSet(int initialSize, float loadFactor_){
138 invalid=randy.nextLong()|MINMASK;
139 assert(invalid<0);
140 assert(initialSize>0) : "Attempting to initialize a "+getClass().getSimpleName()+" of size<1.";
141 assert(loadFactor_>0 && loadFactor_<1) : "Attempting to initialize a "+getClass().getSimpleName()+" with invalid load factor: "+loadFactor_;
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(long 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(long 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 * Remove this value from the set.
180 * @param value
181 * @return true if the value was removed, false if it was not present.
182 */
183 public boolean remove(long value){
184 if(value==invalid){return false;}
185 final int cell=findCell(value);
186 if(cell<0){return false;}
187 assert(array[cell]==value);
188 array[cell]=invalid;
189 size--;
190
191 rehashFrom(cell);
192 return true;
193 }
194
195 public int size(){return size;}
196
197 public boolean isEmpty(){return size==0;}
198
199 /*--------------------------------------------------------------*/
200 /*---------------- String Methods ----------------*/
201 /*--------------------------------------------------------------*/
202
203 @Override
204 public String toString(){
205 return toStringListView();
206 }
207
208 public String toStringSetView(){
209 StringBuilder sb=new StringBuilder();
210 sb.append('[');
211 String comma="";
212 for(int i=0; i<array.length; i++){
213 if(array[i]!=invalid){
214 sb.append(comma+"("+i+", "+array[i]+")");
215 comma=", ";
216 }
217 }
218 sb.append(']');
219 return sb.toString();
220 }
221
222 public String toStringListView(){
223 StringBuilder sb=new StringBuilder();
224 sb.append('[');
225 String comma="";
226 for(int i=0; i<array.length; i++){
227 if(array[i]!=invalid){
228 sb.append(comma+array[i]);
229 comma=", ";
230 }
231 }
232 sb.append(']');
233 return sb.toString();
234 }
235
236 public long[] toArray(){
237 long[] x=new long[array.length];
238 int i=0;
239 for(long v : array){
240 x[i]=v;
241 i++;
242 }
243 return x;
244 }
245
246 /*--------------------------------------------------------------*/
247 /*---------------- Private Methods ----------------*/
248 /*--------------------------------------------------------------*/
249
250 public boolean verify(){
251 int numValues=0;
252 int numFound=0;
253 for(int i=0; i<array.length; i++){
254 final long value=array[i];
255 if(value!=invalid){
256 numValues++;
257 final int cell=findCell(value);
258 if(i==cell){
259 numFound++;
260 }else{
261 return false;
262 }
263 }
264 }
265 return numValues==numFound && numValues==size;
266 }
267
268 private void rehashFrom(int initial){
269 if(size<1){return;}
270 final int limit=array.length;
271 for(int cell=initial+1; cell<limit; cell++){
272 final long x=array[cell];
273 if(x==invalid){return;}
274 rehashCell(cell);
275 }
276 for(int cell=0; cell<initial; cell++){
277 final long x=array[cell];
278 if(x==invalid){return;}
279 rehashCell(cell);
280 }
281 }
282
283 private boolean rehashCell(final int cell){
284 final long value=array[cell];
285 assert(value!=invalid);
286 if(value==invalid){resetInvalid();}
287 final int dest=findCellOrEmpty(value);
288 if(cell==dest){return false;}
289 assert(array[dest]==invalid);
290 array[cell]=invalid;
291 array[dest]=value;
292 return true;
293 }
294
295 private void resetInvalid(){
296 final long old=invalid;
297 long x=invalid;
298 while(x==old || contains(x)){x=randy.nextLong()|MINMASK;}
299 assert(x<0);
300 invalid=x;
301 for(int i=0; i<array.length; i++){
302 if(array[i]==old){array[i]=invalid;}
303 }
304 }
305
306 private int findCell(final long value){
307 if(value==invalid){return -1;}
308
309 final int limit=array.length, initial=(int)((value&MASK)%modulus);
310 for(int cell=initial; cell<limit; cell++){
311 final long x=array[cell];
312 if(x==value){return cell;}
313 if(x==invalid){return -1;}
314 }
315 for(int cell=0; cell<initial; cell++){
316 final long x=array[cell];
317 if(x==value){return cell;}
318 if(x==invalid){return -1;}
319 }
320 return -1;
321 }
322
323 private int findCellOrEmpty(final long value){
324 assert(value!=invalid) : "Collision - this should have been intercepted.";
325
326 final int limit=array.length, initial=(int)((value&MASK)%modulus);
327 for(int cell=initial; cell<limit; cell++){
328 final long x=array[cell];
329 if(x==value || x==invalid){return cell;}
330 }
331 for(int cell=0; cell<initial; cell++){
332 final long x=array[cell];
333 if(x==value || x==invalid){return cell;}
334 }
335 throw new RuntimeException("No empty cells - size="+size+", limit="+limit);
336 }
337
338 private final void resize(){
339 assert(size>=sizeLimit);
340 resize(array.length*2L+1);
341 }
342
343 private final void resize(final long size2){
344 assert(size2>size) : size+", "+size2;
345 long newPrime=Primes.primeAtLeast(size2);
346 if(newPrime+extra>Integer.MAX_VALUE){
347 newPrime=Primes.primeAtMost(Integer.MAX_VALUE-extra);
348 }
349 assert(newPrime>modulus) : "Overflow: "+size+", "+size2+", "+modulus+", "+newPrime;
350 modulus=(int)newPrime;
351
352 final int size3=(int)(newPrime+extra);
353 sizeLimit=(int)(modulus*loadFactor);
354 final long[] old=array;
355 array=KillSwitch.allocLong1D(size3);
356 Arrays.fill(array, invalid);
357
358 // System.err.println("Resizing "+(old==null ? "null" : ""+old.length)+" to "+size3);
359
360 if(size<1){return;}
361
362 size=0;
363 for(long value : old){
364 if(value!=invalid){
365 add(value);
366 }
367 }
368 }
369
370 public final void resizeDestructive(int newSize){
371 size=0;
372 sizeLimit=0;
373 array=null;
374 resize(newSize);
375 }
376
377 /** Number of values that can be held without resizing */
378 public int capacity(){
379 return sizeLimit;
380 }
381
382
383 /*--------------------------------------------------------------*/
384 /*---------------- Fields ----------------*/
385 /*--------------------------------------------------------------*/
386
387 private long[] array;
388 private int size=0;
389 /** Value for empty cells */
390 private long invalid;
391 private int modulus;
392 private int sizeLimit;
393 private final float loadFactor;
394
395 private static final Random randy=new Random(1);
396 private static final long MASK=Long.MAX_VALUE;
397 private static final long MINMASK=Long.MIN_VALUE;
398
399 private static final int extra=10;
400
401
402 }