edu.umd.cloud9.util.map
Class HMapKI<K extends Comparable<?>>

java.lang.Object
  extended by edu.umd.cloud9.util.map.HMapKI<K>
Type Parameters:
K - the type of keys maintained by this map
All Implemented Interfaces:
MapKI<K>, Serializable, Cloneable
Direct Known Subclasses:
HMapKIW, HMapSIW

public class HMapKI<K extends Comparable<?>>
extends Object
implements MapKI<K>, Cloneable, Serializable

Hash-based implementation of the MapKI interface. MapKI is a specialized variant the standard Java Map interface, except that the values are hard coded as ints for efficiency reasons (keys can be arbitrary objects). This implementation was adapted from HashMap version 1.73, 03/13/07. See this benchmark for an efficiency comparison.

See Also:
Serialized Form

Field Summary
 
Fields inherited from interface edu.umd.cloud9.util.map.MapKI
DEFAULT_VALUE
 
Constructor Summary
HMapKI()
          Constructs an empty HMapKI with the default initial capacity (1024) and the default load factor (0.75).
HMapKI(int initialCapacity)
          Constructs an empty HMapKI with the specified initial capacity and the default load factor (0.75).
HMapKI(int initialCapacity, float loadFactor)
          Constructs an empty HMapKI with the specified initial capacity and load factor.
HMapKI(MapKI<? extends K> m)
          Constructs a new HMapKI with the same mappings as the specified MapKI.
 
