/** * Creates a hash map from two index-aligned arrays of key-value pairs. */ public static <VType> IntObjectHashMap<VType> from(int[] keys, VType[] values) { if (keys.length != values.length) { throw new IllegalArgumentException("Arrays of keys and values must have an identical length."); } IntObjectHashMap<VType> map = new IntObjectHashMap<>(keys.length); for (int i = 0; i < keys.length; i++) { map.put(keys[i], values[i]); } return map; }
private void searchInternal(int from) { checkAlreadyRun(); currEdge = new IsoLabel(-1, from, 0, 0, 0); fromMap.put(from, currEdge); EdgeExplorer explorer = reverseFlow ? inEdgeExplorer : outEdgeExplorer; while (true) { long tmpTime = weighting.calcMillis(iter, reverseFlow, currEdge.edge) + currEdge.time; int tmpNode = iter.getAdjNode(); IsoLabel nEdge = fromMap.get(tmpNode); if (nEdge == null) { nEdge = new IsoLabel(iter.getEdge(), tmpNode, tmpWeight, tmpTime, tmpDistance); nEdge.parent = currEdge; fromMap.put(tmpNode, nEdge); fromHeap.add(nEdge); } else if (nEdge.weight > tmpWeight) {
private RpcOutcome<?> removeFromMap(int coordinationId) { final RpcOutcome<?> rpc; synchronized (map) { rpc = map.remove(coordinationId); } if (rpc == null) { throw new IllegalStateException( "Attempting to retrieve an rpc that wasn't first stored in the rpc coordination queue. This would most likely happen if you're opposite endpoint sent multiple messages on the same coordination id."); } return rpc; }
private void addFragment(final FragmentData fragmentData) { final FragmentHandle fragmentHandle = fragmentData.getHandle(); final int majorFragmentId = fragmentHandle.getMajorFragmentId(); final int minorFragmentId = fragmentHandle.getMinorFragmentId(); IntObjectHashMap<FragmentData> minorMap = fragmentDataMap.get(majorFragmentId); if (minorMap == null) { minorMap = new IntObjectHashMap<>(); fragmentDataMap.put(majorFragmentId, minorMap); } minorMap.put(minorFragmentId, fragmentData); fragmentDataSet.add(fragmentData); }
public RequestIdMap() { map = new IntObjectHashMap<>(); }
@Override public int test() { final IntObjectHashMap<Integer> m_map = new IntObjectHashMap<>( m_keys.length, 0.5f ); for ( int i = 0; i < m_keys.length; ++i ) m_map.put( m_keys[ i ], null ); for ( int i = 0; i < m_keys.length; ++i ) m_map.put( m_keys[ i ], null ); return m_map.size(); } }
public <V> ChannelListenerWithCoordinationId createNewRpcListener(RpcOutcomeListener<V> handler, Class<V> clazz, RemoteConnection connection) { final int i = lastCoordinationId.incrementAndGet(); final RpcListener<V> future = new RpcListener<>(handler, clazz, i, connection); final Object old; synchronized (map) { Preconditions.checkArgument(isOpen.get(), "Attempted to send a message when connection is no longer valid."); old = map.put(i, future); } Preconditions.checkArgument(old == null, "You attempted to reuse a coordination id when the previous coordination id has not been removed. " + "This is likely rpc future callback memory leak."); return future; }
/** * @return Returns the value associated with the given key or the default value * for the key type, if the key is not associated with any value. * <p> * <b>Important note:</b> For primitive type values, the value returned for a non-existing * key may not be the default value of the primitive type (it may be any value previously * assigned to that slot). */ public VType get(int key) { return map.get(key); }
/** * Return true if all keys of some other container exist in this container. * Values are compared using {@link Objects#equals(Object)} method. */ protected boolean equalElements(IntObjectHashMap<?> other) { if (other.size() != size()) { return false; } for (IntObjectCursor<?> c : other) { int key = c.key; if (!containsKey(key) || !java.util.Objects.equals(get(key), c.value)) { return false; } } return true; }
public List<Set<Integer>> search(int from, final int bucketCount) { searchInternal(from); final double bucketSize = limit / bucketCount; final List<Set<Integer>> list = new ArrayList<>(bucketCount); for (int i = 0; i < bucketCount; i++) { list.add(new HashSet<Integer>()); } fromMap.forEach(new IntObjectProcedure<IsoLabel>() { @Override public void apply(int nodeId, IsoLabel label) { if (finished()) { return; } int bucketIndex = (int) (getExploreValue(label) / bucketSize); if (bucketIndex < 0) { throw new IllegalArgumentException("edge cannot have negative explore value " + nodeId + ", " + label); } else if (bucketIndex == bucketCount) { bucketIndex = bucketCount - 1; } else if (bucketIndex > bucketCount) { return; } list.get(bucketIndex).add(nodeId); } }); return list; }
/** * Returns <code>true</code> if this container has an association to a value for * the given key. */ public boolean containsKey(int key) { return map.containsKey(key); }
/** * @return Return <code>true</code> if this hash map contains no assigned keys. */ public boolean isEmpty() { return map.isEmpty(); }
final int before = size(); if (other.size() >= size() && other instanceof IntLookupContainer) { if (hasEmptyKey) { if (!((existing = keys[slot]) == 0) && other.contains(existing)) { shiftConflictingKeys(slot); } else { slot++; this.remove( c.value); return before - size();
@Override public void clear() { map.clear(); }
public Builder(ImmutableOpenIntMap<VType> map) { this.map = map.map.clone(); }
/** * @return Returns the current size (number of assigned keys) in the container. */ public int size() { return map.size(); }
/** * {@inheritDoc} */ @Override public int removeAll(IntPredicate predicate) { final int before = size(); if (hasEmptyKey) { if (predicate.apply(0)) { hasEmptyKey = false; values[mask + 1] = null; } } final int[] keys = this.keys; for (int slot = 0, max = this.mask; slot <= max;) { int existing; if (!((existing = keys[slot]) == 0) && predicate.apply(existing)) { // Shift, do not increment slot. shiftConflictingKeys(slot); } else { slot++; } } return before - size(); }
/** * This method is invoked when there is a new key/ value pair to be inserted into * the buffers but there is not enough empty slots to do so. * * New buffers are allocated. If this succeeds, we know we can proceed * with rehashing so we assign the pending element to the previous buffer * (possibly violating the invariant of having at least one empty slot) * and rehash all keys, substituting new buffers at the end. */ protected void allocateThenInsertThenRehash(int slot, int pendingKey, VType pendingValue) { assert assigned == resizeAt && (( keys[slot]) == 0) && !((pendingKey) == 0); // Try to allocate new buffers first. If we OOM, we leave in a consistent state. final int[] prevKeys = this.keys; final VType[] prevValues = (VType[]) this.values; allocateBuffers(nextBufferSize(mask + 1, size(), loadFactor)); assert this.keys.length > prevKeys.length; // We have succeeded at allocating new data so insert the pending key/value at // the free slot in the old arrays before rehashing. prevKeys[slot] = pendingKey; prevValues[slot] = pendingValue; // Rehash old keys, including the pending key. rehash(prevKeys, prevValues); }