Skip to content

Commit

Permalink
Implement Count for bitArray and sparseBitArray
Browse files Browse the repository at this point in the history
  • Loading branch information
danielway-wk committed May 16, 2023
1 parent d95ee5d commit 64dafb7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 9 additions & 0 deletions bitarray/bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ func (ba *bitArray) ClearBit(k uint64) error {
return nil
}

// Count returns the number of set bits in this array.
func (ba *bitArray) Count() uint64 {
count := 0
for _, block := range ba.blocks {
count += bits.OnesCount64(uint64(block))
}
return uint64(count)
}

// Or will bitwise or two bit arrays and return a new bit array
// representing the result.
func (ba *bitArray) Or(other BitArray) BitArray {
Expand Down
14 changes: 13 additions & 1 deletion bitarray/sparse_bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ limitations under the License.

package bitarray

import "sort"
import (
"math/bits"
"sort"
)

// uintSlice is an alias for a slice of ints. Len, Swap, and Less
// are exported to fulfill an interface needed for the search
Expand Down Expand Up @@ -243,6 +246,15 @@ func (sba *sparseBitArray) Equals(other BitArray) bool {
return true
}

// Count returns the number of set bits in this array.
func (sba *sparseBitArray) Count() uint64 {
count := 0
for _, block := range sba.blocks {
count += bits.OnesCount64(uint64(block))
}
return uint64(count)
}

// Or will perform a bitwise or operation with the provided bitarray and
// return a new result bitarray.
func (sba *sparseBitArray) Or(other BitArray) BitArray {
Expand Down

0 comments on commit 64dafb7

Please sign in to comment.