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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*
* esodata - data structures and other things, of varying utility
* Copyright 2022, Ben Culkin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package bjc.esodata;
import java.util.*;
import bjc.data.Pair;
import bjc.data.SimplePair;
import bjc.data.TransformIterator;
/**
* Represents a counted set, that overflows to a map.
*
* More specifically, this is a set/map combo type.
*
* Initially, when you add an item, it will go into the set. Attempting to add a
* duplicate item to that set will cause the entry to be removed from the set,
* and added to the map, which will count the number of times that particular
* item has been added to the set. If you remove enough copies of that item to
* put it back down to 1 copy, that copy will be removed from the map, and
* readded to the set.
*
* The iterator that this type gives by default is an iterator over all of the
* values in the set, not including any of those in the map.
*
* @param <KeyType> The value being counted.
*
* @author Ben Culkin
*/
public class ThresholdSet<KeyType> implements Iterable<Pair<KeyType, Integer>> {
// View of this class as a java.util.Set
private class SetView extends AbstractSet<KeyType> {
/*
* This is technically not a valid implementation of add, because it does not
* guarantee that the set will contain key after it returns (as a matter of
* fact, attempting to add the component might actually cause it to be removed
* from the collection).
*/
@Override
public boolean add(KeyType key) {
// Qualified-this; allows us to reference the 'this' of our enclosing type.
int ret = ThresholdSet.this.add(key);
// No change to set contents
if (ret > 2)
return false;
return true;
}
@Override
public boolean remove(Object o) {
// Will throw a ClassCastException if you give us something bad.
@SuppressWarnings("unchecked")
KeyType k = (KeyType) o;
int ret = ThresholdSet.this.remove(k);
// We removed the element.
if (ret == 0)
return true;
return false;
}
@Override
public boolean contains(Object o) {
// Will throw a ClassCastException if you give us something bad.
@SuppressWarnings("unchecked")
KeyType k = (KeyType) o;
int ret = ThresholdSet.this.contains(k);
// The object is set-visible
if (ret == 1)
return true;
return false;
}
@Override
public int size() {
return ThresholdSet.this.setSize();
}
@Override
public Iterator<KeyType> iterator() {
return ThresholdSet.this.setIterator();
}
}
// Set of uniquely stored keys
private Set<KeyType> keySet;
// @TODO :CountMap Ben Culkin 6/19/2019
// Replace this with a CountSet or some equivalent concept, whenever that gets
// written, if that
// gets written
private Map<KeyType, Integer> keyMap;
/**
* Create a new empty threshold set.
*/
public ThresholdSet() {
keySet = new HashSet<>();
keyMap = new HashMap<>();
}
/**
* Add multiple keys at once to the map.
*
* @param keys The keys to add.
*
* @return An array containing the results of adding the keys.
*/
public int[] addKeys(@SuppressWarnings("unchecked") KeyType... keys) {
int[] ret = new int[keys.length];
for (int i = 0; i < keys.length; i++)
ret[i] = add(keys[i]);
return ret;
}
/**
* Add a key to the collection.
*
* @param key The key to add to the collection.
*
* @return The number of times that key now exists in the collection. Should
* always be > 0.
*/
public int add(KeyType key) {
if (keySet.contains(key)) {
// Promote to counted item
keySet.remove(key);
keyMap.put(key, 2);
return 2;
} else if (keyMap.containsKey(key)) {
// Increment count
int cnt = keyMap.get(key) + 1;
keyMap.put(key, cnt);
return cnt;
} else {
// New key
keySet.add(key);
return 1;
}
}
/**
* Remove a bunch of keys from the collection.
*
* @param keys The keys to remove from the collection.
*
* @return The results from removing the keys.
*/
public int[] removeKeys(@SuppressWarnings("unchecked") KeyType... keys) {
int[] ret = new int[keys.length];
for (int i = 0; i < keys.length; i++)
ret[i] = remove(keys[i]);
return ret;
}
/**
* Remove a key from the collection.
*
* @param key The key to remove from the collection.
*
* @return The number of times that key now exists in the collection. Returns -1
* if that key wasn't in the collection beforehand.
*/
public int remove(KeyType key) {
if (keySet.contains(key)) {
// No more occurrences
keySet.remove(key);
return 0;
} else if (keyMap.containsKey(key)) {
// Decrement count
int cnt = keyMap.get(key) - 1;
if (cnt == 1) {
// Move key to set
keyMap.remove(key);
keySet.add(key);
return 1;
}
keyMap.put(key, cnt);
return cnt;
} else {
// We don't know about that key
return -1;
}
}
/**
* Get the number of times the set contains a set of given keys.
*
* @param keys The keys to look for.
*
* @return The containment counts for each key.
*/
public int[] containsKeys(@SuppressWarnings("unchecked") KeyType... keys) {
int[] ret = new int[keys.length];
for (int i = 0; i < keys.length; i++)
ret[i] = contains(keys[i]);
return ret;
}
/**
* Get the number of times the set contains a given key.
*
* @param key The key to look for.
*
* @return The number of times the key occurs; -1 if it doesn't occur.
*/
public int contains(KeyType key) {
if (keySet.contains(key))
return 1;
if (!keyMap.containsKey(key))
return -1;
return keyMap.get(key);
}
/**
* Get a set containing all of the values that are in this set at least once.
*
* @return A set containing every value that occurs at least once in this set.
*/
public Set<KeyType> values() {
Set<KeyType> retSet = new HashSet<>();
retSet.addAll(keySet);
retSet.addAll(keyMap.keySet());
return retSet;
}
/**
* Get a view of this collection as a java.util.Set.
*
* @return A view of the collection as a set.
*/
public Set<KeyType> setView() {
return new SetView();
}
@Override
public Iterator<Pair<KeyType, Integer>> iterator() {
return new TransformIterator<>(keyMap.entrySet().iterator(),
(entry) -> new SimplePair<>(entry.getKey(), entry.getValue()));
}
/**
* Static threshold set constructor.
*
* @param <KType> The type of keys for the threshold set.
*
* @param keys The initial keys to add to the threshold set.
* @return A threshold set with the given keys.
*/
@SafeVarargs
public static <KType> ThresholdSet<KType> TS(KType... keys) {
ThresholdSet<KType> ts = new ThresholdSet<>();
ts.addKeys(keys);
return ts;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Set: ");
sb.append(keySet);
sb.append("\nMap: ");
sb.append(keyMap);
return sb.toString();
}
// Implementation methods for setView
int setSize() {
return keySet.size();
}
Iterator<KeyType> setIterator() {
return keySet.iterator();
}
}
|