edu.rice.cs.plt.collect
Class HashMultiset<T>

java.lang.Object
  extended by java.util.AbstractCollection<T>
      extended by edu.rice.cs.plt.collect.HashMultiset<T>
All Implemented Interfaces:
Multiset<T>, SizedIterable<T>, java.io.Serializable, java.lang.Iterable<T>, java.util.Collection<T>

public class HashMultiset<T>
extends java.util.AbstractCollection<T>
implements Multiset<T>, java.io.Serializable

A HashMap-based implementation of Multiset. Values in the set must be valid as keys in a HashMap. Care is taken so that most operations are either performed in constant time or time linear to the number of unique elements in the multiset (as opposed to time linear in the size of the multiset, which may be significantly larger).

See Also:
Serialized Form

Constructor Summary
HashMultiset()
          Create an empty HashMultiset
HashMultiset(java.util.Collection<? extends T> coll)
          Create a HashMultiset containing all the elements of coll
 
Method Summary
 boolean add(T val)
          Add a single instance of val to the multiset.
 boolean add(T val, int instances)
          Add the given number of instances of val to the multiset.
 boolean addAll(java.util.Collection<? extends T> coll)
          Add all the elements of coll to this multiset.
 PredicateSet<T> asSet()
          Produce a set view of the multiset.
 void clear()
          Remove all elements from the multiset
 boolean contains(java.lang.Object obj)
          Whether the multiset contains at least one instance of obj.
 boolean containsAll(java.util.Collection<?> c)
          Whether each element of the collection appears at least once in this multiset.
 int count(java.lang.Object value)
          The number of times value appears in the multiset (if it does not appear, the result is 0).
 boolean equals(java.lang.Object obj)
          Compares two multisets.
 boolean hasFixedSize()
          Whether the multiset can change size.
 int hashCode()
          A hash code for the multiset, computed by summing the hash codes of each element in the iterator.
 boolean isEmpty()
          Whether the multiset is empty.
 boolean isInfinite()
          Whether the iterator has an infinite number of elements.
 boolean isStatic()
          Whether the multiset will always contain the same values.
 boolean isSupersetOf(Multiset<?> m)
          Whether each element (elt) of s appears at least s.count(elt) times in this multiset.
 java.util.Iterator<T> iterator()
          An iterator for the multiset.
 boolean remove(java.lang.Object obj)
          Remove a single instance of obj from the multiset, if one exists.
 boolean remove(java.lang.Object obj, int instances)
          Remove the given number of instances of obj from the multiset.
 boolean removeAll(java.util.Collection<?> coll)
          Remove all the elements of coll from this multiset.
 boolean removeAllInstances(java.lang.Object obj)
          Remove all instances of obj from the multiset
 boolean retainAll(java.util.Collection<?> coll)
          Remove all the elements of this multiset except those contained in coll.
 int size()
          The size of the multiset.
 int size(int bound)
          The size of the multiset bounded by the given value.
 java.lang.String toString()
           
 
Methods inherited from class java.util.AbstractCollection
toArray, toArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface edu.rice.cs.plt.collect.Multiset
toArray, toArray
 

Constructor Detail

HashMultiset

public HashMultiset()
Create an empty HashMultiset


HashMultiset

public HashMultiset(java.util.Collection<? extends T> coll)
Create a HashMultiset containing all the elements of coll

Method Detail

size

public int size()
Description copied from interface: Multiset
The size of the multiset.

Specified by:
size in interface Multiset<T>
Specified by:
size in interface SizedIterable<T>
Specified by:
size in interface java.util.Collection<T>
Specified by:
size in class java.util.AbstractCollection<T>

size

public int size(int bound)
Description copied from interface: Multiset
The size of the multiset bounded by the given value.

Specified by:
size in interface Multiset<T>
Specified by:
size in interface SizedIterable<T>
Parameters:
bound - Maximum result. Assumed to be nonnegative.

isInfinite

public boolean isInfinite()
Description copied from interface: Multiset
Whether the iterator has an infinite number of elements.

Specified by:
isInfinite in interface Multiset<T>
Specified by:
isInfinite in interface SizedIterable<T>

hasFixedSize

public boolean hasFixedSize()
Description copied from interface: Multiset
Whether the multiset can change size.

Specified by:
hasFixedSize in interface Multiset<T>
Specified by:
hasFixedSize in interface SizedIterable<T>

isStatic

public boolean isStatic()
Description copied from interface: Multiset
Whether the multiset will always contain the same values.

Specified by:
isStatic in interface Multiset<T>
Specified by:
isStatic in interface SizedIterable<T>

isEmpty

public boolean isEmpty()
Description copied from interface: Multiset
Whether the multiset is empty.

Specified by:
isEmpty in interface Multiset<T>
Specified by:
isEmpty in interface SizedIterable<T>
Specified by:
isEmpty in interface java.util.Collection<T>
Overrides:
isEmpty in class java.util.AbstractCollection<T>

contains

public boolean contains(java.lang.Object obj)
Description copied from interface: Multiset
Whether the multiset contains at least one instance of obj.

Specified by:
contains in interface Multiset<T>
Specified by:
contains in interface java.util.Collection<T>
Overrides:
contains in class java.util.AbstractCollection<T>

containsAll

