org.dishevelled.weighted
Class HashWeightedMap<E>

java.lang.Object
  extended by org.dishevelled.weighted.HashWeightedMap<E>
Type Parameters:
E - the type of elements maintained by this weighted map
All Implemented Interfaces:
Map<E,Double>, WeightedMap<E>

public final class HashWeightedMap<E>
extends Object
implements WeightedMap<E>

Implementation of WeightedMap that delegates to a HashMap.

Version:
$Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $
Author:
Michael Heuer, Mark Schreiber

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
HashWeightedMap()
          Create a new weighted map with the default initial capacity and load factor.
HashWeightedMap(int initialCapacity)
          Create a new weighted map with the specified initial capacity and default load factor.
HashWeightedMap(int initialCapacity, float loadFactor)
          Create a new weighted map with the specified initial capacity and load factor.
HashWeightedMap(WeightedMap<? extends E> weightedMap)
          Create a new weighted map with the elements and weights in the specified weighted map (copy constructor).
 
Method Summary
 void clear()
          
 boolean containsKey(Object o)
          
 boolean containsValue(Object o)
          
 Set<Map.Entry<E,Double>> entrySet()
          
 Double get(Object o)
          
 boolean isEmpty()
          
 Set<E> keySet()
          
 int maximumRank()
          Return the maximum rank in this weighted map.
 Double normalizedWeight(E e)
          Return the normalized weight for the specified element in this weighted map.
 Double put(E e, Double w)
          
 void putAll(Map<? extends E,? extends Double> t)
          
 int rank(E e)
          Return an integer rank for the specified element in this weighted map based on its weight.
 Double remove(Object o)
          
 E sample()
          Randomly sample an element from this weighted map according to its normalized weight.
 void setRandom(Random random)
          Set the source of randomness for this weighted map to random.
 int size()
          
 Double totalWeight()
          Return the sum of the weights in this weighted map.
 Collection<Double> values()
          
 Double weight(E e)
          Return the weight for the specified element in this weighted map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

HashWeightedMap

public HashWeightedMap()
Create a new weighted map with the default initial capacity and load factor.


HashWeightedMap

public HashWeightedMap(int initialCapacity)
Create a new weighted map with the specified initial capacity and default load factor.

Parameters:
initialCapacity - initial capacity

HashWeightedMap

public HashWeightedMap(int initialCapacity,
                       float loadFactor)
Create a new weighted map with the specified initial capacity and load factor.

Parameters:
initialCapacity - initial capacity
loadFactor - load factor

HashWeightedMap

public HashWeightedMap(WeightedMap<? extends E> weightedMap)
Create a new weighted map with the elements and weights in the specified weighted map (copy constructor).

Parameters:
weightedMap - weighted map to copy, must not be null
Method Detail

setRandom

public void setRandom(Random random)
Set the source of randomness for this weighted map to random.

Parameters:
random - source of randomness, must not be null

clear

public void clear()

Specified by:
clear in interface Map<E,Double>

size

public int size()

Specified by:
size in interface Map<E,Double>

isEmpty

public boolean isEmpty()

Specified by:
isEmpty in interface Map<E,Double>

containsKey

public boolean containsKey(Object o)

Specified by:
containsKey in interface Map<E,Double>

containsValue

public boolean containsValue(Object o)

Specified by:
containsValue in interface Map<E,Double>

get

public Double get(Object o)

Specified by:
get in interface Map<E,Double>

put

public Double put(E e,
                  Double w)

Specified by:
put in interface Map<E,Double>

putAll

public void putAll(Map<? extends E,? extends Double> t)

Specified by:
putAll in interface Map<E,Double>

remove

public Double remove(Object o)

Specified by:
remove in interface Map<E,Double>

sample

public E sample()
Randomly sample an element from this weighted map according to its normalized weight.

Specified by:
sample in interface WeightedMap<E>
Returns:
a random element from this weighted map according to its normalized weight, or null if this weighted map is empty or if the total weight is zero
See Also:
WeightedMap.totalWeight(), WeightedMap.normalizedWeight(E)

weight

public Double weight(E e)
Return the weight for the specified element in this weighted map. Returns the same value as get(E e).

Specified by:
weight in interface WeightedMap<E>
Parameters:
e - element
Returns:
the weight for the specified element in this weighted map, or null if this weighted map is empty

normalizedWeight

public Double normalizedWeight(E e)
Return the normalized weight for the specified element in this weighted map.

Specified by:
normalizedWeight in interface WeightedMap<E>
Parameters:
e - element
Returns:
the normalized weight for the specified element in this weighted map, or null if this weighted map is empty

totalWeight

public Double totalWeight()
Return the sum of the weights in this weighted map.

Specified by:
totalWeight in interface WeightedMap<E>
Returns:
the sum of the weights in this weighted map

rank

public int rank(E e)
Return an integer rank for the specified element in this weighted map based on its weight.

Specified by:
rank in interface WeightedMap<E>
Parameters:
e - element
Returns:
an integer rank for the specified element in this weighted map based on its weight, or -1 if this weighted map is empty or if e is not an element in this weighted map

maximumRank

public int maximumRank()
Return the maximum rank in this weighted map.

Specified by:
maximumRank in interface WeightedMap<E>
Returns:
the maximum integer rank in this weighted map or -1 if this weighted map is empty

keySet

public Set<E> keySet()

Specified by:
keySet in interface Map<E,Double>

values

public Collection<Double> values()

Specified by:
values in interface Map<E,Double>

entrySet

public Set<Map.Entry<E,Double>> entrySet()

Specified by:
entrySet in interface Map<E,Double>


Copyright © 2005-2012 dishevelled.org. All Rights Reserved.