File tree Expand file tree Collapse file tree 1 file changed +9
-8
lines changed
src/main/java/io/github/abductcows/bitarray Expand file tree Collapse file tree 1 file changed +9
-8
lines changed Original file line number Diff line number Diff line change @@ -540,16 +540,17 @@ private int longsRequiredForNBits(int nBits) {
540
540
public int countOnes () {
541
541
if (isEmpty ()) return 0 ;
542
542
int oneCount = 0 ;
543
- int limit = longsRequiredForNBits (size ()) - 1 ;
543
+ int limit = longsRequiredForNBits (size ()) - 1 ; // all full longs
544
544
545
- oneCount += Arrays .stream (data )
546
- .limit (limit )
547
- .map (Long ::bitCount )
548
- .sum ();
545
+ for (int i = 0 ; i < limit ; i ++) {
546
+ oneCount += Long .bitCount (data [i ]);
547
+ }
548
+
549
+ // last occupied long, not filled
550
+ int remainingBits = elements - limit * BITS_PER_LONG ;
549
551
550
- int remainingBitsIndex = limit * BITS_PER_LONG ;
551
- for (int i = remainingBitsIndex ; i < elements ; i ++) {
552
- if (get (i )) {
552
+ for (int i = 0 ; i < remainingBits ; i ++) {
553
+ if (getBit (limit , i )) {
553
554
oneCount ++;
554
555
}
555
556
}
You can’t perform that action at this time.
0 commit comments