Debian Codegraph @

current/structures/IntHashMap.java

raw symbols SCIP
1 package structures;
2
3 import java.io.Serializable;
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 June 7, 2017
17 *
18 */
19 public final class IntHashMap extends AbstractIntHashMap implements Serializable {
20
21 /**
22 *
23 */
24 private static final long serialVersionUID = 5525726007591609843L;
25
26 public static void main(String[] args){
27 // IntHashMap set=new IntHashMap(20, 0.7f);
28 // test(set);
29 int size=args.length>0 ? Integer.parseInt(args[0]) : 100000000;
30 bench(size);
31 }
32
33 private static void bench(int size){
34 System.gc();
35 Timer t=new Timer();
36
37 {
38 System.err.println("\nIntHashMap:");
39 Shared.printMemory();
40 t.start();
41 IntHashMap map=new IntHashMap();
42 for(int i=0; i<size; i++){
43 map.put(i, 2*i);
44 }
45 t.stop("Time: \t");
46 System.gc();
47 Shared.printMemory();
48 map=null;
49 System.gc();
50 }
51
52 {
53 System.err.println("\nHashMap<Integer, Integer>:");
54 Shared.printMemory();
55 t.start();
56 HashMap<Integer, Integer> map=new HashMap<Integer, Integer>();
57 for(int i=0; i<size; i++){
58 map.put(i, 2*i);
59 }
60 t.stop("Time: \t");
61 System.gc();
62 Shared.printMemory();
63 map=null;
64 System.gc();
65 }
66 }
67
68 /*--------------------------------------------------------------*/
69 /*---------------- Initialization ----------------*/
70 /*--------------------------------------------------------------*/
71
72 public IntHashMap(){
73 this(256);
74 }
75
76 public IntHashMap(int initialSize){
77 this(initialSize, 0.7f);
78 }
79
80 public IntHashMap(int initialSize, float loadFactor_){
81 invalid=randy.nextInt()|MINMASK;
82 assert(invalid<0);
83 assert(initialSize>0);
84 assert(loadFactor_>0 && loadFactor_<1);
85 loadFactor=Tools.mid(0.25f, loadFactor_, 0.90f);
86 resize(initialSize);
87 }
88
89 /*--------------------------------------------------------------*/
90 /*---------------- Public Methods ----------------*/
91 /*--------------------------------------------------------------*/
92
93 @Override
94 public void clear(){
95 if(size<1){return;}
96 Arrays.fill(keys, invalid);
97 Arrays.fill(values, 0);
98 size=0;
99 }
100
101 @Override
102 public int get(int key){
103 int cell=findCell(key);
104 return cell<0 ? -1 : values[cell];
105 }
106
107 @Override
108 public int put(int key, int value){return set(key, value);}
109
110 public void putAll(IntHashMap map) {
111 for(int i=0; i<map.keys.length; i++) {
112 if(map.keys[i]!=map.invalid) {
113 put(map.keys[i], map.values[i]);
114 }
115 }
116 }
117
118 @Override
119 public int set(int key, int value){
120 if(key==invalid){resetInvalid();}
121 final int cell=findCellOrEmpty(key);
122 final int oldV=values[cell];
123 values[cell]=value;
124 if(keys[cell]==invalid){
125 keys[cell]=key;
126 size++;
127 if(size>sizeLimit){resize();}
128 }
129 // assert(get(key)==value);//TODO: slow
130 return oldV;
131 }
132
133 @Override
134 public int increment(int key){
135 return increment(key, 1);
136 }
137
138 @Override
139 public int increment(int key, int incr){
140 if(key==invalid){resetInvalid();}
141 final int cell=findCellOrEmpty(key);
142 final int oldV=values[cell];
143 final int value=oldV+incr;
144 values[cell]=value;
145 values[cell]=Tools.min(Integer.MAX_VALUE, value);
146 if(keys[cell]==invalid){
147 keys[cell]=key;
148 size++;
149 if(size>sizeLimit){resize();}
150 }
151 // assert(get(key)==value);//TODO: slow
152 return value;
153 }
154
155 public void incrementAll(IntHashMap map) {
156 for(int i=0; i<map.keys.length; i++) {
157 if(map.keys[i]!=map.invalid) {
158 increment(map.keys[i], map.values[i]);
159 }
160 }
161 }
162
163 public void setToMax(IntHashMap map) {
164 for(int i=0; i<map.keys.length; i++) {
165 final int key=map.keys[i];
166 if(key!=map.invalid) {
167 put(key, Tools.max(map.values[i], get(i)));
168 }
169 }
170 }
171
172 @Override
173 public boolean remove(int key){
174 if(key==invalid){return false;}
175 final int cell=findCell(key);
176 if(cell<0){return false;}
177 assert(keys[cell]==key);
178 keys[cell]=invalid;
179 values[cell]=0;
180 size--;
181
182 rehashFrom(cell);
183 return true;
184 }
185
186 /*--------------------------------------------------------------*/
187 /*---------------- Private Methods ----------------*/
188 /*--------------------------------------------------------------*/
189
190 private void rehashFrom(int initial){
191 if(size<1){return;}
192 final int limit=keys.length;
193 for(int cell=initial+1; cell<limit; cell++){
194 final int key=keys[cell];
195 if(key==invalid){return;}
196 rehashCell(cell);
197 }
198 for(int cell=0; cell<initial; cell++){
199 final int key=keys[cell];
200 if(key==invalid){return;}
201 rehashCell(cell);
202 }
203 }
204
205 private boolean rehashCell(final int cell){
206 final int key=keys[cell];
207 final int value=values[cell];
208 assert(key!=invalid);
209 if(key==invalid){resetInvalid();}
210 final int dest=findCellOrEmpty(key);
211 if(cell==dest){return false;}
212 assert(keys[dest]==invalid);
213 keys[cell]=invalid;
214 values[cell]=0;
215 keys[dest]=key;
216 values[dest]=value;
217
218 return true;
219 }
220
221 private void resetInvalid(){
222 final int old=invalid;
223 int x=invalid;
224 while(x==old || contains(x)){x=randy.nextInt()|MINMASK;}
225 assert(x<0);
226 invalid=x;
227 for(int i=0; i<keys.length; i++){
228 if(keys[i]==old){
229 keys[i]=invalid;
230 // assert(volues[i]==0); //TODO: slow
231 }
232 }
233 }
234
235 @Override
236 int findCell(final int key){
237 if(key==invalid){return -1;}
238
239 final int limit=keys.length, initial=(int)((key&MASK)%modulus);
240 for(int cell=initial; cell<limit; cell++){
241 final int x=keys[cell];
242 if(x==key){return cell;}
243 if(x==invalid){return -1;}
244 }
245 for(int cell=0; cell<initial; cell++){
246 final int x=keys[cell];
247 if(x==key){return cell;}
248 if(x==invalid){return -1;}
249 }
250 return -1;
251 }
252
253 private int findCellOrEmpty(final int key){
254 assert(key!=invalid) : "Collision - this should have been intercepted.";
255
256 final int limit=keys.length, initial=(int)((key&MASK)%modulus);
257 for(int cell=initial; cell<limit; cell++){
258 final int x=keys[cell];
259 if(x==key || x==invalid){return cell;}
260 }
261 for(int cell=0; cell<initial; cell++){
262 final int x=keys[cell];
263 if(x==key || x==invalid){return cell;}
264 }
265 throw new RuntimeException("No empty cells - size="+size+", limit="+limit);
266 }
267
268 private final void resize(){
269 assert(size>=sizeLimit);
270 resize(keys.length*2L+1);
271 }
272
273 private final void resize(final long size2){
274 assert(size2>size) : size+", "+size2;
275 long newPrime=Primes.primeAtLeast(size2);
276 if(newPrime+extra>Integer.MAX_VALUE){
277 newPrime=Primes.primeAtMost(Integer.MAX_VALUE-extra);
278 }
279 assert(newPrime>modulus) : "Overflow: "+size+", "+size2+", "+modulus+", "+newPrime;
280 modulus=(int)newPrime;
281
282 final int size3=(int)(newPrime+extra);
283 sizeLimit=(int)(modulus*loadFactor);
284 final int[] oldK=keys;
285 final int[] oldV=values;
286 keys=KillSwitch.allocInt1D(size3);
287 values=KillSwitch.allocInt1D(size3);
288 Arrays.fill(keys, invalid);
289
290 // System.err.println("Resizing "+(old==null ? "null" : ""+old.length)+" to "+size3);
291
292 if(size<1){return;}
293
294 size=0;
295 for(int i=0; i<oldK.length; i++){
296 final int k=oldK[i], v=oldV[i];
297 if(k!=invalid){
298 set(k, v);
299 }
300 }
301 }
302
303 /*--------------------------------------------------------------*/
304 /*---------------- Getters ----------------*/
305 /*--------------------------------------------------------------*/
306
307 @Override
308 public int[] toArray(){
309 int[] x=KillSwitch.allocInt1D(size);
310 int i=0;
311 for(int key : keys){
312 if(key!=invalid){
313 x[i]=key;
314 i++;
315 }
316 }
317 return x;
318 }
319
320 @Override
321 public int[] keys(){return keys;}
322
323 @Override
324 public int[] values(){return values;}
325
326 @Override
327 public int invalid(){return invalid;}
328
329 @Override
330 public int size(){return size;}
331
332 @Override
333 public boolean isEmpty(){return size==0;}
334
335 /*--------------------------------------------------------------*/
336 /*---------------- Fields ----------------*/
337 /*--------------------------------------------------------------*/
338
339 private int[] keys;
340 private int[] values;
341 private int size=0;
342 /** Value for empty cells */
343 private int invalid;
344 private int modulus;
345 private int sizeLimit;
346 private final float loadFactor;
347
348 private static final Random randy=new Random(1);
349
350 }