Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
62template <typename Key, typename T>
64 private:
65 using GenericIterator = containers::internal::GenericAATree::iterator;
67 using Compare = typename Tree::Compare;
68 using GetKey = typename Tree::GetKey;
69
70 public:
75 using Item = typename Tree::Item;
76 using Pair = typename Tree::Pair;
77
78 using key_type = Key;
79 using mapped_type = std::remove_cv_t<T>;
80 using value_type = Item;
81 using size_type = std::size_t;
82 using difference_type = std::ptrdiff_t;
83 using key_compare = Compare;
84 using reference = value_type&;
85 using const_reference = const value_type&;
86 using pointer = value_type*;
87 using const_pointer = const value_type*;
88
89 public:
90 class iterator : public containers::internal::AATreeIterator<T> {
91 public:
92 constexpr iterator() = default;
93
94 private:
95 friend IntrusiveMap;
96 constexpr explicit iterator(GenericIterator iter)
97 : containers::internal::AATreeIterator<T>(iter) {}
98 };
99
101 : public containers::internal::AATreeIterator<std::add_const_t<T>> {
102 public:
103 constexpr const_iterator() = default;
104
105 private:
106 friend IntrusiveMap;
107 constexpr explicit const_iterator(GenericIterator iter)
108 : containers::internal::AATreeIterator<std::add_const_t<T>>(iter) {}
109 };
110
111 using reverse_iterator = std::reverse_iterator<iterator>;
112 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
113
115 constexpr explicit IntrusiveMap() : IntrusiveMap(std::less<Key>()) {}
116
124 template <typename Comparator>
125 constexpr explicit IntrusiveMap(Comparator compare)
126 : IntrusiveMap(std::move(compare), [](const T& t) { return t.key(); }) {}
127
134 template <typename Comparator, typename KeyRetriever>
135 constexpr IntrusiveMap(Comparator&& compare, KeyRetriever&& get_key)
136 : tree_(true,
137 std::forward<Comparator>(compare),
138 std::forward<KeyRetriever>(get_key)) {
139 CheckItemType();
140 }
141
146 template <typename Iterator, typename... Functors>
147 IntrusiveMap(Iterator first, Iterator last, Functors&&... functors)
148 : IntrusiveMap(std::forward<Functors>(functors)...) {
149 tree_.insert(first, last);
150 }
151
154 template <typename... Functors>
155 explicit IntrusiveMap(std::initializer_list<T*> items, Functors&&... functors)
156 : IntrusiveMap(
157 items.begin(), items.end(), std::forward<Functors>(functors)...) {}
158
159 // Element access
160
164 T& at(const key_type& key) {
165 auto iter = tree_.find(key);
166 PW_DASSERT(iter != tree_.end());
167 return static_cast<T&>(*iter);
168 }
169
170 const T& at(const key_type& key) const {
171 auto iter = tree_.find(key);
172 PW_DASSERT(iter != tree_.end());
173 return static_cast<const T&>(*iter);
174 }
175
176 // Iterators
177
178 iterator begin() noexcept { return iterator(tree_.begin()); }
179 const_iterator begin() const noexcept {
180 return const_iterator(tree_.begin());
181 }
182 const_iterator cbegin() const noexcept { return begin(); }
183
184 iterator end() noexcept { return iterator(tree_.end()); }
185 const_iterator end() const noexcept { return const_iterator(tree_.end()); }
186 const_iterator cend() const noexcept { return end(); }
187
188 reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
189 const_reverse_iterator rbegin() const noexcept {
190 return const_reverse_iterator(end());
191 }
192 const_reverse_iterator crbegin() const noexcept { return rbegin(); }
193
194 reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
195 const_reverse_iterator rend() const noexcept {
196 return const_reverse_iterator(begin());
197 }
198 const_reverse_iterator crend() const noexcept { return rend(); }
199
200 // Capacity
201
203 [[nodiscard]] bool empty() const noexcept { return tree_.empty(); }
204
206 size_t size() const { return tree_.size(); }
207
211 constexpr size_t max_size() const noexcept { return tree_.max_size(); }
212
213 // Modifiers
214
218 void clear() { tree_.clear(); }
219
227 std::pair<iterator, bool> insert(T& item) {
228 auto result = tree_.insert(item);
229 return std::make_pair(iterator(result.first), result.second);
230 }
231
232 iterator insert(iterator, T& item) {
233 // Disregard the hint.
234 return insert(item).first;
235 }
236
237 template <class Iterator>
238 void insert(Iterator first, Iterator last) {
239 tree_.insert(first, last);
240 }
241
242 void insert(std::initializer_list<T*> ilist) {
243 tree_.insert(ilist.begin(), ilist.end());
244 }
245
250 iterator erase(T& item) { return iterator(tree_.erase_one(item)); }
251
252 iterator erase(iterator pos) { return iterator(tree_.erase_one(*pos)); }
253
254 iterator erase(iterator first, iterator last) {
255 return iterator(tree_.erase_range(*first, *last));
256 }
257
258 size_t erase(const key_type& key) { return tree_.erase_all(key); }
259
261 void swap(IntrusiveMap<Key, T>& other) { tree_.swap(other.tree_); }
262
264 template <typename MapType>
265 void merge(MapType& other) {
266 tree_.merge(other.tree_);
267 }
268
272 size_t count(const key_type& key) const { return tree_.count(key); }
273
276 iterator find(const key_type& key) { return iterator(tree_.find(key)); }
277
278 const_iterator find(const key_type& key) const {
279 return const_iterator(tree_.find(key));
280 }
281
285 std::pair<iterator, iterator> equal_range(const key_type& key) {
286 auto result = tree_.equal_range(key);
287 return std::make_pair(iterator(result.first), iterator(result.second));
288 }
289 std::pair<const_iterator, const_iterator> equal_range(
290 const key_type& key) const {
291 auto result = tree_.equal_range(key);
292 return std::make_pair(const_iterator(result.first),
293 const_iterator(result.second));
294 }
295
298 iterator lower_bound(const key_type& key) {
299 return iterator(tree_.lower_bound(key));
300 }
301 const_iterator lower_bound(const key_type& key) const {
302 return const_iterator(tree_.lower_bound(key));
303 }
304
307 iterator upper_bound(const key_type& key) {
308 return iterator(tree_.upper_bound(key));
309 }
310 const_iterator upper_bound(const key_type& key) const {
311 return const_iterator(tree_.upper_bound(key));
312 }
313
314 private:
315 // Check that T is an Item in a function, since the class T will not be fully
316 // defined when the IntrusiveList<T> class is instantiated.
317 static constexpr void CheckItemType() {
318 using ItemBase = containers::internal::AATreeItem;
319 using IntrusiveItemType =
320 typename containers::internal::IntrusiveItem<ItemBase, T>::Type;
321 static_assert(
322 std::is_base_of<IntrusiveItemType, T>(),
323 "IntrusiveMap items must be derived from IntrusiveMap<Key, T>::Item, "
324 "where T is the item or one of its bases.");
325 }
326
327 // Allow multimaps to access the tree for `merge`.
328 template <typename, typename>
329 friend class IntrusiveMultiMap;
330
331 // The AA tree that stores the map.
332 //
333 // This field is mutable so that it doesn't need const overloads.
334 mutable Tree tree_;
335};
336
337} // namespace pw
Definition: intrusive_map.h:101
Definition: intrusive_map.h:90
Definition: intrusive_map.h:63
void swap(IntrusiveMap< Key, T > &other)
Exchanges this map's items with the other map's items.
Definition: intrusive_map.h:261
T & at(const key_type &key)
Definition: intrusive_map.h:164
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition: intrusive_map.h:285
IntrusiveMap(Iterator first, Iterator last, Functors &&... functors)
Definition: intrusive_map.h:147
iterator lower_bound(const key_type &key)
Definition: intrusive_map.h:298
size_t count(const key_type &key) const
Definition: intrusive_map.h:272
constexpr IntrusiveMap(Comparator &&compare, KeyRetriever &&get_key)
Definition: intrusive_map.h:135
iterator upper_bound(const key_type &key)
Definition: intrusive_map.h:307
constexpr IntrusiveMap(Comparator compare)
Definition: intrusive_map.h:125
typename Tree::Item Item
Definition: intrusive_map.h:75
std::pair< iterator, bool > insert(T &item)
Definition: intrusive_map.h:227
iterator erase(T &item)
Definition: intrusive_map.h:250
size_t size() const
Returns the number of items in the map.
Definition: intrusive_map.h:206
iterator find(const key_type &key)
Definition: intrusive_map.h:276
constexpr size_t max_size() const noexcept
Definition: intrusive_map.h:211
IntrusiveMap(std::initializer_list< T * > items, Functors &&... functors)
Definition: intrusive_map.h:155
bool empty() const noexcept
Returns whether the map has zero items or not.
Definition: intrusive_map.h:203
void clear()
Definition: intrusive_map.h:218
constexpr IntrusiveMap()
Constructs an empty map of items.
Definition: intrusive_map.h:115
void merge(MapType &other)
Splices items from the other map into this one.
Definition: intrusive_map.h:265
constexpr iterator begin() noexcept
Returns a pointer to the first item, if any.
Definition: aa_tree.h:60
constexpr size_t max_size() const noexcept
Definition: aa_tree.h:81
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:73
constexpr iterator end() noexcept
Returns a pointer to the last item, if any.
Definition: aa_tree.h:65
iterator erase_range(AATreeItem &first, AATreeItem &last)
iterator lower_bound(Key key)
Definition: aa_tree.h:398
std::pair< iterator, iterator > equal_range(Key key)
Definition: aa_tree.h:393
iterator upper_bound(Key key)
Definition: aa_tree.h:422
void merge(KeyedAATree< K > &other)
Definition: aa_tree.h:368
iterator find(Key key)
Definition: aa_tree.h:383
std::pair< iterator, bool > insert(AATreeItem &item)
Definition: aa_tree.h:304
iterator erase_one(AATreeItem &item)
size_t erase_all(Key key)
Definition: aa_tree.h:357
size_t count(Key key)
Definition: aa_tree.h:378
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27