public int indexBinaryStateUnsafeIndexing(char key) { if (key != 0 && key != zeroMask) { char[] keys = set; long capacityMask = (long) (keys.length - 1); long index = ((long) Primitives.hashCode(key)) & capacityMask; long offset = index << CHAR_SCALE_SHIFT; long CHAR_BASE = UnsafeConstants.CHAR_BASE; char cur = U.getChar(keys, CHAR_BASE + offset); if (cur == key) { return (int) index; } else { if (cur == 0) { return -1; } else { long capacityOffsetMask = capacityMask << CHAR_SCALE_SHIFT; while (true) { offset = (offset + CHAR_SCALE) & capacityOffsetMask; if ((cur = U.getChar(keys, CHAR_BASE + offset)) == key) { return (int) (offset >> CHAR_SCALE_SHIFT); } else if (cur == 0) { return -1; } } } } } else { return -1; } }
public boolean addBinaryStateUnsafeIndexing(char key) { if (key == 0 || key == zeroMask) { return false; } char[] keys = set; long capacityMask = (long) (keys.length - 1); long index = ((long) Primitives.hashCode(key)) & capacityMask; long offset = index << CHAR_SCALE_SHIFT; char cur = U.getChar(keys, CHAR_BASE + offset); keyAbsent: if (cur != 0) { if (cur == key) { return false; } else { long capacityOffsetMask = capacityMask << CHAR_SCALE_SHIFT; while (true) { offset = (offset + CHAR_SCALE) & capacityOffsetMask; if ((cur = U.getChar(keys, CHAR_BASE + offset)) == 0) { break keyAbsent; } else if (cur == key) { return false; } } } } // key is absent U.putChar(keys, CHAR_BASE + offset, key); size++; return true; }
public boolean removeUnsafeIndexing(char key) { if (key == 0 || key == zeroMask) { return false; } char[] keys = set; int capacity = set.length; int capacityMask = capacity - 1; long capacityOffsetMask = ((long) capacityMask) << CHAR_SCALE_SHIFT; long index = (long) (Primitives.hashCode(key) & capacityMask); long offset = index << CHAR_SCALE_SHIFT; char cur = U.getChar(keys, CHAR_BASE + offset); keyPresent: if (cur != key) { if (cur == 0) return false; while (true) { offset = (offset + CHAR_SCALE) & capacityOffsetMask; if ((cur = U.getChar(keys, CHAR_BASE + offset)) == key) { break keyPresent; } else if (cur == 0) { return false; } } } // key is present long offsetToRemove = offset; long offsetToShift = offset; int shiftDistance = 1; while (true) { offsetToShift = (offsetToShift + CHAR_SCALE) & capacityOffsetMask; char keyToShift = U.getChar(keys, CHAR_BASE + offsetToShift); if (keyToShift == 0) break; int indexToShift = (int) (offsetToShift >> CHAR_SCALE_SHIFT); int keyDistance = (indexToShift - Primitives.hashCode(keyToShift)) & capacityMask; if (keyDistance >= shiftDistance) { U.putChar(keys, CHAR_BASE + offsetToRemove, keyToShift); offsetToRemove = offsetToShift; shiftDistance = 1; } else { shiftDistance++; } } U.putChar(keys, CHAR_BASE + offsetToRemove, (char) 0); return true; }
final long readSlot(long slotIndex) { return U.getChar(this, HASH_TABLE_OFFSET + (slotIndex << 1)); }