-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter.java
More file actions
276 lines (243 loc) · 6.76 KB
/
Copy pathCounter.java
File metadata and controls
276 lines (243 loc) · 6.76 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.rutgers.util;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
/**
* A map from objects to doubles. Includes convenience methods for getting,
* setting, and incrementing element counts. Objects not in the counter will
* return a count of zero. The counter is backed by a HashMap (unless specified
* otherwise with the MapFactory constructor).
*/
public class Counter <E> implements Serializable {
private static final long serialVersionUID = 5724671156522771655L;
protected Map<E, Double> entries;
int currentModCount = 0;
int cacheModCount = -1;
double cacheTotalCount = 0.0;
private Double uniform_prob;
/**
* The elements in the counter.
*
* @return set of keys
*/
public Set<E> keySet() {
return entries.keySet();
}
/**
* The number of entries in the counter (not the total count -- use
* totalCount() instead).
*/
public int size() {
return entries.size();
}
/**
* True if there are no entries in the counter (false does not mean totalCount
* > 0)
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* Returns whether the counter contains the given key. Note that this is the
* way to distinguish keys which are in the counter with count zero, and those
* which are not in the counter (and will therefore return count zero from
* getCount().
*
* @param key
* @return whether the counter contains the key
*/
public boolean containsKey(E key) {
return entries.containsKey(key);
}
/**
* Remove a key from the counter. Returns the count associated with that
* key or zero if the key wasn't in the counter to begin with
* @param key
* @return the count associated with the key
*/
public double removeKey(E key) {
Double d = entries.remove(key);
return (d == null? 0.0: d);
}
/**
* Get the count of the element, or zero if the element is not in the
* counter.
*
* @param key
* @return
*/
public double getCount(E key) {
Double value = entries.get(key);
if (value == null)
return 0;
return value;
}
/**
* Set the count for the given key, clobbering any previous count.
*
* @param key
* @param count
*/
public void setCount(E key, double count) {
currentModCount++;
entries.put(key, count);
}
/**
* Increment a key's count by the given amount.
*
* @param key
* @param increment
*/
public void incrementCount(E key, double increment) {
setCount(key, getCount(key) + increment);
}
/**
* Increment each element in a given collection by a given amount.
*/
public void incrementAll(Collection<? extends E> collection, double count) {
for (E key : collection) {
incrementCount(key, count);
}
}
public <T extends E> void incrementAll(Counter<T> counter) {
for (T key : counter.keySet()) {
double count = counter.getCount(key);
incrementCount(key, count);
}
}
public <T extends E> void elementwiseMax(Counter<T> counter) {
for (T key : counter.keySet()) {
double count = counter.getCount(key);
if ( getCount(key) < count ) {
setCount(key, count);
}
}
}
/**
* Finds the total of all counts in the counter. This implementation uses
* cached count which may get out of sync if the entries map is modified in
* some unantipicated way.
*
* @return the counter's total
*/
public double totalCount() {
if (currentModCount != cacheModCount) {
double total = 0.0;
for (Map.Entry<E, Double> entry : entries.entrySet()) {
total += entry.getValue();
}
cacheTotalCount = total;
cacheModCount = currentModCount;
}
return cacheTotalCount;
}
/**
* Destructively normalize this Counter in place.
*/
public void normalize() {
double totalCount = totalCount();
for (E key : keySet()) {
setCount(key, getCount(key) / totalCount);
}
}
/**
* Destructively scale this Counter in place.
*/
public void scale(double scaleFactor) {
for (E key : keySet()) {
setCount(key, getCount(key) * scaleFactor);
}
}
/**
* Finds the key with maximum count. This is a linear operation, and ties are
* broken arbitrarily.
*
* @return a key with minumum count
*/
public E argMax() {
double maxCount = Double.NEGATIVE_INFINITY;
E maxKey = null;
for (Map.Entry<E, Double> entry : entries.entrySet()) {
if (entry.getValue() > maxCount || maxKey == null) {
maxKey = entry.getKey();
maxCount = entry.getValue();
}
}
return maxKey;
}
/**
* Returns a string representation with the keys ordered by decreasing
* counts.
*
* @return string representation
*/
public String toString() {
return toString(keySet().size());
}
/**
* Returns a string representation which includes no more than the
* maxKeysToPrint elements with largest counts.
*
* @param maxKeysToPrint
* @return partial string representation
*/
public String toString(int maxKeysToPrint) {
return asPriorityQueue().toString(maxKeysToPrint);
}
/**
* Builds a priority queue whose elements are the counter's elements, and
* whose priorities are those elements' counts in the counter.
*/
public PriorityQueue<E> asPriorityQueue() {
PriorityQueue<E> pq = new FastPriorityQueue<E>(entries.size());
for (Map.Entry<E, Double> entry : entries.entrySet()) {
pq.setPriority(entry.getKey(), entry.getValue());
}
return pq;
}
/**
* Entry sets are an efficient way to iterate over
* the key-value pairs in a map
* @return entrySet
*/
public Set<Entry<E, Double>> getEntrySet() {
return entries.entrySet();
}
public Counter() {
this(new MapFactory.HashMapFactory<E, Double>());
}
public Counter(MapFactory<E, Double> mf) {
entries = mf.buildMap();
}
public Counter(Counter<? extends E> counter) {
this();
incrementAll(counter);
}
public Counter(Collection<? extends E> collection) {
this();
incrementAll(collection, 1.0);
}
public static void main(String[] args) {
Counter<String> counter = new Counter<String>();
System.out.println(counter);
counter.incrementCount("planets", 7);
System.out.println(counter);
counter.incrementCount("planets", 1);
System.out.println(counter);
counter.setCount("suns", 1);
System.out.println(counter);
counter.setCount("aliens", 1);
System.out.println(counter);
System.out.println(counter.toString(2));
System.out.println("Total: " + counter.totalCount());
counter.normalize() ;
System.out.println(counter);
}
public void setDefault(Double uniform_prob)
{
// TODO Auto-generated method stub
this.uniform_prob = uniform_prob;
}
}