public boolean containsAll(java.util.Collection<?> c)
Description copied from interface: Multiset
Whether each element of the collection appears at least once in this multiset.

Specified by:
containsAll in interface Multiset<T>
Specified by:
containsAll in interface java.util.Collection<T>
Overrides:
containsAll in class java.util.AbstractCollection<T>

isSupersetOf

public boolean isSupersetOf(Multiset<?> m)
Description copied from interface: Multiset
Whether each element (elt) of s appears at least s.count(elt) times in this multiset.

Specified by:
isSupersetOf in interface Multiset<T>

count

public int count(java.lang.Object value)
Description copied from interface: Multiset
The number of times value appears in the multiset (if it does not appear, the result is 0). If the number is too large, Integer.MAX_VALUE may be returned.

Specified by:
count in interface Multiset<T>

asSet

public PredicateSet<T> asSet()
Produce a set view of the multiset. Removing a value from the set is the equivalent of invoking removeAllInstances(java.lang.Object) for that value. Adding to the set is not supported.

Specified by:
asSet in interface Multiset<T>

iterator

public java.util.Iterator<T> iterator()
Description copied from interface: Multiset
An iterator for the multiset. If the set contains n instances of a value, that value will appear n times during iteration. Invoking remove on the iterator removes one instance.

Specified by:
iterator in interface Multiset<T>
Specified by:
iterator in interface java.lang.Iterable<T>
Specified by:
iterator in interface java.util.Collection<T>
Specified by:
iterator in class java.util.AbstractCollection<T>

add

public boolean add(T val)
Description copied from interface: Multiset
Add a single instance of val to the multiset.

Specified by:
add in interface Multiset<T>
Specified by:
add in interface java.util.Collection<T>
Overrides:
add in class java.util.AbstractCollection<T>
Returns:
true (indicating that the multiset was modified)

add

public boolean add(T val,
                   int instances)
Description copied from interface: Multiset
Add the given number of instances of val to the multiset.

Specified by:
add in interface Multiset<T>
Returns:
true (indicating that the multiset was modified)

addAll

public boolean addAll(java.util.Collection<? extends T> coll)
Description copied from interface: Multiset
Add all the elements of coll to this multiset. If the same value appears multiple times in coll, it will appear multiple times in this multiset.

Specified by:
addAll in interface Multiset<T>
Specified by:
addAll in interface java.util.Collection<T>
Overrides:
addAll in class java.util.AbstractCollection<T>
Returns:
true iff the multiset was successfully modified

remove

public boolean remove(java.lang.Object obj)
Description copied from interface: Multiset
Remove a single instance of obj from the multiset, if one exists.

Specified by:
remove in interface Multiset<T>
Specified by:
remove in interface java.util.Collection<T>
Overrides:
remove in class java.util.AbstractCollection<T>
Returns:
true iff the multiset was modified

remove

public boolean remove(java.lang.Object obj,
                      int instances)
Description copied from interface: Multiset
Remove the given number of instances of obj from the multiset. If count(obj) <= instances, removes all instances of the given value.

Specified by:
remove in interface Multiset<T>
Returns:
true iff the multiset was modified

removeAllInstances

public boolean removeAllInstances(java.lang.Object obj)
Description copied from interface: Multiset
Remove all instances of obj from the multiset

Specified by:
removeAllInstances in interface Multiset<T>
Returns:
true iff the multiset was modified

removeAll

public boolean removeAll(java.util.Collection<?> coll)
Description copied from interface: Multiset
Remove all the elements of coll from this multiset. If the same value appears multiple times in coll, the same number of instances will be removed from this multiset.

Specified by:
removeAll in interface Multiset<T>
Specified by:
removeAll in interface java.util.Collection<T>
Overrides:
removeAll in class java.util.AbstractCollection<T>
Returns:
true iff the multiset was modified

retainAll

public boolean retainAll(java.util.Collection<?> coll)
Description copied from interface: Multiset
Remove all the elements of this multiset except those contained in coll. If the same value appears multiple times in coll, at most that number of instances will not be removed from this multiset.

Specified by:
retainAll in interface Multiset<T>
Specified by:
retainAll in interface java.util.Collection<T>
Overrides:
retainAll in class java.util.AbstractCollection<T>
Returns:
true iff the multiset was modified

clear

public void clear()
Description copied from interface: Multiset
Remove all elements from the multiset

Specified by:
clear in interface Multiset<T>
Specified by:
clear in interface java.util.Collection<T>
Overrides:
clear in class java.util.AbstractCollection<T>

toString

public java.lang.String toString()
Overrides:
toString in class java.util.AbstractCollection<T>

equals

public boolean equals(java.lang.Object obj)
Description copied from interface: Multiset
Compares two multisets. Two multisets are equal if they contain all the same values and the same number of instances of those values.

Specified by:
equals in interface Multiset<T>
Specified by:
equals in interface java.util.Collection<T>
Overrides:
equals in class java.lang.Object
Returns:
true iff obj is a multiset with the same elements as this

hashCode

public int hashCode()
Description copied from interface: Multiset
A hash code for the multiset, computed by summing the hash codes of each element in the iterator. (An empty multiset has hash code 0; each null element has hash code 1.)

Specified by:
hashCode in interface Multiset<T>
Specified by:
hashCode in interface java.util.Collection<T>
Overrides:
hashCode in class java.lang.Object