MCPcopy Create free account
hub / github.com/ByteStorage/FlyDB / BitCount

Method BitCount

structure/bitmap.go:236–262  ·  view source on GitHub ↗

BitCount function counts the number of set bits in the specified range in the bitmap for the provided key

(k string, start, end uint)

Source from the content-addressed store, hash-verified

234
235// BitCount function counts the number of set bits in the specified range in the bitmap for the provided key
236func (b *BitmapStructure) BitCount(k string, start, end uint) (uint, error) {
237 // Convert the key to bytes
238 key := stringToBytesWithKey(k)
239 // Get the bits from the database
240 value, err := b.db.Get(key)
241 if err != nil {
242 return 0, err
243 }
244 // Create a new bitset
245 bit := bitset.New(1)
246 // Unmarshal the retrieved bits into the bitset
247 err = bit.UnmarshalBinary(value)
248 if err != nil {
249 return 0, err
250 }
251 // Initialize the count to 0
252 var count uint
253 // Iterate over the set bits in the specified range and increment the count
254 for i, e := bit.NextSet(start); e; i, e = bit.NextSet(i + 1) {
255 if i > end {
256 break
257 }
258 count++
259 }
260 // Return the count
261 return count, nil
262}
263
264// BitOp function performs the specified bit operation on the provided keys and stores
265// the result in the destination key

Callers 2

Calls 3

stringToBytesWithKeyFunction · 0.85
GetMethod · 0.65
UnmarshalBinaryMethod · 0.45

Tested by 2