gsmiller commented on code in PR #14273:
URL: https://github.com/apache/lucene/pull/14273#discussion_r2017721363
##########
lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java:
##########
@@ -204,6 +205,40 @@ public int cardinality() {
return Math.toIntExact(tot);
}
+ /**
+ * Return the number of set bits between indexes {@code from} inclusive and
{@code to} exclusive.
+ */
+ public int cardinality(int from, int to) {
+ Objects.checkFromToIndex(from, to, length());
+
+ int cardinality = 0;
+
+ // First, align `from` with a word start, ie. a multiple of Long.SIZE (64)
+ if ((from & 0x3F) != 0) {
+ long bits = this.bits[from >> 6] >>> from;
+ int numBitsTilNextWord = -from & 0x3F;
+ if (to - from < numBitsTilNextWord) {
+ bits &= (1L << (to - from)) - 1L;
+ return Long.bitCount(bits);
+ }
+ cardinality += Long.bitCount(bits);
+ from += numBitsTilNextWord;
+ assert (from & 0x3F) == 0;
+ }
+
+ for (int i = from >> 6, end = to >> 6; i < end; ++i) {
+ cardinality += Long.bitCount(bits[i]);
+ }
+
+ // Now handle bits between the last complete word and to
+ if ((to & 0x3F) != 0) {
+ long bits = this.bits[to >> 6] << -to;
Review Comment:
minor: one other small comment. I noticed in the new `forEach` method you
added you use `long bits = this.bits[to >> 6] & ((1L << to) - 1);`. Is the
rationale here that you need to persist the correct number of trailing zeros in
the `forEach` implementation but not in this case since you're doing a bit
count? Is this approach (shifting by `-to`) more performant (I ask since you
could use the same approach as `forEach` here too for consistency, so I assume
you had a reason ;))
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]