1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
| import java.util.*;
public class Histogram<T> { private final Map<T, Integer> map;
public Histogram() { this.map = new HashMap<T, Integer>(); }
public void clear() { this.map.clear(); }
public int put(T t) { return this.put(t, 1); }
public int put(T t, int amount) { if (amount < 0) throw new IllegalArgumentException();
int count = this.get(t); if (amount == 0) return count;
count += amount; this.map.put(t, Integer.valueOf(count)); return count; }
public int get(T t) { Integer count = this.map.get(t); if (count == null) return 0; return count.intValue(); }
public Set<T> keys() { return this.map.keySet(); }
public List<T> thresholdKeys(int atLeast) { int found = 0; for (Integer c : this.map.values()) if (c.intValue() >= atLeast) found++; return topKeys(found); }
public List<T> topKeys() { return this.topKeys(Integer.MAX_VALUE); }
public List<T> topKeys(int amount) { List<T> kList = new ArrayList<T>(); List<Integer> vList = new ArrayList<Integer>();
for (Entry<T, Integer> e : this.map.entrySet()) { if (vList.size() == amount + 1) { int index = indexOfMin(vList); kList.remove(index); vList.remove(index); }
kList.add(e.getKey()); vList.add(e.getValue()); }
for (int i = 0; i < vList.size(); i++) { for (int k = i + 1; k < vList.size(); k++) { if (vList.get(i).intValue() >= vList.get(k).intValue()) { continue; }
Integer a = vList.get(i); Integer b = vList.get(k); vList.set(k, a); vList.set(i, b);
T x = kList.get(i); T y = kList.get(k); kList.set(k, x); kList.set(i, y); } }
if (kList.size() == amount + 1) { kList.remove(kList.size() - 1); vList.remove(vList.size() - 1); }
return kList; }
public int remove(T t) { return this.remove(t, 1); }
public int remove(T t, int amount) { Integer count = this.map.get(t); if (count == null) throw new NoSuchElementException(String.valueOf(t)); if (amount < 0) throw new IllegalArgumentException(); if (count.intValue() < amount) throw new IllegalStateException("cannot remove");
count = Integer.valueOf(count.intValue() - amount); if (count.intValue() == 0) this.map.remove(t); else this.map.put(t, count);
return count.intValue(); }
public int reset(T t) { Integer count = this.map.remove(t); if (count == null) return 0; return count.intValue(); }
public int set(T t, int val) { if (val < 0) throw new IllegalArgumentException(); Integer count = this.map.get(t);
if (count == null) { if (val != 0) this.map.put(t, Integer.valueOf(val)); return 0; }
if (val == 0) return this.map.remove(t).intValue(); return this.map.put(t, Integer.valueOf(val)).intValue(); }
private static int indexOfMin(List<Integer> ids) { int minAt = 0; for (int i = 1; i < ids.size(); i++) if (ids.get(i).intValue() < ids.get(minAt).intValue()) minAt = i; return minAt; }
public Histogram<T> asSynchronized() { final Histogram<T> backing = this;
return new Histogram<T>() { public synchronized int put(T t) { return backing.put(t); }
public synchronized int put(T t, int amount) { return backing.put(t, amount); }
public synchronized int get(T t) { return backing.get(t); }
public synchronized int remove(T t) { return backing.remove(t); }
public synchronized int reset(T t) { return backing.reset(t); }
public synchronized int set(T t, int val) { return backing.set(t, val); }
@Override public synchronized Set<T> keys() { return backing.keys(); }
@Override public synchronized List<T> topKeys() { return backing.topKeys(); }
@Override public synchronized List<T> topKeys(int amount) { return backing.topKeys(amount); }
@Override public boolean equals(Object obj) { return backing.equals(obj); }
@Override public int hashCode() { return backing.hashCode(); }
@Override public String toString() { return backing.toString(); }
@Override public Histogram<T> asSynchronized() { return this; } }; } } |