Skip to content

Instantly share code, notes, and snippets.

@rjhall
Created November 15, 2013 22:38
Show Gist options
  • Save rjhall/7492876 to your computer and use it in GitHub Desktop.
Save rjhall/7492876 to your computer and use it in GitHub Desktop.
the_fugly.java
static class TObjectDoubleHashMapNoDouble<K> extends TObjectDoubleHashMap<K> {
public TObjectDoubleHashMapNoDouble(int initialSize, float loadFactor) {
super(initialSize, loadFactor);
}
public double put(K key, double value) {
int index = insertKey(key);
double previous = 0.0;
boolean isNewMapping = true;
if (index < 0) {
index = -index - 1;
previous = _values[index];
isNewMapping = false;
}
_values[index] = value;
if (isNewMapping) {
postInsertHook2(consumeFreeSlot);
}
return previous;
}
protected final void postInsertHook2( 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 ? gnu.trove.impl.PrimeFinder.nextPrime( (int)(capacity() * 1.2) + 10 ) : capacity();
rehash( newCapacity );
computeMaxSize( capacity() );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment