Skip to content

Commit

Permalink
Sequence hierarchy refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 12, 2024
1 parent 40d167e commit 9577f5d
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 58 deletions.
6 changes: 4 additions & 2 deletions convex-core/src/main/java/convex/core/data/AMapEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public final V setValue(V value) {

@Override
public abstract boolean isCanonical();



@Override
public AVector<ACell> append(ACell value) {
Expand All @@ -74,14 +76,14 @@ public Iterator<ACell> iterator() {
}

@Override
public long longIndexOf(Object o) {
public long longIndexOf(ACell o) {
if (Utils.equals(o,get(0))) return 0;
if (Utils.equals(o,get(1))) return 1;
return -1;
}

@Override
public long longLastIndexOf(Object o) {
public long longLastIndexOf(ACell o) {
if (Utils.equals(o,get(1))) return 1;
if (Utils.equals(o,get(0))) return 0;
return -1;
Expand Down
23 changes: 20 additions & 3 deletions convex-core/src/main/java/convex/core/data/ASequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,24 @@ public ASequence(long count) {

@Override
public boolean contains(Object o) {
return longIndexOf(o) >= 0;
if (!(o==null||(o instanceof ACell))) return false;
return longIndexOf((ACell)o) >= 0;
}

@Override
public int indexOf(Object o) {
if (!(o==null||(o instanceof ACell))) return -1;
long pos =longIndexOf((ACell) o);
if (pos < 0) return -1;
return Utils.checkedInt(pos);
}

@Override
public int lastIndexOf(Object o) {
if (!(o==null||(o instanceof ACell))) return -1;
long pos =longLastIndexOf((ACell) o);
if (pos < 0) return -1;
return Utils.checkedInt(pos);
}

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

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

@Override
public abstract <R extends ACell> ASequence<R> map(Function<? super T, ? extends R> mapper);
Expand Down
16 changes: 5 additions & 11 deletions convex-core/src/main/java/convex/core/data/AVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import convex.core.data.util.BlobBuilder;
import convex.core.lang.RT;
import convex.core.util.MergeFunction;
import convex.core.util.Utils;

/**
* Abstract base class for vectors.
Expand Down Expand Up @@ -157,16 +156,6 @@ public Object[] toArray() {
return result;
}

@Override
public final int indexOf(Object o) {
return Utils.checkedInt(longIndexOf(o));
}

@Override
public final int lastIndexOf(Object o) {
return Utils.checkedInt(longLastIndexOf(o));
}

@Override
public final ListIterator<T> listIterator(int index) {
return listIterator((long) index);
Expand All @@ -188,6 +177,11 @@ public final ListIterator<T> listIterator(int index) {
// Vectors are always valid CVM values
return true;
}

@Override
public AVector<T> toVector() {
return this;
}

@Override
public abstract AVector<T> updateRefs(IRefFunction func);
Expand Down
145 changes: 145 additions & 0 deletions convex-core/src/main/java/convex/core/data/DenseRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package convex.core.data;

import java.util.ListIterator;
import java.util.function.Consumer;
import java.util.function.Function;

import convex.core.data.type.AType;

public class DenseRecord extends ACAD3Record {

protected AVector<ACell> data;

public DenseRecord(long count) {
super(count);
}

@Override
public ListIterator<ACell> listIterator() {
return data.listIterator();
}

@Override
public ListIterator<ACell> listIterator(int index) {
return data.listIterator(index);
}

@Override
public long longIndexOf(ACell value) {
return data.longIndexOf(value);
}

@Override
public long longLastIndexOf(ACell value) {
return data.longLastIndexOf(value);
}

@Override
public <R extends ACell> ASequence<R> map(Function<? super ACell, ? extends R> mapper) {
// TODO Auto-generated method stub
return null;
}

@Override
public void forEach(Consumer<? super ACell> action) {
// TODO Auto-generated method stub

}

@Override
public void visitElementRefs(Consumer<Ref<ACell>> f) {
// TODO Auto-generated method stub

}

@Override
public ASequence<ACell> concat(ASequence<? extends ACell> vals) {
// TODO Auto-generated method stub
return null;
}

@Override
public ASequence<ACell> next() {
// TODO Auto-generated method stub
return null;
}

@Override
public ASequence<ACell> empty() {
// TODO Auto-generated method stub
return null;
}

@Override
public ACell get(long index) {
// TODO Auto-generated method stub
return null;
}

@Override
public Ref<ACell> getElementRef(long index) {
// TODO Auto-generated method stub
return null;
}

@Override
public ASequence<ACell> assoc(long i, ACell value) {
// TODO Auto-generated method stub
return null;
}

@Override
public ASequence<ACell> conj(ACell value) {
// TODO Auto-generated method stub
return null;
}

@Override
public ASequence<ACell> slice(long start, long end) {
// TODO Auto-generated method stub
return null;
}

@Override
public AList<ACell> cons(ACell x) {
// TODO Auto-generated method stub
return null;
}

@Override
public AVector<ACell> subVector(long start, long length) {
// TODO Auto-generated method stub
return null;
}

@Override
protected ListIterator<ACell> listIterator(long l) {
// TODO Auto-generated method stub
return null;
}

@Override
public ASequence<ACell> reverse() {
// TODO Auto-generated method stub
return null;
}

@Override
public AType getType() {
// TODO Auto-generated method stub
return null;
}

@Override
public AVector<ACell> toVector() {
// TODO Auto-generated method stub
return null;
}

@Override
protected <R> void copyToArray(R[] arr, int offset) {
// TODO Auto-generated method stub

}

}
18 changes: 2 additions & 16 deletions convex-core/src/main/java/convex/core/data/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,28 +118,14 @@ public AList<T> assoc(long i, T value) {
}

@Override
public int indexOf(Object o) {
int pos = data.lastIndexOf(o);
if (pos < 0) return -1;
return size() - 1 - pos;
}

@Override
public int lastIndexOf(Object o) {
int pos = data.indexOf(o);
if (pos < 0) return -1;
return size() - 1 - pos;
}

@Override
public long longIndexOf(Object o) {
public long longIndexOf(ACell o) {
long pos = data.longLastIndexOf(o);
if (pos < 0) return -1;
return count - 1 - pos;
}

@Override
public long longLastIndexOf(Object o) {
public long longLastIndexOf(ACell o) {
long pos = data.longIndexOf(o);
if (pos < 0) return -1;
return count - 1 - pos;
Expand Down
24 changes: 8 additions & 16 deletions convex-core/src/main/java/convex/core/data/MapEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import convex.core.exceptions.InvalidDataException;
import convex.core.lang.RT;
import convex.core.util.ErrorMessages;
import convex.core.util.Utils;

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

@SuppressWarnings("unchecked")
@Override
public AVector<ACell> toVector() {
return new VectorLeaf<ACell>(new Ref[] { keyRef, valueRef });
}

@Override
public boolean contains(Object o) {
return (Utils.equals(o, getKey()) || Utils.equals(o, getValue()));
}

@Override
public ACell get(long i) {
if (i == 0) return getKey();
Expand Down Expand Up @@ -279,18 +267,22 @@ public AVector<ACell> concat(ASequence<?> b) {

@Override
public void validateCell() throws InvalidDataException {
// TODO: is there really Nothing to do?
// Nothing to do, a map entry is always valid in itself
}

@Override
public boolean isCanonical() {
// TODO: probably should be canonical?
return false;
return true;
}

@Override
public AVector<ACell> toCanonical() {
return this;
}

@SuppressWarnings("unchecked")
@Override
public VectorLeaf<ACell> toCanonical() {
public AVector<ACell> toVector() {
return new VectorLeaf<ACell>(new Ref[] { keyRef, valueRef });
}

Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/data/VectorArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ protected Ref<T> getElementRefUnsafe(long i) {
}

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

@Override
public long longLastIndexOf(Object value) {
public long longLastIndexOf(ACell value) {
for (int i=Utils.checkedInt(count)-1; i>=0; i--) {
if (Utils.equals(data[start+i],value)) return i;
}
Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/data/VectorLeaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ protected <K> void copyToArray(K[] arr, int offset) {
}

@Override
public long longIndexOf(Object o) {
public long longIndexOf(ACell o) {
if (prefix != null) {
long pi = prefix.getValue().longIndexOf(o);
if (pi >= 0L) return pi;
Expand All @@ -468,7 +468,7 @@ public long longIndexOf(Object o) {
}

@Override
public long longLastIndexOf(Object o) {
public long longLastIndexOf(ACell o) {
for (int i = items.length - 1; i >= 0; i--) {
if (Utils.equals(items[i].getValue(), o)) return (count - items.length + i);
}
Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/data/VectorTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ protected <K> void copyToArray(K[] arr, int offset) {
}

@Override
public long longIndexOf(Object o) {
public long longIndexOf(ACell o) {
long offset = 0;
for (int i = 0; i < children.length; i++) {
AVector<T> b = children[i].getValue();
Expand All @@ -368,7 +368,7 @@ public long longIndexOf(Object o) {
}

@Override
public long longLastIndexOf(Object o) {
public long longLastIndexOf(ACell o) {
long offset = count;
for (int i = children.length - 1; i >= 0; i--) {
AVector<T> b = children[i].getValue();
Expand Down
Loading

0 comments on commit 9577f5d

Please sign in to comment.