Pigweed
 
Loading...
Searching...
No Matches
fast_sorted.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstddef>
17
18#include "pw_allocator/bucket/base.h"
19#include "pw_containers/intrusive_multimap.h"
20#include "pw_function/function.h"
21
22namespace pw::allocator {
23
31template <typename BlockType>
33 : public IntrusiveMultiMap<size_t, FastSortedItem<BlockType>>::Item {
34 public:
35 size_t key() const {
36 const auto* block = BlockType::FromUsableSpace(this);
37 return block->InnerSize();
38 }
39};
40
51 : public IntrusiveMultiMap<size_t, GenericFastSortedItem>::Item {};
52
59template <typename BlockType>
61 : public internal::BucketBase<FastSortedBucket<BlockType>,
62 BlockType,
63 FastSortedItem<BlockType>> {
64 private:
66 BlockType,
68 friend Base;
69
70 template <typename>
71 friend class ReverseFastSortedBucket;
72
73 using Compare = Function<bool(size_t, size_t)>;
74
75 public:
76 constexpr FastSortedBucket() = default;
78
79 private:
80 // Constructor used by `ReverseFastSortedBucket`.
81 constexpr explicit FastSortedBucket(Compare&& compare)
82 : items_(std::move(compare)) {}
83
85 void DoAdd(BlockType& block) {
86 auto* item = new (block.UsableSpace()) FastSortedItem<BlockType>();
87 items_.insert(*item);
88 }
89
91 BlockType* DoRemoveAny() {
92 auto iter = items_.begin();
93 FastSortedItem<BlockType>& item = *iter;
94 items_.erase(iter);
95 return BlockType::FromUsableSpace(&item);
96 }
97
99 bool DoRemove(BlockType& block) {
100 FastSortedItem<BlockType>& item_to_remove = Base::GetItemFrom(block);
101 auto iters = items_.equal_range(block.InnerSize());
102 auto iter =
103 std::find_if(iters.first,
104 iters.second,
105 [&item_to_remove](FastSortedItem<BlockType>& item) {
106 return &item_to_remove == &item;
107 });
108 if (iter == items_.end()) {
109 return false;
110 }
111 items_.erase(iter);
112 return true;
113 }
114
116 BlockType* DoRemoveCompatible(Layout layout) {
117 auto iter = items_.lower_bound(layout.size());
118 return RemoveImpl(iter, layout);
119 }
120
121 template <typename Iterator>
122 BlockType* RemoveImpl(Iterator iter, Layout layout) {
123 iter =
124 std::find_if(iter, items_.end(), Base::MakeCanAllocPredicate(layout));
125 auto* block = Base::GetBlockFromIterator(iter, items_.end());
126 if (block != nullptr) {
127 items_.erase(iter);
128 }
129 return block;
130 }
131
133};
134
138template <typename BlockType>
140 : public internal::BucketBase<ReverseFastSortedBucket<BlockType>,
141 BlockType,
142 FastSortedItem<BlockType>> {
143 private:
145 BlockType,
147 friend Base;
148
149 public:
150 constexpr ReverseFastSortedBucket()
151 : impl_(std::greater<>()), items_(impl_.items_) {}
152
153 private:
155 void DoAdd(BlockType& block) { impl_.DoAdd(block); }
156
158 BlockType* DoRemoveAny() {
159 auto iter = items_.begin();
160 FastSortedItem<BlockType>& item = *iter;
161 items_.erase(iter);
162 return BlockType::FromUsableSpace(&item);
163 }
164
166 bool DoRemove(BlockType& block) { return impl_.DoRemove(block); }
167
169 BlockType* DoRemoveCompatible(Layout layout) {
170 return impl_.RemoveImpl(impl_.items_.begin(), layout);
171 }
172
175};
176
177} // namespace pw::allocator
Definition: intrusive_multimap.h:60
Definition: fast_sorted.h:63
Definition: fast_sorted.h:33
Definition: layout.h:56
Definition: fast_sorted.h:142
constexpr BlockType * GetBlockFromIterator(Iterator iter, Iterator last)
Definition: base.h:167
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:74
Definition: fast_sorted.h:51