|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectmathCollection.BitMathIntSet
This class represents mathematical sets with integer elements and is backed
by an instance of BitSet
. It offers destructive as well as
non-destructive mathematical set manipulation methods and features several
performance tweaks like cached hash code values and cardinality values.
Note that this implementation is not synchronized. If multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set.
BitSet
,
Serialized FormNested Class Summary | |
private class |
BitMathIntSet.BitMathIntSetIterator
An iterator class that returns every int element of this set
as an Integer object. |
Field Summary | |
private boolean |
isConcurrentlyModified
Used to check, whether this mathematical set was modified by an destructive method while iterating over it. |
private java.util.BitSet |
myBitSet
The backing instance of BitSet where the elements of this
set are stored. |
private int |
storedHashCode
Acts as a cache for the hash code value of this set out of performance considerations. |
private int |
storedMinimum
Acts as a cache for the smallest element of this set out of performance considerations. |
private int |
storedSize
Acts as a cache for the cardinality value of this set out of performance considerations. |
Constructor Summary | |
BitMathIntSet()
Constructs a new, empty mathematical set; the backing BitSet 's initial size is large enough to explicitly
store integers in the range 0 through 63 . |
|
BitMathIntSet(int nBits)
Constructs a new, empty mathematical set; the backing BitSet 's initial size is large enough to explicitly
store ints in the range 0 through nBits - 1 . |
|
BitMathIntSet(int[] intArray)
Constructs a new mathematical set containing the integer values from the specified array. |
Method Summary | |
boolean |
add(int bitIndex)
Adds the specified element to this set if not already present. |
boolean |
addAll(BitMathIntSet set)
Adds all of the integers in the specified BitMathIntSet to
this mathematical set. |
void |
clear()
Removes all elements from this set. |
java.lang.Object |
clone()
Returns a complete copy of this BitMathIntSet instance. |
boolean |
contains(int bitIndex)
Returns true if this set contains the specified integer. |
BitMathIntSet |
difference(BitMathIntSet set)
Returns the asymmetric difference between this mathematical set and the specified set. |
boolean |
equals(java.lang.Object o)
Compares the specified object with this set for equality. |
int |
getMaximum()
Returns the highest integer value in this set (its maximum). |
int |
getMinimum()
Returns the smallest integer value in this set (its minimum).If this set is empty then -1 is returned. |
int |
getNext(int bitIndex)
Returns the smallest integer value in this set which is equal or greater than the specified integer (the next integer value). |
int |
hashCode()
Returns the hash code value for this mathematical set. |
BitMathIntSet |
intersection(BitMathIntSet set)
Returns the intersection with the specified set. |
boolean |
isDisjoint(BitMathIntSet set)
Returns true if this mathematical set has no common
elements with the specified set. |
boolean |
isEmpty()
Returns true if this set contains no integers. |
boolean |
isSubset(BitMathIntSet set)
Returns true if this mathematical set is a subset of the
specified set. |
boolean |
isSuperset(BitMathIntSet set)
Returns true if this mathematical set is a superset of the
specified set. |
java.util.Iterator |
iterator()
Returns an iterator over the integers in this mathematical set. |
boolean |
remove(int bitIndex)
Removes the specified integer from this set if present. |
boolean |
removeAll(BitMathIntSet set)
Removes from this mathematical set all of its integers that are contained in the specified BitMathIntSet . |
boolean |
retainAll(BitMathIntSet set)
Retains only the integers in this mathematical set that are contained in the specified BitMathIntSet . |
int |
size()
Returns the number of integers in this set (its cardinality). |
BitMathIntSet |
symmetricDifference(BitMathIntSet set)
Returns the symmetric difference between this mathematical set and the specified set. |
int[] |
toArray()
Returns an array with the elements of this set, sorted in ascending order. |
java.lang.String |
toString()
Returns a string representation of this mathematical set. |
BitMathIntSet |
union(BitMathIntSet set)
Returns the union with the specified set. |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
private java.util.BitSet myBitSet
BitSet
where the elements of this
set are stored.
private int storedHashCode
storedHashCode
is set to 0 and gets updated as soon as the hashCode()
method is called.
private int storedSize
storedSize
is
set to -1 and gets updated as soon as the size()
method is
called.
private int storedMinimum
storedMinimum
cannot be determined in constant time,
storedMinimum
is set to -1 and gets updated as soon as the
getMinimum()
method is called.
private boolean isConcurrentlyModified
Constructor Detail |
public BitMathIntSet()
BitSet
's initial size is large enough to explicitly
store integers in the range 0
through 63
.
public BitMathIntSet(int nBits)
BitSet
's initial size is large enough to explicitly
store ints in the range 0
through nBits - 1
.
nBits
- the initial size of the backing BitSet
.
java.lang.NegativeArraySizeException
- if the specified initial size of
the backing BitSet
is negative.public BitMathIntSet(int[] intArray)
intArray
- the array containing the integer values.
java.lang.IndexOutOfBoundsException
- if an integer in the specified array
is negative.Method Detail |
public java.util.Iterator iterator()
Integer
objects in ascending order.
ConcurrentModificationException
public int hashCode()
s1.equals(s2)
implies that
s1.hashCode()==s2.hashCode()
for any two mathematical sets
s1
and s2
, as required by the general contract
of Object.hashCode()
.
This implementation first checks whether a cached hash code value is
available. If not (i.e. storedHashCode
is zero), the hash
code gets calculated using hashCode()
of the backing
BitSet
.
public int size()
This implementation first checks whether a cached cardinality value is
available. If not (i.e. storedSize
is -1), the cardinality
gets calculated using size()
of the backing
BitSet
.
public int getMinimum()
This implementation first checks whether a cached minimum value is
available. If not (i.e. storedMinimum
is -1), the minimum
gets calculated using nextSetBit(0)
of the backing
BitSet
.
public int getMaximum()
public int getNext(int bitIndex)
bitIndex
- the index to start checking from (inclusive).
bitIndex
.
java.lang.IndexOutOfBoundsException
- if the specified index is negative.public boolean isEmpty()
true
if this set contains no integers.
true
if this set contains no integers,
false
otherwise.public java.lang.Object clone()
BitMathIntSet
instance.
public int[] toArray()
public java.lang.String toString()
"{}"
). Adjacent elements are separated by the characters
", "
(comma and space).
public void clear()
public boolean contains(int bitIndex)
true
if this set contains the specified integer.
bitIndex
- integer whose presence in this set is to be tested.
true
if this set contains the specified
integer, false
otherwise.
java.lang.IndexOutOfBoundsException
- if the specified index is negative.public boolean add(int bitIndex)
bitIndex
- integer to be added to this set.
true
if the set did not already contain
the specified integer, false
otherwise.
java.lang.IndexOutOfBoundsException
- if the specified index is negative.public boolean remove(int bitIndex)
bitIndex
- integer to be removed from this set, if present.
true
if the set contained the specified
integer, false
otherwise.
java.lang.IndexOutOfBoundsException
- if the specified index is negative.public boolean addAll(BitMathIntSet set)
BitMathIntSet
to
this mathematical set.
This implementation adds all of the integers by calling
or()
of the backing BitSet
.
set
- mathematical set whose integers are to be added to this
set.
true
if this mathematical set changed as a
result of the call, false
otherwise.public boolean retainAll(BitMathIntSet set)
BitMathIntSet
. In other words, removes
from this mathematical set all of its elements that are not contained in
the specified BitMathIntSet
.
This implementation retains all of the integers by calling
and()
of the backing BitSet
.
set
- integers to be retained in this mathematical set.
true
if this mathematical set changed as a
result of the call, false
otherwise.public boolean removeAll(BitMathIntSet set)
BitMathIntSet
.
This implementation removes all of the integers by calling
andNot()
of the backing BitSet
.
set
- integers to be removed from this mathematical set.
true
if this mathematical set changed as a
result of the call, false
otherwise.public boolean equals(java.lang.Object o)
true
if the specified object is also a
BitMathIntSet
, the two sets have the same size, and every
integer of the specified set is contained in this set.
o
- object to be compared for equality with this mathematical
set.
true
if the specified object is equal to this
mathematical set, false
otherwise.public boolean isSubset(BitMathIntSet set)
true
if this mathematical set is a subset of the
specified set. That is, if all elements of this mathematical set are also
present in the specified set.
set
- set to be checked for being a superset.
true
if this mathematical set is a subset of
the specifed set, false
otherwise.public boolean isSuperset(BitMathIntSet set)
true
if this mathematical set is a superset of the
specified set. That is, if all elements of the specified set are also
present in this mathematical set.
set
- set to be checked for being a subset.
true
if this mathematical set is a superset
of the specifed set, false
otherwise.public boolean isDisjoint(BitMathIntSet set)
true
if this mathematical set has no common
elements with the specified set.
This implementation calls intersects()
of the backing
BitSet
.
set
- set to be checked for common integers.
true
if this mathematical set has no common
elements with the specifed set, false
otherwise.public BitMathIntSet union(BitMathIntSet set)
BitMathIntSet
containing all integers that are present in
this mathematical set or in the specified set.
set
- set to be united with this one.
public BitMathIntSet intersection(BitMathIntSet set)
BitMathIntSet
containing all integers that are present in
this mathematical set as well as in the specified set.
set
- set to be intersected with this one.
public BitMathIntSet difference(BitMathIntSet set)
BitMathIntSet
containing all
integers that are present in this mathematical set but not in the
specified set.
set
- set which gets subtracted from this mathematical set.
public BitMathIntSet symmetricDifference(BitMathIntSet set)
BitMathIntSet
containing all
integers that are present either in this mathematical set or in the
specified set but not in both.
set
- set from which the symmetric difference is calculated.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |