Skip to content

Commit

Permalink
Update SeqLike::binarySearch
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Dec 31, 2024
1 parent e2f428c commit 629a26a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kala.collection.base.primitive.${Type}Iterator;
import kala.control.primitive.${Type}Option;
import kala.function.*;
import kala.index.Index;
import kala.index.Indexes;
import org.jetbrains.annotations.NotNull;

import java.util.NoSuchElementException;
Expand Down Expand Up @@ -116,13 +117,14 @@ public interface Indexed${Type}SeqLike extends ${Type}SeqLike, RandomAccess {
//endregion

default int binarySearch(${PrimitiveType} value) {
return binarySearch(0, size(), value);
return binarySearch(0, ~0, value);
}

default int binarySearch(int beginIndex, int endIndex, ${PrimitiveType} value) {
Conditions.checkPositionIndices(beginIndex, endIndex, size());
int low = beginIndex;
int high = endIndex - 1;
default int binarySearch(@Index int beginIndex, @Index int endIndex, ${PrimitiveType} value) {
final int size = size();

int low = Indexes.checkBeginIndex(beginIndex, size);
int high = Indexes.checkEndIndex(low, endIndex, size) - 1;

while (low <= high) {
final int mid = (low + high) >>> 1;
Expand Down
10 changes: 6 additions & 4 deletions kala-collection/src/main/java/kala/collection/ArraySeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -795,14 +795,16 @@ public final E get(@Index int index) {
//region Search Operations

@Override
public final int binarySearch(int beginIndex, int endIndex, E value) {
Conditions.checkPositionIndices(beginIndex, endIndex, elements.length);
public final int binarySearch(@Index int beginIndex, @Index int endIndex, E value) {
beginIndex = Indexes.checkBeginIndex(beginIndex, elements.length);
endIndex = Indexes.checkEndIndex(beginIndex, endIndex, elements.length);
return Arrays.binarySearch(elements, beginIndex, endIndex, value);
}

@Override
public int binarySearch(int beginIndex, int endIndex, E value, Comparator<? super E> comparator) {
Conditions.checkPositionIndices(beginIndex, endIndex, elements.length);
public int binarySearch(@Index int beginIndex, @Index int endIndex, E value, Comparator<? super E> comparator) {
beginIndex = Indexes.checkBeginIndex(beginIndex, elements.length);
endIndex = Indexes.checkEndIndex(beginIndex, endIndex, elements.length);
return Arrays.binarySearch((E[]) elements, beginIndex, endIndex, value, comparator);
}

Expand Down
40 changes: 13 additions & 27 deletions kala-collection/src/main/java/kala/collection/SeqLike.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
import kala.collection.internal.SeqIterators;
import kala.collection.internal.view.SeqViews;
import kala.collection.mutable.MutableSeq;
import kala.comparator.Comparators;
import kala.control.Option;
import kala.function.IndexedBiConsumer;
import kala.function.IndexedFunction;
import kala.index.Index;
import kala.index.Indexes;
import kala.internal.ComparableUtils;
import kala.tuple.Tuple2;
import org.intellij.lang.annotations.Flow;
import org.jetbrains.annotations.Contract;
Expand Down Expand Up @@ -106,48 +109,31 @@ default E get(@Index int index) {
@Contract(pure = true)
@DelegateBy("binarySearch(int, int, E)")
default int binarySearch(E value) {
return binarySearch(0, size(), value);
return binarySearch(0, ~0, value);
}

@Contract(pure = true)
@DelegateBy("binarySearch(int, int, E, Comparator<E>)")
default int binarySearch(E value, Comparator<? super E> comparator) {
return binarySearch(0, size(), value, comparator);
return binarySearch(0, ~0, value, comparator);
}

@Contract(pure = true)
@SuppressWarnings("unchecked")
default int binarySearch(int beginIndex, int endIndex, E value) {
Conditions.checkPositionIndices(beginIndex, endIndex, size());

int low = beginIndex;
int high = endIndex - 1;

while (low <= high) {
final int mid = (low + high) >>> 1;
final E midVal = get(mid);
final int cmp = ((Comparable<E>) midVal).compareTo(value);
if (cmp < 0) {
low = mid + 1;
} else if (cmp > 0) {
high = mid - 1;
} else {
return mid;
}
}
return -(low + 1);
@DelegateBy("binarySearch(int, int, E, Comparator<E>)")
default int binarySearch(@Index int beginIndex, @Index int endIndex, E value) {
return binarySearch(beginIndex, endIndex, value, Comparators.naturalOrder());
}

@Contract(pure = true)
default int binarySearch(int beginIndex, int endIndex, E value, Comparator<? super E> comparator) {
default int binarySearch(@Index int beginIndex, @Index int endIndex, E value, Comparator<? super E> comparator) {
if (comparator == null) {
return binarySearch(beginIndex, endIndex, value);
comparator = Comparators.naturalOrder();
}

Conditions.checkPositionIndices(beginIndex, endIndex, size());
final int size = size();

int low = beginIndex;
int high = endIndex - 1;
int low = Indexes.checkBeginIndex(beginIndex, size);
int high = Indexes.checkEndIndex(low, endIndex, size) - 1;

while (low <= high) {
final int mid = (low + high) >>> 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,16 @@ private void appendThis() {
//region Search Operations

@Override
public int binarySearch(int beginIndex, int endIndex, E value) {
Conditions.checkPositionIndices(beginIndex, endIndex, size);
public int binarySearch(@Index int beginIndex, @Index int endIndex, E value) {
beginIndex = Indexes.checkBeginIndex(beginIndex, size);
endIndex = Indexes.checkEndIndex(beginIndex, endIndex, size);
return Arrays.binarySearch(elements, beginIndex, endIndex, value);
}

@Override
public int binarySearch(int beginIndex, int endIndex, E value, Comparator<? super E> comparator) {
Conditions.checkPositionIndices(beginIndex, endIndex, size);
public int binarySearch(@Index int beginIndex, @Index int endIndex, E value, Comparator<? super E> comparator) {
beginIndex = Indexes.checkBeginIndex(beginIndex, size);
endIndex = Indexes.checkEndIndex(beginIndex, endIndex, size);
return Arrays.binarySearch((E[]) elements, beginIndex, endIndex, value, comparator);
}

Expand Down

0 comments on commit 629a26a

Please sign in to comment.