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:
@@ -22,7 +22,7 @@ namespace BKernel {
|
|||||||
|
|
||||||
class Bitmap {
|
class Bitmap {
|
||||||
public:
|
public:
|
||||||
Bitmap(size_t bitCount);
|
Bitmap(size_t bitCount = 0);
|
||||||
~Bitmap();
|
~Bitmap();
|
||||||
|
|
||||||
status_t InitCheck();
|
status_t InitCheck();
|
||||||
@@ -34,6 +34,11 @@ public:
|
|||||||
inline void Set(size_t index);
|
inline void Set(size_t index);
|
||||||
inline void Clear(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;
|
ssize_t GetHighestSet() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -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
|
ssize_t
|
||||||
Bitmap::GetHighestSet() const
|
Bitmap::GetHighestSet() const
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user