Bitmap: Add find_region() and find_and_toggle_region()

This commit is contained in:
apio 2023-01-13 18:55:31 +01:00
parent 59db656f25
commit 09dc8bd522
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 39 additions and 0 deletions

View File

@ -39,6 +39,10 @@ class Bitmap
Option<usize> find_and_toggle(bool value, usize begin = 0); Option<usize> find_and_toggle(bool value, usize begin = 0);
Option<usize> find_region(bool value, usize count, usize begin = 0) const;
Option<usize> find_and_toggle_region(bool value, usize count, usize begin = 0);
void clear(bool value); void clear(bool value);
void clear_region(usize start, usize bits, bool value); void clear_region(usize start, usize bits, bool value);

View File

@ -132,3 +132,38 @@ Option<usize> Bitmap::find_and_toggle(bool value, usize begin)
set(index, !value); set(index, !value);
return index; return index;
} }
Option<usize> Bitmap::find_region(bool value, usize count, usize begin) const
{
// FIXME: Optimize this using bit and byte manipulation.
u64 region_bits_found = 0;
u64 region_start = 0;
for (u64 index = begin; index < m_size_in_bytes * 8; index++)
{
if (get(index) != value)
{
region_bits_found = 0;
continue;
}
if (region_bits_found == 0)
{
region_start = index;
region_bits_found++;
}
else
region_bits_found++;
if (region_bits_found == count) return region_start;
}
return {};
}
Option<usize> Bitmap::find_and_toggle_region(bool value, usize count, usize begin)
{
usize index = TRY(find_region(value, count, begin));
clear_region(index, count, !value);
return index;
}