Skip to content

Commit

Permalink
Improve generic types for assoc
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed May 3, 2024
1 parent c5f68cc commit 1c32271
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 27 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 @@ -62,7 +62,7 @@ public AList<T> empty() {
public abstract <R extends ACell> AList<R> concat(ASequence<R> vals);

@Override
public abstract <R extends ACell> AList<R> assoc(long i, R value);
public abstract AList<T> assoc(long i, T value);

/**
* Drops elements from the front of the list.
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 @@ -189,7 +189,7 @@ public ASequence<T> assoc(ACell key, ACell value) {
* @param value New element value
* @return Updated sequence, or null if index is out of range
*/
public abstract <R extends ACell> ASequence<R> assoc(long i, R value);
public abstract ASequence<T> assoc(long i, T value);

/**
* Checks if an index range is valid for this sequence
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/AVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public final AVector<T> slice(long start, long end) {
}

@Override
public abstract <R extends ACell> AVector<R> assoc(long i, R value);
public abstract AVector<T> assoc(long i, T value);

@SuppressWarnings("unchecked")
@Override
Expand Down
9 changes: 4 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 @@ -113,14 +113,13 @@ public Ref<T> getElementRef(long i) {
return data.getElementRef(count - 1 - i);
}

@SuppressWarnings("unchecked")
@Override
public <R extends ACell> AList<R> assoc(long i, R value) {
AVector<R> newData;
public AList<T> assoc(long i, T value) {
AVector<T> newData;
newData = data.assoc(count - 1 - i, value);
if (data == newData) return (AList<R>) this;
if (data == newData) return this;
if (newData==null) return null;
return new List<>(newData);
return new List<T>(newData);
}

@Override
Expand Down
6 changes: 3 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 @@ -86,9 +86,9 @@ public MapEntry<K, V> withValue(V value) {

@SuppressWarnings("unchecked")
@Override
public <R extends ACell> AVector<R> assoc(long i, R a) {
if (i == 0) return (AVector<R>) withKey((K) a);
if (i == 1) return (AVector<R>) withValue((V) a);
public MapEntry<K,V> assoc(long i, ACell a) {
if (i == 0) return withKey((K) a);
if (i == 1) return withValue((V) a);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public AVector<T> next() {
}

@Override
public <R extends ACell> AVector<R> assoc(long i, R value) {
public AVector<T> assoc(long i, T value) {
return toVector().assoc(i,value);
}

Expand Down
16 changes: 8 additions & 8 deletions convex-core/src/main/java/convex/core/data/VectorLeaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,21 @@ protected Ref<T> getElementRefUnsafe(long i) {

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public <R extends ACell> AVector<R> assoc(long i, R value) {
public AVector<T> assoc(long i, T value) {
if ((i < 0) || (i >= count)) return null;

long ix = i - prefixLength();
if (ix >= 0) {
R old = (R) items[(int) ix].getValue();
if (old == value) return (AVector<R>) this;
Ref<R>[] newItems = (Ref<R>[]) items.clone();
T old = items[(int) ix].getValue();
if (old == value) return this;
Ref<T>[] newItems = (Ref<T>[]) items.clone();
newItems[(int) ix] = Ref.get(value);
return new VectorLeaf<R>(newItems, (Ref)prefix, count);
return new VectorLeaf<T>(newItems, (Ref)prefix, count);
} else {
AVector<T> tl = prefix.getValue();
AVector<R> newTail = tl.assoc(i, value);
if (tl == newTail) return (AVector<R>) this;
return new VectorLeaf<R>((Ref[])items, newTail.getRef(), count);
AVector<T> newTail = tl.assoc(i, value);
if (tl == newTail) return (AVector<T>) this;
return new VectorLeaf<T>((Ref[])items, newTail.getRef(), count);
}
}

Expand Down
14 changes: 7 additions & 7 deletions convex-core/src/main/java/convex/core/data/VectorTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,20 @@ protected Ref<T> getElementRefUnsafe(long i) {

@SuppressWarnings("unchecked")
@Override
public <R extends ACell> AVector<R> assoc(long i, R value) {
public AVector<T> assoc(long i, T value) {
if ((i < 0) || (i >= count)) return null;

Ref<AVector<R>>[] rchildren=(Ref[])children;
Ref<AVector<T>>[] rchildren=(Ref[])children;

long bSize = 1L << shift; // size of a fully packed block
int b = (int) (i >> shift);
AVector<R> oc = rchildren[b].getValue();
AVector<R> nc = oc.assoc(i - (b * bSize), value);
if (oc == nc) return (AVector<R>) this;
AVector<T> oc = rchildren[b].getValue();
AVector<T> nc = oc.assoc(i - (b * bSize), value);
if (oc == nc) return (AVector<T>) this;

Ref<AVector<R>>[] newChildren = rchildren.clone();
Ref<AVector<T>>[] newChildren = rchildren.clone();
newChildren[b] = nc.getRef();
return new VectorTree<R>(newChildren, count);
return new VectorTree<T>(newChildren, count);
}

@Override
Expand Down

0 comments on commit 1c32271

Please sign in to comment.