DVL1 dishevelled.org

Skip to: [content] [navigation]

dishevelled.org weighted 1.1-SNAPSHOT :: Downloads | Repository | Javadocs | Checkstyle | Test coverage | Changes

Summary

dishevelled.org weighted provides a weighted map interface and implementation, that is, a map of elements to weights with sampling and ranking functionality. The weighted library requires java version 1.5 or later.

The WeightedMap interface extends the Map<K,V> interface and fills in the V type as Double. Specifically, WeightedMap<E> extends Map<E,Double>. Thus clients of this interface only need to provide a type for the key:

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)

Acknowledgements

SourceForge.net Logo