/** * Empties the collection. */ public void clear() { _size = 0; _free = capacity(); }
protected int calculateGrownCapacity() { return capacity() << 1; }
/** * Create an instance of TIterator over the specified THash. */ public TIterator(THash hash) { _hash = hash; _expectedSize = _hash.size(); _index = _hash.capacity(); }
/** * After an insert, this hook is called to adjust the size/free values of the set and to perform * rehashing if necessary. */ protected final void postInsertHook(boolean usedFreeSlot) { if (usedFreeSlot) { _free--; } // rehash whenever we exhaust the available space in the table if (++_size > _maxSize || _free == 0) { // choose a new capacity suited to the new state of the table // if we've grown beyond our maximum size, double capacity; // if we've exhausted the free spots, rehash to the same capacity, // which will free up any stale removed slots for reuse. int newCapacity = _size > _maxSize ? PrimeFinder.nextPrime(capacity() << 1) : capacity(); rehash(newCapacity); computeMaxSize(capacity()); } }
/** * 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((int) Math.ceil( (desiredCapacity + size()) / _loadFactor) + 1)); computeMaxSize(capacity()); } }
/** * 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> * * <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.</li> </ol> */ public void compact() { // need at least one free spot for open addressing rehash(PrimeFinder.nextPrime((int) Math.ceil(size() / _loadFactor) + 1)); computeMaxSize(capacity()); // If auto-compaction is enabled, re-determine the compaction interval if (_autoCompactionFactor != 0) { computeNextAutoCompactionAmount(size()); } }