|
10 | 10 | import fj.data.fingertrees.MakeTree; |
11 | 11 | import fj.data.fingertrees.Measured; |
12 | 12 |
|
| 13 | +import java.util.AbstractList; |
| 14 | +import java.util.Iterator; |
| 15 | +import java.util.NoSuchElementException; |
| 16 | + |
13 | 17 | /** |
14 | 18 | * Provides an immutable finite sequence, implemented as a finger tree. This structure gives O(1) access to |
15 | 19 | * the head and tail, as well as O(log n) random access and concatenation of sequences. |
16 | 20 | */ |
17 | | -public final class Seq<A> { |
| 21 | +public final class Seq<A> implements Iterable<A> { |
18 | 22 | private static final Measured<Integer, Object> ELEM_MEASURED = measured(intAdditionMonoid, Function.constant(1)); |
19 | 23 | private static final MakeTree<Integer, Object> MK_TREE = FingerTree.mkTree(ELEM_MEASURED); |
20 | 24 | private static final Seq<Object> EMPTY = new Seq<Object>(MK_TREE.empty()); |
@@ -88,10 +92,79 @@ public Seq<A> snoc(final A a) { |
88 | 92 | return new Seq<A>(ftree.snoc(a)); |
89 | 93 | } |
90 | 94 |
|
| 95 | + /** |
| 96 | + * The first element of this sequence. This is an O(1) operation. |
| 97 | + * |
| 98 | + * @return The first element if this sequence is nonempty, otherwise throws an error. |
| 99 | + */ |
| 100 | + public A head() { return ftree.head(); } |
| 101 | + |
| 102 | + /** |
| 103 | + * The last element of this sequence. This is an O(1) operation. |
| 104 | + * |
| 105 | + * @return The last element if this sequence is nonempty, otherwise throws an error. |
| 106 | + */ |
| 107 | + public A last() { return ftree.last(); } |
| 108 | + |
| 109 | + /** |
| 110 | + * The sequence without the first element. This is an O(1) operation. |
| 111 | + * |
| 112 | + * @return The sequence without the first element if this sequence is nonempty, otherwise throws an error. |
| 113 | + */ |
| 114 | + public Seq<A> tail() { |
| 115 | + return (length() == 1) ? empty() : new Seq<>(ftree.tail()); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * The sequence without the last element. This is an O(1) operation. |
| 120 | + * |
| 121 | + * @return The sequence without the last element if this sequence is nonempty, otherwise throws an error. |
| 122 | + */ |
| 123 | + public Seq<A> init() { |
| 124 | + return (length() == 1) ? empty() : new Seq<>(ftree.init()); |
| 125 | + } |
| 126 | + |
91 | 127 | public Stream<A> toStream() { |
92 | 128 | return ftree.foldLeft((b, a) -> b.cons(a), Stream.<A>nil()).reverse(); |
93 | 129 | } |
94 | 130 |
|
| 131 | + public final java.util.List<A> toJavaList() { |
| 132 | + return new AbstractList<A>() { |
| 133 | + @Override public A get(int i) { return index(i); } |
| 134 | + @Override public Iterator<A> iterator() { return Seq.this.iterator(); } |
| 135 | + @Override public int size() { return length(); } |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Returns an iterator for this seq. This method exists to permit the use in a <code>for</code>-each loop. |
| 141 | + * |
| 142 | + * @return A iterator for this seq. |
| 143 | + */ |
| 144 | + public final Iterator<A> iterator() { |
| 145 | + return new Iterator<A>() { |
| 146 | + private FingerTree<Integer, A> ftree = Seq.this.ftree; |
| 147 | + |
| 148 | + public boolean hasNext() { |
| 149 | + return !ftree.isEmpty(); |
| 150 | + } |
| 151 | + |
| 152 | + public A next() { |
| 153 | + if (ftree.isEmpty()) |
| 154 | + throw new NoSuchElementException(); |
| 155 | + else { |
| 156 | + final A a = ftree.head(); |
| 157 | + ftree = ftree.tail(); |
| 158 | + return a; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + public void remove() { |
| 163 | + throw new UnsupportedOperationException(); |
| 164 | + } |
| 165 | + }; |
| 166 | + } |
| 167 | + |
95 | 168 | @Override |
96 | 169 | public String toString() { |
97 | 170 | return Show.seqShow(Show.<A>anyShow()).showS(this); |
@@ -125,18 +198,56 @@ public int length() { |
125 | 198 | return ftree.measure(); |
126 | 199 | } |
127 | 200 |
|
| 201 | + public P2<Seq<A>, Seq<A>> split(final int i) { |
| 202 | + final P2<FingerTree<Integer, A>, FingerTree<Integer, A>> lr = ftree.split(index -> index > i); |
| 203 | + return P.p(new Seq<>(lr._1()), new Seq<>(lr._2())); |
| 204 | + } |
| 205 | + |
128 | 206 | /** |
129 | | - * Returns the element at the given index. |
| 207 | + * Returns the element at the given index. This is an O(log(n)) operation. |
130 | 208 | * |
131 | 209 | * @param i The index of the element to return. |
132 | 210 | * @return The element at the given index, or throws an error if the index is out of bounds. |
133 | 211 | */ |
134 | 212 | public A index(final int i) { |
135 | | - if (i < 0 || i >= length()) |
136 | | - throw error("Index " + i + "out of bounds."); |
| 213 | + checkBounds(i); |
137 | 214 | return ftree.lookup(Function.<Integer>identity(), i)._2(); |
138 | 215 | } |
139 | 216 |
|
| 217 | + /** |
| 218 | + * Replace the element at the given index with the supplied value. This is an O(log(n)) operation. |
| 219 | + * |
| 220 | + * @param i The index of the element to update. |
| 221 | + * @param a The new value. |
| 222 | + * |
| 223 | + * @return The updated sequence, or throws an error if the index is out of bounds. |
| 224 | + */ |
| 225 | + public Seq<A> update(final int i, final A a) { |
| 226 | + checkBounds(i); |
| 227 | + final P3<FingerTree<Integer, A>, A, FingerTree<Integer, A>> lxr = ftree.split1(index -> index > i); |
| 228 | + return new Seq<>(lxr._1().append(lxr._3().cons(a))); |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Takes the given number of elements from the head of this sequence if they are available. |
| 233 | + * |
| 234 | + * @param n The maximum number of elements to take from this sequence. |
| 235 | + * @return A sequence consisting only of the first n elements of this sequence, or else the whole sequence, |
| 236 | + * if it has less than n elements. |
| 237 | + */ |
| 238 | + public Seq<A> take(final int n) { return split(n)._1(); } |
| 239 | + |
| 240 | + /** |
| 241 | + * Drops the given number of elements from the head of this sequence if they are available. |
| 242 | + * |
| 243 | + * @param n The number of elements to drop from this sequence. |
| 244 | + * @return A sequence consisting of all elements of this sequence except the first n ones, or else the empty sequence, |
| 245 | + * if this sequence has less than n elements. |
| 246 | + */ |
| 247 | + public Seq<A> drop(final int n) { return split(n)._2(); } |
| 248 | + |
| 249 | + private void checkBounds(final int i) { if (i < 0 || i >= length()) throw error("Index " + i + " is out of bounds."); } |
| 250 | + |
140 | 251 | public <B> B foldLeft(final F2<B, A, B> f, final B z) { |
141 | 252 | return ftree.foldLeft(f, z); |
142 | 253 | } |
|
0 commit comments