IntIntHash.java
5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package is2.data;
import java.util.Arrays;
final public class IntIntHash {
protected int _size;
protected int _free;
protected float _loadFactor;
public int _maxSize;
protected int _autoCompactRemovesRemaining;
protected float _autoCompactionFactor;
public int _set[];
private int _values[];
public IntIntHash() {
this(102877, 0.5F);
}
public IntIntHash(int initialCapacity, float loadFactor) {
_loadFactor = loadFactor;
_autoCompactionFactor = loadFactor;
setUp((int)Math.ceil(initialCapacity / loadFactor));
}
public int size() { return _size;}
public void ensureCapacity(int desiredCapacity) {
if(desiredCapacity > _maxSize - size()) {
rehash(PrimeFinder.nextPrime((int)Math.ceil((desiredCapacity + size()) / _loadFactor) + 1));
computeMaxSize(capacity());
}
}
public void compact() {
rehash(PrimeFinder.nextPrime((int)Math.ceil(size() / _loadFactor) + 1));
computeMaxSize(capacity());
if(_autoCompactionFactor != 0.0F) computeNextAutoCompactionAmount(size());
}
public void setAutoCompactionFactor(float factor) {
if(factor < 0.0F) {
throw new IllegalArgumentException((new StringBuilder()).append("Factor must be >= 0: ").append(factor).toString());
} else
{
_autoCompactionFactor = factor;
return;
}
}
public float getAutoCompactionFactor() { return _autoCompactionFactor; }
private void computeMaxSize(int capacity)
{
_maxSize = Math.min(capacity - 1, (int)Math.floor(capacity * _loadFactor));
_free = capacity - _size;
}
private void computeNextAutoCompactionAmount(int size)
{
if(_autoCompactionFactor != 0.0F)
_autoCompactRemovesRemaining = Math.round(size * _autoCompactionFactor);
}
protected final void postInsertHook(boolean usedFreeSlot)
{
if(usedFreeSlot) _free--;
if(++_size > _maxSize || _free == 0) {
int newCapacity = _size <= _maxSize ? capacity() : PrimeFinder.nextPrime(capacity() << 1);
rehash(newCapacity);
computeMaxSize(capacity());
}
}
protected int calculateGrownCapacity() { return capacity() << 1; }
protected int capacity() { return _values.length; }
public boolean contains(int val) { return index(val) >= 0;}
private int index(int v) {
int length = _set.length;
int index = Math.abs((computeHashCode(v) /*& 2147483647*/ ) % length);
while(true) {
// first
long l =_set[index];
if (l == 0) {
// good++;
return -1;
}
// second
if (l == v) {
return index;
}
if(--index < 0) index += length;
}
//return -1;
}
protected int insertionIndex(long val)
{
int length = _set.length;
int index = Math.abs((computeHashCode(val) /*& 2147483647*/ ) % length);
while(true) {
if(_set[index] == 0) return index;
if(_set[index] == val) return -index - 1;
if(--index < 0) index += length;
}
}
public int computeHashCode(long value)
{
return (int)(( value ^ (value&0xffffffff00000000L) >>> 32 ) *31);//0x811c9dc5 ^ // 29
}
protected int setUp(int initialCapacity)
{
int capacity = PrimeFinder.nextPrime(initialCapacity);
computeMaxSize(capacity);
computeNextAutoCompactionAmount(initialCapacity);
_set = new int[capacity];
_values = new int[capacity];
return capacity;
}
public void put(int key, int value)
{
int index = insertionIndex(key);
doPut(key, value, index);
}
private void doPut(int key, int value, int index)
{
boolean isNewMapping = true;
if(index < 0)
{
index = -index - 1;
isNewMapping = false;
}
_set[index] = key;
_values[index] = value;
if(isNewMapping) postInsertHook(true);
}
protected void rehash(int newCapacity)
{
int oldCapacity = _set.length;
int oldKeys[] = _set;
int oldVals[] = _values;
_set = new int[newCapacity];
_values = new int[newCapacity];
int i = oldCapacity;
while(true){
if(i-- <= 0) break;
if(oldVals[i] != 0) {
int o = oldKeys[i];
int index = insertionIndex(o);
_set[index] = o;
_values[index] = oldVals[i];
}
}
}
int index =0;
public int get(int key)
{
int index = index(key);
return index >= 0 ? _values[index] : 0;
}
public void clear()
{
_size = 0;
_free = capacity();
Arrays.fill(_set, 0, _set.length, 0);
// Arrays.fill(_values, 0, _values.length, 0);
}
public int remove(int key)
{
int prev = 0;
int index = index(key);
if(index >= 0)
{
prev = _values[index];
_values[index] = 0;
_set[index] = 0;
_size--;
if(_autoCompactionFactor != 0.0F) {
_autoCompactRemovesRemaining--;
if( _autoCompactRemovesRemaining <= 0) compact();
}
}
return prev;
}
public int[] getValues()
{
int vals[] = new int[size()];
int v[] = _values;
int i = v.length;
int j = 0;
do
{
if(i-- <= 0) break;
if(v[i] != 0) vals[j++] = v[i];
} while(true);
return vals;
}
public int[] keys()
{
int keys[] = new int[size()];
int k[] = _set;
// byte states[] = _states;
int i = k.length;
int j = 0;
do
{
if(i-- <= 0)
break;
if(k[i] != 0)
keys[j++] = k[i];
} while(true);
return keys;
}
/**
* @param index2
* @param i
* @return
*/
public boolean adjustValue(int key, int i) {
int index = index(key);
if (index >= 0){
_values[index] +=i;
return true;
}
return false;
}
}