Skip to content

Commit 9577f5d

Browse files
committed
Sequence hierarchy refactoring
1 parent 40d167e commit 9577f5d

File tree

11 files changed

+196
-58
lines changed

11 files changed

+196
-58
lines changed

convex-core/src/main/java/convex/core/data/AMapEntry.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public final V setValue(V value) {
5252

5353
@Override
5454
public abstract boolean isCanonical();
55+
56+
5557

5658
@Override
5759
public AVector<ACell> append(ACell value) {
@@ -74,14 +76,14 @@ public Iterator<ACell> iterator() {
7476
}
7577

7678
@Override
77-
public long longIndexOf(Object o) {
79+
public long longIndexOf(ACell o) {
7880
if (Utils.equals(o,get(0))) return 0;
7981
if (Utils.equals(o,get(1))) return 1;
8082
return -1;
8183
}
8284

8385
@Override
84-
public long longLastIndexOf(Object o) {
86+
public long longLastIndexOf(ACell o) {
8587
if (Utils.equals(o,get(1))) return 1;
8688
if (Utils.equals(o,get(0))) return 0;
8789
return -1;

convex-core/src/main/java/convex/core/data/ASequence.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,24 @@ public ASequence(long count) {
2727

2828
@Override
2929
public boolean contains(Object o) {
30-
return longIndexOf(o) >= 0;
30+
if (!(o==null||(o instanceof ACell))) return false;
31+
return longIndexOf((ACell)o) >= 0;
32+
}
33+
34+
@Override
35+
public int indexOf(Object o) {
36+
if (!(o==null||(o instanceof ACell))) return -1;
37+
long pos =longIndexOf((ACell) o);
38+
if (pos < 0) return -1;
39+
return Utils.checkedInt(pos);
40+
}
41+
42+
@Override
43+
public int lastIndexOf(Object o) {
44+
if (!(o==null||(o instanceof ACell))) return -1;
45+
long pos =longLastIndexOf((ACell) o);
46+
if (pos < 0) return -1;
47+
return Utils.checkedInt(pos);
3148
}
3249

3350
/**
@@ -37,7 +54,7 @@ public boolean contains(Object o) {
3754
* @param value Any value which could appear as an element of the sequence.
3855
* @return Index of the value, or -1 if not found.
3956
*/
40-
public abstract long longIndexOf(Object value);
57+
public abstract long longIndexOf(ACell value);
4158

4259
/**
4360
* Gets the last long index at which the specified value appears in the the sequence.
@@ -47,7 +64,7 @@ public boolean contains(Object o) {
4764
* @param value Any value which could appear as an element of the sequence.
4865
* @return Index of the value, or -1 if not found.
4966
*/
50-
public abstract long longLastIndexOf(Object value);
67+
public abstract long longLastIndexOf(ACell value);
5168

5269
@Override
5370
public abstract <R extends ACell> ASequence<R> map(Function<? super T, ? extends R> mapper);

convex-core/src/main/java/convex/core/data/AVector.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import convex.core.data.util.BlobBuilder;
1313
import convex.core.lang.RT;
1414
import convex.core.util.MergeFunction;
15-
import convex.core.util.Utils;
1615

1716
/**
1817
* Abstract base class for vectors.
@@ -157,16 +156,6 @@ public Object[] toArray() {
157156
return result;
158157
}
159158

160-
@Override
161-
public final int indexOf(Object o) {
162-
return Utils.checkedInt(longIndexOf(o));
163-
}
164-
165-
@Override
166-
public final int lastIndexOf(Object o) {
167-
return Utils.checkedInt(longLastIndexOf(o));
168-
}
169-
170159
@Override
171160
public final ListIterator<T> listIterator(int index) {
172161
return listIterator((long) index);
@@ -188,6 +177,11 @@ public final ListIterator<T> listIterator(int index) {
188177
// Vectors are always valid CVM values
189178
return true;
190179
}
180+
181+
@Override
182+
public AVector<T> toVector() {
183+
return this;
184+
}
191185

192186
@Override
193187
public abstract AVector<T> updateRefs(IRefFunction func);
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package convex.core.data;
2+
3+
import java.util.ListIterator;
4+
import java.util.function.Consumer;
5+
import java.util.function.Function;
6+
7+
import convex.core.data.type.AType;
8+
9+
public class DenseRecord extends ACAD3Record {
10+
11+
protected AVector<ACell> data;
12+
13+
public DenseRecord(long count) {
14+
super(count);
15+
}
16+
17+
@Override
18+
public ListIterator<ACell> listIterator() {
19+
return data.listIterator();
20+
}
21+
22+
@Override
23+
public ListIterator<ACell> listIterator(int index) {
24+
return data.listIterator(index);
25+
}
26+
27+
@Override
28+
public long longIndexOf(ACell value) {
29+
return data.longIndexOf(value);
30+
}
31+
32+
@Override
33+
public long longLastIndexOf(ACell value) {
34+
return data.longLastIndexOf(value);
35+
}
36+
37+
@Override
38+
public <R extends ACell> ASequence<R> map(Function<? super ACell, ? extends R> mapper) {
39+
// TODO Auto-generated method stub
40+
return null;
41+
}
42+
43+
@Override
44+
public void forEach(Consumer<? super ACell> action) {
45+
// TODO Auto-generated method stub
46+
47+
}
48+
49+
@Override
50+
public void visitElementRefs(Consumer<Ref<ACell>> f) {
51+
// TODO Auto-generated method stub
52+
53+
}
54+
55+
@Override
56+
public ASequence<ACell> concat(ASequence<? extends ACell> vals) {
57+
// TODO Auto-generated method stub
58+
return null;
59+
}
60+
61+
@Override
62+
public ASequence<ACell> next() {
63+
// TODO Auto-generated method stub
64+
return null;
65+
}
66+
67+
@Override
68+
public ASequence<ACell> empty() {
69+
// TODO Auto-generated method stub
70+
return null;
71+
}
72+
73+
@Override
74+
public ACell get(long index) {
75+
// TODO Auto-generated method stub
76+
return null;
77+
}
78+
79+
@Override
80+
public Ref<ACell> getElementRef(long index) {
81+
// TODO Auto-generated method stub
82+
return null;
83+
}
84+
85+
@Override
86+
public ASequence<ACell> assoc(long i, ACell value) {
87+
// TODO Auto-generated method stub
88+
return null;
89+
}
90+
91+
@Override
92+
public ASequence<ACell> conj(ACell value) {
93+
// TODO Auto-generated method stub
94+
return null;
95+
}
96+
97+
@Override
98+
public ASequence<ACell> slice(long start, long end) {
99+
// TODO Auto-generated method stub
100+
return null;
101+
}
102+
103+
@Override
104+
public AList<ACell> cons(ACell x) {
105+
// TODO Auto-generated method stub
106+
return null;
107+
}
108+
109+
@Override
110+
public AVector<ACell> subVector(long start, long length) {
111+
// TODO Auto-generated method stub
112+
return null;
113+
}
114+
115+
@Override
116+
protected ListIterator<ACell> listIterator(long l) {
117+
// TODO Auto-generated method stub
118+
return null;
119+
}
120+
121+
@Override
122+
public ASequence<ACell> reverse() {
123+
// TODO Auto-generated method stub
124+
return null;
125+
}
126+
127+
@Override
128+
public AType getType() {
129+
// TODO Auto-generated method stub
130+
return null;
131+
}
132+
133+
@Override
134+
public AVector<ACell> toVector() {
135+
// TODO Auto-generated method stub
136+
return null;
137+
}
138+
139+
@Override
140+
protected <R> void copyToArray(R[] arr, int offset) {
141+
// TODO Auto-generated method stub
142+
143+
}
144+
145+
}

convex-core/src/main/java/convex/core/data/List.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,28 +118,14 @@ public AList<T> assoc(long i, T value) {
118118
}
119119

120120
@Override
121-
public int indexOf(Object o) {
122-
int pos = data.lastIndexOf(o);
123-
if (pos < 0) return -1;
124-
return size() - 1 - pos;
125-
}
126-
127-
@Override
128-
public int lastIndexOf(Object o) {
129-
int pos = data.indexOf(o);
130-
if (pos < 0) return -1;
131-
return size() - 1 - pos;
132-
}
133-
134-
@Override
135-
public long longIndexOf(Object o) {
121+
public long longIndexOf(ACell o) {
136122
long pos = data.longLastIndexOf(o);
137123
if (pos < 0) return -1;
138124
return count - 1 - pos;
139125
}
140126

141127
@Override
142-
public long longLastIndexOf(Object o) {
128+
public long longLastIndexOf(ACell o) {
143129
long pos = data.longIndexOf(o);
144130
if (pos < 0) return -1;
145131
return count - 1 - pos;

convex-core/src/main/java/convex/core/data/MapEntry.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import convex.core.exceptions.InvalidDataException;
1010
import convex.core.lang.RT;
1111
import convex.core.util.ErrorMessages;
12-
import convex.core.util.Utils;
1312

1413
/**
1514
* Map.Entry implementation for persistent maps. This is primarily intended as an efficient
@@ -201,17 +200,6 @@ public boolean keyEquals(MapEntry<K, V> b) {
201200
return keyRef.equals(b.keyRef);
202201
}
203202

204-
@SuppressWarnings("unchecked")
205-
@Override
206-
public AVector<ACell> toVector() {
207-
return new VectorLeaf<ACell>(new Ref[] { keyRef, valueRef });
208-
}
209-
210-
@Override
211-
public boolean contains(Object o) {
212-
return (Utils.equals(o, getKey()) || Utils.equals(o, getValue()));
213-
}
214-
215203
@Override
216204
public ACell get(long i) {
217205
if (i == 0) return getKey();
@@ -279,18 +267,22 @@ public AVector<ACell> concat(ASequence<?> b) {
279267

280268
@Override
281269
public void validateCell() throws InvalidDataException {
282-
// TODO: is there really Nothing to do?
270+
// Nothing to do, a map entry is always valid in itself
283271
}
284272

285273
@Override
286274
public boolean isCanonical() {
287-
// TODO: probably should be canonical?
288-
return false;
275+
return true;
289276
}
290277

278+
@Override
279+
public AVector<ACell> toCanonical() {
280+
return this;
281+
}
282+
291283
@SuppressWarnings("unchecked")
292284
@Override
293-
public VectorLeaf<ACell> toCanonical() {
285+
public AVector<ACell> toVector() {
294286
return new VectorLeaf<ACell>(new Ref[] { keyRef, valueRef });
295287
}
296288

convex-core/src/main/java/convex/core/data/VectorArray.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,15 @@ protected Ref<T> getElementRefUnsafe(long i) {
203203
}
204204

205205
@Override
206-
public long longIndexOf(Object value) {
206+
public long longIndexOf(ACell value) {
207207
for (int i=0; i<count; i++) {
208208
if (Utils.equals(data[start+i],value)) return i;
209209
}
210210
return -1;
211211
}
212212

213213
@Override
214-
public long longLastIndexOf(Object value) {
214+
public long longLastIndexOf(ACell value) {
215215
for (int i=Utils.checkedInt(count)-1; i>=0; i--) {
216216
if (Utils.equals(data[start+i],value)) return i;
217217
}

convex-core/src/main/java/convex/core/data/VectorLeaf.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ protected <K> void copyToArray(K[] arr, int offset) {
456456
}
457457

458458
@Override
459-
public long longIndexOf(Object o) {
459+
public long longIndexOf(ACell o) {
460460
if (prefix != null) {
461461
long pi = prefix.getValue().longIndexOf(o);
462462
if (pi >= 0L) return pi;
@@ -468,7 +468,7 @@ public long longIndexOf(Object o) {
468468
}
469469

470470
@Override
471-
public long longLastIndexOf(Object o) {
471+
public long longLastIndexOf(ACell o) {
472472
for (int i = items.length - 1; i >= 0; i--) {
473473
if (Utils.equals(items[i].getValue(), o)) return (count - items.length + i);
474474
}

convex-core/src/main/java/convex/core/data/VectorTree.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ protected <K> void copyToArray(K[] arr, int offset) {
356356
}
357357

358358
@Override
359-
public long longIndexOf(Object o) {
359+
public long longIndexOf(ACell o) {
360360
long offset = 0;
361361
for (int i = 0; i < children.length; i++) {
362362
AVector<T> b = children[i].getValue();
@@ -368,7 +368,7 @@ public long longIndexOf(Object o) {
368368
}
369369

370370
@Override
371-
public long longLastIndexOf(Object o) {
371+
public long longLastIndexOf(ACell o) {
372372
long offset = count;
373373
for (int i = children.length - 1; i >= 0; i--) {
374374
AVector<T> b = children[i].getValue();

0 commit comments

Comments
 (0)