|
|||||||||
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.AbstractSetOfSets
mathCollection.HashSetOfSets
This class implements the SetOfSets
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. An instance of
HashMultiset
is used for permanently maintaining a 'flat'
version of the set of sets (a simple set containing all the elements from the
elementary sets once).
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.synchronizedSetOfSets
method. This is best done
at creation time, to prevent accidental unsynchronized access to the
HashSetOfSets
instance:
SetOfSets sos = MathCollections.synchronizedSetofSets(new HashSetofSets(...));
Set
,
MathSet
,
AbstractSetOfSets
,
HashSet
,
HashMap
,
Serialized FormNested Class Summary | |
private static class |
HashSetOfSets.Element
|
private class |
HashSetOfSets.HashSetOfSetsIterator
A 'wrapper' iterator class that uses HashSet.iterator()
while accounting for the additional storedHashCode attribute. |
Field Summary | |
private HashMultiset |
flatVersion
Acts as a cache for the 'flattened' multiset version of this set of sets out of performance considerations. |
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 | |
HashSetOfSets()
Constructs a new, empty set of sets; the backing HashSet
instance as well as the HashMap flatVersion have default
initial capacity (16) and load factor (0.75). |
|
HashSetOfSets(java.util.Collection c)
Constructs a new set of sets set containing the elements in the specified collection. |
|
HashSetOfSets(int initialCapacity)
Constructs a new, empty set of sets; the backing HashSet
instance has the specified initial capacity and default load factor,
which is 0.75. |
|
HashSetOfSets(int initialCapacity,
float loadFactor)
Constructs a new, empty set of sets; 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 (Set) to this set of sets if it is not already present. |
void |
clear()
Removes all elements from this set of sets. |
java.lang.Object |
clone()
Returns a shallow copy of this HashSetOfSets instance: the
elements themselves are not cloned. |
SetOfSets |
containingSets(java.lang.Object o)
Returns a set containing the elementary sets from within this set of sets that contain the specified element. |
boolean |
contains(java.lang.Object o)
Returns true if this set of sets contains the specified
element (Set). |
boolean |
containsAtom(java.lang.Object o)
Returns true if this set of sets contains a set in which the
specified element is present. |
boolean |
equals(java.lang.Object o)
Compares the specified object with this set of sets for equality. |
int |
hashCode()
Returns the hash code value for this set of sets. |
boolean |
isEmpty()
Returns true if this set of sets contains no elements. |
java.util.Iterator |
iterator()
Returns an iterator over the elements in this set of sets. |
boolean |
remove(java.lang.Object o)
Removes the specified element (Set) from this set of sets if it is present. |
int |
size()
Returns the number of elements (contained sets) in this set of sets (its cardinality). |
SetOfSets |
sortedArraySupersets(java.util.Set s)
Returns a new set containing the supersets of the specified set. |
SetOfSets |
subsets(java.util.Set s)
Returns a new set containing the subsets of the specified set. |
SetOfSets |
supersets(java.util.Set s)
Returns a new set containing the supersets of the specified set. |
Multiset |
toMultiset()
Returns the 'flattened' multiset version of this set of sets, containing the same elements as in all sets of this set of sets. |
java.util.Set |
toSet()
Returns the 'flattened' version of this set of sets, in which every element from the elementary sets is present once. |
Methods inherited from class mathCollection.AbstractSetOfSets |
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 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 HashMultiset flatVersion
private int storedHashCode
storedHashCode
is set to 0 and
gets updated as soon as the hashCode()
method is called.
Constructor Detail |
public HashSetOfSets()
HashSet
instance as well as the HashMap flatVersion
have default
initial capacity (16) and load factor (0.75).
public HashSetOfSets(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 HashSetOfSets(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 HashSetOfSets(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 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 AbstractSetOfSets
public java.lang.Object clone()
HashSetOfSets
instance: the
elements themselves are not cloned.
public boolean isEmpty()
true
if this set of sets contains no elements.
isEmpty
in interface java.util.Set
true
if this set of sets 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 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 SetOfSets
o
- element to be added to this set of sets.
true
if the set of sets did not already contain
the specified element, false
otherwise.
java.lang.ClassCastException
- if the type of the specified element
is incompatible (!(o instanceof Set)
).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 SetOfSets
o
- object to be removed from this set of sets, if present.
true
if the set of sets contained the specified
element, false
otherwise.
java.lang.ClassCastException
- if the type of the specified element
is incompatible (!(o instanceof Set)
).public boolean contains(java.lang.Object o)
true
if this set of sets contains the specified
element (Set).
contains
in interface SetOfSets
o
- element whose presence in this set of sets is to be tested.
true
if this set of sets contains the specified
element, false
otherwise.
java.lang.ClassCastException
- if the type of the specified element
is incompatible (!(o instanceof Set)
).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
HashSetOfSets
. If so, the hash code values of this
set of sets and the specified HashSetOfSets
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 of sets.
true
if the specified object is equal to this set
of sets, false
otherwise.public boolean containsAtom(java.lang.Object o)
true
if this set of sets contains a set in which the
specified element is present.
containsAtom
in interface SetOfSets
containsAtom
in class AbstractSetOfSets
o
- the element whose presence in any elementary set within this
set of sets is tested for.
true
if any set within this set of sets contains
the specified element, false
otherwise.public java.util.Set toSet()
toSet
in interface SetOfSets
public Multiset toMultiset()
toMultiset
in interface SetOfSets
public SetOfSets containingSets(java.lang.Object o)
containingSets
in interface SetOfSets
o
- the element that the returned sets have to contain.
public SetOfSets subsets(java.util.Set s)
subsets
in interface SetOfSets
s
- the set that the returned sets have to be subsets of.
public SetOfSets supersets(java.util.Set s)
supersets
in interface SetOfSets
s
- the set that the returned sets have to be supersets of.
public SetOfSets sortedArraySupersets(java.util.Set s)
This implementation is faster for large numbers of large sets. An array containing the elements of the specified set, sorted by their frequency of occurrence in this set of sets, is used to make the superset relation checks faster.
Note that this implementation requires the elements of this set of sets
to have an efficient contains()
method (constant complexity)
to achieve the improved performance.
s
- the set that the returned sets have to be supersets of.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |