org.apache.mahout.math.set
Class OpenHashSet<T>

java.lang.Object
  extended by org.apache.mahout.math.PersistentObject
      extended by org.apache.mahout.math.set.AbstractSet
          extended by org.apache.mahout.math.set.OpenHashSet<T>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<T>, java.util.Collection<T>, java.util.Set<T>

public class OpenHashSet<T>
extends AbstractSet
implements java.util.Set<T>

Open hashing alternative to java.util.HashSet.

See Also:
Serialized Form

Field Summary
protected static byte FREE
           
protected static byte FULL
           
protected static char NO_KEY_VALUE
           
protected static byte REMOVED
           
 
Fields inherited from class org.apache.mahout.math.set.AbstractSet
defaultCapacity, defaultMaxLoadFactor, defaultMinLoadFactor, distinct, highWaterMark, lowWaterMark, maxLoadFactor, minLoadFactor
 
Constructor Summary
OpenHashSet()
          Constructs an empty map with default capacity and default load factors.
OpenHashSet(int initialCapacity)
          Constructs an empty map with the specified initial capacity and default load factors.
OpenHashSet(int initialCapacity, double minLoadFactor, double maxLoadFactor)
          Constructs an empty map with the specified initial capacity and the specified minimum and maximum load factor.
 
Method Summary
 boolean add(java.lang.Object key)
           
 boolean addAll(java.util.Collection<? extends T> c)
           
 void clear()
          Removes all values associations from the receiver.
 java.lang.Object clone()
          Returns a deep copy of the receiver.
 boolean contains(java.lang.Object key)
          Returns true if the receiver contains the specified key.
 boolean containsAll(java.util.Collection<?> c)
           
 void ensureCapacity(int minCapacity)
          Ensures that the receiver can hold at least the specified number of associations without needing to allocate new internal memory.
 boolean equals(java.lang.Object obj)
          OpenHashSet instances are only equal to other OpenHashSet instances, not to any other collection.
 boolean forEachKey(ObjectProcedure<T> procedure)
          Applies a procedure to each key of the receiver, if any.
protected  int indexOfInsertion(T key)
           
protected  int indexOfKey(T key)
           
 boolean isEmpty()
          Returns true if the receiver contains no (key,value) associations.
 java.util.Iterator<T> iterator()
          Implement the standard Java Collections iterator.
 java.util.List<T> keys()
           
 void keys(java.util.List<T> list)
          Fills all keys contained in the receiver into the specified list.
protected  void rehash(int newCapacity)
          Rehashes the contents of the receiver into a new table with a smaller or larger capacity.
 boolean remove(java.lang.Object key)
          Removes the given key with its associated element from the receiver, if present.
 boolean removeAll(java.util.Collection<?> c)
           
 boolean retainAll(java.util.Collection<?> c)
           
protected  void setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor)
          Initializes the receiver.
 java.lang.Object[] toArray()
           
<T2> T2[]
toArray(T2[] a)
           
 void trimToSize()
          Trims the capacity of the receiver to be the receiver's current size.
 
Methods inherited from class org.apache.mahout.math.set.AbstractSet
chooseGrowCapacity, chooseHighWaterMark, chooseLowWaterMark, chooseMeanCapacity, chooseShrinkCapacity, equalsMindTheNull, nextPrime, size
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Set
hashCode, size
 

Field Detail

FREE

protected static final byte FREE
See Also:
Constant Field Values

FULL

protected static final byte FULL
See Also:
Constant Field Values

REMOVED

protected static final byte REMOVED
See Also:
Constant Field Values

NO_KEY_VALUE

protected static final char NO_KEY_VALUE
See Also:
Constant Field Values
Constructor Detail

OpenHashSet

public OpenHashSet()
Constructs an empty map with default capacity and default load factors.


OpenHashSet

public OpenHashSet(int initialCapacity)
Constructs an empty map with the specified initial capacity and default load factors.

Parameters:
initialCapacity - the initial capacity of the map.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is less than zero.

OpenHashSet

public OpenHashSet(int initialCapacity,
                   double minLoadFactor,
                   double maxLoadFactor)
Constructs an empty map with the specified initial capacity and the specified minimum and maximum load factor.

Parameters:
initialCapacity - the initial capacity.
minLoadFactor - the minimum load factor.
maxLoadFactor - the maximum load factor.
Throws:
java.lang.IllegalArgumentException - if initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) || (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >= maxLoadFactor).
Method Detail

clear

public void clear()
Removes all values associations from the receiver. Implicitly calls trimToSize().

Specified by:
clear in interface java.util.Collection<T>
Specified by:
clear in interface java.util.Set<T>
Specified by:
clear in class AbstractSet

clone

public java.lang.Object clone()
Returns a deep copy of the receiver.

Overrides:
clone in class PersistentObject
Returns:
a deep copy of the receiver.

contains

public boolean contains(java.lang.Object key)
Returns true if the receiver contains the specified key.

Specified by:
contains in interface java.util.Collection<T>
Specified by:
contains in interface java.util.Set<T>
Returns:
true if the receiver contains the specified key.

ensureCapacity

public void ensureCapacity(int minCapacity)
Ensures that the receiver can hold at least the specified number of associations without needing to allocate new internal memory. If necessary, allocates new internal memory and increases the capacity of the receiver.

