Debian Codegraph @

current/structures/LongLongListHashMap.java

raw symbols SCIP
1 package structures;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashMap;
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 August 29, 2023
17 *
18 */
19 public final class LongLongListHashMap{
20
21 public static void main(String[] args){
22 Random randy2=Shared.threadLocalRandom();
23 LongLongListHashMap map=new LongLongListHashMap(20, 0.7f);
24 HashMap<Long, Integer> map2=new HashMap<Long, Integer>(20, 0.7f);
25 ArrayList<Long> list=new ArrayList<Long>();
26 ArrayList<Long> list2=new ArrayList<Long>();
27 // ArrayList<Integer> vals=new ArrayList<Integer>();
28 for(long i=0; i<1000; i++){
29 assert(!map.contains(i));
30 assert(!map2.containsKey(i));
31 list.add(Long.valueOf(i));
32 }
33 for(int i=0; i<1000; i++){
34 long r=randy2.nextLong();
35 list2.add(r);
36 }
37
38 for(long x : list){
39 map.put(x, (int)(2*x));
40 map2.put(x, (int)(2*x));
41 assert(map.get(x).contains((int)(2*x)));
42 assert(map2.get(x)==((int)(2*x)));
43 }
44
45 for(long x : list){
46 assert(map.get(x).contains((int)(2*x)));
47 assert(map2.get(x)==((int)(2*x)));
48 map.remove(x);
49 map2.remove(x);
50 assert(!map.contains(x));
51 assert(!map2.containsKey(x));
52 }
53 assert(map.isEmpty());
54 assert(map2.isEmpty());
55
56 for(long x : list2){
57 map.put(x, (int)(2*x));
58 map2.put(x, (int)(2*x));
59 assert(map.get(x).contains((int)(2*x)));
60 assert(map2.get(x)==((int)(2*x)));
61 }
62
63 for(long x : list2){
64 assert(map.get(x).contains((int)(2*x)));
65 assert(map2.get(x)==((int)(2*x)));
66 map.remove(x);
67 map2.remove(x);
68 assert(!map.contains(x));
69 assert(!map2.containsKey(x));
70 }
71 assert(map.isEmpty());
72 assert(map2.isEmpty());
73
74 int count=2000000;
75 int runs=16;
76 LongList ll=new LongList(count);
77 for(int i=0; i<count; i++){ll.add(randy2.nextLong());}
78
79 Shared.printMemory();
80 Timer t=new Timer();
81 for(int k=0; k<2; k++){
82 System.err.println("LongHashMap:");
83 t.start();
84 for(int i=0; i<runs; i++){
85 // for(long x : ll.array){
86 // map.add(x);
87 // }
88 final long[] y=ll.array;
89 for(int z=0; z<count; z++){
90 final long key=y[z];
91 map.put(key, key);
92 map.contains(key);
93 map.remove(key);
94 map.put(key, key);
95 }
96 // for(long x : ll.array){
97 // map.remove(x);
98 // }
99 // map.clear();
100 // assert(map.isEmpty());
101 // System.err.println("Finished run "+i);
102 }
103 t.stop();
104 System.err.println(t);
105 Shared.printMemory();
106
107 System.err.println("HashMap:");
108 t.start();
109 for(int i=0; i<runs; i++){
110 for(long x : ll.array){
111 map2.put(x, (int)x);
112 map2.containsKey(x);
113 map2.remove(x);
114 map2.put(x, (int)x);
115 }
116 // assert(map2.isEmpty());
117 // System.err.println("Finished run "+i);
118 }
119 t.stop();
120 System.err.println(t);
121 Shared.printMemory();
122 }
123 t.stop();
124 }
125
126 /*--------------------------------------------------------------*/
127 /*---------------- Initialization ----------------*/
128 /*--------------------------------------------------------------*/
129
130 public LongLongListHashMap(){
131 this(256);
132 }
133
134 public LongLongListHashMap(int initialSize){
135 this(initialSize, 0.7f);
136 }
137
138 public LongLongListHashMap(int initialSize, float loadFactor_){
139 invalid=randy.nextLong()|MINMASK;
140 assert(invalid<0);
141 assert(initialSize>0);
142 assert(loadFactor_>0 && loadFactor_<1);
143 loadFactor=Tools.mid(0.25f, loadFactor_, 0.90f);
144 resize(initialSize);
145 }
146
147 /*--------------------------------------------------------------*/
148 /*---------------- Public Methods ----------------*/
149 /*--------------------------------------------------------------*/
150
151 public void clear(){
152 if(size<1){return;}
153 for(int i=0; i<values.length /*&& size>0*/; i++) {//TODO: Enable the early exit
154 if(keys[i]!=invalid) {
155 keys[i]=invalid;
156 values[i].clear();
157 if(values[i].capacity()>10) {values[i]=null;}//Recycle long lists
158 size--;
159 }
160 assert(values[i]==null || values[i].isEmpty());
161 }
162 assert(size==0);
163 size=0;
164 // assert(verify()); //123
165 }
166
167 public boolean contains(long key){
168 // assert(verify()); //123
169 return key==invalid ? false : findCell(key)>=0;
170 }
171
172 public boolean containsKey(long key){
173 return contains(key);
174 }
175
176 public LongList get(long key){
177 // assert(verify()); //123
178 final int cell=findCell(key);
179 return cell<0 ? null : values[cell];
180 }
181
182 /**
183 * Map this key to value.
184 * @param key
185 * @param value
186 * @return true if the key was added, false if it was already contained.
187 */
188 public boolean put(long key, long value){
189 // assert(verify()); //123
190 if(key==invalid){resetInvalid();}
191 int cell=findCellOrEmpty(key);
192 if(keys[cell]==invalid){
193 keys[cell]=key;
194 if(values[cell]==null) {
195 values[cell]=new LongList(2);
196 }
197 values[cell].add(value);
198 size++;
199 if(size>sizeLimit){resize();}
200 return true;
201 }
202 values[cell].add(value);
203 return false;
204 }
205
206 /**
207 * Map this key to value.
208 * @param key
209 * @param value
210 * @return true if the key was added, false if it was already contained.
211 */
212 public boolean put(long key, LongList value){
213 // assert(verify()); //123
214 if(key==invalid){resetInvalid();}
215 int cell=findCellOrEmpty(key);
216 if(keys[cell]==invalid){
217 keys[cell]=key;
218 if(values[cell]==null) {
219 values[cell]=new LongList(2);
220 }
221 values[cell].addAll(value);
222 size++;
223 if(size>sizeLimit){resize();}
224 return true;
225 }
226 values[cell].addAll(value);
227 return false;
228 }
229
230 /**
231 * Remove this key from the map.
232 * @param key
233 * @return true if it was present
234 */
235 public boolean remove(long key){
236 // assert(verify()); //123
237 if(key==invalid){return false;}
238 final int cell=findCell(key);
239 if(cell<0){return false;}
240 assert(keys[cell]==key);
241 keys[cell]=invalid;
242 values[cell].clear();
243 if(values[cell].capacity()>=10) {values[cell]=null;}
244 size--;
245
246 rehashFrom(cell);
247 // assert(verify()); //123
248 return true;
249 }
250
251 public int size(){return size;}
252
253 public boolean isEmpty(){return size==0;}
254
255 /*--------------------------------------------------------------*/
256 /*---------------- String Methods ----------------*/
257 /*--------------------------------------------------------------*/
258
259 @Override
260 public String toString(){
261 return toStringListView();
262 }
263
264 public String toStringSetView(){
265 StringBuilder sb=new StringBuilder();
266 sb.append('[');
267 String comma="";
268 for(int i=0; i<keys.length; i++){
269 if(keys[i]!=invalid){
270 sb.append(comma+"("+i+", "+keys[i]+", "+values[i]+")");
271 comma=", ";
272 }
273 }
274 sb.append(']');
275 return sb.toString();
276 }
277
278 public String toStringListView(){
279 StringBuilder sb=new StringBuilder();
280 sb.append('[');
281 String comma="";
282 for(int i=0; i<keys.length; i++){
283 if(keys[i]!=invalid){
284 sb.append(comma+keys[i]);
285 comma=", ";
286 }
287 }
288 sb.append(']');
289 return sb.toString();
290 }
291
292 public long[] toArray(){
293 long[] x=KillSwitch.allocLong1D(size);
294 int i=0;
295 for(long key : keys){
296 if(key!=invalid){
297 x[i]=key;
298 i++;
299 }
300 }
301 return x;
302 }
303
304 /*--------------------------------------------------------------*/
305 /*---------------- Private Methods ----------------*/
306 /*--------------------------------------------------------------*/
307
308 public boolean verify(){
309 if(keys==null){return true;}
310 int numValues=0;
311 int numFound=0;
312 for(int i=0; i<keys.length; i++){
313 final long key=keys[i];
314 final LongList value=values[i];
315
316 if(key==invalid){
317 if(value!=null && !value.isEmpty()){
318 assert(false) : i+", "+key+", "+value;
319 return false;
320 }
321 }else{
322 numValues++;
323 if(value==null || value.isEmpty()){
324 assert(false) : i+", "+key+", "+value;
325 return false;
326 }
327 final int cell=findCell(key);
328 if(i==cell){
329 numFound++;
330 }else{
331 assert(false) : i+", "+key+", "+value+", "+cell+"\n"+((cell>=0) ? keys[cell]+", "+values[cell]+"\n" : "");
332 return false;
333 }
334 }
335 }
336 boolean pass=(numValues==numFound && numValues==size);
337 assert(pass) : numValues+", "+numFound+", "+size;
338 return pass;
339 }
340
341 private void rehashFrom(int initial){
342 if(size<1){return;}
343 final int limit=keys.length;
344 for(int cell=initial+1; cell<limit; cell++){
345 final long x=keys[cell];
346 if(x==invalid){return;}
347 rehashCell(cell);
348 }
349 for(int cell=0; cell<initial; cell++){
350 final long x=keys[cell];
351 if(x==invalid){return;}
352 rehashCell(cell);
353 }
354 }
355
356 private boolean rehashCell(final int sourceCell){
357 final long key=keys[sourceCell];
358 final LongList value=values[sourceCell];
359 assert(key!=invalid);
360 if(key==invalid){resetInvalid();}
361 final int destCell=findCellOrEmpty(key);
362 if(sourceCell==destCell){return false;}
363 assert(keys[destCell]==invalid);
364 keys[sourceCell]=invalid;
365 values[sourceCell]=values[destCell];
366 keys[destCell]=key;
367 values[destCell]=value;
368 return true;
369 }
370
371 private void resetInvalid(){
372 final long old=invalid;
373 long x=invalid;
374 while(x==old || contains(x)){x=randy.nextLong()|MINMASK;}
375 assert(x<0);
376 invalid=x;
377 for(int i=0; i<keys.length; i++){
378 if(keys[i]==old){
379 assert(values[i]==null || values[i].isEmpty());
380 keys[i]=invalid;
381 }
382 }
383 }
384
385 private int findCell(final long key){
386 if(key==invalid){return -1;}
387
388 final int limit=keys.length, initial=(int)((key&MASK)%modulus);
389 for(int cell=initial; cell<limit; cell++){
390 final long x=keys[cell];
391 if(x==key){return cell;}
392 if(x==invalid){return -1;}
393 }
394 for(int cell=0; cell<initial; cell++){
395 final long x=keys[cell];
396 if(x==key){return cell;}
397 if(x==invalid){return -1;}
398 }
399 return -1;
400 }
401
402 private int findCellOrEmpty(final long key){
403 assert(key!=invalid) : "Collision - this should have been intercepted.";
404
405 final int limit=keys.length, initial=(int)((key&MASK)%modulus);
406 for(int cell=initial; cell<limit; cell++){
407 final long x=keys[cell];
408 if(x==key || x==invalid){return cell;}
409 }
410 for(int cell=0; cell<initial; cell++){
411 final long x=keys[cell];
412 if(x==key || x==invalid){return cell;}
413 }
414 throw new RuntimeException("No empty cells - size="+size+", limit="+limit);
415 }
416
417 private final void resize(){
418 assert(size>=sizeLimit);
419 resize(keys.length*2L+1);
420 }
421
422 private final void resize(final long size2){
423 // assert(verify()); //123
424 assert(size2>size) : size+", "+size2;
425 long newPrime=Primes.primeAtLeast(size2);
426 if(newPrime+extra>Integer.MAX_VALUE){
427 newPrime=Primes.primeAtMost(Integer.MAX_VALUE-extra);
428 }
429 assert(newPrime>modulus) : "Overflow: "+size+", "+size2+", "+modulus+", "+newPrime;
430 modulus=(int)newPrime;
431
432 final int size3=(int)(newPrime+extra);
433 sizeLimit=(int)(modulus*loadFactor);
434 final long[] oldKeys=keys;
435 final LongList[] oldValues=values;
436 keys=KillSwitch.allocLong1D(size3);
437 values=KillSwitch.allocObject1D(size3, LongList.class);
438 Arrays.fill(keys, invalid);
439
440 // System.err.println("Resizing "+(old==null ? "null" : ""+old.length)+" to "+size3);
441
442 if(size<1){return;}
443
444 size=0;
445 for(int i=0; i<oldKeys.length; i++){
446 long key=oldKeys[i];
447 if(key!=invalid){
448 put(key, oldValues[i]);
449 }
450 }
451 // assert(verify()); //123
452 }
453
454 /*--------------------------------------------------------------*/
455 /*---------------- Getters ----------------*/
456 /*--------------------------------------------------------------*/
457
458 public long[] keys() {return keys;}
459
460 public LongList[] values() {return values;}
461
462 public long invalid() {return invalid;}
463
464 /*--------------------------------------------------------------*/
465 /*---------------- Fields ----------------*/
466 /*--------------------------------------------------------------*/
467
468 private long[] keys;
469 private LongList[] values;
470 // private LongList keyList;//TODO: Store keys here for quick clearing, perhaps
471 private int size=0;
472 /** Value for empty cells */
473 private long invalid;
474 private int modulus;
475 private int sizeLimit;
476 private final float loadFactor;
477
478 private static final Random randy=new Random(1);
479 private static final long MASK=Long.MAX_VALUE;
480 private static final long MINMASK=Long.MIN_VALUE;
481
482 private static final int extra=10;
483
484 }