1 package org.apache.lucene.util.collections;
3 import java.util.Arrays;
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * An Array-based hashtable which maps primitive int to a primitive double.<br>
24 * The hashtable is constracted with a given capacity, or 16 as a default. In
25 * case there's not enough room for new pairs, the hashtable grows. <br>
26 * Capacity is adjusted to a power of 2, and there are 2 * capacity entries for
29 * The pre allocated arrays (for keys, values) are at length of capacity + 1,
30 * when index 0 is used as 'Ground' or 'NULL'.<br>
32 * The arrays are allocated ahead of hash operations, and form an 'empty space'
33 * list, to which the key,value pair is allocated.
35 * @lucene.experimental
37 public class IntToDoubleMap {
39 public static final double GROUND = Double.NaN;
42 * Implements an IntIterator which iterates over all the allocated indexes.
44 private final class IndexIterator implements IntIterator {
46 * The last used baseHashIndex. Needed for "jumping" from one hash entry
49 private int baseHashIndex = 0;
52 * The next not-yet-visited index.
54 private int index = 0;
57 * Index of the last visited pair. Used in {@link #remove()}.
59 private int lastIndex = 0;
62 * Create the Iterator, make <code>index</code> point to the "first"
63 * index which is not empty. If such does not exist (eg. the map is
64 * empty) it would be zero.
66 public IndexIterator() {
67 for (baseHashIndex = 0; baseHashIndex < baseHash.length; ++baseHashIndex) {
68 index = baseHash[baseHashIndex];
75 public boolean hasNext() {
80 // Save the last index visited
86 // if the next index points to the 'Ground' it means we're done with
87 // the current hash entry and we need to jump to the next one. This
88 // is done until all the hash entries had been visited.
89 while (index == 0 && ++baseHashIndex < baseHash.length) {
90 index = baseHash[baseHashIndex];
96 public void remove() {
97 IntToDoubleMap.this.remove(keys[lastIndex]);
103 * Implements an IntIterator, used for iteration over the map's keys.
105 private final class KeyIterator implements IntIterator {
106 private IntIterator iterator = new IndexIterator();
110 public boolean hasNext() {
111 return iterator.hasNext();
115 return keys[iterator.next()];
118 public void remove() {
124 * Implements an Iterator of a generic type T used for iteration over the
127 private final class ValueIterator implements DoubleIterator {
128 private IntIterator iterator = new IndexIterator();
132 public boolean hasNext() {
133 return iterator.hasNext();
136 public double next() {
137 return values[iterator.next()];
140 public void remove() {
146 * Default capacity - in case no capacity was specified in the constructor
148 private static int defaultCapacity = 16;
151 * Holds the base hash entries. if the capacity is 2^N, than the base hash
152 * holds 2^(N+1). It can hold
157 * The current capacity of the map. Always 2^N and never less than 16. We
158 * never use the zero index. It is needed to improve performance and is also
161 private int capacity;
163 * All objects are being allocated at map creation. Those objects are "free"
164 * or empty. Whenever a new pair comes along, a pair is being "allocated" or
165 * taken from the free-linked list. as this is just a free list.
167 private int firstEmpty;
170 * hashFactor is always (2^(N+1)) - 1. Used for faster hashing.
172 private int hashFactor;
175 * This array holds the unique keys
180 * In case of collisions, we implement a double linked list of the colliding
181 * hash's with the following next[] and prev[]. Those are also used to store
189 * Number of currently objects in the map.
194 * This array holds the values
199 * Constructs a map with default capacity.
201 public IntToDoubleMap() {
202 this(defaultCapacity);
206 * Constructs a map with given capacity. Capacity is adjusted to a native
207 * power of 2, with minimum of 16.
210 * minimum capacity for the map.
212 public IntToDoubleMap(int capacity) {
214 // Minimum capacity is 16..
215 while (this.capacity < capacity) {
216 // Multiply by 2 as long as we're still under the requested capacity
220 // As mentioned, we use the first index (0) as 'Ground', so we need the
221 // length of the arrays to be one more than the capacity
222 int arrayLength = this.capacity + 1;
224 this.values = new double[arrayLength];
225 this.keys = new int[arrayLength];
226 this.next = new int[arrayLength];
228 // Hash entries are twice as big as the capacity.
229 int baseHashSize = this.capacity << 1;
231 this.baseHash = new int[baseHashSize];
233 this.values[0] = GROUND;
235 // The has factor is 2^M - 1 which is used as an "AND" hashing operator.
236 // {@link #calcBaseHash()}
237 this.hashFactor = baseHashSize - 1;
245 * Adds a pair to the map. Takes the first empty position from the
246 * empty-linked-list's head - {@link firstEmpty}.
248 * New pairs are always inserted to baseHash, and are followed by the old
252 * integer which maps the given Object
254 * double value which is being mapped using the given key
256 private void prvt_put(int key, double v) {
257 // Hash entry to which the new pair would be inserted
258 int hashIndex = calcBaseHashIndex(key);
260 // 'Allocating' a pair from the "Empty" list.
261 int objectIndex = firstEmpty;
264 firstEmpty = next[firstEmpty];
265 values[objectIndex] = v;
266 keys[objectIndex] = key;
268 // Inserting the new pair as the first node in the specific hash entry
269 next[objectIndex] = baseHash[hashIndex];
270 baseHash[hashIndex] = objectIndex;
272 // Announcing a new pair was added!
277 * Calculating the baseHash index using the internal <code>hashFactor</code>
282 protected int calcBaseHashIndex(int key) {
283 return key & hashFactor;
287 * Empties the map. Generates the "Empty" space list for later allocation.
289 public void clear() {
290 // Clears the hash entries
291 Arrays.fill(this.baseHash, 0);
296 // Mark all array entries as empty. This is done with
297 // <code>firstEmpty</code> pointing to the first valid index (1 as 0 is
298 // used as 'Ground').
301 // And setting all the <code>next[i]</code> to point at
303 for (int i = 1; i < this.capacity;) {
307 // Surly, the last one should point to the 'Ground'.
308 next[this.capacity] = 0;
312 * Checks if a given key exists in the map.
315 * that is checked against the map data.
316 * @return true if the key exists in the map. false otherwise.
318 public boolean containsKey(int key) {
319 return find(key) != 0;
323 * Checks if the given value exists in the map.<br>
324 * This method iterates over the collection, trying to find an equal object.
327 * double value that is checked against the map data.
328 * @return true if the value exists in the map, false otherwise.
330 public boolean containsValue(double value) {
331 for (DoubleIterator iterator = iterator(); iterator.hasNext();) {
332 double d = iterator.next();
341 * Find the actual index of a given key.
344 * @return index of the key. zero if the key wasn't found.
346 protected int find(int key) {
347 // Calculate the hash entry.
348 int baseHashIndex = calcBaseHashIndex(key);
350 // Start from the hash entry.
351 int localIndex = baseHash[baseHashIndex];
353 // while the index does not point to the 'Ground'
354 while (localIndex != 0) {
355 // returns the index found in case of of a matching key.
356 if (keys[localIndex] == key) {
360 // next the local index
361 localIndex = next[localIndex];
364 // If we got this far, it could only mean we did not find the key we
365 // were asked for. return 'Ground' index.
370 * Find the actual index of a given key with it's baseHashIndex.<br>
371 * Some methods use the baseHashIndex. If those call {@link #find()} there's
372 * no need to re-calculate that hash.
375 * @param baseHashIndex
376 * @return the index of the given key, or 0 as 'Ground' if the key wasn't
379 private int findForRemove(int key, int baseHashIndex) {
380 // Start from the hash entry.
382 int index = baseHash[baseHashIndex];
384 // while the index does not point to the 'Ground'
386 // returns the index found in case of of a matching key.
387 if (keys[index] == key) {
391 // next the local index
396 // If we got this far, it could only mean we did not find the key we
397 // were asked for. return 'Ground' index.
403 * Returns the value mapped with the given key.
406 * int who's mapped object we're interested in.
407 * @return a double value mapped by the given key. Double.NaN if the key wasn't found.
409 public double get(int key) {
410 return values[find(key)];
414 * Grows the map. Allocates a new map of double the capacity, and
415 * fast-insert the old key-value pairs.
417 protected void grow() {
418 IntToDoubleMap that = new IntToDoubleMap(
421 // Iterates fast over the collection. Any valid pair is put into the new
422 // map without checking for duplicates or if there's enough space for
424 for (IndexIterator iterator = new IndexIterator(); iterator.hasNext();) {
425 int index = iterator.next();
426 that.prvt_put(this.keys[index], this.values[index]);
429 // Copy that's data into this.
430 this.capacity = that.capacity;
431 this.size = that.size;
432 this.firstEmpty = that.firstEmpty;
433 this.values = that.values;
434 this.keys = that.keys;
435 this.next = that.next;
436 this.baseHash = that.baseHash;
437 this.hashFactor = that.hashFactor;
442 * @return true if the map is empty. false otherwise.
444 public boolean isEmpty() {
449 * Returns a new iterator for the mapped double values.
451 public DoubleIterator iterator() {
452 return new ValueIterator();
455 /** Returns an iterator on the map keys. */
456 public IntIterator keyIterator() {
457 return new KeyIterator();
461 * Prints the baseHash array, used for debug purposes.
463 @SuppressWarnings("unused")
464 private void printBaseHash() {
465 for (int i = 0; i < this.baseHash.length; i++) {
466 System.out.println(i + ".\t" + baseHash[i]);
471 * Inserts the <key,value> pair into the map. If the key already exists,
472 * this method updates the mapped value to the given one, returning the old
475 * @return the old mapped value, or {@link Double#NaN} if the key didn't exist.
477 public double put(int key, double v) {
479 int index = find(key);
483 // Set new data and exit.
484 double old = values[index];
489 // Is there enough room for a new pair?
490 if (size == capacity) {
495 // Now that everything is set, the pair can be just put inside with no
503 * Removes a <key,value> pair from the map and returns the mapped value,
504 * or {@link Double#NaN} if the none existed.
506 * @param key used to find the value to remove
507 * @return the removed value or {@link Double#NaN} if none existed.
509 public double remove(int key) {
510 int baseHashIndex = calcBaseHashIndex(key);
511 int index = findForRemove(key, baseHashIndex);
513 // If it is the first in the collision list, we should promote its
514 // next colliding element.
516 baseHash[baseHashIndex] = next[index];
519 next[prev] = next[index];
520 next[index] = firstEmpty;
523 return values[index];
530 * @return number of pairs currently in the map
537 * Translates the mapped pairs' values into an array of Objects
539 * @return a double array of all the values currently in the map.
541 public double[] toArray() {
543 double[] array = new double[size];
545 // Iterates over the values, adding them to the array.
546 for (DoubleIterator iterator = iterator(); iterator.hasNext();) {
547 array[++j] = iterator.next();
553 * Translates the mapped pairs' values into an array of T
556 * the array into which the elements of the list are to be
557 * stored. If it is big enough use whatever space we need,
558 * setting the one after the true data as {@link Double#NaN}.
560 * @return an array containing the elements of the list, using the given
561 * parameter if big enough, otherwise allocate an appropriate array
565 public double[] toArray(double[] a) {
567 if (a.length < this.size()) {
568 a = new double[this.size()];
571 // Iterates over the values, adding them to the array.
572 for (DoubleIterator iterator = iterator(); iterator.hasNext(); ++j) {
573 a[j] = iterator.next();
584 public String toString() {
585 StringBuffer sb = new StringBuffer();
587 IntIterator keyIterator = keyIterator();
588 while (keyIterator.hasNext()) {
589 int key = keyIterator.next();
593 if (keyIterator.hasNext()) {
599 return sb.toString();
603 public int hashCode() {
604 return getClass().hashCode() ^ size();
608 public boolean equals(Object o) {
609 IntToDoubleMap that = (IntToDoubleMap)o;
610 if (that.size() != this.size()) {
614 IntIterator it = keyIterator();
615 while (it.hasNext()) {
617 if (!that.containsKey(key)) {
621 double v1 = this.get(key);
622 double v2 = that.get(key);
623 if (Double.compare(v1, v2) != 0) {