BitDels delete a group of bits at the specified offsets in the bitmap If the key does not exist, it returns an error There is room for optimization
(key string, offsets ...uint)
| 277 | // If the key does not exist, it returns an error |
| 278 | // There is room for optimization |
| 279 | func (b *BitMapStructure) BitDels(key string, offsets ...uint) error { |
| 280 | // Check the parameters |
| 281 | if len(offsets) == 0 { |
| 282 | return ErrInvalidValue |
| 283 | } |
| 284 | |
| 285 | bitmap, length, err := b.getBitmapFromDB(key, false) |
| 286 | if err != nil { |
| 287 | return err |
| 288 | } |
| 289 | |
| 290 | sort.Slice(offsets, func(i, j int) bool { |
| 291 | return offsets[i] < offsets[j] |
| 292 | }) |
| 293 | |
| 294 | // Number of deletions so far |
| 295 | cntDel := uint(0) |
| 296 | // Current offset to delete |
| 297 | offsetIndex := 0 |
| 298 | |
| 299 | length -= uint(len(offsets)) |
| 300 | for i := uint(0); i < length; i++ { |
| 301 | if offsetIndex < len(offsets) && i+cntDel == offsets[offsetIndex] { |
| 302 | cntDel++ |
| 303 | offsetIndex++ |
| 304 | i-- |
| 305 | continue |
| 306 | } |
| 307 | b.setBit(bitmap, i, b.getBit(bitmap, i+cntDel)) |
| 308 | } |
| 309 | |
| 310 | return b.setBitmapToDB(key, bitmap, length) |
| 311 | } |
| 312 | |
| 313 | var ( |
| 314 | // ErrListEmpty is returned if the list is empty. |