1
14
15 package gate.util;
16
17
23
24 public class HashMapLong {
25
26
29 private transient Entry table[];
30
31 private transient int count;
32
33 private int threshold;
34
35 private float loadFactor;
36
37
40 public HashMapLong(int initialCapacity, float loadFactor) {
41 if (initialCapacity < 0)
42 throw new IllegalArgumentException("Illegal Initial Capacity: "+
43 initialCapacity);
44 if (loadFactor <= 0 || Float.isNaN(loadFactor))
45 throw new IllegalArgumentException("Illegal Load factor: "+
46 loadFactor);
47 if (initialCapacity==0)
48 initialCapacity = 1;
49 this.loadFactor = loadFactor;
50 table = new Entry[initialCapacity];
51 threshold = (int)(initialCapacity * loadFactor);
52 }
53
54 public HashMapLong(int initialCapacity) {
55 this(initialCapacity, 0.75f);
56 }
57
58 public HashMapLong() {
59 this(11, 0.75f);
60 }
61
62 public boolean isEmpty() {
63 return count == 0;
64 }
65
66 public Object get(long key) {
67 Entry tab[] = table;
68 int hash = (int)(key ^ (key >> 32));
69 int index = (hash & 0x7FFFFFFF) % tab.length;
70 for (Entry e = tab[index]; e != null; e = e.next)
71 if ((e.hash == hash) && key == e.key)
72 return e.value;
73
74 return null;
75 }
76
77
82 private void rehash() {
83 int oldCapacity = table.length;
84 Entry oldMap[] = table;
85
86 int newCapacity = oldCapacity * 2 + 1;
87 Entry newMap[] = new Entry[newCapacity];
88
89 threshold = (int) (newCapacity * loadFactor);
90 table = newMap;
91
92 for (int i = oldCapacity; i-- > 0; ) {
93 for (Entry old = oldMap[i]; old != null; ) {
94 Entry e = old;
95 old = old.next;
96
97 int index = (e.hash & 0x7FFFFFFF) % newCapacity;
98 e.next = newMap[index];
99 newMap[index] = e;
100 }
101 }
102 }
103
104 public Object put(long key, Object value) {
105 Entry tab[] = table;
107 int hash = (int)(key ^ (key >> 32));
108 int index = (hash & 0x7FFFFFFF) % tab.length;
109
110 for (Entry e = tab[index] ; e != null ; e = e.next) {
111 if ((e.hash == hash) && key == e.key) {
112 Object old = e.value;
113 e.value = value;
114 return old;
115 }
116 }
117
118 if (count >= threshold) {
119 rehash();
121
122 tab = table;
123 index = (hash & 0x7FFFFFFF) % tab.length;
124 }
125
126 Entry e = new Entry(hash, key, value, tab[index]);
128 tab[index] = e;
129 count++;
130 return null;
131 }
132
133
136 private static class Entry {
137 int hash;
138 long key;
139 Object value;
140 Entry next;
141
142 Entry(int hash, long key, Object value, Entry next) {
143 this.hash = hash;
144 this.key = key;
145 this.value = value;
146 this.next = next;
147 }
148 } }