Method Summary
 void clear()
          Removes all of the mappings from this map.
 Object clone()
          Returns a shallow copy of this HMapKI instance: the keys and values themselves are not cloned.
 boolean containsKey(K key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue(int value)
          Returns true if this map contains one or more mappings with the specified value.
 int dot(MapKI<K> m)
          Computes the dot product of this map with another map.
 Set<MapKI.Entry<K>> entrySet()
          Returns a Set view of the mappings contained in this map.
 int get(K key)
          Returns the value to which the specified key is mapped, or throws NoSuchElementException if this map contains no mapping for the key.
 MapKI.Entry<K>[] getEntriesSortedByKey()
           
 MapKI.Entry<K>[] getEntriesSortedByKey(int n)
           
 MapKI.Entry<K>[] getEntriesSortedByValue()
          Returns entries sorted by descending value.
 MapKI.Entry<K>[] getEntriesSortedByValue(int n)
          Returns top n entries sorted by descending value.
 void increment(K key)
          Increments the key.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
 Set<K> keySet()
          Returns a Set view of the keys contained in this map.
 void plus(MapKI<K> m)
          Adds values of keys from another map to this map.
 int put(K key, int value)
          Associates the specified value with the specified key in this map.
 void putAll(MapKI<? extends K> m)
          Copies all of the mappings from the specified map to this map.
 int remove(K key)
          Removes the mapping for a key from this map if it is present.
 int size()
          Returns the number of key-value mappings in this map.
 String toString()
           
 Collection<Integer> values()
          Returns a Collection view of the values contained in this map.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface edu.umd.cloud9.util.map.MapKI
equals, hashCode
 

Constructor Detail

HMapKI

public HMapKI(int initialCapacity,
              float loadFactor)
Constructs an empty HMapKI with the specified initial capacity and load factor.

Parameters:
initialCapacity - the initial capacity
loadFactor - the load factor
Throws:
IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive

HMapKI

public HMapKI(int initialCapacity)
Constructs an empty HMapKI with the specified initial capacity and the default load factor (0.75).

Parameters:
initialCapacity - the initial capacity.
Throws:
IllegalArgumentException - if the initial capacity is negative.

HMapKI

public HMapKI()
Constructs an empty HMapKI with the default initial capacity (1024) and the default load factor (0.75).


HMapKI

public HMapKI(MapKI<? extends K> m)
Constructs a new HMapKI with the same mappings as the specified MapKI. The HMapKI is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified MapKI.

Parameters:
m - the map whose mappings are to be placed in this map
Throws:
NullPointerException - if the specified map is null
Method Detail

size

public int size()
Description copied from interface: MapKI
Returns the number of key-value mappings in this map.

Specified by:
size in interface MapKI<K extends Comparable<?>>
Returns:
the number of key-value mappings in this map

isEmpty

public boolean isEmpty()
Description copied from interface: MapKI
Returns true if this map contains no key-value mappings.

Specified by:
isEmpty in interface MapKI<K extends Comparable<?>>
Returns:
true if this map contains no key-value mappings

get

public int get(K key)
Description copied from interface: MapKI
Returns the value to which the specified key is mapped, or throws NoSuchElementException if this map contains no mapping for the key.

Specified by:
get in interface MapKI<K extends Comparable<?>>
Parameters:
key - the key whose associated value is to be returned
Returns:
the value to which the specified key is mapped

containsKey

public boolean containsKey(K key)
Description copied from interface: MapKI
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey in interface MapKI<K extends Comparable<?>>
Parameters:
key - key whose presence in this map is to be tested
Returns:
true if this map contains a mapping for the specified key

put

public int put(K key,
               int value)
Description copied from interface: MapKI
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced by the specified value.

Specified by:
put in interface MapKI<K extends Comparable<?>>
Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the specified key

putAll

public void putAll(MapKI<? extends K> m)
Description copied from interface: MapKI
Copies all of the mappings from the specified map to this map.

Specified by:
putAll in interface MapKI<K extends Comparable<?>>
Parameters:
m - mappings to be stored in this map

remove

public int remove(K key)
Description copied from interface: MapKI
Removes the mapping for a key from this map if it is present. No action is performed if this map does not contain the key.

Specified by:
remove in interface MapKI<K extends Comparable<?>>
Parameters:
key - key whose mapping is to be removed from the map

clear

public void clear()
Description copied from interface: MapKI
Removes all of the mappings from this map. The map will be empty after this call returns.

Specified by:
clear in interface MapKI<K extends Comparable<?>>

containsValue

public boolean containsValue(int value)
Description copied from interface: MapKI
Returns true if this map contains one or more mappings with the specified value.

Specified by:
containsValue in interface MapKI<K extends Comparable<?>>
Parameters:
value - value whose presence in this map is to be tested
Returns:
true this map contains one or more mappings with the specified value

clone

public Object clone()
Returns a shallow copy of this HMapKI instance: the keys and values themselves are not cloned.

Overrides:
clone in class Object
Returns:
a shallow copy of this map

keySet

public Set<K> keySet()
Description copied from interface: MapKI
Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. See Map.keySet() for more details.

Specified by:
keySet in interface MapKI<K extends Comparable<?>>
Returns:
a set view of the keys contained in this map

values

public Collection<Integer> values()
Description copied from interface: MapKI
Returns a Collection view of the values contained in this map. Note that this is a inefficient operation since it triggers autoboxing of the int values, which is exactly what this implementation is trying to avoid. Unlike a standard Java Map, values in the backing map cannot be altered with this collection view.

Specified by:
values in interface MapKI<K extends Comparable<?>>
Returns:
a collection view of the values contained in this map

entrySet

public Set<MapKI.Entry<K>> entrySet()
Description copied from interface: MapKI
Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. See Map.entrySet() for more details.

Specified by:
entrySet in interface MapKI<K extends Comparable<?>>
Returns:
a set view of the mappings contained in this map

toString

public String toString()
Overrides:
toString in class Object

plus

public void plus(MapKI<K> m)
Adds values of keys from another map to this map.

Parameters:
m - the other map

dot

public int dot(MapKI<K> m)
Computes the dot product of this map with another map.

Parameters:
m - the other map

increment

public void increment(K key)
Increments the key. If the key does not exist in the map, its value is set to one.

Parameters:
key - key to increment

getEntriesSortedByValue

public MapKI.Entry<K>[] getEntriesSortedByValue()
Returns entries sorted by descending value. Ties broken by the key.

Returns:
entries sorted by descending value

getEntriesSortedByValue

public MapKI.Entry<K>[] getEntriesSortedByValue(int n)
Returns top n entries sorted by descending value. Ties broken by the key.

Parameters:
n - number of entries to return
Returns:
top n entries sorted by descending value

getEntriesSortedByKey

public MapKI.Entry<K>[] getEntriesSortedByKey()

getEntriesSortedByKey

public MapKI.Entry<K>[] getEntriesSortedByKey(int n)