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:
Serializable, Cloneable, Iterable<T>, Collection<T>, Set<T>

public class OpenHashSet<T>
extends AbstractSet
implements 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
DEFAULT_CAPACITY, DEFAULT_MAX_LOAD_FACTOR, DEFAULT_MIN_LOAD_FACTOR, 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(Object key)
           
 boolean addAll(Collection<? extends T> c)
           
 void clear()
          Removes all values associations from the receiver.
 Object clone()
          Returns a deep copy of the receiver.
 boolean contains(Object key)
          Returns true if the receiver contains the specified key.
 boolean containsAll(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(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.
 int hashCode()
           
protected  int indexOfInsertion(T key)
           
protected  int indexOfKey(T key)
           
 boolean isEmpty()
          Returns true if the receiver contains no (key,value) associations.
 Iterator<T> iterator()
          Implement the standard Java Collections iterator.
 List<T> keys()
           
 void keys(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(Object key)
          Removes the given key with its associated element from the receiver, if present.
 boolean removeAll(Collection<?> c)
           
 boolean retainAll(Collection<?> c)
           
protected  void setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor)
          Initializes the receiver.
 Object[] toArray()
           
<T> T[]
toArray(T[] 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, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Set
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:
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:
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 Collection<T>
Specified by:
clear in interface Set<T>
Specified by:
clear in class AbstractSet

clone

public 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(Object key)
Returns true if the receiver contains the specified key.

Specified by:
contains in interface Collection<T>
Specified by:
contains in interface 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(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(Object key)
Specified by:
add in interface Collection<T>
Specified by:
add in interface 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(Object key)
Removes the given key with its associated element from the receiver, if present.

Specified by:
remove in interface Collection<T>
Specified by:
remove in interface 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:
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 Collection<T>
Specified by:
isEmpty in interface Set<T>
Overrides:
isEmpty in class AbstractSet
Returns:
true if the receiver contains no (key,value) associations.

equals

public boolean equals(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 Collection<T>
Specified by:
equals in interface Set<T>
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Collection<T>
Specified by:
hashCode in interface Set<T>
Overrides:
hashCode in class Object

iterator

public 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 Iterable<T>
Specified by:
iterator in interface Collection<T>
Specified by:
iterator in interface Set<T>

toArray

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

addAll

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

containsAll

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

removeAll

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

retainAll

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

toArray

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

keys

public List<T> keys()


Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.