From 73ba2660c024a3746ae449c95153c57d501972d7 Mon Sep 17 00:00:00 2001 From: "daniel.w" Date: Thu, 5 Sep 2024 11:03:52 +0800 Subject: [PATCH] add set all Treu and all False --- utils/bitmap/bitmap.go | 20 ++++++++++++++++++++ utils/bitmap/bitmap_test.go | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/utils/bitmap/bitmap.go b/utils/bitmap/bitmap.go index 5218eef..6ff775b 100644 --- a/utils/bitmap/bitmap.go +++ b/utils/bitmap/bitmap.go @@ -150,3 +150,23 @@ func (b Bitmap) UpdateFrom(newBitmap Bitmap) Bitmap { return b } + +// SetAllTrue 將 Bitmap 中的所有位設置為 true(1)。 +func (b Bitmap) SetAllTrue() Bitmap { + // 將 Bitmap 中的每個 byte 設置為 0xFF,表示所有 bit 都為 1 + for i := range b { + b[i] = 0xFF + } + + return b +} + +// SetAllFalse 將 Bitmap 中的所有位設置為 false(0)。 +func (b Bitmap) SetAllFalse() Bitmap { + // 將 Bitmap 中的每個 byte 設置為 0,表示所有 bit 都為 0 + for i := range b { + b[i] = 0 + } + + return b +} diff --git a/utils/bitmap/bitmap_test.go b/utils/bitmap/bitmap_test.go index d743d38..7af288e 100644 --- a/utils/bitmap/bitmap_test.go +++ b/utils/bitmap/bitmap_test.go @@ -135,3 +135,23 @@ func TestBitmap_UpdateFrom(t *testing.T) { }) } } + +func TestBitmap_SetAllTrueAndSetAllFalse(t *testing.T) { + bitmap := MakeBitmapWithBitSize(128) + + // 測試 SetAllTrue + bitmap.SetAllTrue() + for i := 0; i < bitmap.BitSize(); i++ { + if !bitmap.IsTrue(uint32(i)) { + t.Errorf("Expected bit %d to be true, but got false", i) + } + } + + // 測試 SetAllFalse + bitmap.SetAllFalse() + for i := 0; i < bitmap.BitSize(); i++ { + if bitmap.IsTrue(uint32(i)) { + t.Errorf("Expected bit %d to be false, but got true", i) + } + } +}