Debian Codegraph @

current/structures/IntHashMapBinary.java

raw symbols SCIP
1 package structures;
2
3 import java.util.Arrays;
4 import java.util.Random;
5
6 import shared.KillSwitch;
7 import shared.Tools;
8
9 /**
10 * Like IntHashMap, but uses a power-of-2 length to avoid modulo operations.
11 * @author Brian Bushnell
12 * @date June 8, 2017
13 *
14 */
15 public final class IntHashMapBinary extends AbstractIntHashMap{
16
17 public static void main(String[] args){
18 IntHashMapBinary set=new IntHashMapBinary(32, 0.7f);
19 test(set);
20 }
21
22 /*--------------------------------------------------------------*/
23 /*---------------- Initialization ----------------*/
24 /*--------------------------------------------------------------*/
25
26 public IntHashMapBinary(){
27 this(256);
28 }
29
30 public IntHashMapBinary(int initialSize){
31 this(initialSize, 0.7f);
32 }
33
34 public IntHashMapBinary(int initialSize, float loadFactor_){
35 if(Integer.bitCount(initialSize)>1){
36 int zeros=Integer.numberOfLeadingZeros(initialSize);
37 if(zeros<2){zeros=2;}
38 initialSize=1<<(32-zeros);
39 }
40 invalid=randy.nextInt()|MINMASK;
41 assert(invalid<0);
42 assert(initialSize>0);
43 assert(loadFactor_>0 && loadFactor_<1);
44 loadFactor=Tools.mid(0.25f, loadFactor_, 0.90f);
45 resize(initialSize);
46 }
47
48 /*--------------------------------------------------------------*/
49 /*---------------- Public Methods ----------------*/
50 /*--------------------------------------------------------------*/
51
52 @Override
53 public void clear(){
54 if(size<1){return;}
55 Arrays.fill(keys, invalid);
56 Arrays.fill(values, 0);
57 size=0;
58 }
59
60 @Override
61 public int get(int key){
62 int cell=findCell(key);
63 return cell<0 ? -1 : values[cell];
64 }
65
66 @Override
67 public int put(int key, int value){return set(key, value);}
68
69 @Override
70 public int set(int key, int value){
71 if(key==invalid){resetInvalid();}
72 final int cell=findCellOrEmpty(key);
73 final int oldV=values[cell];
74 values[cell]=value;
75 if(keys[cell]==invalid){
76 keys[cell]=key;
77 size++;
78 if(size>sizeLimit){resize();}
79 }
80 // assert(get(key)==value);//TODO: slow
81 return oldV;
82 }
83
84 @Override
85 public int increment(int key){
86 return increment(key, 1);
87 }
88
89 @Override
90 public int increment(int key, int incr){
91 if(key==invalid){resetInvalid();}
92 final int cell=findCellOrEmpty(key);
93 final int oldV=values[cell];
94 final int value=oldV+incr;
95 values[cell]=value;
96 values[cell]=Tools.min(Integer.MAX_VALUE, value);
97 if(keys[cell]==invalid){
98 keys[cell]=key;
99 size++;
100 if(size>sizeLimit){resize();}
101 }
102 // assert(get(key)==value);//TODO: slow
103 return value;
104 }
105
106 @Override
107 public boolean remove(int key){
108 if(key==invalid){return false;}
109 final int cell=findCell(key);
110 if(cell<0){return false;}
111 assert(keys[cell]==key);
112 keys[cell]=invalid;
113 values[cell]=0;
114 size--;
115
116 rehashFrom(cell);
117 return true;
118 }
119
120 /*--------------------------------------------------------------*/
121 /*---------------- Private Methods ----------------*/
122 /*--------------------------------------------------------------*/
123
124 private void rehashFrom(int initial){
125 if(size<1){return;}
126 final int limit=keys.length;
127 for(int cell=initial+1; cell<limit; cell++){
128 final int key=keys[cell];
129 if(key==invalid){return;}
130 rehashCell(cell);
131 }
132 for(int cell=0; cell<initial; cell++){
133 final int key=keys[cell];
134 if(key==invalid){return;}
135 rehashCell(cell);
136 }
137 }
138
139 private boolean rehashCell(final int cell){
140 final int key=keys[cell];
141 final int value=values[cell];
142 assert(key!=invalid);
143 if(key==invalid){resetInvalid();}
144 final int dest=findCellOrEmpty(key);
145 if(cell==dest){return false;}
146 assert(keys[dest]==invalid);
147 keys[cell]=invalid;
148 values[cell]=0;
149 keys[dest]=key;
150 values[dest]=value;
151
152 return true;
153 }
154
155 private void resetInvalid(){
156 final int old=invalid;
157 int x=invalid;
158 while(x==old || contains(x)){x=randy.nextInt()|MINMASK;}
159 assert(x<0);
160 invalid=x;
161 for(int i=0; i<keys.length; i++){
162 if(keys[i]==old){
163 keys[i]=invalid;
164 // assert(volues[i]==0); //TODO: slow
165 }
166 }
167 }
168
169 @Override
170 int findCell(final int key){
171 if(key==invalid){return -1;}
172
173 final int limit=keys.length, initial=key&modulus;
174 for(int cell=initial; cell<limit; cell++){
175 final int x=keys[cell];
176 if(x==key){return cell;}
177 if(x==invalid){return -1;}
178 }
179 for(int cell=0; cell<initial; cell++){
180 final int x=keys[cell];
181 if(x==key){return cell;}
182 if(x==invalid){return -1;}
183 }
184 return -1;
185 }
186
187 private int findCellOrEmpty(final int key){
188 assert(key!=invalid) : "Collision - this should have been intercepted.";
189
190 final int limit=keys.length, initial=key&modulus;
191 for(int cell=initial; cell<limit; cell++){
192 final int x=keys[cell];
193 if(x==key || x==invalid){return cell;}
194 }
195 for(int cell=0; cell<initial; cell++){
196 final int x=keys[cell];
197 if(x==key || x==invalid){return cell;}
198 }
199 throw new RuntimeException("No empty cells - size="+size+", limit="+limit);
200 }
201
202 private final void resize(){
203 assert(size>=sizeLimit);
204 resize(Tools.max(2, modulus+1)*2L);
205 }
206
207 private final void resize(final long size2){
208 assert(size2>size) : size+", "+size2;
209 assert(Long.bitCount(size2)==1) : size+", "+size2;
210 long newModulus=size2-1;
211 assert(newModulus>0) : newModulus;
212 assert(newModulus+extra<Integer.MAX_VALUE) : "Overflow";
213 assert(newModulus>modulus);
214
215 // System.err.println("size="+size+", modulus="+modulus+" -> size2="+size2+", modulus2="+newModulus);
216
217 modulus=(int)newModulus;
218
219 final int size3=(int)(newModulus+extra);
220 sizeLimit=(int)(newModulus*loadFactor);
221 final int[] oldK=keys;
222 final int[] oldV=values;
223 keys=KillSwitch.allocInt1D(size3);
224 values=KillSwitch.allocInt1D(size3);
225 Arrays.fill(keys, invalid);
226
227 // System.err.println("Resizing "+(old==null ? "null" : ""+old.length)+" to "+size3);
228
229 if(size<1){return;}
230
231 size=0;
232 for(int i=0; i<oldK.length; i++){
233 final int k=oldK[i], v=oldV[i];
234 if(k!=invalid){
235 set(k, v);
236 }
237 }
238 }
239
240 /*--------------------------------------------------------------*/
241 /*---------------- Getters ----------------*/
242 /*--------------------------------------------------------------*/
243
244 @Override
245 public int[] toArray(){
246 int[] x=KillSwitch.allocInt1D(size);
247 int i=0;
248 for(int key : keys){
249 if(key!=invalid){
250 x[i]=key;
251 i++;
252 }
253 }
254 return x;
255 }
256
257 @Override
258 public int[] keys(){return keys;}
259
260 @Override
261 public int[] values(){return values;}
262
263 @Override
264 public int invalid(){return invalid;}
265
266 @Override
267 public int size(){return size;}
268
269 @Override
270 public boolean isEmpty(){return size==0;}
271
272 /*--------------------------------------------------------------*/
273 /*---------------- Fields ----------------*/
274 /*--------------------------------------------------------------*/
275
276 private int[] keys;
277 private int[] values;
278 private int size=0;
279 /** Value for empty cells */
280 private int invalid;
281 private int modulus;
282 private int sizeLimit;
283 private final float loadFactor;
284
285 private static final Random randy=new Random(1);
286
287 }