Skip to content

Commit

Permalink
Improve generic type for ASequence.concat
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed May 3, 2024
1 parent 1c32271 commit dcb2f62
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/AList.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public AList<T> empty() {
public abstract <R extends ACell> AList<R> map(Function<? super T, ? extends R> mapper);

@Override
public abstract <R extends ACell> AList<R> concat(ASequence<R> vals);
public abstract AList<T> concat(ASequence<? extends T> vals);

@Override
public abstract AList<T> assoc(long i, T value);
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/ASequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public <R extends ACell> ASequence<R> flatMap(Function<? super T, ? extends ASeq
* @param vals A sequence of values to concatenate.
* @return The concatenated sequence, of the same type as this sequence.
*/
public abstract <R extends ACell> ASequence<R> concat(ASequence<R> vals);
public abstract ASequence<T> concat(ASequence<? extends T> vals);

@Override
public final boolean addAll(int index, Collection<? extends T> c) {
Expand Down
7 changes: 4 additions & 3 deletions convex-core/src/main/java/convex/core/data/AVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public <R extends ACell> AVector<R> flatMap(Function<? super T, ? extends ASeque
}

@Override
public abstract <R extends ACell> AVector<R> concat(ASequence<R> b);
public abstract AVector<T> concat(ASequence<? extends T> b);

public abstract <R> R reduce(BiFunction<? super R, ? super T, ? extends R> func, R value);

Expand Down Expand Up @@ -212,11 +212,12 @@ public final <R extends ACell> AVector<R> conj(R value) {
return (AVector<R>) append((T) value);
}

@SuppressWarnings("unchecked")
public <R extends ACell> AVector<R> conjAll(ACollection<R> xs) {
if (xs instanceof ASequence) {
return concat((ASequence<R>)xs);
return (AVector<R>) concat((ASequence<T>)xs);
}
return concat(Vectors.create(xs));
return (AVector<R>) concat(Vectors.create(xs));
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions convex-core/src/main/java/convex/core/data/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,13 @@ public <R extends ACell> AList<R> map(Function<? super T, ? extends R> mapper) {

@SuppressWarnings("unchecked")
@Override
public <R extends ACell> List<R> concat(ASequence<R> vals) {
public List<T> concat(ASequence<? extends T> vals) {
long n = RT.count(vals);
if (n==0) return (List<R>) this;
if (n==0) return this;

AVector<R> rvals;
AVector<T> rvals;
if (vals instanceof List) {
List<R> vlist=(List<R>) vals;
List<T> vlist=(List<T>) vals;
if (count==0) return vlist;
rvals = vlist.data;
} else {
Expand All @@ -363,7 +363,7 @@ public <R extends ACell> List<R> concat(ASequence<R> vals) {
}
}
if (count>0) {
rvals=rvals.concat((AVector<R>)data);
rvals=rvals.concat(data);
}
return List.reverse(rvals);
}
Expand Down
5 changes: 2 additions & 3 deletions convex-core/src/main/java/convex/core/data/MapEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,9 @@ public void visitElementRefs(Consumer<Ref<ACell>> f) {
f.accept((Ref<ACell>) valueRef);
}

@SuppressWarnings("unchecked")
@Override
public <R extends ACell> AVector<R> concat(ASequence<R> b) {
if (b.isEmpty()) return (AVector<R>) this;
public AVector<ACell> concat(ASequence<?> b) {
if (b.isEmpty()) return this;
return toVector().concat(b);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public <R extends ACell> AVector<R> map(Function<? super T, ? extends R> mapper)
}

@Override
public <R extends ACell> AVector<R> concat(ASequence<R> b) {
public AVector<T> concat(ASequence<? extends T> b) {
return toVector().concat(b);
}

Expand Down
6 changes: 3 additions & 3 deletions convex-core/src/main/java/convex/core/data/VectorLeaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,19 @@ public AVector<T> append(T value) {

@SuppressWarnings("unchecked")
@Override
public <R extends ACell> AVector<R> concat(ASequence<R> b) {
public AVector<T> concat(ASequence<? extends T> b) {
// Maybe can optimise?
long aLen = count();
long bLen = b.count();
AVector<R> result = (AVector<R>) this;
AVector<T> result = this;
long i = aLen;
long end = aLen + bLen;
while (i < end) {
if ((i & Vectors.BITMASK) == 0) {
int rn = Utils.checkedInt(Math.min(Vectors.CHUNK_SIZE, end - i));
if (rn == Vectors.CHUNK_SIZE) {
// we can append a whole chunk. Not toVector in case of VectorArrays
result = result.appendChunk((VectorLeaf<R>) b.subVector(i - aLen, rn).toVector());
result = result.appendChunk((AVector<T>) b.subVector(i - aLen, rn));
i += Vectors.CHUNK_SIZE;
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions convex-core/src/main/java/convex/core/data/VectorTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,21 +293,21 @@ public AVector<T> append(T value) {

@SuppressWarnings("unchecked")
@Override
public <R extends ACell> AVector<R> concat(ASequence<R> b) {
public AVector<T> concat(ASequence<? extends T> b) {
long bLen = b.count();
VectorTree<R> result = (VectorTree<R>) this;
VectorTree<T> result = this;
long bi = 0;
while (bi < bLen) {
if ((bi + Vectors.CHUNK_SIZE) <= bLen) {
// can append a whole chunk
AVector<R> chunk = b.subVector(bi, Vectors.CHUNK_SIZE);
AVector<T> chunk = (AVector<T>) b.subVector(bi, Vectors.CHUNK_SIZE);
result = result.appendChunk(chunk);
bi += Vectors.CHUNK_SIZE;
} else {
// we have less than a chunk left, so final result must be a VectorLeaf with the
// current result as tail
VectorLeaf<T> head = (VectorLeaf<T>) b.subVector(bi, bLen - bi).toVector();
return ((VectorLeaf<R>)head).withPrefix(result);
return ((VectorLeaf<T>)head).withPrefix(result);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static int size(ACell a) {
}

@Test public void testVectorTree() {
AVector<?> a=Samples.MAX_EMBEDDED_VECTOR;
AVector<ACell> a=Samples.MAX_EMBEDDED_VECTOR;
for (int i=0; i<15; i++) {
a=a.concat(Samples.MAX_EMBEDDED_VECTOR);
}
Expand Down
6 changes: 3 additions & 3 deletions convex-core/src/test/java/convex/core/data/EncodingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,15 @@ public void testFailedMissingEncoding() throws BadFormatException {
doMultiEncodingTest(Vectors.of(1,2,3));

// Two non-embedded identical children
AVector<?> v1=Vectors.of(1,Samples.NON_EMBEDDED_STRING,Samples.NON_EMBEDDED_STRING,Samples.INT_VECTOR_23);
AVector<ACell> v1=Vectors.of(1,Samples.NON_EMBEDDED_STRING,Samples.NON_EMBEDDED_STRING,Samples.INT_VECTOR_23);
doMultiEncodingTest(v1);

// Moar layers
AVector<?> v2=Vectors.of(7,Samples.NON_EMBEDDED_STRING,v1.concat(Samples.INT_VECTOR_23));
AVector<ACell> v2=Vectors.of(7,Samples.NON_EMBEDDED_STRING,v1.concat(Samples.INT_VECTOR_23));
doMultiEncodingTest(v2);

// Mooooar layers
AVector<?> v3=Vectors.of(13,v2,v1,Samples.KEY_PAIR.signData(v2));
AVector<ACell> v3=Vectors.of(13,v2,v1,Samples.KEY_PAIR.signData(v2));
doMultiEncodingTest(v3);

// Wrap in transaction
Expand Down

0 comments on commit dcb2f62

Please sign in to comment.