Skip to content

Commit

Permalink
Update OrderedTraversable::isDefinedAt(int)
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Dec 30, 2024
1 parent 0356a6d commit 22a6aa1
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ public interface OrderedTraversable<T> extends Traversable<T> {
return res;
}

@Contract(pure = true)
default boolean isDefinedAt(@Index int index) {
if (index >= 0) {
return sizeGreaterThan(index);
} else {
return index != ~0 && sizeGreaterThanOrEquals(~index);
}
}

//region Element Retrieval Operations

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ public interface Indexed${Type}SeqLike extends ${Type}SeqLike, RandomAccess {
return true;
}

@Override
default boolean isDefinedAt(int index) {
return index >= 0 && index < size();
}

@Override
${PrimitiveType} get(int index);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import kala.collection.primitive.internal.${Type}SeqIterators;
import kala.collection.mutable.primitive.Mutable${Type}ArrayList;
import kala.control.primitive.${Type}Option;
import kala.function.*;
import kala.index.Index;
import kala.index.Indexes;
import org.intellij.lang.annotations.Flow;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -79,8 +81,12 @@ public interface ${Type}SeqLike extends PrimitiveSeqLike<${WrapperType}>, ${Type
//region Positional Access Operations

@Contract(pure = true)
default boolean isDefinedAt(int index) {
return index >= 0 && sizeGreaterThan(index);
default boolean isDefinedAt(@Index int index) {
if (index >= 0) {
return sizeGreaterThan(index);
} else {
return index != ~0 && sizeGreaterThanOrEquals(~index);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ public final class ${Type}SeqViews {
return source.get(index);
}

public boolean isDefinedAt(int index) {
return source.isDefinedAt(index);
}

public int indexOf(${PrimitiveType} value) {
return source.indexOf(value);
}
Expand Down Expand Up @@ -477,11 +473,6 @@ public final class ${Type}SeqViews {
return source.knownSize();
}

@Override
public boolean isDefinedAt(int index) {
return source.isDefinedAt(index);
}

@Override
public final ${PrimitiveType} get(int index) {
if (index == this.index) {
Expand Down Expand Up @@ -1094,11 +1085,6 @@ public final class ${Type}SeqViews {
return source.knownSize();
}

@Override
public boolean isDefinedAt(int index) {
return source.isDefinedAt(index);
}

@Override
public final E get(int index) {
return mapper.apply(source.get(index));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ default boolean supportsFastRandomAccess() {
return true;
}

@Override
default boolean isDefinedAt(int index) {
return index >= 0 && index < size();
}

@Override
E get(int index);

Expand Down
5 changes: 0 additions & 5 deletions kala-collection/src/main/java/kala/collection/SeqLike.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ static <E> SeqLike<E> narrow(SeqLike<? extends E> view) {

//region Positional Access Operations

@Contract(pure = true)
default boolean isDefinedAt(int index) {
return index >= 0 && sizeGreaterThan(index);
}

@Override
default E elementAt(int index) {
return get(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ public E get(int index) {
return source.getOption(index);
}

public boolean isDefinedAt(int index) {
return source.isDefinedAt(index);
}

public int indexOf(Object value) {
return source.indexOf(value);
}
Expand Down Expand Up @@ -1317,11 +1313,6 @@ public final int knownSize() {

//endregion

@Override
public final boolean isDefinedAt(int index) {
return source.isDefinedAt(index);
}

@Override
public final E get(int index) {
return mapper.apply(index, source.get(index));
Expand Down
5 changes: 4 additions & 1 deletion src/test/template/kala/collection/SeqLikeTestTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ default void isDefinedAtTest() {

for (Integer[] data : data1()) {
SeqLike<Integer> seq = from(data);
assertFalse(seq.isDefinedAt(-1));
assertFalse(seq.isDefinedAt(~0));
assertFalse(seq.isDefinedAt(Integer.MIN_VALUE));

for (int i = 0; i < data.length; i++) {
assertTrue(seq.isDefinedAt(i));
assertTrue(seq.isDefinedAt(~(i + 1)));
}

assertFalse(seq.isDefinedAt(~(data.length + 1)));
assertFalse(seq.isDefinedAt(data.length));
assertFalse(seq.isDefinedAt(Integer.MAX_VALUE));
}
Expand Down

0 comments on commit 22a6aa1

Please sign in to comment.