private static long testByte() { LongObjectMap<ConcurrentSkipListSet<ByteEntry>> tx = new LongObjectHashMap<ConcurrentSkipListSet<ByteEntry>>(NUM); for (int i = 0; i < NUM; i++) { tx.put(i, new ConcurrentSkipListSet<ByteEntry>()); } for (int i = 0; i < NUM; i++) { for (int j = 0; j < NUM; j++) { if (i == j) continue; if (Math.random() < FRACTION) { ByteBuffer key = ByteBuffer.allocate(16); key.putLong(5).putLong(j).flip(); ByteBuffer value = ByteBuffer.allocate(4); value.putInt(random.nextInt(ROUNDSIZE)).flip(); tx.get(i).add(new ByteEntry(key, value)); } } } long time = System.currentTimeMillis(); long sum = 0; for (int t = 0; t < TRIALS; t++) { for (int i = 0; i < NUM; i++) { for (Vertex v : (new ByteVertex(i, tx)).getNeighbors(0)) { sum += v.getId(); } } } time = System.currentTimeMillis() - time; return time; }
private void readInlineTypes(long[] keyIds, LongObjectHashMap properties, ReadBuffer in, TypeInspector tx, InlineType inlineType) { for (long keyId : keyIds) { PropertyKey keyType = tx.getExistingPropertyKey(keyId); Object value = readInline(in, keyType, inlineType); if (value != null) properties.put(keyId, value); } }
@SuppressWarnings("unchecked") public <O> O get(long key) { return (O) properties.get(key); }
/** * Creates a hash map from two index-aligned arrays of key-value pairs. */ public static <VType> LongObjectHashMap<VType> from(long[] keys, VType[] values) { if (keys.length != values.length) { throw new IllegalArgumentException("Arrays of keys and values must have an identical length."); } LongObjectHashMap<VType> map = new LongObjectHashMap<>(keys.length); for (int i = 0; i < keys.length; i++) { map.put(keys[i], values[i]); } return map; }
/** * Marks this sequence number and returns {@code true} if it is seen before. */ boolean getAndSet(long value) { assert value >= 0; final long key = value / BIT_SET_SIZE; CountedBitSet bitset = bitSets.get(key); if (bitset == null) { bitset = new CountedBitSet(BIT_SET_SIZE); bitSets.put(key, bitset); } final int index = Math.toIntExact(value % BIT_SET_SIZE); final boolean wasOn = bitset.get(index); bitset.set(index); return wasOn; } }
private CountedBitSet getBitSetForSeqNo(final long seqNo) { assert Thread.holdsLock(this); final long bitSetKey = getBitSetKey(seqNo); final int index = processedSeqNo.indexOf(bitSetKey); final CountedBitSet bitSet; if (processedSeqNo.indexExists(index)) { bitSet = processedSeqNo.indexGet(index); } else { bitSet = new CountedBitSet(BIT_SET_SIZE); processedSeqNo.indexInsert(index, bitSetKey, bitSet); } return bitSet; }
/** * 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(LongObjectHashMap<?> other) { if (other.size() != size()) { return false; } for (LongObjectCursor<?> c : other) { long key = c.key; if (!containsKey(key) || !java.util.Objects.equals(get(key), c.value)) { return false; } } return true; }
final int before = size(); if (other.size() >= size() && other instanceof LongLookupContainer) { if (hasEmptyKey) { if (!((existing = keys[slot]) == 0) && other.contains(existing)) { shiftConflictingKeys(slot); } else { slot++; this.remove( c.value); return before - size();
final int key = leafCollectors.indexOf(bucket); if (key < 0) { leafCollector = topDocsCollector.getLeafCollector(ctx); leafCollector.setScorer(scorer); leafCollectors.indexInsert(key, bucket, leafCollector); } else { leafCollector = leafCollectors.indexGet(key);
/** * <a href="http://trove4j.sourceforge.net">Trove</a>-inspired API method. An equivalent * of the following code: * <pre> * if (!map.containsKey(key)) map.put(value); * </pre> * * @param key The key of the value to check. * @param value The value to put if <code>key</code> does not exist. * @return <code>true</code> if <code>key</code> did not exist and <code>value</code> * was placed in the map. */ public boolean putIfAbsent(long key, VType value) { int keyIndex = indexOf(key); if (!indexExists(keyIndex)) { indexInsert(keyIndex, key, value); return true; } else { return false; } }
/** * {@inheritDoc} */ @Override public int removeAll(LongPredicate predicate) { final int before = size(); if (hasEmptyKey) { if (predicate.apply(0L)) { hasEmptyKey = false; values[mask + 1] = null; } } final long[] keys = this.keys; for (int slot = 0, max = this.mask; slot <= max;) { long existing; if (!((existing = keys[slot]) == 0) && predicate.apply(existing)) { // Shift, do not increment slot. shiftConflictingKeys(slot); } else { slot++; } } return before - size(); }
public RelationCache(final Direction direction, final long typeId, final long relationId, final Object other, final LongObjectHashMap<Object> properties) { this.direction = direction; this.typeId = typeId; this.relationId = relationId; this.other = other; this.properties = (properties == null || properties.size() > 0) ? properties : EMPTY; }
/** * Ensure this container can hold at least the * given number of keys (entries) without resizing its buffers. * * @param expectedElements The total number of keys, inclusive. */ @Override public void ensureCapacity(int expectedElements) { if (expectedElements > resizeAt || keys == null) { final long[] prevKeys = this.keys; final VType[] prevValues = (VType[]) this.values; allocateBuffers(minBufferSize(expectedElements, loadFactor)); if (prevKeys != null && !isEmpty()) { rehash(prevKeys, prevValues); } } }
/** * 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, long 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 long[] 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); }
/** * <a href="http://trove4j.sourceforge.net">Trove</a>-inspired API method. An equivalent * of the following code: * <pre> * if (!map.containsKey(key)) map.put(value); * </pre> * * @param key The key of the value to check. * @param value The value to put if <code>key</code> does not exist. * @return <code>true</code> if <code>key</code> did not exist and <code>value</code> * was placed in the map. */ public boolean putIfAbsent(long key, VType value) { if (!containsKey(key)) { put(key, value); return true; } else { return false; } }
public ArrowBuf replace(ArrowBuf old, int newSize) { if (managedBuffers.remove(old.memoryAddress()) == null) { throw new IllegalStateException("Tried to remove unmanaged buffer."); } old.release(1); return getManagedBuffer(newSize); }
public boolean hasProperties() { return properties != null && !properties.isEmpty(); }