All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.carrotsearch.hppcrt.maps.LongShortOpenHashMap Maven / Gradle / Ivy

Go to download

High Performance Primitive Collections Realtime (fork of HPPC of Carrotsearch) Fundamental data structures (maps, sets, lists, stacks, queues, heaps, sorts) generated for combinations of object and primitive types to conserve JVM memory and speed up execution. The Realtime fork intend of extending collections while tweaking and optimizations to remove any dynamic allocations at runtime, and low variance execution times.

There is a newer version: 0.7.5
Show newest version
package com.carrotsearch.hppcrt.maps;

import com.carrotsearch.hppcrt.*;
import com.carrotsearch.hppcrt.cursors.*;
import com.carrotsearch.hppcrt.predicates.*;
import com.carrotsearch.hppcrt.procedures.*;
import com.carrotsearch.hppcrt.hash.*;

  
  
  
  
// If RH is defined, RobinHood Hashing is in effect :
  

/**
 * A hash map of long to short, implemented using open
 * addressing with linear probing for collision resolution.
 * 
 * 

* The internal buffers of this implementation ({@link #keys}, {@link #values}), * are always allocated to the nearest size that is a power of two. When * the capacity exceeds the given load factor, the buffer size is doubled. *

* *

See {@link ObjectObjectOpenHashMap} class for API similarities and differences against Java * Collections. * * * *

Important note. The implementation uses power-of-two tables and linear * probing, which may cause poor performance (many collisions) if hash values are * not properly distributed. * * * * @author This code is inspired by the collaboration and implementation in the fastutil project. * * */ @javax.annotation.Generated(date = "2015-02-27T19:21:14+0100", value = "HPPC-RT generated from: LongShortOpenHashMap.java") public class LongShortOpenHashMap implements LongShortMap, Cloneable { /** * Minimum capacity for the map. */ public final static int MIN_CAPACITY = HashContainerUtils.MIN_CAPACITY; /** * Default capacity. */ public final static int DEFAULT_CAPACITY = HashContainerUtils.DEFAULT_CAPACITY; /** * Default load factor. */ public final static float DEFAULT_LOAD_FACTOR = HashContainerUtils.DEFAULT_LOAD_FACTOR; protected short defaultValue = ((short)0); /** * Hash-indexed array holding all keys. *

* Direct map iteration: iterate {keys[i], values[i]} for i in [0; keys.length[ where keys[i] != 0/null, then also * {0/null, {@link #allocatedDefaultKeyValue} } is in the map if {@link #allocatedDefaultKey} = true. *

* *

Direct iteration warning: * If the iteration goal is to fill another hash container, please iterate {@link #keys} in reverse to prevent performance losses. * @see #values */ public long[] keys; /** * Hash-indexed array holding all values associated to the keys. * stored in {@link #keys}. * * @see #keys */ public short[] values; /** *True if key = 0/null is in the map. */ public boolean allocatedDefaultKey = false; /** * if allocatedDefaultKey = true, contains the associated V to the key = 0/null */ public short allocatedDefaultKeyValue; /** * Cached number of assigned slots in {@link #keys}. */ protected int assigned; /** * The load factor for this map (fraction of allocated slots * before the buffers must be rehashed or reallocated). */ protected final float loadFactor; /** * Resize buffers when {@link #keys} hits this value. */ protected int resizeAt; /** * The most recent slot accessed in {@link #containsKey} (required for * {@link #lget}). * * @see #containsKey * @see #lget */ protected int lastSlot; /** * Creates a hash map with the default capacity of {@value #DEFAULT_CAPACITY}, * load factor of {@value #DEFAULT_LOAD_FACTOR}. * *

See class notes about hash distribution importance.

*/ public LongShortOpenHashMap() { this(LongShortOpenHashMap.DEFAULT_CAPACITY); } /** * Creates a hash map with the given initial capacity, default load factor of * {@value #DEFAULT_LOAD_FACTOR}. * *

See class notes about hash distribution importance.

* * @param initialCapacity Initial capacity (greater than zero and automatically * rounded to the next power of two). */ public LongShortOpenHashMap(final int initialCapacity) { this(initialCapacity, LongShortOpenHashMap.DEFAULT_LOAD_FACTOR); } /** * Creates a hash map with the given initial capacity, * load factor. * *

See class notes about hash distribution importance.

* * @param initialCapacity Initial capacity (greater than zero and automatically * rounded to the next power of two). * * @param loadFactor The load factor (greater than zero and smaller than 1). */ public LongShortOpenHashMap(final int initialCapacity, final float loadFactor) { assert loadFactor > 0 && loadFactor <= 1 : "Load factor must be between (0, 1]."; this.loadFactor = loadFactor; //take into account of the load factor to garantee no reallocations before reaching initialCapacity. int internalCapacity = (int) (initialCapacity / loadFactor) + LongShortOpenHashMap.MIN_CAPACITY; //align on next power of two internalCapacity = HashContainerUtils.roundCapacity(internalCapacity); this.keys = (new long[internalCapacity]); this.values = (new short[internalCapacity]); //Take advantage of the rounding so that the resize occur a bit later than expected. //allocate so that there is at least one slot that remains allocated = false //this is compulsory to guarantee proper stop in searching loops this.resizeAt = Math.max(3, (int) (internalCapacity * loadFactor)) - 2; } /** * Create a hash map from all key-value pairs of another container. */ public LongShortOpenHashMap(final LongShortAssociativeContainer container) { this(container.size()); putAll(container); } /** * {@inheritDoc} */ @Override public short put(long key, short value) { if (key == (0L)) { if (this.allocatedDefaultKey) { final short previousValue = this.allocatedDefaultKeyValue; this.allocatedDefaultKeyValue = value; return previousValue; } this.allocatedDefaultKeyValue = value; this.allocatedDefaultKey = true; return this.defaultValue; } final int mask = this.keys.length - 1; final long[] keys = this.keys; final short[] values = this.values; //copied straight from fastutil "fast-path" int slot; long curr; //1.1 The rehashed key slot is occupied... if ((curr = keys[slot = ((int)PhiMix.hash(key)) & mask]) != (0L)) { //1.2 the occupied place is indeed key, so only updates the value and nothing else. if ((curr == key)) { final short oldValue = this.values[slot]; values[slot] = value; return oldValue; } //1.3 key is colliding, manage below : } else if (this.assigned < this.resizeAt) { //1.4 key is not colliding, without resize, so insert, return defaultValue. keys[slot] = key; values[slot] = value; this.assigned++; return this.defaultValue; } //2. Slow path, find a place somewhere down there... while ((keys[slot] != (0L))) { if ((key == keys[slot])) { final short oldValue = values[slot]; values[slot] = value; return oldValue; } slot = (slot + 1) & mask; } // Check if we need to grow. If so, reallocate new data, fill in the last element // and rehash. if (this.assigned == this.resizeAt) { expandAndPut(key, value, slot); } else { this.assigned++; keys[slot] = key; values[slot] = value; } return this.defaultValue; } /** * {@inheritDoc} */ @Override public int putAll(final LongShortAssociativeContainer container) { return putAll((Iterable) container); } /** * {@inheritDoc} */ @Override public int putAll(final Iterable iterable) { final int count = this.size(); for (final LongShortCursor c : iterable) { put(c.key, c.value); } return this.size() - count; } /** * {@inheritDoc} */ @Override public boolean putIfAbsent(final long key, final short value) { if (!containsKey(key)) { put(key, value); return true; } return false; } /** * Trove-inspired API method. An equivalent * of the following code: *
     *  if (containsKey(key))
     *  {
     *      short v = (short) (lget() + additionValue);
     *      lset(v);
     *      return v;
     *  }
     *  else
     *  {
     *     put(key, putValue);
     *     return putValue;
     *  }
     * 
* * @param key The key of the value to adjust. * @param putValue The value to put if key does not exist. * @param additionValue The value to add to the existing value if key exists. * @return Returns the current value associated with key (after changes). */ @Override public short putOrAdd(long key, short putValue, short additionValue) { if ((0L) == key) { if (this.allocatedDefaultKey) { this.allocatedDefaultKeyValue += additionValue; return this.allocatedDefaultKeyValue; } this.allocatedDefaultKeyValue = putValue; this.allocatedDefaultKey = true; return putValue; } final int mask = this.keys.length - 1; final long[] keys = this.keys; final short[] values = this.values; short value = putValue; //copied straight from fastutil "fast-path" int slot; long curr; //1.1 The rehashed key slot is occupied... if ((curr = keys[slot = ((int)PhiMix.hash(key)) & mask]) != (0L)) { //1.2 the occupied place is indeed key, so only increments the value and nothing else. if ((curr == key)) { values[slot] += additionValue; return values[slot]; } //1.3 key is colliding, manage below : } else if (this.assigned < this.resizeAt) { //1.4 key is not colliding, without resize, so insert, return defaultValue. keys[slot] = key; values[slot] = value; this.assigned++; return putValue; } while ((keys[slot] != (0L))) { if ((key == keys[slot])) { values[slot] += additionValue; return values[slot]; } slot = (slot + 1) & mask; } if (assigned == resizeAt) { expandAndPut(key, value, slot); } else { assigned++; keys[slot] = key; values[slot] = value; } return putValue; } /** * An equivalent of calling *
     *  if (containsKey(key))
     *  {
     *      short v = (short) (lget() + additionValue);
     *      lset(v);
     *      return v;
     *  }
     *  else
     *  {
     *     put(key, additionValue);
     *     return additionValue;
     *  }
     * 
* * @param key The key of the value to adjust. * @param additionValue The value to put or add to the existing value if key exists. * @return Returns the current value associated with key (after changes). */ @Override public short addTo(long key, short additionValue) { return putOrAdd(key, additionValue, additionValue); } /** * Expand the internal storage buffers (capacity) and rehash. */ private void expandAndPut(final long pendingKey, final short pendingValue, final int freeSlot) { assert this.assigned == this.resizeAt; //default sentinel value is never in the keys[] array, so never trigger reallocs assert (pendingKey != (0L)); // Try to allocate new buffers first. If we OOM, it'll be now without // leaving the data structure in an inconsistent state. final long[] oldKeys = this.keys; final short[] oldValues = this.values; allocateBuffers(HashContainerUtils.nextCapacity(this.keys.length)); // We have succeeded at allocating new data so insert the pending key/value at // the free slot in the old arrays before rehashing. this.lastSlot = -1; this.assigned++; oldKeys[freeSlot] = pendingKey; oldValues[freeSlot] = pendingValue; //for inserts final int mask = this.keys.length - 1; long key = (0L); short value = ((short)0); int slot = -1; final long[] keys = this.keys; final short[] values = this.values; //iterate all the old arrays to add in the newly allocated buffers //It is important to iterate backwards to minimize the conflict chain length ! for (int i = oldKeys.length; --i >= 0;) { if ((oldKeys[i] != (0L))) { key = oldKeys[i]; value = oldValues[i]; slot = ((int)PhiMix.hash(key)) & mask; while ((keys[slot] != (0L))) { slot = (slot + 1) & mask; } //end while keys[slot] = key; values[slot] = value; } } } /** * Allocate internal buffers for a given capacity. * * @param capacity New capacity (must be a power of two). */ private void allocateBuffers(final int capacity) { final long[] keys = (new long[capacity]); final short[] values = (new short[capacity]); this.keys = keys; this.values = values; //allocate so that there is at least one slot that remains allocated = false //this is compulsory to guarantee proper stop in searching loops this.resizeAt = Math.max(3, (int) (capacity * this.loadFactor)) - 2; } /** * {@inheritDoc} */ @Override public short remove(final long key) { if (key == (0L)) { if (this.allocatedDefaultKey) { final short previousValue = this.allocatedDefaultKeyValue; this.allocatedDefaultKey = false; return previousValue; } return this.defaultValue; } final int mask = this.keys.length - 1; final long[] keys = this.keys; final short[] values = this.values; //copied straight from fastutil "fast-path" int slot; long curr; //1.1 The rehashed slot is free, nothing to remove, return default value if ((curr = keys[slot = ((int)PhiMix.hash(key)) & mask]) == (0L)) { return this.defaultValue; } //1.2) The rehashed entry is occupied by the key, remove it, return value if ((curr == key)) { final short value = values[slot]; this.assigned--; shiftConflictingKeys(slot); return value; } //2. Hash collision, search for the key along the path slot = (slot + 1) & mask; //2. Slow path here while ((keys[slot] != (0L)) ) { if ((key == keys[slot])) { final short value = values[slot]; this.assigned--; shiftConflictingKeys(slot); return value; } slot = (slot + 1) & mask; } //end while true return this.defaultValue; } /** * Shift all the slot-conflicting keys allocated to (and including) slot. */ protected void shiftConflictingKeys(int slotCurr) { // Copied nearly verbatim from fastutil's impl. final int mask = this.keys.length - 1; int slotPrev, slotOther; final long[] keys = this.keys; final short[] values = this.values; while (true) { slotCurr = ((slotPrev = slotCurr) + 1) & mask; while ((keys[slotCurr] != (0L))) { slotOther = ((int)PhiMix.hash(keys[slotCurr])) & mask; if (slotPrev <= slotCurr) { // we're on the right of the original slot. if (slotPrev >= slotOther || slotOther > slotCurr) { break; } } else { // we've wrapped around. if (slotPrev >= slotOther && slotOther > slotCurr) { break; } } slotCurr = (slotCurr + 1) & mask; } if (!(keys[slotCurr] != (0L))) { break; } // Shift key/value/allocated triplet. keys[slotPrev] = keys[slotCurr]; values[slotPrev] = values[slotCurr]; } //means not allocated keys[slotPrev] = (0L); /* */ } /** * {@inheritDoc} */ @Override public int removeAll(final LongContainer container) { final int before = this.size(); for (final LongCursor cursor : container) { remove(cursor.value); } return before - this.size(); } /** * {@inheritDoc} *

Important! * If the predicate actually injects the removed keys in another hash container, you may experience performance losses. */ @Override public int removeAll(final LongPredicate predicate) { final int before = this.size(); if (this.allocatedDefaultKey) { if (predicate.apply((0L))) { this.allocatedDefaultKey = false; } } final long[] keys = this.keys; for (int i = 0; i < keys.length;) { if ((keys[i] != (0L))) { if (predicate.apply(keys[i])) { this.assigned--; shiftConflictingKeys(i); // Repeat the check for the same i. continue; } } i++; } return before - this.size(); } /** * {@inheritDoc} * *

Use the following snippet of code to check for key existence * first and then retrieve the value if it exists.

*
     * if (map.containsKey(key))
     *   value = map.lget();
     * 
*/ @Override public short get(final long key) { if (key == (0L)) { if (this.allocatedDefaultKey) { return this.allocatedDefaultKeyValue; } return this.defaultValue; } final int mask = this.keys.length - 1; final long[] keys = this.keys; final short[] values = this.values; //copied straight from fastutil "fast-path" int slot; long curr; //1.1 The rehashed slot is free, nothing to get, return default value if ((curr = keys[slot = ((int)PhiMix.hash(key)) & mask]) == (0L)) { return this.defaultValue; } //1.2) The rehashed entry is occupied by the key, return value if ((curr == key)) { return values[slot]; } //2. Hash collision, search for the key along the path slot = (slot + 1) & mask; while ((keys[slot] != (0L)) ) { if ((key == keys[slot])) { return values[slot]; } slot = (slot + 1) & mask; } //end while true return this.defaultValue; } /** * Returns the last key stored in this has map for the corresponding * most recent call to {@link #containsKey}. * Precondition : {@link #containsKey} must have been called previously ! *

Use the following snippet of code to check for key existence * first and then retrieve the key value if it exists.

*
     * if (map.containsKey(key))
     *   value = map.lkey();
     * 
* *

This is equivalent to calling:

*
     * if (map.containsKey(key))
     *   key = map.keys[map.lslot()];
     * 
*/ public long lkey() { if (this.lastSlot == -2) { return (0L); } assert this.lastSlot >= 0 : "Call containsKey() first."; assert (this.keys[this.lastSlot] != (0L)) : "Last call to exists did not have any associated value."; return this.keys[this.lastSlot]; } /** * Returns the last value saved in a call to {@link #containsKey}. * Precondition : {@link #containsKey} must have been called previously ! * @see #containsKey */ public short lget() { if (this.lastSlot == -2) { return this.allocatedDefaultKeyValue; } assert this.lastSlot >= 0 : "Call containsKey() first."; assert (this.keys[this.lastSlot] != (0L)) : "Last call to exists did not have any associated value."; return this.values[this.lastSlot]; } /** * Sets the value corresponding to the key saved in the last * call to {@link #containsKey}, if and only if the key exists * in the map already. * Precondition : {@link #containsKey} must have been called previously ! * @see #containsKey * @return Returns the previous value stored under the given key. */ public short lset(final short value) { if (this.lastSlot == -2) { final short previous = this.allocatedDefaultKeyValue; this.allocatedDefaultKeyValue = value; return previous; } assert this.lastSlot >= 0 : "Call containsKey() first."; assert (this.keys[this.lastSlot] != (0L)) : "Last call to exists did not have any associated value."; final short previous = this.values[this.lastSlot]; this.values[this.lastSlot] = value; return previous; } /** * @return Returns the slot of the last key looked up in a call to {@link #containsKey} if * it returned true * or else -2 if {@link #containsKey} were successful on key = 0 * @see #containsKey */ public int lslot() { assert this.lastSlot >= 0 || this.lastSlot == -2 : "Call containsKey() first."; return this.lastSlot; } /** * {@inheritDoc} * *

Saves the associated value for fast access using {@link #lget} * or {@link #lset}.

*
     * if (map.containsKey(key))
     *   value = map.lget();
     * 
* or, to modify the value at the given key without looking up * its slot twice: *
     * if (map.containsKey(key))
     *   map.lset(map.lget() + 1);
     * 
* */ @Override public boolean containsKey(final long key) { if (key == (0L)) { if (this.allocatedDefaultKey) { this.lastSlot = -2; } else { this.lastSlot = -1; } return this.allocatedDefaultKey; } final int mask = this.keys.length - 1; final long[] keys = this.keys; //copied straight from fastutil "fast-path" int slot; long curr; //1.1 The rehashed slot is free, return false if ((curr = keys[slot = ((int)PhiMix.hash(key)) & mask]) == (0L)) { this.lastSlot = -1; return false; } //1.2) The rehashed entry is occupied by the key, return true if ((curr == key)) { this.lastSlot = slot; return true; } //2. Hash collision, search for the key along the path slot = (slot + 1) & mask; while ((keys[slot] != (0L)) ) { if ((key == keys[slot])) { this.lastSlot = slot; return true; } slot = (slot + 1) & mask; } //end while true //unsuccessful search this.lastSlot = -1; return false; } /** * {@inheritDoc} * *

Does not release internal buffers.

*/ @Override public void clear() { this.assigned = 0; this.lastSlot = -1; this.allocatedDefaultKey = false; //Faster than Arrays.fill(keys, null); // Help the GC. LongArrays.blankArray(this.keys, 0, this.keys.length); } /** * {@inheritDoc} */ @Override public int size() { return this.assigned + (this.allocatedDefaultKey ? 1 : 0); } /** * {@inheritDoc} */ @Override public int capacity() { return this.resizeAt - 1; } /** * {@inheritDoc} * *

Note that an empty container may still contain many deleted keys (that occupy buffer * space). Adding even a single element to such a container may cause rehashing.

*/ @Override public boolean isEmpty() { return size() == 0; } /** * {@inheritDoc} */ @Override public int hashCode() { int h = 0; if (this.allocatedDefaultKey) { h += 0 + Internals.rehash(this.allocatedDefaultKeyValue); } final long[] keys = this.keys; final short[] values = this.values; for (int i = keys.length; --i >= 0;) { if ((keys[i] != (0L))) { h += ((int)PhiMix.hash(keys[i])) + Internals.rehash(values[i]); } } return h; } /** * {@inheritDoc} */ @Override public boolean equals(final Object obj) { if (obj != null) { if (obj == this) { return true; } if (!(obj instanceof LongShortOpenHashMap)) { return false; } /* */ LongShortMap other = (LongShortMap) obj; if (other.size() == this.size()) { final EntryIterator it = this.iterator(); while (it.hasNext()) { final LongShortCursor c = it.next(); if (other.containsKey(c.key)) { final short v = other.get(c.key); if ((c.value == v)) { continue; } } //recycle it.release(); return false; } return true; } } return false; } /** * An iterator implementation for {@link #iterator}. */ public final class EntryIterator extends AbstractIterator { public final LongShortCursor cursor; public EntryIterator() { this.cursor = new LongShortCursor(); this.cursor.index = -2; } /** * Iterate backwards w.r.t the buffer, to * minimize collision chains when filling another hash container (ex. with putAll()) */ @Override protected LongShortCursor fetch() { if (this.cursor.index == LongShortOpenHashMap.this.keys.length + 1) { if (LongShortOpenHashMap.this.allocatedDefaultKey) { this.cursor.index = LongShortOpenHashMap.this.keys.length; this.cursor.key = (0L); this.cursor.value = LongShortOpenHashMap.this.allocatedDefaultKeyValue; return this.cursor; } //no value associated with the default key, continue iteration... this.cursor.index = LongShortOpenHashMap.this.keys.length; } int i = this.cursor.index - 1; while (i >= 0 && !(LongShortOpenHashMap.this.keys[i] != (0L))) { i--; } if (i == -1) { return done(); } this.cursor.index = i; this.cursor.key = LongShortOpenHashMap.this.keys[i]; this.cursor.value = LongShortOpenHashMap.this.values[i]; return this.cursor; } } /** * internal pool of EntryIterator */ protected final IteratorPool entryIteratorPool = new IteratorPool( new ObjectFactory() { @Override public EntryIterator create() { return new EntryIterator(); } @Override public void initialize(final EntryIterator obj) { obj.cursor.index = LongShortOpenHashMap.this.keys.length + 1; } @Override public void reset(final EntryIterator obj) { // nothing } }); /** * {@inheritDoc} * @return */ @Override public EntryIterator iterator() { //return new EntryIterator(); return this.entryIteratorPool.borrow(); } /** * {@inheritDoc} */ @Override public T forEach(final T procedure) { if (this.allocatedDefaultKey) { procedure.apply((0L), this.allocatedDefaultKeyValue); } final long[] keys = this.keys; final short[] values = this.values; //Iterate in reverse for side-stepping the longest conflict chain //in another hash, in case apply() is actually used to fill another hash container. for (int i = keys.length - 1; i >= 0; i--) { if ((keys[i] != (0L))) { procedure.apply(keys[i], values[i]); } } return procedure; } /** * {@inheritDoc} */ @Override public T forEach(final T predicate) { if (this.allocatedDefaultKey) { if (!predicate.apply((0L), this.allocatedDefaultKeyValue)) { return predicate; } } final long[] keys = this.keys; final short[] values = this.values; //Iterate in reverse for side-stepping the longest conflict chain //in another hash, in case apply() is actually used to fill another hash container. for (int i = keys.length - 1; i >= 0; i--) { if ((keys[i] != (0L))) { if (!predicate.apply(keys[i], values[i])) { break; } } } //end for return predicate; } /** * @return a new KeysContainer view of the keys of this associated container. * This view then reflects all changes from the map. */ @Override public KeysContainer keys() { return new KeysContainer(); } /** * A view of the keys inside this hash map. */ public final class KeysContainer extends AbstractLongCollection implements LongLookupContainer { private final LongShortOpenHashMap owner = LongShortOpenHashMap.this; @Override public boolean contains(final long e) { return containsKey(e); } @Override public T forEach(final T procedure) { if (this.owner.allocatedDefaultKey) { procedure.apply((0L)); } final long[] keys = this.owner.keys; //Iterate in reverse for side-stepping the longest conflict chain //in another hash, in case apply() is actually used to fill another hash container. for (int i = keys.length - 1; i >= 0; i--) { if ((keys[i] != (0L))) { procedure.apply(keys[i]); } } return procedure; } @Override public T forEach(final T predicate) { if (this.owner.allocatedDefaultKey) { if (!predicate.apply((0L))) { return predicate; } } final long[] keys = this.owner.keys; //Iterate in reverse for side-stepping the longest conflict chain //in another hash, in case apply() is actually used to fill another hash container. for (int i = keys.length - 1; i >= 0; i--) { if ((keys[i] != (0L))) { if (!predicate.apply(keys[i])) { break; } } } return predicate; } /** * {@inheritDoc} */ @Override public KeysIterator iterator() { //return new KeysIterator(); return this.keyIteratorPool.borrow(); } /** * {@inheritDoc} */ @Override public int size() { return this.owner.size(); } /** * {@inheritDoc} */ @Override public int capacity() { return this.owner.capacity(); } @Override public void clear() { this.owner.clear(); } @Override public int removeAll(final LongPredicate predicate) { return this.owner.removeAll(predicate); } @Override public int removeAllOccurrences(final long e) { final boolean hasKey = this.owner.containsKey(e); int result = 0; if (hasKey) { this.owner.remove(e); result = 1; } return result; } /** * internal pool of KeysIterator */ protected final IteratorPool keyIteratorPool = new IteratorPool( new ObjectFactory() { @Override public KeysIterator create() { return new KeysIterator(); } @Override public void initialize(final KeysIterator obj) { obj.cursor.index = LongShortOpenHashMap.this.keys.length + 1; } @Override public void reset(final KeysIterator obj) { // nothing } }); @Override public long[] toArray(final long[] target) { int count = 0; if (this.owner.allocatedDefaultKey) { target[count++] = (0L); } final long[] keys = this.owner.keys; for (int i = 0; i < keys.length; i++) { if ((keys[i] != (0L))) { target[count++] = keys[i]; } } assert count == this.owner.size(); return target; } }; /** * An iterator over the set of keys. */ public final class KeysIterator extends AbstractIterator { public final LongCursor cursor; public KeysIterator() { this.cursor = new LongCursor(); this.cursor.index = -2; } /** * Iterate backwards w.r.t the buffer, to * minimize collision chains when filling another hash container (ex. with putAll()) */ @Override protected LongCursor fetch() { if (this.cursor.index == LongShortOpenHashMap.this.keys.length + 1) { if (LongShortOpenHashMap.this.allocatedDefaultKey) { this.cursor.index = LongShortOpenHashMap.this.keys.length; this.cursor.value = (0L); return this.cursor; } //no value associated with the default key, continue iteration... this.cursor.index = LongShortOpenHashMap.this.keys.length; } int i = this.cursor.index - 1; while (i >= 0 && !(LongShortOpenHashMap.this.keys[i] != (0L))) { i--; } if (i == -1) { return done(); } this.cursor.index = i; this.cursor.value = LongShortOpenHashMap.this.keys[i]; return this.cursor; } } /** * @return a new ValuesContainer, view of the values of this map. * This view then reflects all changes from the map. */ @Override public ValuesContainer values() { return new ValuesContainer(); } /** * A view over the set of values of this map. */ public final class ValuesContainer extends AbstractShortCollection { private final LongShortOpenHashMap owner = LongShortOpenHashMap.this; /** * {@inheritDoc} */ @Override public int size() { return this.owner.size(); } /** * {@inheritDoc} */ @Override public int capacity() { return this.owner.capacity(); } @Override public boolean contains(final short value) { if (this.owner.allocatedDefaultKey && (value == this.owner.allocatedDefaultKeyValue)) { return true; } // This is a linear scan over the values, but it's in the contract, so be it. final long[] keys = this.owner.keys; final short[] values = this.owner.values; for (int slot = 0; slot < keys.length; slot++) { if ((keys[slot] != (0L)) && (value == values[slot])) { return true; } } return false; } @Override public T forEach(final T procedure) { if (this.owner.allocatedDefaultKey) { procedure.apply(this.owner.allocatedDefaultKeyValue); } final long[] keys = this.owner.keys; final short[] values = this.owner.values; for (int slot = 0; slot < keys.length; slot++) { if ((keys[slot] != (0L))) { procedure.apply(values[slot]); } } return procedure; } @Override public T forEach(final T predicate) { if (this.owner.allocatedDefaultKey) { if (!predicate.apply(this.owner.allocatedDefaultKeyValue)) { return predicate; } } final long[] keys = this.owner.keys; final short[] values = this.owner.values; for (int slot = 0; slot < keys.length; slot++) { if ((keys[slot] != (0L))) { if (!predicate.apply(values[slot])) { break; } } } return predicate; } @Override public ValuesIterator iterator() { // return new ValuesIterator(); return this.valuesIteratorPool.borrow(); } /** * {@inheritDoc} * Indeed removes all the (key,value) pairs matching * (key ? , e) with the same e, from the map. */ @Override public int removeAllOccurrences(final short e) { final int before = this.owner.size(); if (this.owner.allocatedDefaultKey) { if ((e == this.owner.allocatedDefaultKeyValue)) { this.owner.allocatedDefaultKey = false; } } final long[] keys = this.owner.keys; final short[] values = this.owner.values; for (int slot = 0; slot < keys.length;) { if ((keys[slot] != (0L))) { if ((e == values[slot])) { this.owner.assigned--; shiftConflictingKeys(slot); // Repeat the check for the same i. continue; } } slot++; } return before - this.owner.size(); } /** * {@inheritDoc} * Indeed removes all the (key,value) pairs matching * the predicate for the values, from the map. */ @Override public int removeAll(final ShortPredicate predicate) { final int before = this.owner.size(); if (this.owner.allocatedDefaultKey) { if (predicate.apply(this.owner.allocatedDefaultKeyValue)) { this.owner.allocatedDefaultKey = false; } } final long[] keys = this.owner.keys; final short[] values = this.owner.values; for (int slot = 0; slot < keys.length;) { if ((keys[slot] != (0L))) { if (predicate.apply(values[slot])) { this.owner.assigned--; shiftConflictingKeys(slot); // Repeat the check for the same i. continue; } } slot++; } return before - this.owner.size(); } /** * {@inheritDoc} * Alias for clear() the whole map. */ @Override public void clear() { this.owner.clear(); } /** * internal pool of ValuesIterator */ protected final IteratorPool valuesIteratorPool = new IteratorPool( new ObjectFactory() { @Override public ValuesIterator create() { return new ValuesIterator(); } @Override public void initialize(final ValuesIterator obj) { obj.cursor.index = LongShortOpenHashMap.this.keys.length + 1; } @Override public void reset(final ValuesIterator obj) { // nothing } }); @Override public short[] toArray(final short[] target) { int count = 0; if (this.owner.allocatedDefaultKey) { target[count++] = this.owner.allocatedDefaultKeyValue; } final long[] keys = this.owner.keys; final short[] values = this.owner.values; for (int i = 0; i < values.length; i++) { if ((keys[i] != (0L))) { target[count++] = values[i]; } } assert count == this.owner.size(); return target; } } /** * An iterator over the set of values. */ public final class ValuesIterator extends AbstractIterator { public final ShortCursor cursor; public ValuesIterator() { this.cursor = new ShortCursor(); this.cursor.index = -2; } /** * Iterate backwards w.r.t the buffer, to * minimize collision chains when filling another hash container (ex. with putAll()) */ @Override protected ShortCursor fetch() { if (this.cursor.index == LongShortOpenHashMap.this.values.length + 1) { if (LongShortOpenHashMap.this.allocatedDefaultKey) { this.cursor.index = LongShortOpenHashMap.this.values.length; this.cursor.value = LongShortOpenHashMap.this.allocatedDefaultKeyValue; return this.cursor; } //no value associated with the default key, continue iteration... this.cursor.index = LongShortOpenHashMap.this.keys.length; } int i = this.cursor.index - 1; while (i >= 0 && !(LongShortOpenHashMap.this.keys[i] != (0L))) { i--; } if (i == -1) { return done(); } this.cursor.index = i; this.cursor.value = LongShortOpenHashMap.this.values[i]; return this.cursor; } } /** * Clone this object. * */ @Override public LongShortOpenHashMap clone() { final LongShortOpenHashMap cloned = new LongShortOpenHashMap(this.size(), this.loadFactor); cloned.putAll(this); cloned.allocatedDefaultKeyValue = this.allocatedDefaultKeyValue; cloned.allocatedDefaultKey = this.allocatedDefaultKey; cloned.defaultValue = this.defaultValue; return cloned; } /** * Convert the contents of this map to a human-friendly string. */ @Override public String toString() { final StringBuilder buffer = new StringBuilder(); buffer.append("["); boolean first = true; for (final LongShortCursor cursor : this) { if (!first) { buffer.append(", "); } buffer.append(cursor.key); buffer.append("=>"); buffer.append(cursor.value); first = false; } buffer.append("]"); return buffer.toString(); } /** * Creates a hash map from two index-aligned arrays of key-value pairs. */ public static LongShortOpenHashMap from(final long[] keys, final short[] values) { if (keys.length != values.length) { throw new IllegalArgumentException("Arrays of keys and values must have an identical length."); } final LongShortOpenHashMap map = new LongShortOpenHashMap(keys.length); for (int i = 0; i < keys.length; i++) { map.put(keys[i], values[i]); } return map; } /** * Create a hash map from another associative container. */ public static LongShortOpenHashMap from(final LongShortAssociativeContainer container) { return new LongShortOpenHashMap(container); } /** * Create a new hash map without providing the full generic signature (constructor * shortcut). */ public static LongShortOpenHashMap newInstance() { return new LongShortOpenHashMap(); } /** * Create a new hash map with initial capacity and load factor control. (constructor * shortcut). */ public static LongShortOpenHashMap newInstance(final int initialCapacity, final float loadFactor) { return new LongShortOpenHashMap(initialCapacity, loadFactor); } /** * Returns the "default value" value used * in containers methods returning "default value" * @return */ public short getDefaultValue() { return this.defaultValue; } /** * Set the "default value" value to be used * in containers methods returning "default value" * @return */ public void setDefaultValue(final short defaultValue) { this.defaultValue = defaultValue; } //Test for existence in template }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy