-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreapSet.java
295 lines (239 loc) · 6.59 KB
/
TreapSet.java
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
package com.github.igabaydulin.collections;
import com.github.igabaydulin.collections.TreapMap.Node;
import com.github.igabaydulin.collections.ValueTreap.Inclusion;
import com.github.igabaydulin.collections.utils.Reference;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.Objects;
import java.util.SortedSet;
import java.util.Stack;
public class TreapSet<K> implements Treap<K> {
private TreapMap<K, K> treapMap;
public TreapSet() {
this.treapMap = new TreapMap<>();
}
@SuppressWarnings("unused")
public TreapSet(long seed) {
this.treapMap = new TreapMap<>(seed);
}
private TreapSet(TreapMap<K, K> treapMap) {
this.treapMap = treapMap;
}
@Override
public K get(int index) {
return treapMap.getByIndex(index);
}
@Override
public boolean contains(Object value) {
try {
//noinspection SuspiciousMethodCalls
return treapMap.get(value) != null;
} catch (ClassCastException ex) {
return false;
}
}
@Override
public boolean containsAll(Collection<?> c) {
return c.stream().map(this::contains).reduce((op1, op2) -> op1 && op2).orElse(true);
}
@Override
public boolean add(K value, double priority) {
return treapMap.put(value, value, priority) == null;
}
@Override
public boolean add(K value) {
return treapMap.put(value, value) == null;
}
@Override
public boolean addBack(K[] values, double[] priorities) {
return treapMap.putBack(values, values, priorities);
}
@Override
public boolean addBack(K[] values) {
return treapMap.putBack(values, values);
}
@Override
public boolean addFront(K[] values, double[] priorities) {
return treapMap.putFront(values, values, priorities);
}
@Override
public boolean addFront(K[] values) {
return treapMap.putFront(values, values);
}
@Override
public boolean addAll(Collection<? extends K> c) {
int currentSize = size();
c.forEach(this::add);
return currentSize != size();
}
@Override
public boolean remove(Object value) {
try {
return treapMap.remove(value) != null;
} catch (ClassCastException ex) {
return false;
}
}
@Override
public boolean removeAll(Collection<?> c) {
int currentSize = size();
c.forEach(this::remove);
return currentSize != size();
}
// TODO: Always returns true, should be fixed to return true if any element were removed only
@Override
@SuppressWarnings("unchecked")
public boolean retainAll(Collection<?> c) {
clear();
c.forEach(it -> add((K) it));
return true;
}
@Override
public void clear() {
treapMap = new TreapMap<>(treapMap.getRandom());
}
@Override
public boolean split(K value, Reference<Treap<K>> left, Reference<Treap<K>> right, Inclusion inclusion) {
Reference<ValueTreap<K, K>> leftDictionary = new Reference<>();
Reference<ValueTreap<K, K>> rightDictionary = new Reference<>();
boolean contains = treapMap.split(value, leftDictionary, rightDictionary, inclusion);
left.set(new TreapSet<>((TreapMap<K, K>) leftDictionary.get()));
right.set(new TreapSet<>((TreapMap<K, K>) rightDictionary.get()));
return contains;
}
@Override
public Treap<K> merge(Treap<K> right) {
return new TreapSet<>(treapMap.merge(((TreapSet<K>) right).treapMap));
}
@Override
public int size() {
return treapMap.size();
}
@Override
public int height() {
return treapMap.height();
}
@Override
public boolean isEmpty() {
return treapMap.isEmpty();
}
@Override
@SuppressWarnings("unchecked")
public Iterator<K> iterator() {
return new Iterator<K>() {
private Object[] array = toArray();
private int index = 0;
@Override
public boolean hasNext() {
return index < array.length;
}
@Override
public K next() {
return (K) array[index++];
}
};
}
@Override
public Object[] toArray() {
return toArray(new Object[0]);
}
@Override
@SuppressWarnings("unchecked")
public <T1> T1[] toArray(T1[] a) {
Node<K, K> root = treapMap.getRoot();
int index = 0;
int size = size();
T1[] array = (T1[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
if (Objects.isNull(root)) {
return array;
}
Stack<Node<K, K>> stack = new Stack<>();
Node<K, K> node = root;
while (!stack.empty() || Objects.nonNull(node)) {
if (Objects.nonNull(node)) {
stack.push(node);
node = node.getLeft();
} else {
node = stack.pop();
array[index++] = (T1) node.getValue();
node = node.getRight();
}
}
return array;
}
@Override
public K lower(K k) {
return treapMap.lowerKey(k);
}
@Override
public K floor(K k) {
return treapMap.floorKey(k);
}
@Override
public K ceiling(K k) {
return treapMap.ceilingKey(k);
}
@Override
public K higher(K k) {
return treapMap.higherKey(k);
}
@Override
public K pollFirst() {
return treapMap.pollFirstEntry().getKey();
}
@Override
public K pollLast() {
return treapMap.pollLastEntry().getKey();
}
@Override
public NavigableSet<K> descendingSet() {
return treapMap.descendingKeySet();
}
@Override
public Iterator<K> descendingIterator() {
return treapMap.new KeyIterator();
}
@Override
public NavigableSet<K> subSet(K fromElement, boolean fromInclusive, K toElement, boolean toInclusive) {
return treapMap.subMap(fromElement, fromInclusive, toElement, toInclusive).navigableKeySet();
}
@Override
public NavigableSet<K> headSet(K toElement, boolean inclusive) {
return treapMap.headMap(toElement, inclusive).navigableKeySet();
}
@Override
public NavigableSet<K> tailSet(K fromElement, boolean inclusive) {
return treapMap.tailMap(fromElement, inclusive).navigableKeySet();
}
@Override
public SortedSet<K> subSet(K fromElement, K toElement) {
return treapMap.subMap(fromElement, toElement).navigableKeySet();
}
@Override
public SortedSet<K> headSet(K toElement) {
return treapMap.headMap(toElement).navigableKeySet();
}
@Override
public SortedSet<K> tailSet(K fromElement) {
return treapMap.tailMap(fromElement).navigableKeySet();
}
@Override
public Comparator<? super K> comparator() {
return treapMap.comparator();
}
@Override
public K first() {
return treapMap.firstKey();
}
@Override
public K last() {
return treapMap.lastKey();
}
@Override
public String toString() {
return "TreapSet{" + "array=" + Arrays.toString(toArray()) + '}';
}
}