Package org.jctools.maps
Class NonBlockingSetInt
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<java.lang.Integer>
-
- org.jctools.maps.NonBlockingSetInt
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<java.lang.Integer>,java.util.Collection<java.lang.Integer>,java.util.Set<java.lang.Integer>
public class NonBlockingSetInt extends java.util.AbstractSet<java.lang.Integer> implements java.io.SerializableA multi-threaded bit-vector set, implemented as an array of primitivelongs. All operations are non-blocking and multi-threaded safe.contains(int)calls are roughly the same speed as a {load, mask} sequence.add(int)andremove(int)calls are a tad more expensive than a {load, mask, store} sequence because they must use a CAS. The bit-vector is auto-sizing.General note of caution: The Set API allows the use of
Integerwith silent autoboxing - which can be very expensive if many calls are being made. Since autoboxing is silent you may not be aware that this is going on. The built-in API takes lower-caseintsand is much more efficient.Space: space is used in proportion to the largest element, as opposed to the number of elements (as is the case with hash-table based Set implementations). Space is approximately (largest_element/8 + 64) bytes. The implementation is a simple bit-vector using CAS for update.
- Since:
- 1.5
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classNonBlockingSetInt.iterprivate static classNonBlockingSetInt.NBSI
-
Field Summary
Fields Modifier and Type Field Description private NonBlockingSetInt.NBSI_nbsiprivate static long_nbsi_offsetprivate static longserialVersionUID
-
Constructor Summary
Constructors Constructor Description NonBlockingSetInt()Create a new empty bit-vector
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(int i)Addito the set.booleanadd(java.lang.Integer i)Addito the set.private booleanCAS_nbsi(NonBlockingSetInt.NBSI old, NonBlockingSetInt.NBSI nnn)voidclear()Empty the bitvector.booleancontains(int i)Test ifiis in the set.booleancontains(java.lang.Object o)Test ifois in the set.java.util.Iterator<java.lang.Integer>iterator()Standard JavaIterator.intlength()Approx largest element in set; at least as big (but max might be smaller).voidprint()Verbose printout of internal structure for debugging.private voidreadObject(java.io.ObjectInputStream s)booleanremove(int i)Removeifrom the set.booleanremove(java.lang.Object o)Removeofrom the set.intsize()Current count of elements in the set.private voidwriteObject(java.io.ObjectOutputStream s)-
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, retainAll, toArray, toArray, toString
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
_nbsi_offset
private static final long _nbsi_offset
-
_nbsi
private transient NonBlockingSetInt.NBSI _nbsi
-
-
Method Detail
-
CAS_nbsi
private final boolean CAS_nbsi(NonBlockingSetInt.NBSI old, NonBlockingSetInt.NBSI nnn)
-
add
public boolean add(java.lang.Integer i)
Addito the set. UppercaseIntegerversion of add, requires auto-unboxing. When possible use theintversion ofadd(int)for efficiency.- Specified by:
addin interfacejava.util.Collection<java.lang.Integer>- Specified by:
addin interfacejava.util.Set<java.lang.Integer>- Overrides:
addin classjava.util.AbstractCollection<java.lang.Integer>- Returns:
- true if i was added to the set.
- Throws:
java.lang.IllegalArgumentException- if i is negative.
-
contains
public boolean contains(java.lang.Object o)
Test ifois in the set. This is the uppercaseIntegerversion of contains, requires a type-check and auto-unboxing. When possible use theintversion ofcontains(int)for efficiency.- Specified by:
containsin interfacejava.util.Collection<java.lang.Integer>- Specified by:
containsin interfacejava.util.Set<java.lang.Integer>- Overrides:
containsin classjava.util.AbstractCollection<java.lang.Integer>- Returns:
- true if i was in the set.
-
remove
public boolean remove(java.lang.Object o)
Removeofrom the set. This is the uppercaseIntegerversion of remove, requires a type-check and auto-unboxing. When possible use theintversion ofremove(int)for efficiency.- Specified by:
removein interfacejava.util.Collection<java.lang.Integer>- Specified by:
removein interfacejava.util.Set<java.lang.Integer>- Overrides:
removein classjava.util.AbstractCollection<java.lang.Integer>- Returns:
- true if i was removed to the set.
-
add
public boolean add(int i)
Addito the set. This is the lower-case 'int' version ofadd(java.lang.Integer)- no autoboxing. Negative values throw IllegalArgumentException.- Returns:
- true if i was added to the set.
- Throws:
java.lang.IllegalArgumentException- if i is negative.
-
contains
public boolean contains(int i)
Test ifiis in the set. This is the lower-case 'int' version ofcontains(java.lang.Object)- no autoboxing.- Returns:
- true if i was int the set.
-
remove
public boolean remove(int i)
Removeifrom the set. This is the fast lower-case 'int' version ofremove(java.lang.Object)- no autoboxing.- Returns:
- true if i was added to the set.
-
size
public int size()
Current count of elements in the set. Due to concurrent racing updates, the size is only ever approximate. Updates due to the calling thread are immediately visible to calling thread.- Specified by:
sizein interfacejava.util.Collection<java.lang.Integer>- Specified by:
sizein interfacejava.util.Set<java.lang.Integer>- Specified by:
sizein classjava.util.AbstractCollection<java.lang.Integer>- Returns:
- count of elements.
-
length
public int length()
Approx largest element in set; at least as big (but max might be smaller).
-
clear
public void clear()
Empty the bitvector.- Specified by:
clearin interfacejava.util.Collection<java.lang.Integer>- Specified by:
clearin interfacejava.util.Set<java.lang.Integer>- Overrides:
clearin classjava.util.AbstractCollection<java.lang.Integer>
-
print
public void print()
Verbose printout of internal structure for debugging.
-
iterator
public java.util.Iterator<java.lang.Integer> iterator()
Standard JavaIterator. Not very efficient because it auto-boxes the returned values.- Specified by:
iteratorin interfacejava.util.Collection<java.lang.Integer>- Specified by:
iteratorin interfacejava.lang.Iterable<java.lang.Integer>- Specified by:
iteratorin interfacejava.util.Set<java.lang.Integer>- Specified by:
iteratorin classjava.util.AbstractCollection<java.lang.Integer>
-
writeObject
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException- Throws:
java.io.IOException
-
readObject
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundException- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
-