C/C++ API Reference
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
25
33template <typename BlockType>
35 : public IntrusiveMultiMap<size_t, FastSortedItem<BlockType>>::Item {
36 public:
37 size_t key() const {
38 const auto* block = BlockType::FromUsableSpace(this);
39 return block->InnerSize();
40 }
41};
42
53 : public IntrusiveMultiMap<size_t, GenericFastSortedItem>::Item {};
54
61template <typename BlockType>
63 : public internal::BucketBase<FastSortedBucket<BlockType>,
64 BlockType,
65 FastSortedItem<BlockType>> {
66 private:
68 BlockType,
70 friend Base;
71
72 template <typename>
73 friend class ReverseFastSortedBucket;
74
75 using Compare = Function<bool(size_t, size_t)>;
76
77 public:
78 constexpr FastSortedBucket() = default;
80
81 private:
82 // Constructor used by `ReverseFastSortedBucket`.
83 constexpr explicit FastSortedBucket(Compare&& compare)
84 : items_(std::move(compare)) {}
85
87 void DoAdd(BlockType& block) {
88 auto* item = new (block.UsableSpace()) FastSortedItem<BlockType>();
89 items_.insert(*item);
90 }
91
93 const BlockType* DoFindLargest() const {
94 auto iter = items_.end();
95 --iter;
96 return BlockType::FromUsableSpace(&(*iter));
97 }
98
100 BlockType* DoRemoveAny() {
101 auto iter = items_.begin();
102 FastSortedItem<BlockType>& item = *iter;
103 items_.erase(iter);
104 return BlockType::FromUsableSpace(&item);
105 }
106
108 bool DoRemove(BlockType& block) {
109 FastSortedItem<BlockType>& item_to_remove = Base::GetItemFrom(block);
110 auto iters = items_.equal_range(block.InnerSize());
111 auto iter =
112 std::find_if(iters.first,
113 iters.second,
114 [&item_to_remove](FastSortedItem<BlockType>& item) {
115 return &item_to_remove == &item;
116 });
117 if (iter == items_.end()) {
118 return false;
119 }
120 items_.erase(iter);
121 return true;
122 }
123
125 BlockType* DoRemoveCompatible(Layout layout) {
126 auto iter = items_.lower_bound(layout.size());
127 return RemoveImpl(iter, layout);
128 }
129
130 template <typename Iterator>
131 BlockType* RemoveImpl(Iterator iter, Layout layout) {
132 iter =
133 std::find_if(iter, items_.end(), Base::MakeCanAllocPredicate(layout));
134 auto* block = Base::GetBlockFromIterator(iter, items_.end());
135 if (block != nullptr) {
136 items_.erase(iter);
137 }
138 return block;
139 }
140
142};
143
147template <typename BlockType>
149 : public internal::BucketBase<ReverseFastSortedBucket<BlockType>,
150 BlockType,
151 FastSortedItem<BlockType>> {
152 private:
154 BlockType,
156 friend Base;
157
158 public:
159 constexpr ReverseFastSortedBucket()
160 : impl_(std::greater<>()), items_(impl_.items_) {}
161
162 private:
164 void DoAdd(BlockType& block) { impl_.DoAdd(block); }
165
167 const BlockType* DoFindLargest() const {
168 auto iter = impl_.items_.begin();
169 return BlockType::FromUsableSpace(&(*iter));
170 }
171
173 BlockType* DoRemoveAny() {
174 auto iter = items_.begin();
175 FastSortedItem<BlockType>& item = *iter;
176 items_.erase(iter);
177 return BlockType::FromUsableSpace(&item);
178 }
179
181 bool DoRemove(BlockType& block) { return impl_.DoRemove(block); }
182
184 BlockType* DoRemoveCompatible(Layout layout) {
185 return impl_.RemoveImpl(impl_.items_.begin(), layout);
186 }
187
190};
191
193
194} // namespace pw::allocator
Definition: intrusive_multimap.h:65
Definition: fast_sorted.h:65
Definition: fast_sorted.h:35
Definition: layout.h:58
Definition: fast_sorted.h:151
static constexpr BlockType * GetBlockFromIterator(Iterator iter, Iterator last)
Definition: base.h:187
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73
Definition: fast_sorted.h:53