Skip to content

Commit

Permalink
fixed asIndexArray data reallocation issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudenw committed Jul 29, 2023
1 parent 06051a5 commit 257d8a9
Showing 1 changed file with 3 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
@FunctionalInterface
public interface IndexProducer {

int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

/**
* Each index is passed to the predicate. The predicate is applied to each
* index value, if the predicate returns {@code false} the execution is stopped, {@code false}
Expand Down Expand Up @@ -124,11 +126,7 @@ class Indices {

boolean add(final int index) {
if (size == data.length) {
// This will throw an out-of-memory error if there are too many bits.
// Since bits are addressed using 32-bit signed integer indices
// the maximum length should be ~2^31 / 2^6 = ~2^25.
// Any more is a broken implementation.
data = Arrays.copyOf(data, size * 2);
data = Arrays.copyOf(data, (int) Math.min(MAX_ARRAY_SIZE, size * 2L));
}
data[size++] = index;
return true;
Expand Down

0 comments on commit 257d8a9

Please sign in to comment.