E
- the type of elements maintained by this weighted mappublic interface WeightedMap<E> extends Map<E,Double>
WeightedMap<String> m = new HashWeightedMap<String>(); m.put("foo", 100.0d); m.put("bar", 500.0d); m.put("baz", 1000.0d); assert(m.get("foo") == 100.0d); assert(m.weight("foo") == 100.0d); assert(m.totalWeight() == 1600.0d); assert(m.normalizedWeight("foo") == 0.0625d); assert(m.rank("baz") == 1); assert(m.rank("bar") == 2); assert(m.rank("foo") == 3); List<String> list = new ArrayList<String>(100); for (int i = 0; i < 100; i++) { list.add(m.sample()); } assert(cardinality in list of "foo" approximately equal to 6.25) assert(cardinality in list of "bar" approximately equal to 31.25) assert(cardinality in list of "baz" approximately equal to 62.5)
Modifier and Type | Method and Description |
---|---|
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.
|
int |
rank(E e)
Return an integer rank for the specified element in this
weighted map based on its weight.
|
E |
sample()
Randomly sample an element from this weighted map according
to its normalized weight.
|
Double |
totalWeight()
Return the sum of the weights in this weighted map.
|
Double |
weight(E e)
Return the weight for the specified element in
this weighted map.
|
E sample()
null
if this weighted
map is empty or if the total weight is zerototalWeight()
,
normalizedWeight(E)
Double weight(E e)
get(E e)
.e
- elementnull
if this weighted map is emptyDouble normalizedWeight(E e)
e
- elementnull
if this weighted map is emptyDouble totalWeight()
int rank(E e)
e
- element-1
if this
weighted map is empty or if e
is not an element
in this weighted mapint maximumRank()
-1
if this weighted map is emptyCopyright (c) 2005-2013 held jointly by the individual authors. Licensed under the GNU Lesser General Public License (LGPL).