Skip to content

Commit

Permalink
Add method ImmutableBitSet.stream()
Browse files Browse the repository at this point in the history
  • Loading branch information
julianhyde committed Jan 14, 2025
1 parent 2f096ff commit a601040
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
18 changes: 17 additions & 1 deletion core/src/main/java/org/apache/calcite/util/ImmutableBitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;
import java.util.stream.Collector;
import java.util.stream.IntStream;

import static org.apache.calcite.linq4j.Nullness.castNonNull;

Expand Down Expand Up @@ -76,7 +77,7 @@ public class ImmutableBitSet
public static final Ordering<ImmutableBitSet> ORDERING =
Ordering.from(COMPARATOR);

// BitSets are packed into arrays of "words." Currently a word is
// BitSets are packed into arrays of "words." Currently, a word is
// a long, which consists of 64 bits, requiring 6 address bits.
// The choice of word size is determined purely by performance concerns.
private static final int ADDRESS_BITS_PER_WORD = 6;
Expand Down Expand Up @@ -142,6 +143,8 @@ public static ImmutableBitSet of(int... bits) {
return new ImmutableBitSet(words);
}

/** Creates an ImmutableBitSet whose contents are the bits denoted by a
* given collection of integers. */
public static ImmutableBitSet of(Iterable<Integer> bits) {
if (bits instanceof ImmutableBitSet) {
return (ImmutableBitSet) bits;
Expand Down Expand Up @@ -514,6 +517,19 @@ public int size() {
}
}

/**
* Returns a stream of indices for which this {@code ImmutableBitSet}
* contains a bit in the set state. The indices are returned
* in order, from lowest to highest. The size of the stream
* is the number of bits in the set state, equal to the value
* returned by the {@link #cardinality()} method.
*
* @return a stream of integers representing set indices
*/
public IntStream stream() {
return toList().stream().mapToInt(i -> i);
}

/**
* Returns the index of the first bit that is set to {@code true}
* that occurs on or after the specified starting index. If no such
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -123,6 +124,16 @@ private void assertToIterBitSet(String expected, ImmutableBitSet bitSet) {
buf.append(i);
}
assertThat(buf, hasToString(expected));

// Now check that bitSet.stream() does the same as bitSet.iterator().
buf.setLength(0);
bitSet.stream().forEach(i -> {
if (buf.length() > 0) {
buf.append(", ");
}
buf.append(i);
});
assertThat(buf, hasToString(expected));
}

/**
Expand All @@ -133,6 +144,16 @@ private void assertToIterBitSet(String expected, ImmutableBitSet bitSet) {
check((bitSet, list) -> assertThat(bitSet.toList(), equalTo(list)));
}

/**
* Tests the method
* {@link org.apache.calcite.util.ImmutableBitSet#stream()}.
*/
@Test void testStream() {
check((bitSet, list) ->
assertThat(bitSet.stream().boxed().collect(Collectors.toList()),
equalTo(list)));
}

/**
* Tests the method
* {@link org.apache.calcite.util.ImmutableBitSet#forEachInt}.
Expand Down

0 comments on commit a601040

Please sign in to comment.