| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.z; |
| 9 |
|
| 10 |
import java.io.PrintStream; |
| 11 |
import java.util.Iterator; |
| 12 |
import java.util.NoSuchElementException; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class IntHashMap<T> { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public IntHashMap() { |
| 29 |
this(8, 0.25); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public IntHashMap(int capacity) { |
| 38 |
this(capacity, 0.25); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public IntHashMap(int capacity, double factor) { |
| 55 |
_factor = factor; |
| 56 |
setCapacity(capacity); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public void clear() { |
| 63 |
_n = 0; |
| 64 |
for (int i = 0; i < _nmax; ++i) { |
| 65 |
_value[i] = null; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public T get(int key) { |
| 76 |
return _value[indexOf(key)]; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public int size() { |
| 85 |
return _n; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public boolean remove(int key) { |
| 95 |
|
| 96 |
int i = indexOf(key); |
| 97 |
|
| 98 |
if (_value[i] == null) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
--_n; |
| 102 |
for (; ; ) { |
| 103 |
|
| 104 |
_value[i] = null; |
| 105 |
int j = i; |
| 106 |
int r; |
| 107 |
do { |
| 108 |
i = (i - 1) & _mask; |
| 109 |
|
| 110 |
if (_value[i] == null) { |
| 111 |
return true; |
| 112 |
} |
| 113 |
r = hash(_key[i]); |
| 114 |
} while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r)); |
| 115 |
_key[j] = _key[i]; |
| 116 |
_value[j] = _value[i]; |
| 117 |
|
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public T put(int key, T value) { |
| 129 |
if (value == null) { |
| 130 |
throw new NullPointerException("IntHashMap does not allow null values"); |
| 131 |
} |
| 132 |
int i = indexOf(key); |
| 133 |
T old = _value[i]; |
| 134 |
if (old != null) { |
| 135 |
_value[i] = value; |
| 136 |
} else { |
| 137 |
_key[i] = key; |
| 138 |
_value[i] = value; |
| 139 |
grow(); |
| 140 |
} |
| 141 |
return old; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
private static final int NBIT = 30; |
| 149 |
private static final int NMAX = 1 << NBIT; |
| 150 |
private double _factor; |
| 151 |
private int _nmax; |
| 152 |
private int _n; |
| 153 |
private int _nlo; |
| 154 |
private int _nhi; |
| 155 |
private int _shift; |
| 156 |
private int _mask; |
| 157 |
private int[] _key; |
| 158 |
|
| 159 |
private T[] _value; |
| 160 |
|
| 161 |
|
| 162 |
private int hash(int key) { |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
return ((1327217885 * key) >> _shift) & _mask; |
| 167 |
} |
| 168 |
|
| 169 |
private int indexOf(int key) { |
| 170 |
int i = hash(key); |
| 171 |
|
| 172 |
while (_value[i] != null) { |
| 173 |
if (_key[i] == key) { |
| 174 |
return i; |
| 175 |
} |
| 176 |
i = (i - 1) & _mask; |
| 177 |
} |
| 178 |
return i; |
| 179 |
} |
| 180 |
|
| 181 |
private void grow() { |
| 182 |
++_n; |
| 183 |
if (_n > NMAX) { |
| 184 |
throw new RuntimeException("number of keys mapped exceeds " + NMAX); |
| 185 |
} |
| 186 |
if (_nlo < _n && _n <= _nhi) { |
| 187 |
setCapacity(_n); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
private void setCapacity(int capacity) { |
| 192 |
if (capacity < _n) { |
| 193 |
capacity = _n; |
| 194 |
} |
| 195 |
double factor = (_factor < 0.01) ? 0.01 : (_factor > 0.99) ? 0.99 : _factor; |
| 196 |
int nbit, nmax; |
| 197 |
for (nbit = 1, nmax = 2; nmax * factor < capacity && nmax < NMAX; ++nbit, nmax *= 2) { |
| 198 |
|
| 199 |
} |
| 200 |
int nold = _nmax; |
| 201 |
if (nmax == nold) { |
| 202 |
return; |
| 203 |
} |
| 204 |
_nmax = nmax; |
| 205 |
_nlo = (int) (nmax * factor); |
| 206 |
_nhi = (int) (NMAX * factor); |
| 207 |
_shift = 1 + NBIT - nbit; |
| 208 |
_mask = nmax - 1; |
| 209 |
int[] key = _key; |
| 210 |
T[] value = _value; |
| 211 |
|
| 212 |
_n = 0; |
| 213 |
_key = new int[nmax]; |
| 214 |
|
| 215 |
_value = (T[]) new Object[nmax]; |
| 216 |
|
| 217 |
if (key != null) { |
| 218 |
for (int i = 0; i < nold; ++i) { |
| 219 |
|
| 220 |
if (value[i] != null) { |
| 221 |
put(key[i], value[i]); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public IntIterator keyIterator() { |
| 234 |
return new IntHashMapKeyIterator(); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public Iterator<T> valueIterator() { |
| 244 |
return new IntHashMapValueIterator(); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public Iterable<T> valueSet() { |
| 253 |
return new Iterable<T>() { |
| 254 |
public Iterator<T> iterator() { |
| 255 |
return valueIterator(); |
| 256 |
} |
| 257 |
}; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public IntHashMap<T> copy() { |
| 267 |
IntHashMap<T> n = new IntHashMap<T>(size()); |
| 268 |
IntIterator it = keyIterator(); |
| 269 |
while (it.hasNext()) { |
| 270 |
int k = it.next(); |
| 271 |
n.put(k, get(k)); |
| 272 |
} |
| 273 |
return n; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public void display(PrintStream ps) { |
| 281 |
IntIterator iter = new IntHashMapKeyIterator(); |
| 282 |
while (iter.hasNext()) { |
| 283 |
int key = iter.next(); |
| 284 |
Object value = get(key); |
| 285 |
ps.println(key + " -> " + value.toString()); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
private class IntHashMapKeyIterator implements IntIterator { |
| 293 |
|
| 294 |
private int i = 0; |
| 295 |
|
| 296 |
public IntHashMapKeyIterator() { |
| 297 |
i = 0; |
| 298 |
} |
| 299 |
|
| 300 |
public boolean hasNext() { |
| 301 |
while (i < _key.length) { |
| 302 |
if (_value[i] != null) { |
| 303 |
return true; |
| 304 |
} else { |
| 305 |
i++; |
| 306 |
} |
| 307 |
} |
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
public int next() { |
| 312 |
return _key[i++]; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private class IntHashMapValueIterator implements Iterator<T> { |
| 320 |
|
| 321 |
private int i = 0; |
| 322 |
|
| 323 |
public IntHashMapValueIterator() { |
| 324 |
i = 0; |
| 325 |
} |
| 326 |
|
| 327 |
public boolean hasNext() { |
| 328 |
while (i < _key.length) { |
| 329 |
if (_value[i] != null) { |
| 330 |
return true; |
| 331 |
} else { |
| 332 |
i++; |
| 333 |
} |
| 334 |
} |
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
public T next() { |
| 339 |
T temp = _value[i++]; |
| 340 |
if (temp == null) { |
| 341 |
throw new NoSuchElementException(); |
| 342 |
} |
| 343 |
return temp; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public void remove() { |
| 354 |
throw new UnsupportedOperationException("remove"); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public IntSet keySet() { |
| 365 |
return new IntSet() { |
| 366 |
public void clear() { |
| 367 |
throw new UnsupportedOperationException("Immutable set"); |
| 368 |
} |
| 369 |
|
| 370 |
public IntSet copy() { |
| 371 |
IntHashSet s = new IntHashSet(); |
| 372 |
IntIterator ii = iterator(); |
| 373 |
while (ii.hasNext()) { |
| 374 |
s.add(ii.next()); |
| 375 |
} |
| 376 |
return s; |
| 377 |
} |
| 378 |
|
| 379 |
public IntSet mutableCopy() { |
| 380 |
return copy(); |
| 381 |
} |
| 382 |
|
| 383 |
public int size() { |
| 384 |
return _n; |
| 385 |
} |
| 386 |
|
| 387 |
public boolean isEmpty() { |
| 388 |
return _n == 0; |
| 389 |
} |
| 390 |
|
| 391 |
public boolean contains(int key) { |
| 392 |
return _value[indexOf(key)] != null; |
| 393 |
} |
| 394 |
|
| 395 |
public boolean remove(int value) { |
| 396 |
throw new UnsupportedOperationException("Immutable set"); |
| 397 |
} |
| 398 |
|
| 399 |
public boolean add(int value) { |
| 400 |
throw new UnsupportedOperationException("Immutable set"); |
| 401 |
} |
| 402 |
|
| 403 |
public IntIterator iterator() { |
| 404 |
return new IntHashMapKeyIterator(); |
| 405 |
} |
| 406 |
|
| 407 |
public IntSet union(IntSet other) { |
| 408 |
return copy().union(other); |
| 409 |
} |
| 410 |
|
| 411 |
public IntSet intersect(IntSet other) { |
| 412 |
return copy().intersect(other); |
| 413 |
} |
| 414 |
|
| 415 |
public IntSet except(IntSet other) { |
| 416 |
return copy().except(other); |
| 417 |
} |
| 418 |
|
| 419 |
public boolean containsAll(IntSet other) { |
| 420 |
return copy().containsAll(other); |
| 421 |
} |
| 422 |
|
| 423 |
public String toString() { |
| 424 |
return IntHashSet.toString(iterator()); |
| 425 |
} |
| 426 |
}; |
| 427 |
} |
| 428 |
|
| 429 |
} |
| 430 |
|