C/C++ API Reference
Loading...
Searching...
No Matches
intrusive_map.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 "pw_containers/internal/aa_tree.h"
17#include "pw_containers/internal/aa_tree_item.h"
18
19namespace pw {
20
22
25
67template <typename Key, typename T>
69 private:
70 using GenericIterator = containers::internal::GenericAATree::iterator;
72 using Compare = typename Tree::Compare;
73 using GetKey = typename Tree::GetKey;
74
75 public:
80 using Item = typename Tree::Item;
81 using Pair = typename Tree::Pair;
82
83 using key_type = Key;
84 using mapped_type = std::remove_cv_t<T>;
85 using value_type = Item;
86 using size_type = std::size_t;
87 using difference_type = std::ptrdiff_t;
88 using key_compare = Compare;
89 using reference = value_type&;
90 using const_reference = const value_type&;
91 using pointer = value_type*;
92 using const_pointer = const value_type*;
93
94 public:
95 class iterator : public containers::internal::AATreeIterator<T> {
96 public:
97 constexpr iterator() = default;
98
99 private:
100 friend IntrusiveMap;
101 constexpr explicit iterator(GenericIterator iter)
102 : containers::internal::AATreeIterator<T>(iter) {}
103 };
104
106 : public containers::internal::AATreeIterator<std::add_const_t<T>> {
107 public:
108 constexpr const_iterator() = default;
109
110 private:
111 friend IntrusiveMap;
112 constexpr explicit const_iterator(GenericIterator iter)
113 : containers::internal::AATreeIterator<std::add_const_t<T>>(iter) {}
114 };
115
116 using reverse_iterator = std::reverse_iterator<iterator>;
117 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
118
120 constexpr explicit IntrusiveMap() : IntrusiveMap(std::less<Key>()) {}
121
129 template <typename Comparator>
130 constexpr explicit IntrusiveMap(Comparator compare)
131 : IntrusiveMap(std::move(compare), [](const T& t) { return t.key(); }) {}
132
139 template <typename Comparator, typename KeyRetriever>
140 constexpr IntrusiveMap(Comparator&& compare, KeyRetriever&& get_key)
141 : tree_(true,
142 std::forward<Comparator>(compare),
143 std::forward<KeyRetriever>(get_key)) {
144 CheckItemType();
145 }
146
151 template <typename Iterator, typename... Functors>
152 IntrusiveMap(Iterator first, Iterator last, Functors&&... functors)
153 : IntrusiveMap(std::forward<Functors>(functors)...) {
154 tree_.insert(first, last);
155 }
156
159 template <typename... Functors>
160 explicit IntrusiveMap(std::initializer_list<T*> items, Functors&&... functors)
161 : IntrusiveMap(
162 items.begin(), items.end(), std::forward<Functors>(functors)...) {}
163
164 // Element access
165
169 T& at(const key_type& key) {
170 auto iter = tree_.find(key);
171 PW_DASSERT(iter != tree_.end());
172 return static_cast<T&>(*iter);
173 }
174
175 const T& at(const key_type& key) const {
176 auto iter = tree_.find(key);
177 PW_DASSERT(iter != tree_.end());
178 return static_cast<const T&>(*iter);
179 }
180
181 // Iterators
182
183 iterator begin() noexcept { return iterator(tree_.begin()); }
184 const_iterator begin() const noexcept {
185 return const_iterator(tree_.begin());
186 }
187 const_iterator cbegin() const noexcept { return begin(); }
188
189 iterator end() noexcept { return iterator(tree_.end()); }
190 const_iterator end() const noexcept { return const_iterator(tree_.end()); }
191 const_iterator cend() const noexcept { return end(); }
192
193 reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
194 const_reverse_iterator rbegin() const noexcept {
195 return const_reverse_iterator(end());
196 }
197 const_reverse_iterator crbegin() const noexcept { return rbegin(); }
198
199 reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
200 const_reverse_iterator rend() const noexcept {
201 return const_reverse_iterator(begin());
202 }
203 const_reverse_iterator crend() const noexcept { return rend(); }
204
205 // Capacity
206
208 [[nodiscard]] bool empty() const noexcept { return tree_.empty(); }
209
211 size_t size() const { return tree_.size(); }
212
216 constexpr size_t max_size() const noexcept { return tree_.max_size(); }
217
218 // Modifiers
219
223 void clear() { tree_.clear(); }
224
232 std::pair<iterator, bool> insert(T& item) {
233 auto result = tree_.insert(item);
234 return std::make_pair(iterator(result.first), result.second);
235 }
236
237 iterator insert(iterator, T& item) {
238 // Disregard the hint.
239 return insert(item).first;
240 }
241
242 template <class Iterator>
243 void insert(Iterator first, Iterator last) {
244 tree_.insert(first, last);
245 }
246
247 void insert(std::initializer_list<T*> ilist) {
248 tree_.insert(ilist.begin(), ilist.end());
249 }
250
255 iterator erase(T& item) { return iterator(tree_.erase_one(item)); }
256
257 iterator erase(iterator pos) { return iterator(tree_.erase_one(*pos)); }
258
259 iterator erase(iterator first, iterator last) {
260 return iterator(tree_.erase_range(*first, *last));
261 }
262
263 size_t erase(const key_type& key) { return tree_.erase_all(key); }
264
266 void swap(IntrusiveMap<Key, T>& other) { tree_.swap(other.tree_); }
267
269 template <typename MapType>
270 void merge(MapType& other) {
271 tree_.merge(other.tree_);
272 }
273
277 size_t count(const key_type& key) const { return tree_.count(key); }
278
281 iterator find(const key_type& key) { return iterator(tree_.find(key)); }
282
283 const_iterator find(const key_type& key) const {
284 return const_iterator(tree_.find(key));
285 }
286
290 std::pair<iterator, iterator> equal_range(const key_type& key) {
291 auto result = tree_.equal_range(key);
292 return std::make_pair(iterator(result.first), iterator(result.second));
293 }
294 std::pair<const_iterator, const_iterator> equal_range(
295 const key_type& key) const {
296 auto result = tree_.equal_range(key);
297 return std::make_pair(const_iterator(result.first),
298 const_iterator(result.second));
299 }
300
303 iterator lower_bound(const key_type& key) {
304 return iterator(tree_.lower_bound(key));
305 }
306 const_iterator lower_bound(const key_type& key) const {
307 return const_iterator(tree_.lower_bound(key));
308 }
309
312 iterator upper_bound(const key_type& key) {
313 return iterator(tree_.upper_bound(key));
314 }
315 const_iterator upper_bound(const key_type& key) const {
316 return const_iterator(tree_.upper_bound(key));
317 }
318
319 private:
320 // Check that T is an Item in a function, since the class T will not be fully
321 // defined when the IntrusiveList<T> class is instantiated.
322 static constexpr void CheckItemType() {
323 using ItemBase = containers::internal::AATreeItem;
324 using IntrusiveItemType =
325 typename containers::internal::IntrusiveItem<ItemBase, T>::Type;
326 static_assert(
327 std::is_base_of<IntrusiveItemType, T>(),
328 "IntrusiveMap items must be derived from IntrusiveMap<Key, T>::Item, "
329 "where T is the item or one of its bases.");
330 }
331
332 // Allow multimaps to access the tree for `merge`.
333 template <typename, typename>
334 friend class IntrusiveMultiMap;
335
336 // The AA tree that stores the map.
337 //
338 // This field is mutable so that it doesn't need const overloads.
339 mutable Tree tree_;
340};
341
342} // namespace pw
Definition: intrusive_map.h:106
Definition: intrusive_map.h:95
Definition: intrusive_map.h:68
iterator erase_range(AATreeItem &first, AATreeItem &last)
iterator erase_one(AATreeItem &item)
void swap(IntrusiveMap< Key, T > &other)
Exchanges this map's items with the other map's items.
Definition: intrusive_map.h:266
T & at(const key_type &key)
Definition: intrusive_map.h:169
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition: intrusive_map.h:290
IntrusiveMap(Iterator first, Iterator last, Functors &&... functors)
Definition: intrusive_map.h:152
iterator lower_bound(const key_type &key)
Definition: intrusive_map.h:303
size_t count(const key_type &key) const
Definition: intrusive_map.h:277
constexpr IntrusiveMap(Comparator &&compare, KeyRetriever &&get_key)
Definition: intrusive_map.h:140
iterator upper_bound(const key_type &key)
Definition: intrusive_map.h:312
constexpr IntrusiveMap(Comparator compare)
Definition: intrusive_map.h:130
typename Tree::Item Item
Definition: intrusive_map.h:80
std::pair< iterator, bool > insert(T &item)
Definition: intrusive_map.h:232
iterator erase(T &item)
Definition: intrusive_map.h:255
size_t size() const
Returns the number of items in the map.
Definition: intrusive_map.h:211
iterator find(const key_type &key)
Definition: intrusive_map.h:281
constexpr size_t max_size() const noexcept
Definition: intrusive_map.h:216
IntrusiveMap(std::initializer_list< T * > items, Functors &&... functors)
Definition: intrusive_map.h:160
bool empty() const noexcept
Returns whether the map has zero items or not.
Definition: intrusive_map.h:208
void clear()
Definition: intrusive_map.h:223
constexpr IntrusiveMap()
Constructs an empty map of items.
Definition: intrusive_map.h:120
void merge(MapType &other)
Splices items from the other map into this one.
Definition: intrusive_map.h:270
iterator lower_bound(Key key)
Definition: aa_tree.h:400
constexpr iterator begin() noexcept
Returns a pointer to the first item, if any.
Definition: aa_tree.h:62
constexpr size_t max_size() const noexcept
Definition: aa_tree.h:83
std::pair< iterator, iterator > equal_range(Key key)
Definition: aa_tree.h:395
iterator upper_bound(Key key)
Definition: aa_tree.h:424
void merge(KeyedAATree< K > &other)
Definition: aa_tree.h:370
iterator find(Key key)
Definition: aa_tree.h:385
std::pair< iterator, bool > insert(AATreeItem &item)
Definition: aa_tree.h:306
size_t erase_all(Key key)
Definition: aa_tree.h:359
void swap(GenericAATree &other)
Exchanges this tree's items with the other tree's items.
size_t size() const
Returns the number of items in the tree.
constexpr bool empty() const
Returns whether the tree has zero items or not.
Definition: aa_tree.h:75
size_t count(Key key)
Definition: aa_tree.h:380
constexpr iterator end() noexcept
Returns a pointer to the last item, if any.
Definition: aa_tree.h:67
The Pigweed namespace.
Definition: alignment.h:27