private static void insertKeyValueNoLock(long[] table, int capacity, long item) { int bucket = signSafeMod(hash(item), capacity); while (true) { long storedKey = table[bucket]; if (storedKey == EmptyItem) { // The bucket is empty, so we can use it table[bucket] = item; return; } bucket = (bucket + 1) & (table.length - 1); } } }
boolean contains(long item, int hash) { long stamp = tryOptimisticRead(); boolean acquiredLock = false; int bucket = signSafeMod(hash, capacity); acquiredLock = true; bucket = signSafeMod(hash, capacity); storedItem = table[bucket];
@SuppressWarnings("NonAtomicVolatileUpdate") private boolean remove(long item, int hash) { long stamp = writeLock(); int bucket = signSafeMod(hash, capacity); try { while (true) { long storedItem = table[bucket]; if (item == storedItem) { --size; cleanBucket(bucket); return true; } else if (storedItem == EmptyItem) { // Key wasn't found return false; } bucket = (bucket + 1) & (table.length - 1); } } finally { unlockWrite(stamp); } }
@SuppressWarnings("NonAtomicVolatileUpdate") boolean add(long item, long hash) { long stamp = writeLock(); int bucket = signSafeMod(hash, capacity);
private static void insertKeyValueNoLock(long[] table, int capacity, long item) { int bucket = signSafeMod(hash(item), capacity); while (true) { long storedKey = table[bucket]; if (storedKey == EmptyItem) { // The bucket is empty, so we can use it table[bucket] = item; return; } bucket = (bucket + 1) & (table.length - 1); } } }
private static void insertKeyValueNoLock(long[] table, int capacity, long item) { int bucket = signSafeMod(hash(item), capacity); while (true) { long storedKey = table[bucket]; if (storedKey == EmptyItem) { // The bucket is empty, so we can use it table[bucket] = item; return; } bucket = (bucket + 1) & (table.length - 1); } } }
private static void insertKeyValueNoLock(long[] table, int capacity, long item) { int bucket = signSafeMod(hash(item), capacity); while (true) { long storedKey = table[bucket]; if (storedKey == EmptyItem) { // The bucket is empty, so we can use it table[bucket] = item; return; } bucket = (bucket + 1) & (table.length - 1); } } }
private static void insertKeyValueNoLock(long[] table, int capacity, long item) { int bucket = signSafeMod(hash(item), capacity); while (true) { long storedKey = table[bucket]; if (storedKey == EmptyItem) { // The bucket is empty, so we can use it table[bucket] = item; return; } bucket = (bucket + 1) & (table.length - 1); } } }
private static void insertKeyValueNoLock(long[] table, int capacity, long item) { int bucket = signSafeMod(hash(item), capacity); while (true) { long storedKey = table[bucket]; if (storedKey == EmptyItem) { // The bucket is empty, so we can use it table[bucket] = item; return; } bucket = (bucket + 1) & (table.length - 1); } } }
private static void insertKeyValueNoLock(long[] table, int capacity, long item) { int bucket = signSafeMod(hash(item), capacity); while (true) { long storedKey = table[bucket]; if (storedKey == EmptyItem) { // The bucket is empty, so we can use it table[bucket] = item; return; } bucket = (bucket + 1) & (table.length - 1); } } }
boolean contains(long item, int hash) { long stamp = tryOptimisticRead(); boolean acquiredLock = false; int bucket = signSafeMod(hash, capacity); acquiredLock = true; bucket = signSafeMod(hash, capacity); storedItem = table[bucket];
boolean contains(long item, int hash) { long stamp = tryOptimisticRead(); boolean acquiredLock = false; int bucket = signSafeMod(hash, capacity); acquiredLock = true; bucket = signSafeMod(hash, capacity); storedItem = table[bucket];
@SuppressWarnings("NonAtomicVolatileUpdate") private boolean remove(long item, int hash) { long stamp = writeLock(); int bucket = signSafeMod(hash, capacity); try { while (true) { long storedItem = table[bucket]; if (item == storedItem) { --size; cleanBucket(bucket); return true; } else if (storedItem == EmptyItem) { // Key wasn't found return false; } bucket = (bucket + 1) & (table.length - 1); } } finally { unlockWrite(stamp); } }
@SuppressWarnings("NonAtomicVolatileUpdate") private boolean remove(long item, int hash) { long stamp = writeLock(); int bucket = signSafeMod(hash, capacity); try { while (true) { long storedItem = table[bucket]; if (item == storedItem) { --size; cleanBucket(bucket); return true; } else if (storedItem == EmptyItem) { // Key wasn't found return false; } bucket = (bucket + 1) & (table.length - 1); } } finally { unlockWrite(stamp); } }
@SuppressWarnings("NonAtomicVolatileUpdate") private boolean remove(long item, int hash) { long stamp = writeLock(); int bucket = signSafeMod(hash, capacity); try { while (true) { long storedItem = table[bucket]; if (item == storedItem) { --size; cleanBucket(bucket); return true; } else if (storedItem == EmptyItem) { // Key wasn't found return false; } bucket = (bucket + 1) & (table.length - 1); } } finally { unlockWrite(stamp); } }
@SuppressWarnings("NonAtomicVolatileUpdate") private boolean remove(long item, int hash) { long stamp = writeLock(); int bucket = signSafeMod(hash, capacity); try { while (true) { long storedItem = table[bucket]; if (item == storedItem) { --size; cleanBucket(bucket); return true; } else if (storedItem == EmptyItem) { // Key wasn't found return false; } bucket = (bucket + 1) & (table.length - 1); } } finally { unlockWrite(stamp); } }
@SuppressWarnings("NonAtomicVolatileUpdate") private boolean remove(long item, int hash) { long stamp = writeLock(); int bucket = signSafeMod(hash, capacity); try { while (true) { long storedItem = table[bucket]; if (item == storedItem) { --size; cleanBucket(bucket); return true; } else if (storedItem == EmptyItem) { // Key wasn't found return false; } bucket = (bucket + 1) & (table.length - 1); } } finally { unlockWrite(stamp); } }
@SuppressWarnings("NonAtomicVolatileUpdate") private boolean remove(long item, int hash) { long stamp = writeLock(); int bucket = signSafeMod(hash, capacity); try { while (true) { long storedItem = table[bucket]; if (item == storedItem) { --size; cleanBucket(bucket); return true; } else if (storedItem == EmptyItem) { // Key wasn't found return false; } bucket = (bucket + 1) & (table.length - 1); } } finally { unlockWrite(stamp); } }
@SuppressWarnings("NonAtomicVolatileUpdate") boolean add(long item, long hash) { long stamp = writeLock(); int bucket = signSafeMod(hash, capacity);
@Test public void testHashConflictWithDeletion() { final int Buckets = 16; ConcurrentLongHashSet set = new ConcurrentLongHashSet(Buckets, 1); // Pick 2 keys that fall into the same bucket long key1 = 1; long key2 = 27; int bucket1 = ConcurrentLongHashSet.signSafeMod(ConcurrentLongHashSet.hash(key1), Buckets); int bucket2 = ConcurrentLongHashSet.signSafeMod(ConcurrentLongHashSet.hash(key2), Buckets); assertEquals(bucket1, bucket2); assertTrue(set.add(key1)); assertTrue(set.add(key2)); assertEquals(set.size(), 2); assertTrue(set.remove(key1)); assertEquals(set.size(), 1); assertTrue(set.add(key1)); assertEquals(set.size(), 2); assertTrue(set.remove(key1)); assertEquals(set.size(), 1); assertFalse(set.add(key2)); assertTrue(set.contains(key2)); assertEquals(set.size(), 1); assertTrue(set.remove(key2)); assertTrue(set.isEmpty()); }