mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:17:35 +00:00
AK: Add Bitmap::find_first_fit()
Add find_first_fit() which implements first fit algorithm.
This commit is contained in:
parent
73901c9b2b
commit
8137ef6252
1 changed files with 26 additions and 0 deletions
26
AK/Bitmap.h
26
AK/Bitmap.h
|
@ -203,6 +203,32 @@ public:
|
||||||
return first_index;
|
return first_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<size_t> find_first_fit(size_t minimum_length) const
|
||||||
|
{
|
||||||
|
auto first_index = find_first_unset();
|
||||||
|
if (!first_index.has_value())
|
||||||
|
return {};
|
||||||
|
if (minimum_length == 1)
|
||||||
|
return first_index;
|
||||||
|
|
||||||
|
size_t free_region_start = first_index.value();
|
||||||
|
size_t free_region_size = 1;
|
||||||
|
|
||||||
|
// Let's try to find the first fit
|
||||||
|
for (size_t j = first_index.value() + 1; j < m_size; j++) {
|
||||||
|
if (!get(j)) {
|
||||||
|
if (free_region_size == 0)
|
||||||
|
free_region_start = j;
|
||||||
|
if (++free_region_size == minimum_length)
|
||||||
|
return free_region_start;
|
||||||
|
} else {
|
||||||
|
free_region_start = 0;
|
||||||
|
free_region_size = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
Bitmap()
|
Bitmap()
|
||||||
: m_size(0)
|
: m_size(0)
|
||||||
, m_owned(true)
|
, m_owned(true)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue