summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/esodata/ThresholdSet.java
blob: b6f677e01fc38c384aa9d8e54dbfe244b7b2ecfb (plain)
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
package bjc.esodata;

import java.util.*;

/**
 * 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> {
	// 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).
		 */
		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;
		}
		
		public boolean remove(Object o) {
			// Will throw a ClassCastException if you give us something bad.
			KeyType k = (KeyType)o;

			int ret = ThresholdSet.this.remove(k);

			// We removed the element.
			if (ret == 0) return true;

			return false;
		}

		public boolean contains(Object o) {
			// Will throw a ClassCastException if you give us something bad.
			KeyType k = (KeyType)o;

			int ret = ThresholdSet.this.remove(k);

			// The object is set-visible
			if (ret == 1) return true;

			return false;
		}

		public int size() {
			return ThresholdSet.this.setSize();
		}

		public Iterator<KeyType> iterator() {
			return ThresholdSet.this.setIterator();
		}
	}

	private Set<KeyType> keySet;
	// @TODO :CountMap Ben Culkin 6/19/2019
	// Replace this with a CountSet or some equivalent concept, whenever 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[] addAll(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 &lt; 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[] removeAll(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 occurances
			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;
			} else {
				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 key
	 * 		The keys to look for.
	 *
	 * @return The containment counts for each key.
	 */
	public int[] containsAll(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 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();
	}

	int setSize() {
		return keySet.size();
	}

	Iterator<KeyType> setIterator() {
		return keySet.iterator();
	}
}