01: /*
02: * @(#)ConstantSet.java 1.3 04/12/06
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.compiler;
10:
11: class ConstantSet {
12:
13: Slot[] table;
14: int count = 0;
15: static final float THRESHOLD = 0.7f;
16:
17: public ConstantSet() {
18: this (256);
19: }
20:
21: public ConstantSet(int initialCapacity) {
22: table = new Slot[initialCapacity];
23: }
24:
25: public Slot getSlot(Object key) {
26: int hash = key.hashCode();
27: int idx = (hash & 0x7FFFFFFF) % table.length;
28: for (Slot s = table[idx]; s != null; s = s.chain) {
29: if (key.equals(s.key)) {
30: return s;
31: }
32: }
33: if (count >= table.length * THRESHOLD) {
34: rehash(2 * table.length);
35: }
36: count++;
37: Slot s = new Slot(key, null);
38: s.chain = table[idx];
39: table[idx] = s;
40:
41: return s;
42: }
43:
44: void rehash(int new_capacity) {
45: Slot[] new_table = new Slot[new_capacity];
46: for (int i = table.length; --i >= 0;) {
47: Slot prev = null;
48: for (Slot cur = table[i]; cur != null;) {
49: Slot next = cur.chain;
50: cur.chain = prev;
51: prev = cur;
52: cur = next;
53: }
54: table[i] = prev;
55:
56: for (Slot cur = table[i]; cur != null;) {
57: int hash = cur.key.hashCode();
58: int new_index = (hash & 0x7FFFFFFF) % new_capacity;
59: Slot next = cur.chain;
60: cur.chain = new_table[new_index];
61: new_table[new_index] = cur;
62: cur = next;
63: }
64: }
65: table = new_table;
66: }
67: }
|