Method from org.apache.mahout.math.map.OpenHashMap Detail: |
public void clear() {
Arrays.fill(this.state, FREE);
distinct = 0;
freeEntries = table.length; // delta
trimToSize();
}
Removes all (key,value) associations from the receiver. Implicitly calls trimToSize(). |
public Object clone() {
OpenHashMap< K,V > copy = (OpenHashMap< K,V >) super.clone();
copy.table = copy.table.clone();
copy.values = copy.values.clone();
copy.state = copy.state.clone();
return copy;
}
Returns a deep copy of the receiver. |
public boolean containsKey(Object key) {
return indexOfKey((K)key) >= 0;
}
Returns true if the receiver contains the specified key. |
public boolean containsValue(Object value) {
return indexOfValue((V)value) >= 0;
}
Returns true if the receiver contains the specified value. |
public void ensureCapacity(int minCapacity) {
if (table.length < minCapacity) {
int newCapacity = nextPrime(minCapacity);
rehash(newCapacity);
}
}
Ensures that the receiver can hold at least the specified number of associations without needing to allocate new
internal memory. If necessary, allocates new internal memory and increases the capacity of the receiver. This
method never need be called; it is for performance tuning only. Calling this method before put()ing a
large number of associations boosts performance, because the receiver will grow only once instead of potentially
many times and hash collisions get less probable. |
public Set<K, V> entrySet() {
final Set< Entry< K, V > > entries = new OpenHashSet< Map.Entry< K,V > >();
forEachPair(new ObjectObjectProcedure< K,V >() {
@Override
public boolean apply(K key, V value) {
entries.add(new MapEntry(key, value));
return true;
}});
return entries;
}
Allocate a set to contain Map.Entry objects for the pairs and return it. |
public boolean equals(Object obj) {
if (! (obj instanceof OpenHashMap)) {
return false;
}
final OpenHashMap< K,V > o = (OpenHashMap< K,V >) obj;
if (o.size() != size()) {
return false;
}
final boolean[] equal = new boolean[1];
equal[0] = true;
forEachPair(new ObjectObjectProcedure< K,V >() {
@Override
public boolean apply(K key, V value) {
Object ov = o.get(key);
if (!value.equals(ov)) {
equal[0] = false;
return false;
}
return true;
}});
return equal[0];
}
|
public boolean forEachKey(ObjectProcedure<K> procedure) {
for (int i = table.length; i-- > 0;) {
if (state[i] == FULL) {
if (!procedure.apply((K)table[i])) {
return false;
}
}
}
return true;
}
Applies a procedure to each key of the receiver, if any. Note: Iterates over the keys in no particular order.
Subclasses can define a particular order, for example, "sorted by key". All methods which can be expressed
in terms of this method (most methods can) must guarantee to use the same order defined by this
method, even if it is no particular order. This is necessary so that, for example, methods keys and
values will yield association pairs, not two uncorrelated lists. |
public boolean forEachPair(ObjectObjectProcedure<K, V> procedure) {
for (int i = table.length; i-- > 0;) {
if (state[i] == FULL) {
if (!procedure.apply((K)table[i], (V)values[i])) {
return false;
}
}
}
return true;
}
Applies a procedure to each (key,value) pair of the receiver, if any. Iteration order is guaranteed to be
identical to the order used by method #forEachKey(ObjectProcedure) . |
public V get(Object key) {
int i = indexOfKey((K)key);
if (i < 0) {
return null;
} //not contained
return (V)values[i];
}
Returns the value associated with the specified key. It is often a good idea to first check with #containsKey(Object) whether the given key has a value associated or not, i.e. whether there exists an association
for the given key or not. |
void getInternalFactors(int[] capacity,
double[] minLoadFactor,
double[] maxLoadFactor) {
capacity[0] = table.length;
minLoadFactor[0] = this.minLoadFactor;
maxLoadFactor[0] = this.maxLoadFactor;
}
|
protected int indexOfInsertion(K key) {
Object[] tab = table;
byte[] stat = state;
int length = tab.length;
int hash = key.hashCode() & 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 (stat[i] == FULL && !equalsMindTheNull(key, tab[i])) {
i -= decrement;
//hashCollisions++;
if (i < 0) {
i += length;
}
}
if (stat[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.
int j = i;
while (stat[i] != FREE && (stat[i] == REMOVED || tab[i] != key)) {
i -= decrement;
//hashCollisions++;
if (i < 0) {
i += length;
}
}
if (stat[i] == FREE) {
i = j;
}
}
if (stat[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;
}
|
protected int indexOfKey(K key) {
Object[] tab = table;
byte[] stat = state;
int length = tab.length;
int hash = key.hashCode() & 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...)
while (stat[i] != FREE && (stat[i] == REMOVED || !equalsMindTheNull(key, tab[i]))) {
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
}
|
protected int indexOfValue(V value) {
Object[] val = values;
byte[] stat = state;
for (int i = stat.length; --i >= 0;) {
if (stat[i] == FULL && equalsMindTheNull(val[i], value)) {
return i;
}
}
return -1; // not found
}
|
public Set<K> keySet() {
final Set< K > keys = new OpenHashSet< K >();
forEachKey(new ObjectProcedure< K >() {
@Override
public boolean apply(K element) {
keys.add(element);
return true;
}});
return keys;
}
Allocate a set to contain keys and return it.
This violates the 'backing' provisions of the map interface. |
public void keys(List<K> list) {
list.clear();
Object [] tab = table;
byte[] stat = state;
for (int i = tab.length; i-- > 0;) {
if (stat[i] == FULL) {
list.add((K)tab[i]);
}
}
}
Fills all keys contained in the receiver into the specified list. Fills the list, starting at index 0. After this
call returns the specified list has a new size that equals this.size().
This method can be used
to iterate over the keys of the receiver. |
public V put(K key,
V value) {
int i = indexOfInsertion(key);
if (i < 0) { //already contained
i = -i - 1;
V previous = (V) this.values[i];
this.values[i] = value;
return previous;
}
if (this.distinct > this.highWaterMark) {
int newCapacity = chooseGrowCapacity(this.distinct + 1, this.minLoadFactor, this.maxLoadFactor);
rehash(newCapacity);
return put(key, value);
}
this.table[i] = key;
this.values[i] = value;
if (this.state[i] == FREE) {
this.freeEntries--;
}
this.state[i] = FULL;
this.distinct++;
if (this.freeEntries < 1) { //delta
int newCapacity = chooseGrowCapacity(this.distinct + 1, this.minLoadFactor, this.maxLoadFactor);
rehash(newCapacity);
}
return null;
}
Associates the given key with the given value. Replaces any old (key,someOtherValue) association, if
existing. |
public void putAll(Map<? extends K, ? extends V> m) {
for (Map.Entry< ? extends K, ? extends V > e : m.entrySet()) {
put(e.getKey(), e.getValue());
}
}
|
protected void rehash(int newCapacity) {
int oldCapacity = table.length;
//if (oldCapacity == newCapacity) return;
Object[] oldTable = table;
Object[] oldValues = values;
byte[] oldState = state;
Object[] newTable = new Object[newCapacity];
Object[] newValues = new Object[newCapacity];
byte[] newState = new byte[newCapacity];
this.lowWaterMark = chooseLowWaterMark(newCapacity, this.minLoadFactor);
this.highWaterMark = chooseHighWaterMark(newCapacity, this.maxLoadFactor);
this.table = newTable;
this.values = newValues;
this.state = newState;
this.freeEntries = newCapacity - this.distinct; // delta
for (int i = oldCapacity; i-- > 0;) {
if (oldState[i] == FULL) {
Object element = oldTable[i];
int index = indexOfInsertion((K)element);
newTable[index] = element;
newValues[index] = oldValues[i];
newState[index] = FULL;
}
}
}
Rehashes the contents of the receiver into a new table with a smaller or larger capacity. This method is called
automatically when the number of keys in the receiver exceeds the high water mark or falls below the low water
mark. |
public V remove(Object key) {
int i = indexOfKey((K)key);
if (i < 0) {
return null;
}
// key not contained
V removed = (V) values[i];
this.state[i] = REMOVED;
//this.values[i]=0; // delta
this.distinct--;
if (this.distinct < this.lowWaterMark) {
int newCapacity = chooseShrinkCapacity(this.distinct, this.minLoadFactor, this.maxLoadFactor);
rehash(newCapacity);
}
return removed;
}
Removes the given key with its associated element from the receiver, if present. |
protected void setUp(int initialCapacity,
double minLoadFactor,
double maxLoadFactor) {
int capacity = initialCapacity;
super.setUp(capacity, minLoadFactor, maxLoadFactor);
capacity = nextPrime(capacity);
if (capacity == 0) {
capacity = 1;
} // open addressing needs at least one FREE slot at any time.
this.table = new Object[capacity];
this.values = new Object[capacity];
this.state = new byte[capacity];
// memory will be exhausted long before this pathological case happens, anyway.
this.minLoadFactor = minLoadFactor;
if (capacity == PrimeFinder.largestPrime) {
this.maxLoadFactor = 1.0;
} else {
this.maxLoadFactor = maxLoadFactor;
}
this.distinct = 0;
this.freeEntries = capacity; // delta
// lowWaterMark will be established upon first expansion.
// establishing it now (upon instance construction) would immediately make the table shrink upon first put(...).
// After all the idea of an "initialCapacity" implies violating lowWaterMarks when an object is young.
// See ensureCapacity(...)
this.lowWaterMark = 0;
this.highWaterMark = chooseHighWaterMark(capacity, this.maxLoadFactor);
}
Initializes the receiver. |
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append('{');
forEachPair(new ObjectObjectProcedure< K,V >() {
@Override
public boolean apply(K key, V value) {
sb.append('[');
sb.append(key);
sb.append(" - > ");
sb.append(value);
sb.append("] ");
return true;
}});
sb.append('}');
return sb.toString();
}
|
public void trimToSize() {
// * 1.2 because open addressing's performance exponentially degrades beyond that point
// so that even rehashing the table can take very long
int newCapacity = nextPrime((int) (1 + 1.2 * size()));
if (table.length > newCapacity) {
rehash(newCapacity);
}
}
Trims the capacity of the receiver to be the receiver's current size. Releases any superfluous internal memory. An
application can use this operation to minimize the storage of the receiver. |
public Collection<V> values() {
final List< V > valueList = new ArrayList< V >();
forEachPair(new ObjectObjectProcedure< K,V >() {
@Override
public boolean apply(K key, V value) {
valueList.add(value);
return true;
}});
return valueList;
}
Allocate a list to contain the values and return it.
This violates the 'backing' provision of the Map interface. |