diff options
author | nimelehin <nimelehin@gmail.com> | 2020-02-29 19:11:54 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-06 08:27:39 +0200 |
commit | 8137ef62529470a8b9c9f8fe0120e69cbc8d8467 (patch) | |
tree | e1430ec4c71e8fe16099d22837e493124714cf09 | |
parent | 73901c9b2bd08efb2bc9a7cfdf2bb1d6b11f2e2f (diff) | |
download | serenity-8137ef62529470a8b9c9f8fe0120e69cbc8d8467.zip |
AK: Add Bitmap::find_first_fit()
Add find_first_fit() which implements first fit algorithm.
-rw-r--r-- | AK/Bitmap.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/AK/Bitmap.h b/AK/Bitmap.h index b285796ea9..d4de7f878a 100644 --- a/AK/Bitmap.h +++ b/AK/Bitmap.h @@ -203,6 +203,32 @@ public: 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() : m_size(0) , m_owned(true) |