Debian Codegraph @

rhino/src/org/mozilla/javascript/UintMap.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import java.io.Serializable;
10 import java.io.IOException;
11 import java.io.ObjectInputStream;
12 import java.io.ObjectOutputStream;
13
14 /**
15 * Map to associate non-negative integers to objects or integers.
16 * The map does not synchronize any of its operation, so either use
17 * it from a single thread or do own synchronization or perform all mutation
18 * operations on one thread before passing the map to others.
19 *
20 * @author Igor Bukanov
21 *
22 */
23
24 public class UintMap implements Serializable
25 {
26 static final long serialVersionUID = 4242698212885848444L;
27
28 // Map implementation via hashtable,
29 // follows "The Art of Computer Programming" by Donald E. Knuth
30
31 public UintMap() {
32 this(4);
33 }
34
35 public UintMap(int initialCapacity) {
36 if (initialCapacity < 0) Kit.codeBug();
37 // Table grow when number of stored keys >= 3/4 of max capacity
38 int minimalCapacity = initialCapacity * 4 / 3;
39 int i;
40 for (i = 2; (1 << i) < minimalCapacity; ++i) { }
41 power = i;
42 if (check && power < 2) Kit.codeBug();
43 }
44
45 public boolean isEmpty() {
46 return keyCount == 0;
47 }
48
49 public int size() {
50 return keyCount;
51 }
52
53 public boolean has(int key) {
54 if (key < 0) Kit.codeBug();
55 return 0 <= findIndex(key);
56 }
57
58 /**
59 * Get object value assigned with key.
60 * @return key object value or null if key is absent
61 */
62 public Object getObject(int key) {
63 if (key < 0) Kit.codeBug();
64 if (values != null) {
65 int index = findIndex(key);
66 if (0 <= index) {
67 return values[index];
68 }
69 }
70 return null;
71 }
72
73 /**
74 * Get integer value assigned with key.
75 * @return key integer value or defaultValue if key is absent
76 */
77 public int getInt(int key, int defaultValue) {
78 if (key < 0) Kit.codeBug();
79 int index = findIndex(key);
80 if (0 <= index) {
81 if (ivaluesShift != 0) {
82 return keys[ivaluesShift + index];
83 }
84 return 0;
85 }
86 return defaultValue;
87 }
88
89 /**
90 * Get integer value assigned with key.
91 * @return key integer value or defaultValue if key does not exist or does
92 * not have int value
93 * @throws RuntimeException if key does not exist
94 */
95 public int getExistingInt(int key) {
96 if (key < 0) Kit.codeBug();
97 int index = findIndex(key);
98 if (0 <= index) {
99 if (ivaluesShift != 0) {
100 return keys[ivaluesShift + index];
101 }
102 return 0;
103 }
104 // Key must exist
105 Kit.codeBug();
106 return 0;
107 }
108
109 /**
110 * Set object value of the key.
111 * If key does not exist, also set its int value to 0.
112 */
113 public void put(int key, Object value) {
114 if (key < 0) Kit.codeBug();
115 int index = ensureIndex(key, false);
116 if (values == null) {
117 values = new Object[1 << power];
118 }
119 values[index] = value;
120 }
121
122 /**
123 * Set int value of the key.
124 * If key does not exist, also set its object value to null.
125 */
126 public void put(int key, int value) {
127 if (key < 0) Kit.codeBug();
128 int index = ensureIndex(key, true);
129 if (ivaluesShift == 0) {
130 int N = 1 << power;
131 // keys.length can be N * 2 after clear which set ivaluesShift to 0
132 if (keys.length != N * 2) {
133 int[] tmp = new int[N * 2];
134 System.arraycopy(keys, 0, tmp, 0, N);
135 keys = tmp;
136 }
137 ivaluesShift = N;
138 }
139 keys[ivaluesShift + index] = value;
140 }
141
142 public void remove(int key) {
143 if (key < 0) Kit.codeBug();
144 int index = findIndex(key);
145 if (0 <= index) {
146 keys[index] = DELETED;
147 --keyCount;
148 // Allow to GC value and make sure that new key with the deleted
149 // slot shall get proper default values
150 if (values != null) { values[index] = null; }
151 if (ivaluesShift != 0) { keys[ivaluesShift + index] = 0; }
152 }
153 }
154
155 public void clear() {
156 int N = 1 << power;
157 if (keys != null) {
158 for (int i = 0; i != N; ++i) {
159 keys[i] = EMPTY;
160 }
161 if (values != null) {
162 for (int i = 0; i != N; ++i) {
163 values[i] = null;
164 }
165 }
166 }
167 ivaluesShift = 0;
168 keyCount = 0;
169 occupiedCount = 0;
170 }
171
172 /** Return array of present keys */
173 public int[] getKeys() {
174 int[] keys = this.keys;
175 int n = keyCount;
176 int[] result = new int[n];
177 for (int i = 0; n != 0; ++i) {
178 int entry = keys[i];
179 if (entry != EMPTY && entry != DELETED) {
180 result[--n] = entry;
181 }
182 }
183 return result;
184 }
185
186 private static int tableLookupStep(int fraction, int mask, int power) {
187 int shift = 32 - 2 * power;
188 if (shift >= 0) {
189 return ((fraction >>> shift) & mask) | 1;
190 }
191 else {
192 return (fraction & (mask >>> -shift)) | 1;
193 }
194 }
195
196 private int findIndex(int key) {
197 int[] keys = this.keys;
198 if (keys != null) {
199 int fraction = key * A;
200 int index = fraction >>> (32 - power);
201 int entry = keys[index];
202 if (entry == key) { return index; }
203 if (entry != EMPTY) {
204 // Search in table after first failed attempt
205 int mask = (1 << power) - 1;
206 int step = tableLookupStep(fraction, mask, power);
207 int n = 0;
208 do {
209 if (check) {
210 if (n >= occupiedCount) Kit.codeBug();
211 ++n;
212 }
213 index = (index + step) & mask;
214 entry = keys[index];
215 if (entry == key) { return index; }
216 } while (entry != EMPTY);
217 }
218 }
219 return -1;
220 }
221
222 // Insert key that is not present to table without deleted entries
223 // and enough free space
224 private int insertNewKey(int key) {
225 if (check && occupiedCount != keyCount) Kit.codeBug();
226 if (check && keyCount == 1 << power) Kit.codeBug();
227 int[] keys = this.keys;
228 int fraction = key * A;
229 int index = fraction >>> (32 - power);
230 if (keys[index] != EMPTY) {
231 int mask = (1 << power) - 1;
232 int step = tableLookupStep(fraction, mask, power);
233 int firstIndex = index;
234 do {
235 if (check && keys[index] == DELETED) Kit.codeBug();
236 index = (index + step) & mask;
237 if (check && firstIndex == index) Kit.codeBug();
238 } while (keys[index] != EMPTY);
239 }
240 keys[index] = key;
241 ++occupiedCount;
242 ++keyCount;
243 return index;
244 }
245
246 private void rehashTable(boolean ensureIntSpace) {
247 if (keys != null) {
248 // Check if removing deleted entries would free enough space
249 if (keyCount * 2 >= occupiedCount) {
250 // Need to grow: less then half of deleted entries
251 ++power;
252 }
253 }
254 int N = 1 << power;
255 int[] old = keys;
256 int oldShift = ivaluesShift;
257 if (oldShift == 0 && !ensureIntSpace) {
258 keys = new int[N];
259 }
260 else {
261 ivaluesShift = N; keys = new int[N * 2];
262 }
263 for (int i = 0; i != N; ++i) { keys[i] = EMPTY; }
264
265 Object[] oldValues = values;
266 if (oldValues != null) { values = new Object[N]; }
267
268 int oldCount = keyCount;
269 occupiedCount = 0;
270 if (oldCount != 0) {
271 keyCount = 0;
272 for (int i = 0, remaining = oldCount; remaining != 0; ++i) {
273 int key = old[i];
274 if (key != EMPTY && key != DELETED) {
275 int index = insertNewKey(key);
276 if (oldValues != null) {
277 values[index] = oldValues[i];
278 }
279 if (oldShift != 0) {
280 keys[ivaluesShift + index] = old[oldShift + i];
281 }
282 --remaining;
283 }
284 }
285 }
286 }
287
288 // Ensure key index creating one if necessary
289 private int ensureIndex(int key, boolean intType) {
290 int index = -1;
291 int firstDeleted = -1;
292 int[] keys = this.keys;
293 if (keys != null) {
294 int fraction = key * A;
295 index = fraction >>> (32 - power);
296 int entry = keys[index];
297 if (entry == key) { return index; }
298 if (entry != EMPTY) {
299 if (entry == DELETED) { firstDeleted = index; }
300 // Search in table after first failed attempt
301 int mask = (1 << power) - 1;
302 int step = tableLookupStep(fraction, mask, power);
303 int n = 0;
304 do {
305 if (check) {
306 if (n >= occupiedCount) Kit.codeBug();
307 ++n;
308 }
309 index = (index + step) & mask;
310 entry = keys[index];
311 if (entry == key) { return index; }
312 if (entry == DELETED && firstDeleted < 0) {
313 firstDeleted = index;
314 }
315 } while (entry != EMPTY);
316 }
317 }
318 // Inserting of new key
319 if (check && keys != null && keys[index] != EMPTY)
320 Kit.codeBug();
321 if (firstDeleted >= 0) {
322 index = firstDeleted;
323 }
324 else {
325 // Need to consume empty entry: check occupation level
326 if (keys == null || occupiedCount * 4 >= (1 << power) * 3) {
327 // Too litle unused entries: rehash
328 rehashTable(intType);
329 return insertNewKey(key);
330 }
331 ++occupiedCount;
332 }
333 keys[index] = key;
334 ++keyCount;
335 return index;
336 }
337
338 private void writeObject(ObjectOutputStream out)
339 throws IOException
340 {
341 out.defaultWriteObject();
342
343 int count = keyCount;
344 if (count != 0) {
345 boolean hasIntValues = (ivaluesShift != 0);
346 boolean hasObjectValues = (values != null);
347 out.writeBoolean(hasIntValues);
348 out.writeBoolean(hasObjectValues);
349
350 for (int i = 0; count != 0; ++i) {
351 int key = keys[i];
352 if (key != EMPTY && key != DELETED) {
353 --count;
354 out.writeInt(key);
355 if (hasIntValues) {
356 out.writeInt(keys[ivaluesShift + i]);
357 }
358 if (hasObjectValues) {
359 out.writeObject(values[i]);
360 }
361 }
362 }
363 }
364 }
365
366 private void readObject(ObjectInputStream in)
367 throws IOException, ClassNotFoundException
368 {
369 in.defaultReadObject();
370
371 int writtenKeyCount = keyCount;
372 if (writtenKeyCount != 0) {
373 keyCount = 0;
374 boolean hasIntValues = in.readBoolean();
375 boolean hasObjectValues = in.readBoolean();
376
377 int N = 1 << power;
378 if (hasIntValues) {
379 keys = new int[2 * N];
380 ivaluesShift = N;
381 }else {
382 keys = new int[N];
383 }
384 for (int i = 0; i != N; ++i) {
385 keys[i] = EMPTY;
386 }
387 if (hasObjectValues) {
388 values = new Object[N];
389 }
390 for (int i = 0; i != writtenKeyCount; ++i) {
391 int key = in.readInt();
392 int index = insertNewKey(key);
393 if (hasIntValues) {
394 int ivalue = in.readInt();
395 keys[ivaluesShift + index] = ivalue;
396 }
397 if (hasObjectValues) {
398 values[index] = in.readObject();
399 }
400 }
401 }
402 }
403
404 // A == golden_ratio * (1 << 32) = ((sqrt(5) - 1) / 2) * (1 << 32)
405 // See Knuth etc.
406 private static final int A = 0x9e3779b9;
407
408 private static final int EMPTY = -1;
409 private static final int DELETED = -2;
410
411 // Structure of kyes and values arrays (N == 1 << power):
412 // keys[0 <= i < N]: key value or EMPTY or DELETED mark
413 // values[0 <= i < N]: value of key at keys[i]
414 // keys[N <= i < 2N]: int values of keys at keys[i - N]
415
416 private transient int[] keys;
417 private transient Object[] values;
418
419 private int power;
420 private int keyCount;
421 private transient int occupiedCount; // == keyCount + deleted_count
422
423 // If ivaluesShift != 0, keys[ivaluesShift + index] contains integer
424 // values associated with keys
425 private transient int ivaluesShift;
426
427 // If true, enables consitency checks
428 private static final boolean check = false;
429
430 /* TEST START
431
432 public static void main(String[] args) {
433 if (!check) {
434 System.err.println("Set check to true and re-run");
435 throw new RuntimeException("Set check to true and re-run");
436 }
437
438 UintMap map;
439 map = new UintMap();
440 testHash(map, 2);
441 map = new UintMap();
442 testHash(map, 10 * 1000);
443 map = new UintMap(30 * 1000);
444 testHash(map, 10 * 100);
445 map.clear();
446 testHash(map, 4);
447 map = new UintMap(0);
448 testHash(map, 10 * 100);
449 }
450
451 private static void testHash(UintMap map, int N) {
452 System.out.print("."); System.out.flush();
453 for (int i = 0; i != N; ++i) {
454 map.put(i, i);
455 check(i == map.getInt(i, -1));
456 }
457
458 System.out.print("."); System.out.flush();
459 for (int i = 0; i != N; ++i) {
460 map.put(i, i);
461 check(i == map.getInt(i, -1));
462 }
463
464 System.out.print("."); System.out.flush();
465 for (int i = 0; i != N; ++i) {
466 map.put(i, new Integer(i));
467 check(-1 == map.getInt(i, -1));
468 Integer obj = (Integer)map.getObject(i);
469 check(obj != null && i == obj.intValue());
470 }
471
472 check(map.size() == N);
473
474 System.out.print("."); System.out.flush();
475 int[] keys = map.getKeys();
476 check(keys.length == N);
477 for (int i = 0; i != N; ++i) {
478 int key = keys[i];
479 check(map.has(key));
480 check(!map.isIntType(key));
481 check(map.isObjectType(key));
482 Integer obj = (Integer) map.getObject(key);
483 check(obj != null && key == obj.intValue());
484 }
485
486
487 System.out.print("."); System.out.flush();
488 for (int i = 0; i != N; ++i) {
489 check(-1 == map.getInt(i, -1));
490 }
491
492 System.out.print("."); System.out.flush();
493 for (int i = 0; i != N; ++i) {
494 map.put(i * i, i);
495 check(i == map.getInt(i * i, -1));
496 }
497
498 System.out.print("."); System.out.flush();
499 for (int i = 0; i != N; ++i) {
500 check(i == map.getInt(i * i, -1));
501 }
502
503 System.out.print("."); System.out.flush();
504 for (int i = 0; i != N; ++i) {
505 map.put(i * i, new Integer(i));
506 check(-1 == map.getInt(i * i, -1));
507 map.remove(i * i);
508 check(!map.has(i * i));
509 map.put(i * i, i);
510 check(map.isIntType(i * i));
511 check(null == map.getObject(i * i));
512 map.remove(i * i);
513 check(!map.isObjectType(i * i));
514 check(!map.isIntType(i * i));
515 }
516
517 int old_size = map.size();
518 for (int i = 0; i != N; ++i) {
519 map.remove(i * i);
520 check(map.size() == old_size);
521 }
522
523 System.out.print("."); System.out.flush();
524 map.clear();
525 check(map.size() == 0);
526 for (int i = 0; i != N; ++i) {
527 map.put(i * i, i);
528 map.put(i * i + 1, new Double(i+0.5));
529 }
530 checkSameMaps(map, (UintMap)writeAndRead(map));
531
532 System.out.print("."); System.out.flush();
533 map = new UintMap(0);
534 checkSameMaps(map, (UintMap)writeAndRead(map));
535 map = new UintMap(1);
536 checkSameMaps(map, (UintMap)writeAndRead(map));
537 map = new UintMap(1000);
538 checkSameMaps(map, (UintMap)writeAndRead(map));
539
540 System.out.print("."); System.out.flush();
541 map = new UintMap(N / 10);
542 for (int i = 0; i != N; ++i) {
543 map.put(2*i+1, i);
544 }
545 checkSameMaps(map, (UintMap)writeAndRead(map));
546
547 System.out.print("."); System.out.flush();
548 map = new UintMap(N / 10);
549 for (int i = 0; i != N; ++i) {
550 map.put(2*i+1, i);
551 }
552 for (int i = 0; i != N / 2; ++i) {
553 map.remove(2*i+1);
554 }
555 checkSameMaps(map, (UintMap)writeAndRead(map));
556
557 System.out.print("."); System.out.flush();
558 map = new UintMap();
559 for (int i = 0; i != N; ++i) {
560 map.put(2*i+1, new Double(i + 10));
561 }
562 for (int i = 0; i != N / 2; ++i) {
563 map.remove(2*i+1);
564 }
565 checkSameMaps(map, (UintMap)writeAndRead(map));
566
567 System.out.println(); System.out.flush();
568
569 }
570
571 private static void checkSameMaps(UintMap map1, UintMap map2) {
572 check(map1.size() == map2.size());
573 int[] keys = map1.getKeys();
574 check(keys.length == map1.size());
575 for (int i = 0; i != keys.length; ++i) {
576 int key = keys[i];
577 check(map2.has(key));
578 check(map1.isObjectType(key) == map2.isObjectType(key));
579 check(map1.isIntType(key) == map2.isIntType(key));
580 Object o1 = map1.getObject(key);
581 Object o2 = map2.getObject(key);
582 if (map1.isObjectType(key)) {
583 check(o1.equals(o2));
584 }else {
585 check(map1.getObject(key) == null);
586 check(map2.getObject(key) == null);
587 }
588 if (map1.isIntType(key)) {
589 check(map1.getExistingInt(key) == map2.getExistingInt(key));
590 }else {
591 check(map1.getInt(key, -10) == -10);
592 check(map1.getInt(key, -11) == -11);
593 check(map2.getInt(key, -10) == -10);
594 check(map2.getInt(key, -11) == -11);
595 }
596 }
597 }
598
599 private static void check(boolean condition) {
600 if (!condition) Kit.codeBug();
601 }
602
603 private static Object writeAndRead(Object obj) {
604 try {
605 java.io.ByteArrayOutputStream
606 bos = new java.io.ByteArrayOutputStream();
607 java.io.ObjectOutputStream
608 out = new java.io.ObjectOutputStream(bos);
609 out.writeObject(obj);
610 out.close();
611 byte[] data = bos.toByteArray();
612 java.io.ByteArrayInputStream
613 bis = new java.io.ByteArrayInputStream(data);
614 java.io.ObjectInputStream
615 in = new java.io.ObjectInputStream(bis);
616 Object result = in.readObject();
617 in.close();
618 return result;
619 }catch (Exception ex) {
620 ex.printStackTrace();
621 throw new RuntimeException("Unexpected");
622 }
623 }
624
625 // TEST END */
626 }