Add Reset method to Bitmap.

Useful for fully clearing bitmap efficiently without reallocation.

PiperOrigin-RevId: 646125349
This commit is contained in:
Konstantin Bogomolov
2024-06-24 09:42:41 -07:00
committed by gVisor bot
parent cd3efc6519
commit a967130bae
2 changed files with 15 additions and 0 deletions
+8
View File
@@ -250,6 +250,14 @@ func (b *Bitmap) FlipRange(begin, end uint32) {
}
}
// Reset zeroes the entire bitmap.
func (b *Bitmap) Reset() {
b.numOnes = 0
for i := range b.bitBlock {
b.bitBlock[i] = 0
}
}
// ForEach calls `f` for each set bit in the range [start, end).
//
// If f returns false, ForEach stops the iteration.
+7
View File
@@ -292,6 +292,13 @@ func TestBitmapNumOnes(t *testing.T) {
if bitmapOnes != uint32(20) {
t.Errorf("After ClearRange, GetNumOnes() returns: %v, wanted: %v", bitmapOnes, 20)
}
// Reset to fully clear bitmap.
bitmap.Reset()
bitmapOnes = bitmap.GetNumOnes()
if bitmapOnes != uint32(0) {
t.Errorf("After Reset, GetNumOnes() returns: %v, wanted: %v", bitmapOnes, 0)
}
}
type bitmapForEachTestcase struct {