public boolean containsKey (K key) { int hashCode = System.identityHashCode(key); int index = hashCode & mask; if (key != keyTable[index]) { index = hash2(hashCode); if (key != keyTable[index]) { index = hash3(hashCode); if (key != keyTable[index]) { if (isBigTable) { index = hash4(hashCode); if (key != keyTable[index]) return containsKeyStash(key); } else { return containsKeyStash(key); } } } } return true; }
public V get (K key) { int hashCode = System.identityHashCode(key); int index = hashCode & mask; if (key != keyTable[index]) { index = hash2(hashCode); if (key != keyTable[index]) { index = hash3(hashCode); if (key != keyTable[index]) { if (isBigTable) { index = hash4(hashCode); if (key != keyTable[index]) return getStash(key, null); } else { return getStash(key, null); } } } } return valueTable[index]; }
private void putStash (K key, V value) { if (stashSize == stashCapacity) { // Too many pushes occurred and the stash is full, increase the table size. resize(capacity << 1); put(key, value); return; } // Store key in the stash. int index = capacity + stashSize; keyTable[index] = key; valueTable[index] = value; stashSize++; size++; }
/** Skips checks for existing keys. */ private void putResize (K key, V value) { // Check for empty buckets. int hashCode = System.identityHashCode(key); int index1 = hashCode & mask; K key1 = keyTable[index1]; if (key1 == null) { keyTable[index1] = key; valueTable[index1] = value; if (size++ >= threshold) resize(capacity << 1); return; } int index2 = hash2(hashCode); K key2 = keyTable[index2]; if (key2 == null) { keyTable[index2] = key; valueTable[index2] = value; if (size++ >= threshold) resize(capacity << 1); return; } int index3 = hash3(hashCode); K key3 = keyTable[index3]; if (key3 == null) { keyTable[index3] = key; valueTable[index3] = value; if (size++ >= threshold) resize(capacity << 1); return; } push(key, value, index1, key1, index2, key2, index3, key3); }
keyTable[index1] = evictedKey; valueTable[index1] = evictedValue; if (size++ >= threshold) resize(capacity << 1); return; index2 = hash2(hashCode); key2 = keyTable[index2]; if (key2 == null) { keyTable[index2] = evictedKey; valueTable[index2] = evictedValue; if (size++ >= threshold) resize(capacity << 1); return; index3 = hash3(hashCode); key3 = keyTable[index3]; if (key3 == null) { keyTable[index3] = evictedKey; valueTable[index3] = evictedValue; if (size++ >= threshold) resize(capacity << 1); return; } while (true); putStash(evictedKey, evictedValue);
/** Returns a shallow copy of the object using the specified serializer. Serializers for the classes involved must support * {@link Serializer#copy(Kryo, Object)}. * @param object May be null. */ public <T> T copyShallow (T object, Serializer serializer) { if (object == null) return null; copyDepth++; copyShallow = true; try { if (originalToCopy == null) originalToCopy = new IdentityMap(); Object existingCopy = originalToCopy.get(object); if (existingCopy != null) return (T)existingCopy; if (copyReferences) needsCopyReference = object; Object copy; if (object instanceof KryoCopyable) copy = ((KryoCopyable)object).copy(this); else copy = serializer.copy(this, object); if (needsCopyReference != null) reference(copy); if (TRACE || (DEBUG && copyDepth == 1)) log("Shallow copy", copy); return (T)copy; } finally { copyShallow = false; if (--copyDepth == 0) reset(); } }
index = hash2(hashCode); if (keyTable[index] == key) { keyTable[index] = null; index = hash3(hashCode); if (keyTable[index] == key) { keyTable[index] = null; index = hash4(hashCode); if (keyTable[index] == key) { keyTable[index] = null; return removeStash(key);
public boolean containsKey (K key) { int hashCode = System.identityHashCode(key); int index = hashCode & mask; if (key != keyTable[index]) { index = hash2(hashCode); if (key != keyTable[index]) { index = hash3(hashCode); if (key != keyTable[index]) return containsKeyStash(key); } } return true; }
public V remove (K key) { int hashCode = System.identityHashCode(key); int index = hashCode & mask; if (keyTable[index] == key) { keyTable[index] = null; V oldValue = valueTable[index]; valueTable[index] = null; size--; return oldValue; } index = hash2(hashCode); if (keyTable[index] == key) { keyTable[index] = null; V oldValue = valueTable[index]; valueTable[index] = null; size--; return oldValue; } index = hash3(hashCode); if (keyTable[index] == key) { keyTable[index] = null; V oldValue = valueTable[index]; valueTable[index] = null; size--; return oldValue; } return removeStash(key); }
/** Resets unregistered class names, references to previously serialized or deserialized objects, and the * {@link #getGraphContext() graph context}. If {@link #setAutoReset(boolean) auto reset} is true, this method is called * automatically when an object graph has been completely serialized or deserialized. If overridden, the super method must be * called. */ public void reset () { depth = 0; if (graphContext != null) graphContext.clear(); classResolver.reset(); if (references) { referenceResolver.reset(); readObject = null; } copyDepth = 0; if (originalToCopy != null) originalToCopy.clear(2048); if (TRACE) trace("kryo", "Object graph complete."); }
/** Called by {@link Serializer#read(Kryo, Input, Class)} and {@link Serializer#copy(Kryo, Object)} before Kryo can be used to * deserialize or copy child objects. Calling this method is unnecessary if Kryo is not used to deserialize or copy child * objects. * @param object May be null, unless calling this method from {@link Serializer#copy(Kryo, Object)}. */ public void reference (Object object) { if (copyDepth > 0) { if (needsCopyReference != null) { if (object == null) throw new IllegalArgumentException("object cannot be null."); originalToCopy.put(needsCopyReference, object); needsCopyReference = null; } } else if (references && object != null) { int id = readReferenceIds.pop(); if (id != NO_REF) referenceResolver.setReadObject(id, object); } }
private void resize (int newSize) { int oldEndIndex = capacity + stashSize; capacity = newSize; threshold = (int)(newSize * loadFactor); mask = newSize - 1; hashShift = 31 - Integer.numberOfTrailingZeros(newSize); stashCapacity = Math.max(3, (int)Math.ceil(Math.log(newSize)) * 2); pushIterations = Math.max(Math.min(newSize, 8), (int)Math.sqrt(newSize) / 8); K[] oldKeyTable = keyTable; V[] oldValueTable = valueTable; keyTable = (K[])new Object[newSize + stashCapacity]; valueTable = (V[])new Object[newSize + stashCapacity]; int oldSize = size; size = 0; stashSize = 0; if (oldSize > 0) { for (int i = 0; i < oldEndIndex; i++) { K key = oldKeyTable[i]; if (key != null) putResize(key, oldValueTable[i]); } } }
/** Skips checks for existing keys. */ private void putResize (K key, V value) { // Check for empty buckets. int hashCode = System.identityHashCode(key); int index1 = hashCode & mask; K key1 = keyTable[index1]; if (key1 == null) { keyTable[index1] = key; valueTable[index1] = value; if (size++ >= threshold) resize(capacity << 1); return; } int index2 = hash2(hashCode); K key2 = keyTable[index2]; if (key2 == null) { keyTable[index2] = key; valueTable[index2] = value; if (size++ >= threshold) resize(capacity << 1); return; } int index3 = hash3(hashCode); K key3 = keyTable[index3]; if (key3 == null) { keyTable[index3] = key; valueTable[index3] = value; if (size++ >= threshold) resize(capacity << 1); return; } push(key, value, index1, key1, index2, key2, index3, key3); }
keyTable[index1] = evictedKey; valueTable[index1] = evictedValue; if (size++ >= threshold) resize(capacity << 1); return; index2 = hash2(hashCode); key2 = keyTable[index2]; if (key2 == null) { keyTable[index2] = evictedKey; valueTable[index2] = evictedValue; if (size++ >= threshold) resize(capacity << 1); return; index3 = hash3(hashCode); key3 = keyTable[index3]; if (key3 == null) { keyTable[index3] = evictedKey; valueTable[index3] = evictedValue; if (size++ >= threshold) resize(capacity << 1); return; index4 = hash4(hashCode); key4 = keyTable[index4]; if (key4 == null) { keyTable[index4] = evictedKey; valueTable[index4] = evictedValue; if (size++ >= threshold) resize(capacity << 1); return;
/** Returns a deep copy of the object using the specified serializer. Serializers for the classes involved must support * {@link Serializer#copy(Kryo, Object)}. * @param object May be null. */ public <T> T copy (T object, Serializer serializer) { if (object == null) return null; if (copyShallow) return object; copyDepth++; try { if (originalToCopy == null) originalToCopy = new IdentityMap(); Object existingCopy = originalToCopy.get(object); if (existingCopy != null) return (T)existingCopy; if (copyReferences) needsCopyReference = object; Object copy; if (object instanceof KryoCopyable) copy = ((KryoCopyable)object).copy(this); else copy = serializer.copy(this, object); if (needsCopyReference != null) reference(copy); if (TRACE || (DEBUG && copyDepth == 1)) log("Copy", copy); return (T)copy; } finally { if (--copyDepth == 0) reset(); } }
index = hash2(hashCode); if (keyTable[index] == key) { keyTable[index] = null; index = hash3(hashCode); if (keyTable[index] == key) { keyTable[index] = null; index = hash4(hashCode); if (keyTable[index] == key) { keyTable[index] = null; return removeStash(key);
public boolean containsKey (K key) { int hashCode = System.identityHashCode(key); int index = hashCode & mask; if (key != keyTable[index]) { index = hash2(hashCode); if (key != keyTable[index]) { index = hash3(hashCode); if (key != keyTable[index]) return containsKeyStash(key); } } return true; }