Class ConcurrentInt62Set
- java.lang.Object
-
- org.stianloader.concurrent.ConcurrentInt62Set
-
- All Implemented Interfaces:
it.unimi.dsi.fastutil.longs.LongCollection,it.unimi.dsi.fastutil.longs.LongIterable,it.unimi.dsi.fastutil.longs.LongSet,Iterable<Long>,Collection<Long>,Set<Long>
public final class ConcurrentInt62Set extends Object implements it.unimi.dsi.fastutil.longs.LongSet
Description
A concurrent set for 62 bit unsigned integers, that is all integers between 0 and 1L << 62 - 1 (or 0x3FFF_FFFF_FFFF_FFFF). The two remaining bits are used for access control logic.This set supports high level of concurrency similar that of
ConcurrentHashMap, however unlikeConcurrentHashMapConcurrentInt62Setdoes not support resizing the amounts of buckets. Performance of this map degrades if too few buckets are used for too many elements. The size of each bucket increases exponentially in factors of two with a starting size of 16 elements. All buckets are initialized eagerly, that is either at creation time or whenclear()is invoked. Buckets cannot be shrunk down after expanding to a certain size. Onlyclear()(or discarding the set itself) would cause the allocated memory to be freed. The amount of buckets is defined from the start and cannot be changed later on. For performance reasons, it must be a power of two.Atomicity of method calls
The atomic operations
add(long),remove(long)andcontains(long)are the smallest unit from which the set can be altered. All other operations (aside from the iterator) are based on either the atomic operations or the iterator. These operations are not atomic, that is the effects might not fully take effect. For example a call toretainAll(LongCollection)might remove a value added viaadd(long)shortly after callingretainAll(LongCollection). However, it is also possible that it keeps that value. Should this atomic behaviour be required nonetheless, the set should be synchronised viaLongSets.synchronize(LongSet)or similar.Even though
add(long)andremove(long)are considered atomic, they are not guaranteed to be non-blocking. During a resizing operation, a thread may need to wait on other threads in order to gain exclusive control over a bucket's internal array. Conversely, once the lock has been acquired, other threads will need to wait until the lock has been relinquished. This behaviour is required in order to guarantee that the effects ofadd(long)andremove(long)persist even across a resize. To reduce the likelihood of this occurring, a larger amount of buckets may need to be used. To expand on this it may be beneficial to redistribute the bits of the longs so that the least significant bits are less frequent to occur in a given combination. This is especially beneficial when storing values that are aligned (for example address values that might frequently be only multiples of 4, 8 or another value), as this will necessarily cause some buckets to be empty.Iteration
Multiple iterators are supported at the same time. Furthermore, this set implementation also supports modification of the set without causing the iterators to fail - except if the modification would cause the iterator to be exhausted. However, as iterators do not represent an atomic snapshot (or any snapshot at all for that matter) it is possible that underlying changes may improperly reflect on the iteration results. That is an
Iterator.next()call can fail even thoughIterator.hasNext()has been called beforehand as aLongSet.remove(long)operation which was called in between hasNext and next caused the iterator to be exhausted. Extreme care should be taken when using Iterators in concurrent environments involving additions to the set coupled with removals.Important non-features
This set does not implement
Object.hashCode()andObject.equals(Object). Should these methods be required nonetheless a wrapper may be appropriate.This set expects that values are distributed evenly and randomly. More specifically, the bucket for any given value is
((value & 0xFFFF) ^ (value >> 32)) & (bucketCount - 1). This contrasts collections such asConcurrentHashMapwhere the value to bucket mapping appears a bit more random. more specifically, this causes issues when values inserted into the collection are guaranteed to be multiples of 2, 4, 8 or 16 (and further), as the buckets will not be used efficiently, causing lookup and mutation times to increase larger than usually.- Author:
- Emeric "geolykt" Werner
-
-
Constructor Summary
Constructors Constructor Description ConcurrentInt62Set(int bucketCount)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanadd(long element)booleanaddAll(it.unimi.dsi.fastutil.longs.LongCollection c)booleanaddAll(Collection<? extends Long> c)voidclear()booleancontains(long element)booleancontainsAll(it.unimi.dsi.fastutil.longs.LongCollection c)booleancontainsAll(Collection<?> c)Deprecated.booleanisEmpty()it.unimi.dsi.fastutil.longs.LongIteratoriterator()booleanremove(long element)booleanremoveAll(it.unimi.dsi.fastutil.longs.LongCollection c)booleanremoveAll(Collection<?> c)booleanretainAll(it.unimi.dsi.fastutil.longs.LongCollection c)booleanretainAll(Collection<?> c)intsize()Object[]toArray()long[]toArray(long[] a)<T> T[]toArray(T[] a)long[]toLongArray()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Collection
toArray
-
Methods inherited from interface it.unimi.dsi.fastutil.longs.LongCollection
longIterator, longParallelStream, longSpliterator, longStream, parallelStream, removeIf, removeIf, removeIf, stream, toLongArray
-
-
-
-
Method Detail
-
add
public boolean add(long element)
- Specified by:
addin interfaceit.unimi.dsi.fastutil.longs.LongCollection
-
remove
public boolean remove(long element)
- Specified by:
removein interfaceit.unimi.dsi.fastutil.longs.LongSet
-
contains
public boolean contains(long element)
- Specified by:
containsin interfaceit.unimi.dsi.fastutil.longs.LongCollection
-
toLongArray
public long[] toLongArray()
- Specified by:
toLongArrayin interfaceit.unimi.dsi.fastutil.longs.LongCollection
-
toArray
public long[] toArray(long[] a)
- Specified by:
toArrayin interfaceit.unimi.dsi.fastutil.longs.LongCollection
-
addAll
public boolean addAll(it.unimi.dsi.fastutil.longs.LongCollection c)
- Specified by:
addAllin interfaceit.unimi.dsi.fastutil.longs.LongCollection
-
containsAll
public boolean containsAll(it.unimi.dsi.fastutil.longs.LongCollection c)
- Specified by:
containsAllin interfaceit.unimi.dsi.fastutil.longs.LongCollection
-
removeAll
public boolean removeAll(it.unimi.dsi.fastutil.longs.LongCollection c)
- Specified by:
removeAllin interfaceit.unimi.dsi.fastutil.longs.LongCollection
-
retainAll
public boolean retainAll(it.unimi.dsi.fastutil.longs.LongCollection c)
- Specified by:
retainAllin interfaceit.unimi.dsi.fastutil.longs.LongCollection
-
addAll
public boolean addAll(Collection<? extends Long> c)
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
toArray
public Object[] toArray()
-
toArray
public <T> T[] toArray(T[] a)
-
containsAll
@Deprecated public boolean containsAll(Collection<?> c)
Deprecated.- Specified by:
containsAllin interfaceCollection<Long>- Specified by:
containsAllin interfaceSet<Long>
-
retainAll
public boolean retainAll(Collection<?> c)
-
removeAll
public boolean removeAll(Collection<?> c)
-
clear
public void clear()
-
iterator
public it.unimi.dsi.fastutil.longs.LongIterator iterator()
- Specified by:
iteratorin interfaceCollection<Long>- Specified by:
iteratorin interfaceIterable<Long>- Specified by:
iteratorin interfaceit.unimi.dsi.fastutil.longs.LongCollection- Specified by:
iteratorin interfaceit.unimi.dsi.fastutil.longs.LongIterable- Specified by:
iteratorin interfaceit.unimi.dsi.fastutil.longs.LongSet- Specified by:
iteratorin interfaceSet<Long>
-
-