util/Bitmap: add more utility methods

Change-Id: I021c2fafa01266e8a38c1cb2fd748fd89a4b75bd
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6742
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
X512
2023-07-24 21:00:56 +00:00
committed by waddlesplash
parent ddde98b06d
commit 0235c04759
2 changed files with 67 additions and 1 deletions
+6 -1
View File
@@ -22,7 +22,7 @@ namespace BKernel {
class Bitmap {
public:
Bitmap(size_t bitCount);
Bitmap(size_t bitCount = 0);
~Bitmap();
status_t InitCheck();
@@ -34,6 +34,11 @@ public:
inline void Set(size_t index);
inline void Clear(size_t index);
void SetRange(size_t index, size_t count);
void ClearRange(size_t index, size_t count);
ssize_t GetLowestClear(size_t fromIndex = 0) const;
ssize_t GetLowestContiguousClear(size_t count, size_t fromIndex = 0) const;
ssize_t GetHighestSet() const;
private:
+61
View File
@@ -70,6 +70,67 @@ Bitmap::Shift(ssize_t bitCount)
}
void
Bitmap::SetRange(size_t index, size_t count)
{
// TODO: optimize
for (; count > 0; count--)
Set(index++);
}
void
Bitmap::ClearRange(size_t index, size_t count)
{
// TODO: optimize
for (; count > 0; count--)
Clear(index++);
}
ssize_t
Bitmap::GetLowestClear(size_t fromIndex) const
{
// TODO: optimize
for (size_t i = fromIndex; i < fSize; i++) {
if (!Get(i))
return i;
}
return -1;
}
ssize_t
Bitmap::GetLowestContiguousClear(size_t count, size_t fromIndex) const
{
// TODO: optimize
// nothing to find
if (count == 0)
return fromIndex;
for (;;) {
ssize_t index = GetLowestClear(fromIndex);
if (index < 0)
return index;
// overflow check
if ((size_t)index + count - 1 < (size_t)index)
return -1;
size_t curCount = 1;
while (curCount < count && Get(index + curCount))
curCount++;
if (curCount == count)
return index;
fromIndex = index + curCount;
}
}
ssize_t
Bitmap::GetHighestSet() const
{