📄 basehashmap.java
字号:
objectKeyTable[arrayLength - 1] = null; } if (isIntValue) { Object array = intValueTable; System.arraycopy(array, lookup + 1, array, lookup, arrayLength - lookup - 1); intValueTable[arrayLength - 1] = 0; } if (isLongValue) { Object array = longValueTable; System.arraycopy(array, lookup + 1, array, lookup, arrayLength - lookup - 1); longValueTable[arrayLength - 1] = 0; } if (isObjectValue) { Object array = objectValueTable; System.arraycopy(array, lookup + 1, array, lookup, arrayLength - lookup - 1); objectValueTable[arrayLength - 1] = null; } } /** * find the next lookup in the key/value tables with an entry * allows the use of old limit and zero int key attributes */ int nextLookup(int lookup, int limitLookup, boolean hasZeroKey, int zeroKeyIndex) { for (++lookup; lookup < limitLookup; lookup++) { if (isObjectKey) { if (objectKeyTable[lookup] != null) { return lookup; } } else if (isIntKey) { if (intKeyTable[lookup] != 0) { return lookup; } else if (hasZeroKey && lookup == zeroKeyIndex) { return lookup; } } else { if (longKeyTable[lookup] != 0) { return lookup; } else if (hasZeroKey && lookup == zeroKeyIndex) { return lookup; } } } return lookup; } /** * find the next lookup in the key/value tables with an entry * uses current limits and zero integer key state */ protected int nextLookup(int lookup) { for (++lookup; lookup < hashIndex.newNodePointer; lookup++) { if (isObjectKey) { if (objectKeyTable[lookup] != null) { return lookup; } } else if (isIntKey) { if (intKeyTable[lookup] != 0) { return lookup; } else if (hasZeroKey && lookup == zeroKeyIndex) { return lookup; } } else { if (longKeyTable[lookup] != 0) { return lookup; } else if (hasZeroKey && lookup == zeroKeyIndex) { return lookup; } } } return lookup; } /** * row must already been freed of key / element */ protected void removeRow(int lookup) { hashIndex.removeEmptyNode(lookup); removeFromElementArrays(lookup); } protected Object removeLookup(int lookup) { if (isObjectKey) { return addOrRemove(0, 0, objectKeyTable[lookup], null, true); } else { return addOrRemove(intKeyTable[lookup], 0, null, null, true); } } /** * Clear the map completely. */ public void clear() { accessCount = 0; accessMin = accessCount; hasZeroKey = false; zeroKeyIndex = -1; clearElementArrays(0, hashIndex.linkTable.length); hashIndex.clear(); if (minimizeOnEmpty) { rehash(initialCapacity); } } /** * Return the max accessCount value for count elements with the lowest * access count. Always return at least accessMin + 1 */ protected int getAccessCountCeiling(int count, int margin) { return ArrayCounter.rank(accessTable, hashIndex.newNodePointer, count, accessMin + 1, accessCount, margin); } /** * Clear approximately count elements from the map, starting with * those with low accessTable ranking. * * Only for maps with Object key table */ protected void clear(int count, int margin) { if (margin < 64) { margin = 64; } int maxlookup = hashIndex.newNodePointer; int accessBase = getAccessCountCeiling(count, margin); for (int lookup = 0; lookup < maxlookup; lookup++) { Object o = objectKeyTable[lookup]; if (o != null && accessTable[lookup] < accessBase) { removeObject(o); } } accessMin = accessBase; } void resetAccessCount() { if (accessCount < Integer.MAX_VALUE) { return; } accessMin >>= 2; accessCount >>= 2; int i = accessTable.length; while (--i >= 0) { accessTable[i] >>= 2; } } public int size() { return hashIndex.elementCount; } public boolean isEmpty() { return hashIndex.elementCount == 0; } protected boolean containsKey(Object key) { if (key == null) { return false; } int lookup = getLookup(key, key.hashCode()); return lookup == -1 ? false : true; } protected boolean containsKey(int key) { int lookup = getLookup(key); return lookup == -1 ? false : true; } protected boolean containsKey(long key) { int lookup = getLookup(key); return lookup == -1 ? false : true; } protected boolean containsValue(Object value) { int lookup = 0; if (value == null) { for (; lookup < hashIndex.newNodePointer; lookup++) { if (objectValueTable[lookup] == null) { if (isObjectKey) { if (objectKeyTable[lookup] != null) { return true; } } else if (isIntKey) { if (intKeyTable[lookup] != 0) { return true; } else if (hasZeroKey && lookup == zeroKeyIndex) { return true; } } else { if (longKeyTable[lookup] != 0) { return true; } else if (hasZeroKey && lookup == zeroKeyIndex) { return true; } } } } } else { for (; lookup < hashIndex.newNodePointer; lookup++) { if (value.equals(objectValueTable[lookup])) { return true; } } } return false; } /** * Iterator returns Object, int or long and is used both for keys and * values */ protected class BaseHashIterator implements org.hsqldb.lib.Iterator { boolean keys; int lookup = -1; int counter; boolean removed; /** * default is iterator for values */ public BaseHashIterator() {} public BaseHashIterator(boolean keys) { this.keys = keys; } public boolean hasNext() { return counter < hashIndex.elementCount; } public Object next() throws NoSuchElementException { if ((keys &&!isObjectKey) || (!keys &&!isObjectValue)) { throw new NoSuchElementException("Hash Iterator"); } removed = false; if (hasNext()) { counter++; lookup = nextLookup(lookup); if (keys) { return objectKeyTable[lookup]; } else { return objectValueTable[lookup]; } } throw new NoSuchElementException("Hash Iterator"); } public int nextInt() throws NoSuchElementException { if ((keys &&!isIntKey) || (!keys &&!isIntValue)) { throw new NoSuchElementException("Hash Iterator"); } removed = false; if (hasNext()) { counter++; lookup = nextLookup(lookup); if (keys) { return intKeyTable[lookup]; } else { return intValueTable[lookup]; } } throw new NoSuchElementException("Hash Iterator"); } public long nextLong() throws NoSuchElementException { if ((!isLongKey ||!keys)) { throw new NoSuchElementException("Hash Iterator"); } removed = false; if (hasNext()) { counter++; lookup = nextLookup(lookup); if (keys) { return longKeyTable[lookup]; } else { return longValueTable[lookup]; } } throw new NoSuchElementException("Hash Iterator"); } public void remove() throws NoSuchElementException { if (removed) { throw new NoSuchElementException("Hash Iterator"); } counter--; removed = true; BaseHashMap.this.removeLookup(lookup); } public int getAccessCount() { if (removed || accessTable == null) { throw new NoSuchElementException(); } return accessTable[lookup]; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -