Skip to content

Commit

Permalink
More refactoring for better generic collections
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed May 3, 2024
1 parent dcb2f62 commit c41d59d
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public Object[] toArray() {
* @return The updated collection
*/
@Override
public abstract <R extends ACell> ACollection<R> conj(R x);
public abstract ACollection<T> conj(ACell x);

/**
* Maps a function over a collection, applying it to each element in turn.
Expand Down
10 changes: 4 additions & 6 deletions convex-core/src/main/java/convex/core/data/ADataStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public final boolean isEmpty() {
return isCVMValue();
}


/**
* Adds an element to this data structure, in the natural manner defined by the
* general data structure type. e.g. append at the end of a vector.
Expand All @@ -65,7 +64,7 @@ public final boolean isEmpty() {
* @param x New element to add
* @return The updated data structure, or null if a failure occurred due to invalid element type
*/
public abstract <R extends ACell> ADataStructure<R> conj(R x);
public abstract ADataStructure<E> conj(ACell x);

/**
* Adds multiple elements to this data structure, in the natural manner defined by the
Expand All @@ -77,10 +76,9 @@ public final boolean isEmpty() {
* @param xs New elements to add
* @return The updated data structure, or null if a failure occurred due to invalid element types
*/
@SuppressWarnings("unchecked")
public <R extends ACell> ADataStructure<R> conjAll(ACollection<R> xs) {
ADataStructure<R> result=(ADataStructure<R>) this;
for (R x: xs) {
public ADataStructure<E> conjAll(ACollection<? extends E> xs) {
ADataStructure<E> result=this;
for (E x: xs) {
result=result.conj(x);
if (result==null) return null;
}
Expand Down
21 changes: 10 additions & 11 deletions convex-core/src/main/java/convex/core/data/AHashSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ protected AHashSet(long count) {
protected abstract AHashSet<T> mergeWith(AHashSet<T> b, int setOp, int shift);

@SuppressWarnings("unchecked")
public <R extends ACell> ASet<R> includeAll(ASet<R> elements) {
return (ASet<R>) mergeWith((AHashSet<T>) elements,OP_UNION);
public AHashSet<T> includeAll(ASet<? extends T> elements) {
return mergeWith((AHashSet<T>) elements,OP_UNION);
};

protected final int reverseOp(int setOp) {
Expand Down Expand Up @@ -62,10 +62,10 @@ public ASet<T> excludeAll(ASet<T> elements) {

public abstract AHashSet<T> toCanonical();

public <R extends ACell> ASet<R> conjAll(ACollection<R> elements) {
if (elements instanceof AHashSet) return includeAll((AHashSet<R>) elements);
@SuppressWarnings("unchecked")
AHashSet<R> result=(AHashSet<R>) this;
@SuppressWarnings("unchecked")
public AHashSet<T> conjAll(ACollection<? extends T> elements) {
if (elements instanceof AHashSet) return includeAll((AHashSet<T>)elements);
AHashSet<T> result=this;
long n=elements.count();
for (long i=0; i<n; i++) {
result=result.conj(elements.get(i));
Expand All @@ -91,19 +91,18 @@ public ASet<T> disjAll(ACollection<T> b) {

@SuppressWarnings("unchecked")
@Override
public <R extends ACell> AHashSet<R> conj(R a) {
return (AHashSet<R>) includeRef((Ref<T>) Ref.get(a));
public AHashSet<T> conj(ACell a) {
return includeRef(Ref.get((T)a));
}

@Override
public ASet<T> exclude(ACell a) {
return excludeRef(Ref.get(a));
}

@SuppressWarnings("unchecked")
@Override
public <R extends ACell> AHashSet<R> include(R a) {
return (AHashSet<R>) includeRef((Ref<T>) Ref.get(a));
public AHashSet<T> include(T a) {
return includeRef((Ref<T>) Ref.get(a));
}

/**
Expand Down
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 @@ -45,7 +45,7 @@ public final AType getType() {
* Returns a new list.
*/
@Override
public abstract <R extends ACell> AList<R> conj(R x);
public abstract AList<T> conj(ACell x);

@Override
public AList<T> empty() {
Expand Down
5 changes: 2 additions & 3 deletions convex-core/src/main/java/convex/core/data/AMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,10 @@ public Set<K> keySet() {
* @return Updated map with the specified entry added, or null if the argument
* is not a valid map entry
*/
@SuppressWarnings("unchecked")
public <R extends ACell> ADataStructure<R> conj(R x) {
public AMap<K,V> conj(ACell x) {
MapEntry<K, V> me = RT.ensureMapEntry(x);
if (me == null) return null;
return (ADataStructure<R>) assocEntry(me);
return assocEntry(me);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/data/ASequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ public ACell[] toCellArray() {
* Adds an element to the sequence in the natural position
*
* @param value Value to add
* @return Updated sequence
* @return Updated sequence, or null if value is invalid
*/
@Override
public abstract <R extends ACell> ASequence<R> conj(R value);
public abstract ASequence<T> conj(ACell value);

/**
* Produces a slice of this sequence, beginning with the specified start index and of the given length.
Expand Down
10 changes: 5 additions & 5 deletions convex-core/src/main/java/convex/core/data/ASet.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final byte getTag() {
* @param a Value to include
* @return Updated set
*/
public abstract <R extends ACell> ASet<R> include(R a);
public abstract ASet<T> include(T a);

/**
* Updates the set to exclude the given element
Expand All @@ -58,7 +58,7 @@ public final byte getTag() {
* @param elements Elements to include
* @return Updated set
*/
public abstract <R extends ACell> ASet<R> includeAll(ASet<R> elements) ;
public abstract ASet<T> includeAll(ASet<? extends T> elements) ;

/**
* Updates the set to exclude all the given elements.
Expand All @@ -69,7 +69,7 @@ public final byte getTag() {
public abstract ASet<T> excludeAll(ASet<T> elements) ;

@Override
public abstract <R extends ACell> ASet<R> conjAll(ACollection<R> xs);
public abstract ASet<T> conjAll(ACollection<? extends T> xs);

/**
* Removes all elements from this set, returning a new set.
Expand Down Expand Up @@ -142,12 +142,12 @@ public final boolean contains(Object o) {
public abstract ASet<T> includeRef(Ref<T> ref) ;

@Override
public abstract <R extends ACell> ASet<R> conj(R a);
public abstract ASet<T> conj(ACell a);

@SuppressWarnings("unchecked")
@Override
public ASet<T> assoc(ACell key, ACell value) {
if (value==CVMBool.TRUE) return (ASet<T>) include(key);
if (value==CVMBool.TRUE) return include((T) key);
if (value==CVMBool.FALSE) return exclude((T) key);
return null;
}
Expand Down
10 changes: 5 additions & 5 deletions convex-core/src/main/java/convex/core/data/AVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,16 @@ public AVector<T> appendAll(List<T> list) {

@SuppressWarnings("unchecked")
@Override
public final <R extends ACell> AVector<R> conj(R value) {
return (AVector<R>) append((T) value);
public final AVector<T> conj(ACell value) {
return append((T) value);
}

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

@Override
Expand Down
6 changes: 3 additions & 3 deletions convex-core/src/main/java/convex/core/data/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,16 @@ public int estimatedEncodingSize() {
* @return Updated list
*/
@Override
public <R extends ACell> List<R> conj(R value) {
return new List<R>((AVector<R>) data.conj(value));
public List<T> conj(ACell value) {
return new List<T>(data.conj(value));
}

@Override
public AList<T> cons(T x) {
return new List<T>((AVector<T>) data.conj(x));
}

public <R extends ACell> List<R> conjAll(ACollection<R> xs) {
public List<T> conjAll(ACollection<? extends T> xs) {
return reverse(data.conjAll(xs));
}

Expand Down
5 changes: 2 additions & 3 deletions convex-core/src/main/java/convex/core/data/Sets.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public static <T extends ACell> Ref<SetLeaf<T>> emptyRef() {
return (Ref)EMPTY_REF;
}

@SuppressWarnings("unchecked")
@SafeVarargs
public static <T extends ACell> ASet<T> of(Object... elements) {
int n=elements.length;
Expand All @@ -49,7 +48,7 @@ public static <T extends ACell> ASet<T> of(ACell... elements) {
int n=elements.length;
ASet<T> result=empty();
for (int i=0; i<n; i++) {
result=(ASet<T>) result.conj(elements[i]);
result=(ASet<T>) result.conj((T) elements[i]);
}
return result;
}
Expand Down Expand Up @@ -95,7 +94,7 @@ public static <T extends ACell> ASet<T> fromCollection(Collection<T> source) {
@SuppressWarnings("unchecked")
private static <T extends ACell> ASet<T> fromCollection(ACountable<T> source) {
long n=source.count();
ASet<?> set=EMPTY;
ASet<T> set=EMPTY;
for (long i=0; i<n; i++) {
set=set.include(source.get(i));
}
Expand Down
12 changes: 5 additions & 7 deletions convex-core/src/main/java/convex/core/data/impl/KeySet.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,21 @@ public int estimatedEncodingSize() {
}

@Override
public <R extends ACell> ASet<R> include(R a) {
public ASet<K> include(K a) {
return getCanonicalSet().include(a);
}

protected ASet<? super K> getCanonicalSet() {
protected ASet<K> getCanonicalSet() {
return getCanonical();
}

@SuppressWarnings("unchecked")
@Override
public ASet<K> exclude(ACell a) {
return (ASet<K>) getCanonicalSet().exclude(a);
}

@Override
public <R extends ACell> ASet<R> includeAll(ASet<R> elements) {
public ASet<K> includeAll(ASet<? extends K> elements) {
return getCanonicalSet().includeAll(elements);
}

Expand All @@ -54,7 +53,7 @@ public ASet<K> excludeAll(ASet<K> elements) {
}

@Override
public <R extends ACell> ASet<R> conjAll(ACollection<R> xs) {
public ASet<K> conjAll(ACollection<? extends K> xs) {
return getCanonicalSet().conjAll(xs);
}

Expand Down Expand Up @@ -82,7 +81,7 @@ public ASet<K> includeRef(Ref<K> ref) {
}

@Override
public <R extends ACell> ASet<R> conj(R a) {
public ASet<K> conj(ACell a) {
return getCanonicalSet().conj(a);
}

Expand All @@ -104,7 +103,6 @@ public boolean containsAll(ASet<?> b) {
return getCanonicalSet().containsAll(b);
}

@SuppressWarnings("unchecked")
@Override
public ASet<K> slice(long start, long end) {
return (ASet<K>) getCanonicalSet().slice(start,end);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class AdversarialDataTest {

// Inserting non-CVM values into existing valid sets
invalidTest(Sets.of(1,2,3,4).include(NON_CVM));
invalidTest(Samples.LONG_SET_100.include(NON_CVM));
invalidTest(Samples.LONG_SET_100.conj(NON_CVM));
}

@Test public void testBadKeywords() {
Expand Down

0 comments on commit c41d59d

Please sign in to comment.