示例#1
0
 public int hashCode() {
   int h = 0;
   for (int i = _pos; i-- > 0; ) {
     h += HashFunctions.hash(_data[i]);
   }
   return h;
 }
示例#2
0
  /**
   * @param key the key to be searched in the receiver.
   * @return the index where the key is contained in the receiver, else returns -1.
   */
  protected int indexOfKey(int key) {
    final int tab[] = table;
    final byte stat[] = state;
    final int length = tab.length;

    final int hash = HashFunctions.hash(key) & 0x7FFFFFFF;
    int i = hash % length;
    int decrement =
        hash
            % (length
                - 2); // double hashing, see
                      // http://www.eece.unm.edu/faculty/heileman/hash/node4.html
    // int decrement = (hash / length) % length;
    if (decrement == 0) decrement = 1;

    // stop if we find a free slot, or if we find the key itself.
    // do skip over removed slots (yes, open addressing is like that...)
    // assertion: there is at least one FREE slot.
    while (stat[i] != FREE && (stat[i] == REMOVED || tab[i] != key)) {
      i -= decrement;
      // hashCollisions++;
      if (i < 0) i += length;
    }

    if (stat[i] == FREE) return -1; // not found
    return i; // found, return index where key is contained
  }
 public int hashCode() {
   int h = 0;
   int i = this._pos;
   while (i-- > 0) {
     h = 37 * h + HashFunctions.hash(this._data[i]);
   }
   return h;
 }
示例#4
0
 /**
  * Ensure that this hashtable has sufficient capacity to hold <tt>desiredCapacity<tt>
  * <b>additional</b> elements without requiring a rehash. This is a tuning method you can call
  * before doing a large insert.
  *
  * @param desiredCapacity an <code>int</code> value
  */
 public void ensureCapacity(int desiredCapacity) {
   if (desiredCapacity > (_maxSize - size())) {
     rehash(
         PrimeFinder.nextPrime(
             HashFunctions.fastCeil((desiredCapacity + size()) / _loadFactor) + 1));
     computeMaxSize(capacity());
   }
 }
 /** {@inheritDoc} */
 @Override
 public int hashCode() {
   int h = 0;
   for (int i = size(); i-- > 0; ) {
     h += HashFunctions.hash(get(i));
   }
   return h;
 }
 /** {@inheritDoc} */
 public int hashCode() {
   int hashcode = 0;
   for (int i = _states.length; i-- > 0; ) {
     if (_states[i] == FULL) {
       hashcode += HashFunctions.hash(_set[i]);
     }
   }
   return hashcode;
 }
示例#7
0
  /**
   * Creates a new <code>THash</code> instance with a prime capacity at or near the minimum needed
   * to hold <tt>initialCapacity</tt> elements with load factor <tt>loadFactor</tt> without
   * triggering a rehash.
   *
   * @param initialCapacity an <code>int</code> value
   * @param loadFactor a <code>float</code> value
   */
  public THash(int initialCapacity, float loadFactor) {
    super();
    _loadFactor = loadFactor;

    // Through testing, the load factor (especially the default load factor) has been
    // found to be a pretty good starting auto-compaction factor.
    _autoCompactionFactor = loadFactor;

    setUp(HashFunctions.fastCeil(initialCapacity / loadFactor));
  }
示例#8
0
  /**
   * Compresses the hashtable to the minimum prime size (as defined by PrimeFinder) that will hold
   * all of the elements currently in the table. If you have done a lot of <tt>remove</tt>
   * operations and plan to do a lot of queries or insertions or iteration, it is a good idea to
   * invoke this method. Doing so will accomplish two things:
   *
   * <ol>
   *   <li>You'll free memory allocated to the table but no longer needed because of the remove()s.
   *   <li>You'll get better query/insert/iterator performance because there won't be any
   *       <tt>REMOVED</tt> slots to skip over when probing for indices in the table.
   * </ol>
   */
  public void compact() {
    // need at least one free spot for open addressing
    rehash(PrimeFinder.nextPrime(HashFunctions.fastCeil(size() / _loadFactor) + 1));
    computeMaxSize(capacity());

    // If auto-compaction is enabled, re-determine the compaction interval
    if (_autoCompactionFactor != 0) {
      computeNextAutoCompactionAmount(size());
    }
  }
  /**
   * @param key the key to be added to the receiver.
   * @return the index where the key would need to be inserted, if it is not already contained.
   *     Returns -index-1 if the key is already contained at slot index. Therefore, if the returned
   *     index < 0, then it is already contained at slot -index-1. If the returned index >= 0, then
   *     it is NOT already contained and should be inserted at slot index.
   */
  protected int indexOfInsertion(double key) {
    final int length = table.length;

    final int hash = HashFunctions.hash(key) & 0x7FFFFFFF;
    int i = hash % length;
    int decrement =
        hash
            % (length
                - 2); // double hashing, see
                      // http://www.eece.unm.edu/faculty/heileman/hash/node4.html
    // int decrement = (hash / length) % length;
    if (decrement == 0) {
      decrement = 1;
    }

    // stop if we find a removed or free slot, or if we find the key itself
    // do NOT skip over removed slots (yes, open addressing is like that...)
    while (state[i] == FULL && table[i] != key) {
      i -= decrement;
      // hashCollisions++;
      if (i < 0) {
        i += length;
      }
    }

    if (state[i] == REMOVED) {
      // stop if we find a free slot, or if we find the key itself.
      // do skip over removed slots (yes, open addressing is like that...)
      // assertion: there is at least one FREE slot.
      final int j = i;
      while (state[i] != FREE && (state[i] == REMOVED || table[i] != key)) {
        i -= decrement;
        // hashCollisions++;
        if (i < 0) {
          i += length;
        }
      }
      if (state[i] == FREE) {
        i = j;
      }
    }

    if (state[i] == FULL) {
      // key already contained at slot i.
      // return a negative number identifying the slot.
      return -i - 1;
    }
    // not already contained, should be inserted at slot i.
    // return a number >= 0 identifying the slot.
    return i;
  }
 public final boolean execute(float key, int value) {
   h += (_hashingStrategy.computeHashCode(key) ^ HashFunctions.hash(value));
   return true;
 }
 public final boolean execute(final int key, final Object value) {
   this.h +=
       (TIntObjectHashMap.this._hashingStrategy.computeHashCode(key)
           ^ HashFunctions.hash(value));
   return true;
 }