This method never need be called; it is for performance tuning only. Calling this method before add()ing a large number of associations boosts performance, because the receiver will grow only once instead of potentially many times and hash collisions get less probable.

Overrides:
ensureCapacity in class AbstractSet
Parameters:
minCapacity - the desired minimum capacity.

forEachKey

public boolean forEachKey(ObjectProcedure<T> procedure)
Applies a procedure to each key of the receiver, if any. Note: Iterates over the keys in no particular order. Subclasses can define a particular order, for example, "sorted by key". All methods which can be expressed in terms of this method (most methods can) must guarantee to use the same order defined by this method, even if it is no particular order. This is necessary so that, for example, methods keys and values will yield association pairs, not two uncorrelated lists.

Parameters:
procedure - the procedure to be applied. Stops iteration if the procedure returns false, otherwise continues.
Returns:
false if the procedure stopped before all keys where iterated over, true otherwise.

indexOfInsertion

protected int indexOfInsertion(T key)
Parameters:
key - the key to be added to the receiver.
Returns:
the index where the key would need to be inserted, if it is not already contained. Returns -index-1 if the key is already contained at slot index. Therefore, if the returned index < 0, then it is already contained at slot -index-1. If the returned index >= 0, then it is NOT already contained and should be inserted at slot index.

indexOfKey

protected int indexOfKey(T key)
Parameters:
key - the key to be searched in the receiver.
Returns:
the index where the key is contained in the receiver, returns -1 if the key was not found.

keys

public void keys(java.util.List<T> list)
Fills all keys contained in the receiver into the specified list. Fills the list, starting at index 0. After this call returns the specified list has a new size that equals this.size(). This method can be used to iterate over the keys of the receiver.

Parameters:
list - the list to be filled, can have any size.

add

public boolean add(java.lang.Object key)
Specified by:
add in interface java.util.Collection<T>
Specified by:
add in interface java.util.Set<T>

rehash

protected void rehash(int newCapacity)
Rehashes the contents of the receiver into a new table with a smaller or larger capacity. This method is called automatically when the number of keys in the receiver exceeds the high water mark or falls below the low water mark.


remove

public boolean remove(java.lang.Object key)
Removes the given key with its associated element from the receiver, if present.

Specified by:
remove in interface java.util.Collection<T>
Specified by:
remove in interface java.util.Set<T>
Parameters:
key - the key to be removed from the receiver.
Returns:
true if the receiver contained the specified key, false otherwise.

setUp

protected void setUp(int initialCapacity,
                     double minLoadFactor,
                     double maxLoadFactor)
Initializes the receiver.

Overrides:
setUp in class AbstractSet
Parameters:
initialCapacity - the initial capacity of the receiver.
minLoadFactor - the minLoadFactor of the receiver.
maxLoadFactor - the maxLoadFactor of the receiver.
Throws:
java.lang.IllegalArgumentException - if initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) || (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >= maxLoadFactor).

trimToSize

public void trimToSize()
Trims the capacity of the receiver to be the receiver's current size. Releases any superfluous internal memory. An application can use this operation to minimize the storage of the receiver.

Overrides:
trimToSize in class AbstractSet

isEmpty

public boolean isEmpty()
Description copied from class: AbstractSet
Returns true if the receiver contains no (key,value) associations.

Specified by:
isEmpty in interface java.util.Collection<T>
Specified by:
isEmpty in interface java.util.Set<T>
Overrides:
isEmpty in class AbstractSet
Returns:
true if the receiver contains no (key,value) associations.

equals

public boolean equals(java.lang.Object obj)
OpenHashSet instances are only equal to other OpenHashSet instances, not to any other collection. Hypothetically, we should check for and permit equals on other Sets.

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

iterator

public java.util.Iterator<T> iterator()
Implement the standard Java Collections iterator. Note that 'remove' is silently ineffectual here. This method is provided for convenience, only.

Specified by:
iterator in interface java.lang.Iterable<T>
Specified by:
iterator in interface java.util.Collection<T>
Specified by:
iterator in interface java.util.Set<T>

toArray

public java.lang.Object[] toArray()
Specified by:
toArray in interface java.util.Collection<T>
Specified by:
toArray in interface java.util.Set<T>

addAll

public boolean addAll(java.util.Collection<? extends T> c)
Specified by:
addAll in interface java.util.Collection<T>
Specified by:
addAll in interface java.util.Set<T>

containsAll

public boolean containsAll(java.util.Collection<?> c)
Specified by:
containsAll in interface java.util.Collection<T>
Specified by:
containsAll in interface java.util.Set<T>

removeAll

public boolean removeAll(java.util.Collection<?> c)
Specified by:
removeAll in interface java.util.Collection<T>
Specified by:
removeAll in interface java.util.Set<T>

retainAll

public boolean retainAll(java.util.Collection<?> c)
Specified by:
retainAll in interface java.util.Collection<T>
Specified by:
retainAll in interface java.util.Set<T>

toArray

public <T2> T2[] toArray(T2[] a)
Specified by:
toArray in interface java.util.Collection<T>
Specified by:
toArray in interface java.util.Set<T>

keys

public java.util.List<T> keys()


Copyright © 2008-2010 The Apache Software Foundation. All Rights Reserved.