|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.AbstractCollection
java.util.AbstractSet
mathCollection.AbstractMathSet
mathCollection.HashMathSet
This class implements the MathSet
interface, backed by a hash
table (specifically, a HashSet
instance). It makes no
guarantees as to the iteration order of the set; in particular, it does not
guarantee that the order will remain constant over time. This class permits
the null
element.
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. If no such object exists, the set should be "wrapped" using the
MathCollections.synchronizedMathSet
method. This is best done
at creation time, to prevent accidental unsynchronized access to the
HashMathSet
instance:
MathSet ms = MathCollections.synchronizedMathSet(new HashMathSet(...));
Set
,
MathSet
,
AbstractMathSet
,
HashSet
,
Serialized FormNested Class Summary | |
private class |
HashMathSet.HashMathSetIterator
A 'wrapper' iterator class that uses HashSet.iterator()
while accounting for the additional storedHashCode
attribute. |
Field Summary | |
private java.util.HashSet |
myHashSet
The backing instance of HashSet |
private int |
storedHashCode
Acts as a cache for the hash code value of this set out of performance considerations. |
Constructor Summary | |
HashMathSet()
Constructs a new, empty mathematical set; the backing HashSet instance has default initial capacity (16) and
load factor (0.75). |
|
HashMathSet(java.util.Collection c)
Constructs a new mathematical set containing the elements in the specified collection. |
|
HashMathSet(int initialCapacity)
Constructs a new, empty mathematical set; the backing HashSet instance has the specified initial capacity and
default load factor, which is 0.75. |
|
HashMathSet(int initialCapacity,
float loadFactor)
Constructs a new, empty mathematical set; the backing HashSet instance has the specified initial capacity and
the specified load factor. |
Method Summary | |
boolean |
add(java.lang.Object o)
Adds the specified element to this set if it is not already present. |
void |
clear()
Removes all elements from this set. |
java.lang.Object |
clone()
Returns a shallow copy of this HashMathSet instance: the
elements themselves are not cloned. |
boolean |
contains(java.lang.Object o)
Returns true if this set contains the specified element. |
MathSet |
difference(java.util.Set s)
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. |
SetOfSets |
fixedSizeSubsets(int size)
Returns all possible subsets of this set with the specified size, contained in a HashSetOfSets instance. |
int |
hashCode()
Returns the hash code value for this mathematical set. |
MathSet |
intersection(java.util.Set s)
Returns the intersection with the specified set. |
boolean |
isEmpty()
Returns true if this set contains no elements. |
boolean |
isSubset(java.util.Set s)
Returns true if this mathematical set is a subset of the
specified set. |
java.util.Iterator |
iterator()
Returns an iterator over the elements in this mathematical set. |
SetOfSets |
powerSet()
Returns the power set of this mathematical set. |
private HashSetOfSets |
recursiveFixedSizeSubsets(int numberToAdd,
java.lang.Object[] elementPool,
int elementPoolStart,
HashMathSet setToAdd,
HashSetOfSets result)
Internal subroutine for recursively determining all fixed size subsets of a set. |
boolean |
remove(java.lang.Object o)
Removes the specified element from this set if it is present. |
int |
size()
Returns the number of elements in this set (its cardinality). |
MathSet |
symmetricDifference(java.util.Set s)
Returns the symmetric difference between this mathematical set and the specified set. |
MathSet |
union(java.util.Set s)
Returns the union with the specified set. |
Methods inherited from class mathCollection.AbstractMathSet |
isDisjoint, isSuperset, toString |
Methods inherited from class java.util.AbstractSet |
removeAll |
Methods inherited from class java.util.AbstractCollection |
addAll, containsAll, retainAll, toArray, toArray |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface mathCollection.MathSet |
isDisjoint, isSuperset |
Methods inherited from interface java.util.Set |
addAll, containsAll, removeAll, retainAll, toArray, toArray |
Field Detail |
private java.util.HashSet myHashSet
HashSet 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.
Constructor Detail |
public HashMathSet()
HashSet
instance has default initial capacity (16) and
load factor (0.75).
public HashMathSet(java.util.Collection c)
HashSet
is created with default
load factor (0.75) and an initial capacity sufficient to contain the
elements in the specified collection.
c
- the collection whose elements are to be placed into this set.
java.lang.NullPointerException
- if the specified collection is null.public HashMathSet(int initialCapacity)
HashSet
instance has the specified initial capacity and
default load factor, which is 0.75.
initialCapacity
- the initial capacity of the hash set.
java.lang.IllegalArgumentException
- if the initial capacity is less
than zero.public HashMathSet(int initialCapacity, float loadFactor)
HashSet
instance has the specified initial capacity and
the specified load factor.
initialCapacity
- the initial capacity of the hash set.loadFactor
- the load factor of the hash set.
java.lang.IllegalArgumentException
- if the initial capacity is less
than zero, or if the load factor is nonpositive.Method Detail |
public java.util.Iterator iterator()
iterator
in interface java.util.Set
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 super class.
hashCode
in interface java.util.Set
hashCode
in class AbstractMathSet
public java.lang.Object clone()
HashMathSet
instance: the
elements themselves are not cloned.
public boolean isEmpty()
true
if this set contains no elements.
isEmpty
in interface java.util.Set
true
if this set contains no elements,
false
otherwise.public int size()
size
in interface java.util.Set
public void clear()
clear
in interface java.util.Set
public boolean contains(java.lang.Object o)
true
if this set contains the specified element.
contains
in interface java.util.Set
o
- element whose presence in this set is to be tested.
true
if this set contains the specified element,
false
otherwise.public boolean add(java.lang.Object o)
If the set gets altered, this implementation sets
storedHashCode
to 0 (representing an unavailable hash code
value), which forces hashCode()
to recalculate the actual
hash code value.
add
in interface java.util.Set
o
- element to be added to this set.
true
if the set did not already contain the
specified element, false
otherwise.public boolean remove(java.lang.Object o)
If the set gets altered, this implementation sets
storedHashCode
to 0 (representing an unavailable hash code
value), which forces hashCode()
to recalculate the actual
hash code value.
remove
in interface java.util.Set
o
- object to be removed from this set, if present.
true
if the set contained the specified element,
false
otherwise.public boolean equals(java.lang.Object o)
true
if the specified object is also a set, the two sets
have the same size, and every element of the specified set is contained
in this set.
This implementation first checks if the given object is a
HashMathSet
. If so, the hash code values of this
mathematical set and the specified HashMathSet
are compared.
Since the hash code values are being cached, this represents a quick
solution if the sets aren't equal. However, if the hash code values are
equal, it cannot be assumed that the sets themselves are equal,
since different sets can have the same hash code value. In this case,
the result of the super method equals()
is returned.
equals
in interface java.util.Set
o
- object to be compared for equality with this set.
true
if the specified object is equal to this set,
false
otherwise.public boolean isSubset(java.util.Set s)
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.
isSubset
in interface MathSet
isSubset
in class AbstractMathSet
s
- set to be checked for being a superset.
true
if this mathematical set is a subset of the
specifed set, false
otherwise.AbstractMathSet.isSuperset(Set)
public MathSet union(java.util.Set s)
HashMathSet
containing all elements that are present in this
mathematical set or in the specified set.
union
in interface MathSet
s
- set that is to be united with.
public MathSet intersection(java.util.Set s)
HashMathSet
containing all elements that are present in this
mathematical set as well as in the specified set.
intersection
in interface MathSet
s
- set that is to be intersected with.
public MathSet difference(java.util.Set s)
HashMathSet
containing all
elements that are present in this mathematical set but not in the
specified set.
difference
in interface MathSet
s
- set from what the difference is calculated.
public MathSet symmetricDifference(java.util.Set s)
HashMathSet
containing all
elements that are present either in this mathematical set or in the
specified set but not in both.
symmetricDifference
in interface MathSet
s
- set from what the symmetric difference is calculated
public SetOfSets powerSet()
powerSet
in interface MathSet
private HashSetOfSets recursiveFixedSizeSubsets(int numberToAdd, java.lang.Object[] elementPool, int elementPoolStart, HashMathSet setToAdd, HashSetOfSets result)
numberToAdd
- number of elements to add to the subsets.
Required to be a positive integer.elementPool
- array containing all source set elements.elementPoolStart
- array index specifying the first of the
elements that are still to be processed.setToAdd
- the incomplete subset which more elements
get added to.result
- SetOfSets
accumulating the
complete subsets.
public SetOfSets fixedSizeSubsets(int size)
HashSetOfSets
instance. In case the
size
parameter is negative or greater than the size of the
set itself, an empty SetOfSets
is returned.
fixedSizeSubsets
in interface MathSet
size
- size of the returned subsets.
SetOfSets
containing all subsets of this
mathematical set with the specified size.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |