forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeq.java
More file actions
399 lines (346 loc) · 11.8 KB
/
Seq.java
File metadata and controls
399 lines (346 loc) · 11.8 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package fj.data;
import fj.*;
import static fj.Bottom.error;
import static fj.Monoid.intAdditionMonoid;
import static fj.data.fingertrees.FingerTree.measured;
import fj.data.List.Buffer;
import fj.data.fingertrees.FingerTree;
import fj.data.fingertrees.MakeTree;
import fj.data.fingertrees.Measured;
import java.util.AbstractList;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Provides an immutable finite sequence, implemented as a finger tree. This structure gives O(1) access to
* the head and tail, as well as O(log n) random access and concatenation of sequences.
*/
public final class Seq<A> implements Iterable<A> {
private static final Measured<Integer, Object> ELEM_MEASURED = measured(intAdditionMonoid, Function.constant(1));
private static final MakeTree<Integer, Object> MK_TREE = FingerTree.mkTree(ELEM_MEASURED);
private static final Seq<Object> EMPTY = new Seq<>(MK_TREE.empty());
@SuppressWarnings("unchecked")
private static <A> MakeTree<Integer, A> mkTree() {
return (MakeTree<Integer, A>) MK_TREE;
}
private final FingerTree<Integer, A> ftree;
private Seq(final FingerTree<Integer, A> ftree) {
this.ftree = ftree;
}
@SuppressWarnings("unchecked")
private static <A> Measured<Integer, A> elemMeasured() {
return (Measured<Integer, A>) ELEM_MEASURED;
}
/**
* The empty sequence.
*
* @return A sequence with no elements.
*/
@SuppressWarnings("unchecked")
public static <A> Seq<A> empty() {
return (Seq<A>) EMPTY;
}
@Override
public boolean equals(Object other) {
return Equal.equals0(Seq.class, this, other, () -> Equal.seqEqual(Equal.anyEqual()));
}
/**
* A singleton sequence.
*
* @param a The single element in the sequence.
* @return A new sequence with the given element in it.
*/
public static <A> Seq<A> single(final A a) {
return new Seq<>(Seq.<A>mkTree().single(a));
}
/**
* Constructs a sequence from the given elements.
* @param as The elements to create the sequence from.
* @return A sequence with the given elements.
*/
@SafeVarargs public static <A> Seq<A> seq(final A... as) {
return arraySeq(as);
}
/**
* Constructs a sequence from the given list.
*
* @deprecated As of release 4.5, use {@link #listSeq(List)}
*
* @param list The list to create the sequence from.
* @return A sequence with the given elements in the list.
*/
@Deprecated
public static <A>Seq<A> seq(final List<A> list) {
return iterableSeq(list);
}
/**
* Constructs a sequence from the given list.
*
* @deprecated As of release 4.5, use {@link #iterableSeq}
*
* @param list The list to create the sequence from.
* @return A sequence with the elements of the list.
*/
@Deprecated
public static <A>Seq<A> listSeq(final List<A> list) {
return iterableSeq(list);
}
/**
* Constructs a sequence from the iterable.
* @param i The iterable to create the sequence from.
* @return A sequence with the elements of the iterable.
*/
public static <A>Seq<A> iterableSeq(final Iterable<A> i) {
Seq<A> s = empty();
for (final A a: i) {
s = s.snoc(a);
}
return s;
}
/**
* Constructs a sequence from the iterator.
* @param i The iterator to create the sequence from.
* @return A sequence with the elements of the iterator.
*/
public static <A>Seq<A> iteratorSeq(final Iterator<A> i) {
return iterableSeq(() -> i);
}
/**
* Constructs a sequence from the array.
*/
@SafeVarargs
public static <A>Seq<A> arraySeq(A... as) {
return iterableSeq(Array.array(as));
}
/**
* Constructs a sequence from the given list.
* @param list The list to create the sequence from.
* @return A sequence with the elements of the list.
*/
public static <A>Seq<A> fromJavaList(final java.util.List<A> list) {
return iterableSeq(list);
}
/**
* Inserts the given element at the front of this sequence.
*
* @param a An element to insert at the front of this sequence.
* @return A new sequence with the given element at the front.
*/
public Seq<A> cons(final A a) {
return new Seq<>(ftree.cons(a));
}
/**
* Inserts the given element at the end of this sequence.
*
* @param a An element to insert at the end of this sequence.
* @return A new sequence with the given element at the end.
*/
public Seq<A> snoc(final A a) {
return new Seq<>(ftree.snoc(a));
}
/**
* The first element of this sequence. This is an O(1) operation.
*
* @return The first element if this sequence is nonempty, otherwise throws an error.
*/
public A head() { return ftree.head(); }
public Option<A> headOption() {
return ftree.headOption();
}
/**
* The last element of this sequence. This is an O(1) operation.
*
* @return The last element if this sequence is nonempty, otherwise throws an error.
*/
public A last() { return ftree.last(); }
/**
* The sequence without the first element. This is an O(1) operation.
*
* @return The sequence without the first element if this sequence is nonempty, otherwise throws an error.
*/
public Seq<A> tail() {
return (length() == 1) ? empty() : new Seq<>(ftree.tail());
}
/**
* The sequence without the last element. This is an O(1) operation.
*
* @return The sequence without the last element if this sequence is nonempty, otherwise throws an error.
*/
public Seq<A> init() {
return (length() == 1) ? empty() : new Seq<>(ftree.init());
}
/**
* Converts this sequence to a Stream
*/
public Stream<A> toStream() {
return ftree.foldLeft((b, a) -> b.cons(a), Stream.<A>nil()).reverse();
}
/**
* Converts this sequence to a List
*/
public List<A> toList() {
final Buffer<A> buf = Buffer.empty();
for (final A a : this) { buf.snoc(a); }
return buf.toList();
}
/**
* Converts the sequence to a java.util.List
*/
public java.util.List<A> toJavaList() {
return new AbstractList<A>() {
@Override public A get(int i) { return index(i); }
@Override public Iterator<A> iterator() { return Seq.this.iterator(); }
@Override public int size() { return length(); }
};
}
/**
* Returns an iterator for this seq. This method exists to permit the use in a <code>for</code>-each loop.
*
* @return A iterator for this seq.
*/
public Iterator<A> iterator() {
return new Iterator<A>() {
private FingerTree<Integer, A> ftree = Seq.this.ftree;
public boolean hasNext() {
return !ftree.isEmpty();
}
public A next() {
if (ftree.isEmpty())
throw new NoSuchElementException();
else {
final A a = ftree.head();
ftree = ftree.tail();
return a;
}
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override
public String toString() {
return Show.seqShow(Show.<A>anyShow()).showS(this);
}
/**
* Appends the given sequence to this sequence.
*
* @param as A sequence to append to this one.
* @return A new sequence with the given sequence appended to this one.
*/
public Seq<A> append(final Seq<A> as) {
return new Seq<>(ftree.append(as.ftree));
}
/**
* Checks if this is the empty sequence.
*
* @return True if this sequence is empty, otherwise false.
*/
public boolean isEmpty() {
return ftree.isEmpty();
}
/**
* Inserts the element at the given index. This is an O(log(n)) operation.
*
* @param index The index of the element to return.
* @return The sequence with the element inserted at the given index,
* or throws an error if the index is out of bounds.
*/
public Seq<A> insert(int index, A a) {
final P2<Seq<A>, Seq<A>> p = split(index);
return p._1().append(single(a)).append(p._2());
}
/**
* Checks if this sequence is not empty.
*
* @return True if this sequence is not empty, otherwise false.
*/
public boolean isNotEmpty() {
return !ftree.isEmpty();
}
/**
* Returns the number of elements in this sequence.
*
* @return the number of elements in this sequence.
*/
public int length() {
return ftree.measure();
}
/**
* Splits this sequence into a pair of sequences at the given position. This is a O(log(n)) operation.
*
* @return Pair: the subsequence containing elements with indices less than <code>i</code>
* and the subsequence containing elements with indices greater than or equal to <code>i</code>.
*/
public P2<Seq<A>, Seq<A>> split(final int i) {
final P2<FingerTree<Integer, A>, FingerTree<Integer, A>> lr = ftree.split(index -> index > i);
return P.p(new Seq<>(lr._1()), new Seq<>(lr._2()));
}
/**
* Returns the element at the given index. This is an O(log(n)) operation.
*
* @param i The index of the element to return.
* @return The element at the given index, or throws an error if the index is out of bounds.
*/
public A index(final int i) {
checkBounds(i);
return ftree.lookup(Function.identity(), i)._2();
}
/**
* Replace the element at the given index with the supplied value. This is an O(log(n)) operation.
*
* @param i The index of the element to update.
* @param a The new value.
*
* @return The updated sequence, or throws an error if the index is out of bounds.
*/
public Seq<A> update(final int i, final A a) {
checkBounds(i);
final P3<FingerTree<Integer, A>, A, FingerTree<Integer, A>> lxr = ftree.split1(index -> index > i);
return new Seq<>(lxr._1().append(lxr._3().cons(a)));
}
/**
* Delete the element at the given index. This is an O(log(n)) operation.
*
* @param i The index of the element to update.
*
* @return The updated sequence, or throws an error if the index is out of bounds.
*/
public Seq<A> delete(final int i) {
checkBounds(i);
final P3<FingerTree<Integer, A>, A, FingerTree<Integer, A>> lxr = ftree.split1(index -> index > i);
return new Seq<>(lxr._1().append(lxr._3()));
}
/**
* Takes the given number of elements from the head of this sequence if they are available.
*
* @param n The maximum number of elements to take from this sequence.
* @return A sequence consisting only of the first n elements of this sequence, or else the whole sequence,
* if it has less than n elements.
*/
public Seq<A> take(final int n) { return split(n)._1(); }
/**
* Drops the given number of elements from the head of this sequence if they are available.
*
* @param n The number of elements to drop from this sequence.
* @return A sequence consisting of all elements of this sequence except the first n ones, or else the empty sequence,
* if this sequence has less than n elements.
*/
public Seq<A> drop(final int n) { return split(n)._2(); }
private void checkBounds(final int i) { if (i < 0 || i >= length()) throw error("Index " + i + " is out of bounds."); }
public <B> B foldLeft(final F2<B, A, B> f, final B z) {
return ftree.foldLeft(f, z);
}
public <B> B foldRight(final F2<A, B, B> f, final B z) {
return ftree.foldRight(f, z);
}
public Seq<A> filter(F<A, Boolean> f) {
return foldLeft((acc, a) -> f.f(a) ? acc.snoc(a) : acc, empty());
}
@Override
public int hashCode() {
return Hash.seqHash(Hash.<A>anyHash()).hash(this);
}
public <B> Seq<B> map(F<A, B> f) {
return new Seq<>(ftree.map(f, Seq.elemMeasured()));
}
}