📄 basehashmap.java
字号:
if (isObjectValue) { objectValueTable[lookup] = objectValue; } else if (isIntValue) { intValueTable[lookup] = (int) longValue; } else if (isLongValue) { longValueTable[lookup] = longValue; } // if (accessTable != null) { accessTable[lookup] = accessCount++; } return returnValue; } /** * type-specific method for adding or removing keys in int->Object maps */ protected Object addOrRemove(int intKey, Object objectValue, boolean remove) { int hash = intKey; int index = hashIndex.getHashIndex(hash); int lookup = hashIndex.hashTable[index]; int lastLookup = -1; Object returnValue = null; for (; lookup >= 0; lastLookup = lookup, lookup = hashIndex.getNextLookup(lookup)) { if (intKey == intKeyTable[lookup]) { break; } } if (lookup >= 0) { if (remove) { if (intKey == 0) { hasZeroKey = false; zeroKeyIndex = -1; } intKeyTable[lookup] = 0; returnValue = objectValueTable[lookup]; objectValueTable[lookup] = null; hashIndex.unlinkNode(index, lastLookup, lookup); if (accessTable != null) { accessTable[lookup] = 0; } return returnValue; } if (isObjectValue) { returnValue = objectValueTable[lookup]; objectValueTable[lookup] = objectValue; } if (accessTable != null) { accessTable[lookup] = accessCount++; } return returnValue; } // not found if (remove) { return returnValue; } if (hashIndex.elementCount >= threshold) { if (reset()) { return addOrRemove(intKey, objectValue, remove); } else { return null; } } lookup = hashIndex.linkNode(index, lastLookup); intKeyTable[lookup] = intKey; if (intKey == 0) { hasZeroKey = true; zeroKeyIndex = lookup; } objectValueTable[lookup] = objectValue; if (accessTable != null) { accessTable[lookup] = accessCount++; } return returnValue; } /** * type specific method for Object sets or Object->Object maps */ protected Object removeObject(Object objectKey) { if (objectKey == null) { return null; } int hash = objectKey.hashCode(); int index = hashIndex.getHashIndex(hash); int lookup = hashIndex.hashTable[index]; int lastLookup = -1; Object returnValue = null; for (; lookup >= 0; lastLookup = lookup, lookup = hashIndex.getNextLookup(lookup)) { if (objectKeyTable[lookup].equals(objectKey)) { objectKeyTable[lookup] = null; hashIndex.unlinkNode(index, lastLookup, lookup); if (isObjectValue) { returnValue = objectValueTable[lookup]; objectValueTable[lookup] = null; } return returnValue; } } // not found return returnValue; } protected boolean reset() { if (maxCapacity == 0 || maxCapacity > threshold) { rehash(hashIndex.hashTable.length * 2); return true; } else if (purgePolicy == PURGE_ALL) { clear(); return true; } else if (purgePolicy == PURGE_QUARTER) { clear(threshold / 4, threshold >> 8); return true; } else if (purgePolicy == PURGE_HALF) { clear(threshold / 2, threshold >> 8); return true; } else if (purgePolicy == NO_PURGE) { return false; } return false; } /** * rehash uses existing key and element arrays. key / value pairs are * put back into the arrays from the top, removing any gaps. any redundant * key / value pairs duplicated at the end of the array are then cleared. * * newCapacity must be larger or equal to existing number of elements. */ protected void rehash(int newCapacity) { int limitLookup = hashIndex.newNodePointer; boolean oldZeroKey = hasZeroKey; int oldZeroKeyIndex = zeroKeyIndex; if (newCapacity < hashIndex.elementCount) { return; } hashIndex.reset((int) (newCapacity * loadFactor), newCapacity); hasZeroKey = false; zeroKeyIndex = -1; threshold = newCapacity; for (int lookup = -1; (lookup = nextLookup(lookup, limitLookup, oldZeroKey, oldZeroKeyIndex)) < limitLookup; ) { long longKey = 0; long longValue = 0; Object objectKey = null; Object objectValue = null; if (isObjectKey) { objectKey = objectKeyTable[lookup]; } else if (isIntKey) { longKey = intKeyTable[lookup]; } else { longKey = longKeyTable[lookup]; } if (isObjectValue) { objectValue = objectValueTable[lookup]; } else if (isIntValue) { longValue = intValueTable[lookup]; } else if (isLongValue) { longValue = longValueTable[lookup]; } addOrRemove(longKey, longValue, objectKey, objectValue, false); if (accessTable != null) { accessTable[hashIndex.elementCount - 1] = accessTable[lookup]; } } resizeElementArrays(hashIndex.newNodePointer, newCapacity); } /** * resize the arrays contianing the key / value data */ private void resizeElementArrays(int dataLength, int newLength) { Object temp; int usedLength = newLength > dataLength ? dataLength : newLength; if (isIntKey) { temp = intKeyTable; intKeyTable = new int[newLength]; System.arraycopy(temp, 0, intKeyTable, 0, usedLength); } if (isIntValue) { temp = intValueTable; intValueTable = new int[newLength]; System.arraycopy(temp, 0, intValueTable, 0, usedLength); } if (isLongKey) { temp = longKeyTable; longKeyTable = new long[newLength]; System.arraycopy(temp, 0, longKeyTable, 0, usedLength); } if (isLongValue) { temp = longValueTable; longValueTable = new long[newLength]; System.arraycopy(temp, 0, longValueTable, 0, usedLength); } if (isObjectKey) { temp = objectKeyTable; objectKeyTable = new Object[newLength]; System.arraycopy(temp, 0, objectKeyTable, 0, usedLength); } if (isObjectValue) { temp = objectValueTable; objectValueTable = new Object[newLength]; System.arraycopy(temp, 0, objectValueTable, 0, usedLength); } if (accessTable != null) { temp = accessTable; accessTable = new int[newLength]; System.arraycopy(temp, 0, accessTable, 0, usedLength); } } /** * clear all the key / value data in a range. */ private void clearElementArrays(final int from, final int to) { if (isIntKey) { int counter = to; while (--counter >= from) { intKeyTable[counter] = 0; } } if (isLongKey) { int counter = to; while (--counter >= from) { longKeyTable[counter] = 0; } } if (isObjectKey) { int counter = to; while (--counter >= from) { objectKeyTable[counter] = null; } } if (isIntValue) { int counter = to; while (--counter >= from) { intValueTable[counter] = 0; } } if (isLongValue) { int counter = to; while (--counter >= from) { longValueTable[counter] = 0; } } if (isObjectValue) { int counter = to; while (--counter >= from) { objectValueTable[counter] = null; } } if (accessTable != null) { int counter = to; while (--counter >= from) { accessTable[counter] = 0; } } } /** * move the elements after a removed key / value pair to fill the gap */ void removeFromElementArrays(int lookup) { int arrayLength = hashIndex.linkTable.length; if (isIntKey) { Object array = intKeyTable; System.arraycopy(array, lookup + 1, array, lookup, arrayLength - lookup - 1); intKeyTable[arrayLength - 1] = 0; } if (isLongKey) { Object array = longKeyTable; System.arraycopy(array, lookup + 1, array, lookup, arrayLength - lookup - 1); longKeyTable[arrayLength - 1] = 0; } if (isObjectKey) { Object array = objectKeyTable; System.arraycopy(array, lookup + 1, array, lookup, arrayLength - lookup - 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -