BitCount function counts the number of set bits in the specified range in the bitmap for the provided key
(k string, start, end uint)
| 234 | |
| 235 | // BitCount function counts the number of set bits in the specified range in the bitmap for the provided key |
| 236 | func (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 |