|
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 |
| package edu.rice.cs.plt.collect; |
|
36 |
| |
|
37 |
| import java.util.*; |
|
38 |
| import edu.rice.cs.plt.iter.SizedIterable; |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| public class ExternallySortedSet<T, C extends Comparable<? super C>> implements SizedIterable<T> { |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| private final SortedSet<T> _set; |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| private final Map<T, C> _orderByMap; |
|
71 |
| |
|
72 |
| |
|
73 |
| private final C _lowerBound; |
|
74 |
| |
|
75 |
| |
|
76 |
| private final C _upperBound; |
|
77 |
| |
|
78 |
| |
|
79 |
0
| public ExternallySortedSet() {
|
|
80 |
0
| _set = new TreeSet<T>(new Comparator<T>() {
|
|
81 |
| |
|
82 |
0
| public int compare(T t1, T t2) {
|
|
83 |
0
| return _orderByMap.get(t1).compareTo(_orderByMap.get(t2));
|
|
84 |
| } |
|
85 |
| }); |
|
86 |
| |
|
87 |
0
| _orderByMap = new HashMap<T, C>();
|
|
88 |
0
| _lowerBound = null;
|
|
89 |
0
| _upperBound = null;
|
|
90 |
| } |
|
91 |
| |
|
92 |
| |
|
93 |
0
| private ExternallySortedSet(SortedSet<T> set, Map<T, C> orderByMap, C lowerBound,
|
|
94 |
| C upperBound) { |
|
95 |
0
| _set = set;
|
|
96 |
0
| _orderByMap = orderByMap;
|
|
97 |
0
| _lowerBound = lowerBound;
|
|
98 |
0
| _upperBound = upperBound;
|
|
99 |
| } |
|
100 |
| |
|
101 |
0
| public boolean isEmpty() { return _set.isEmpty(); }
|
|
102 |
| |
|
103 |
0
| public int size() { return _set.size(); }
|
|
104 |
| |
|
105 |
0
| public int size(int bound) {
|
|
106 |
0
| int result = _set.size();
|
|
107 |
0
| return result <= bound ? result : bound;
|
|
108 |
| } |
|
109 |
| |
|
110 |
0
| public boolean isInfinite() { return false; }
|
|
111 |
| |
|
112 |
0
| public boolean hasFixedSize() { return false; }
|
|
113 |
| |
|
114 |
0
| public boolean isStatic() { return false; }
|
|
115 |
| |
|
116 |
0
| public boolean contains(Object element) {
|
|
117 |
| |
|
118 |
0
| return _orderByMap.containsKey(element) && _set.contains(element);
|
|
119 |
| } |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
0
| public Iterator<T> iterator() {
|
|
126 |
0
| final Iterator<T> setI = _set.iterator();
|
|
127 |
0
| return new Iterator<T>() {
|
|
128 |
| private T _last = null; |
|
129 |
| |
|
130 |
0
| public boolean hasNext() { return setI.hasNext(); }
|
|
131 |
| |
|
132 |
0
| public T next() {
|
|
133 |
0
| _last = setI.next();
|
|
134 |
0
| return _last;
|
|
135 |
| } |
|
136 |
| |
|
137 |
0
| public void remove() {
|
|
138 |
0
| if (_last == null) { throw new IllegalStateException(); }
|
|
139 |
| else { |
|
140 |
0
| setI.remove();
|
|
141 |
0
| _orderByMap.remove(_last);
|
|
142 |
0
| _last = null;
|
|
143 |
| } |
|
144 |
| } |
|
145 |
| |
|
146 |
| }; |
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
0
| public Object[] toArray() { return _set.toArray(); }
|
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
0
| public <S> S[] toArray(S[] a) { return _set.toArray(a); }
|
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
0
| public boolean add(T element, C orderBy) {
|
|
169 |
0
| if (element == null) { throw new NullPointerException(); }
|
|
170 |
| else { |
|
171 |
0
| assertInBounds(orderBy);
|
|
172 |
0
| if (contains(element)) { return false; }
|
|
173 |
| else { |
|
174 |
0
| _orderByMap.put(element, orderBy);
|
|
175 |
0
| _set.add(element);
|
|
176 |
0
| return true;
|
|
177 |
| } |
|
178 |
| } |
|
179 |
| } |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
0
| public boolean remove(Object element) {
|
|
186 |
0
| if (contains(element)) {
|
|
187 |
0
| _set.remove(element);
|
|
188 |
0
| _orderByMap.remove(element);
|
|
189 |
0
| return true;
|
|
190 |
| } |
|
191 |
0
| else { return false; }
|
|
192 |
| } |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
0
| public boolean containsAll(Iterable<?> i) {
|
|
198 |
0
| for (Object o : i) { if (! contains(o)) { return false; } }
|
|
199 |
0
| return true;
|
|
200 |
| } |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
0
| public boolean addAll(ExternallySortedSet<? extends T, ? extends C> s) {
|
|
207 |
0
| if ((_lowerBound == null ||
|
|
208 |
| (s._lowerBound != null && _lowerBound.compareTo(s._lowerBound) <= 0)) && |
|
209 |
| (_upperBound == null || |
|
210 |
| (s._upperBound != null && _upperBound.compareTo(s._upperBound) >= 0))) { |
|
211 |
| |
|
212 |
0
| return uncheckedAddAll(s);
|
|
213 |
| } |
|
214 |
| else { |
|
215 |
0
| return checkedAddAll(s);
|
|
216 |
| } |
|
217 |
| } |
|
218 |
| |
|
219 |
| |
|
220 |
0
| private boolean checkedAddAll(ExternallySortedSet<? extends T, ? extends C> s) {
|
|
221 |
0
| boolean result = false;
|
|
222 |
0
| for (T t : s) {
|
|
223 |
| |
|
224 |
0
| result = result | add(t, s._orderByMap.get(t));
|
|
225 |
| } |
|
226 |
0
| return result;
|
|
227 |
| } |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
0
| private boolean uncheckedAddAll(ExternallySortedSet<? extends T, ? extends C> s) {
|
|
235 |
0
| for (Map.Entry<? extends T, ? extends C> e : s._orderByMap.entrySet()) {
|
|
236 |
0
| if (! _orderByMap.containsKey(e.getKey())) { _orderByMap.put(e.getKey(), e.getValue()); }
|
|
237 |
| } |
|
238 |
0
| boolean result = _set.addAll(s._set);
|
|
239 |
0
| return result;
|
|
240 |
| } |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
0
| public boolean retainAll(Collection<?> c) {
|
|
247 |
0
| boolean result = false;
|
|
248 |
0
| for (Iterator<T> i = iterator(); i.hasNext(); ) {
|
|
249 |
0
| T t = i.next();
|
|
250 |
0
| if (! c.contains(t)) { i.remove(); result = true; }
|
|
251 |
| } |
|
252 |
0
| return result;
|
|
253 |
| } |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
0
| public boolean retainAll(ExternallySortedSet<?, ?> s) {
|
|
260 |
0
| boolean result = false;
|
|
261 |
0
| for (Iterator<T> i = iterator(); i.hasNext(); ) {
|
|
262 |
0
| T t = i.next();
|
|
263 |
0
| if (! s.contains(t)) { i.remove(); result = true; }
|
|
264 |
| } |
|
265 |
0
| return result;
|
|
266 |
| } |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
0
| public boolean removeAll(Iterable<?> i) {
|
|
273 |
0
| boolean result = false;
|
|
274 |
| |
|
275 |
0
| for (Object o : i) { result = result | remove(o); }
|
|
276 |
0
| return result;
|
|
277 |
| } |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
0
| public void clear() {
|
|
283 |
| |
|
284 |
0
| for (T t : _set) { _orderByMap.remove(t); }
|
|
285 |
0
| _set.clear();
|
|
286 |
| } |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
0
| public ExternallySortedSet<T, C> subSet(C from, C to) {
|
|
296 |
0
| assertInBounds(from);
|
|
297 |
0
| assertInBounds(to);
|
|
298 |
0
| T fromElement = firstAt(from);
|
|
299 |
0
| T toElement = firstAt(to);
|
|
300 |
| |
|
301 |
0
| SortedSet<T> subSet;
|
|
302 |
0
| if (fromElement == null) {
|
|
303 |
0
| if (toElement == null) { subSet = _set; }
|
|
304 |
0
| else { subSet = _set.headSet(toElement); }
|
|
305 |
| } |
|
306 |
0
| else if (toElement == null) { subSet = _set.tailSet(fromElement); }
|
|
307 |
0
| else { subSet = _set.subSet(fromElement, toElement); }
|
|
308 |
| |
|
309 |
0
| return new ExternallySortedSet<T, C>(subSet, _orderByMap, from, to);
|
|
310 |
| } |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
0
| public ExternallySortedSet<T, C> headSet(C to) {
|
|
318 |
0
| assertInBounds(to);
|
|
319 |
0
| T toElement = firstAt(to);
|
|
320 |
0
| SortedSet<T> subSet;
|
|
321 |
0
| if (toElement == null) { subSet = _set; }
|
|
322 |
0
| else { subSet = _set.headSet(toElement); }
|
|
323 |
0
| return new ExternallySortedSet<T, C>(subSet, _orderByMap, null, to);
|
|
324 |
| } |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
0
| public ExternallySortedSet<T, C> tailSet(C from) {
|
|
332 |
0
| assertInBounds(from);
|
|
333 |
0
| T fromElement = firstAt(from);
|
|
334 |
0
| SortedSet<T> subSet;
|
|
335 |
0
| if (fromElement == null) { subSet = _set; }
|
|
336 |
0
| else { subSet = _set.tailSet(fromElement); }
|
|
337 |
0
| return new ExternallySortedSet<T, C>(subSet, _orderByMap, from, null);
|
|
338 |
| } |
|
339 |
| |
|
340 |
0
| public T first() { return _set.first(); }
|
|
341 |
| |
|
342 |
0
| public T last() { return _set.last(); }
|
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
0
| private T firstAt(C c) {
|
|
349 |
0
| _orderByMap.put(null, c);
|
|
350 |
0
| SortedSet<T> resultSet = _set.tailSet(null);
|
|
351 |
0
| T result = null;
|
|
352 |
0
| if (! resultSet.isEmpty()) { result = resultSet.first(); }
|
|
353 |
0
| _orderByMap.remove(null);
|
|
354 |
0
| return result;
|
|
355 |
| } |
|
356 |
| |
|
357 |
0
| private void assertInBounds(C c) {
|
|
358 |
0
| if (_lowerBound != null && c.compareTo(_lowerBound) < 0) {
|
|
359 |
0
| throw new IllegalArgumentException(c + " is < this set's lower bound: " + _lowerBound);
|
|
360 |
| } |
|
361 |
0
| if (_upperBound != null && c.compareTo(_upperBound) >= 0) {
|
|
362 |
0
| throw new IllegalArgumentException(c + " is >= this set's upper bound: " + _upperBound);
|
|
363 |
| } |
|
364 |
| } |
|
365 |
| |
|
366 |
| } |