MCPcopy Index your code
hub / github.com/WinVector/Logistic / CountMap

Class CountMap

src/com/winvector/util/CountMap.java:9–96  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7import java.util.TreeMap;
8
9public final class CountMap<T> implements Serializable {
10 private static final long serialVersionUID = 1L;
11
12 private static final class MutableDouble implements Serializable {
13 private static final long serialVersionUID = 1L;
14 public double v = 0.0;
15 }
16
17 private final SortedMap<T,MutableDouble> counts;
18
19 public CountMap(final Comparator<T> comp) {
20 counts = new TreeMap<T,MutableDouble>(comp);
21 }
22
23 /**
24 * Invariant: key set calls all keys ever observed (even those with net-zero count)
25 * @param key
26 * @param delta
27 */
28 public void observe(final T key, final double delta) {
29 MutableDouble v = counts.get(key);
30 if(v==null) {
31 v = new MutableDouble();
32 counts.put(key,v);
33 }
34 v.v += delta;
35 }
36
37 public void observe(final CountMap<T> o) {
38 for(final T k: o.keySet()) {
39 final double d = o.get(k);
40 observe(k,d);
41 }
42 }
43
44 public boolean contains(final T key) {
45 return counts.containsKey(key);
46 }
47
48 public Set<T> keySet() {
49 return counts.keySet();
50 }
51
52 public double get(final T key) {
53 final MutableDouble v = counts.get(key);
54 if(v!=null) {
55 return v.v;
56 } else {
57 return 0.0;
58 }
59 }
60
61 private static class StrCmp implements Comparator<String>, Serializable {
62 private static final long serialVersionUID = 1L;
63
64 @Override
65 public int compare(final String arg0, final String arg1) {
66 return arg0.compareTo(arg1);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected