C/C++ API Reference
Loading...
Searching...
No Matches
intrusive_multimap.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
64template <typename Key, typename T>
66 private:
67 using GenericIterator = containers::internal::GenericAATree::iterator;
69 using Compare = typename Tree::Compare;
70 using GetKey = typename Tree::GetKey;
71
72 public:
77 using Item = typename Tree::Item;
78 using Pair = typename Tree::Pair;
79
80 using key_type = Key;
81 using mapped_type = std::remove_cv_t<T>;
82 using value_type = Item;
83 using size_type = std::size_t;
84 using difference_type = std::ptrdiff_t;
85 using key_compare = Compare;
86 using reference = value_type&;
87 using const_reference = const value_type&;
88 using pointer = value_type*;
89 using const_pointer = const value_type*;
90
91 class iterator : public containers::internal::AATreeIterator<T> {
92 public:
93 constexpr iterator() = default;
94
95 private:
96 using Base = containers::internal::AATreeIterator<T>;
97 friend IntrusiveMultiMap;
98 constexpr explicit iterator(GenericIterator iter) : Base(iter) {}
99 };
100
102 : public containers::internal::AATreeIterator<std::add_const_t<T>> {
103 public:
104 constexpr const_iterator() = default;
105
106 private:
107 using Base = containers::internal::AATreeIterator<std::add_const_t<T>>;
108 friend IntrusiveMultiMap;
109 constexpr explicit const_iterator(GenericIterator iter) : Base(iter) {}
110 };
111
112 using reverse_iterator = std::reverse_iterator<iterator>;
113 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
114
116 constexpr explicit IntrusiveMultiMap()
117 : IntrusiveMultiMap(std::less<Key>()) {}
118
128 template <typename Comparator>
129 constexpr explicit IntrusiveMultiMap(Comparator compare)
130 : IntrusiveMultiMap(std::move(compare),
131 [](const T& t) { return t.key(); }) {}
132
139 template <typename Comparator, typename KeyRetriever>
140 constexpr IntrusiveMultiMap(Comparator&& compare, KeyRetriever&& get_key)
141 : tree_(false,
142 std::forward<Comparator>(compare),
143 std::forward<KeyRetriever>(get_key)) {
144 CheckItemType();
145 }
146
151 template <typename Iterator, typename... Functors>
152 IntrusiveMultiMap(Iterator first, Iterator last, Functors&&... functors)
153 : IntrusiveMultiMap(std::forward<Functors>(functors)...) {
154 tree_.insert(first, last);
155 }
156
159 template <typename... Functors>
160 IntrusiveMultiMap(std::initializer_list<T*> items, Functors&&... functors)
162 items.begin(), items.end(), std::forward<Functors>(functors)...) {}
163
164 // Iterators
165
166 iterator begin() noexcept { return iterator(tree_.begin()); }
167 const_iterator begin() const noexcept {
168 return const_iterator(tree_.begin());
169 }
170 const_iterator cbegin() const noexcept { return begin(); }
171
172 iterator end() noexcept { return iterator(tree_.end()); }
173 const_iterator end() const noexcept { return const_iterator(tree_.end()); }
174 const_iterator cend() const noexcept { return end(); }
175
176 reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
177 const_reverse_iterator rbegin() const noexcept {
178 return const_reverse_iterator(end());
179 }
180 const_reverse_iterator crbegin() const noexcept { return rbegin(); }
181
182 reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
183 const_reverse_iterator rend() const noexcept {
184 return const_reverse_iterator(begin());
185 }
186 const_reverse_iterator crend() const noexcept { return rend(); }
187
188 // Capacity
189
191 [[nodiscard]] bool empty() const noexcept { return tree_.empty(); }
192
194 size_t size() const { return tree_.size(); }
195
199 constexpr size_t max_size() const noexcept { return tree_.max_size(); }
200
201 // Modifiers
202
206 void clear() { tree_.clear(); }
207
209 iterator insert(T& item) { return iterator(tree_.insert(item).first); }
210
211 iterator insert(iterator, T& item) {
212 // Disregard the hint.
213 return iterator(insert(item));
214 }
215
216 template <class Iterator>
217 void insert(Iterator first, Iterator last) {
218 tree_.insert(first, last);
219 }
220
221 void insert(std::initializer_list<T*> ilist) {
222 tree_.insert(ilist.begin(), ilist.end());
223 }
224
229 iterator erase(T& item) { return iterator(tree_.erase_one(item)); }
230
231 iterator erase(iterator pos) { return iterator(tree_.erase_one(*pos)); }
232
233 iterator erase(iterator first, iterator last) {
234 return iterator(tree_.erase_range(*first, *last));
235 }
236
237 size_t erase(const Key& key) { return tree_.erase_all(key); }
238
240 void swap(IntrusiveMultiMap<Key, T>& other) { tree_.swap(other.tree_); }
241
243 template <typename MapType>
244 void merge(MapType& other) {
245 tree_.merge(other.tree_);
246 }
247
249 size_t count(const Key& key) const { return tree_.count(key); }
250
253 iterator find(const Key& key) { return iterator(tree_.find(key)); }
254
255 const_iterator find(const Key& key) const {
256 return const_iterator(tree_.find(key));
257 }
258
262 std::pair<iterator, iterator> equal_range(const Key& key) {
263 auto result = tree_.equal_range(key);
264 return std::make_pair(iterator(result.first), iterator(result.second));
265 }
266 std::pair<const_iterator, const_iterator> equal_range(const Key& key) const {
267 auto result = tree_.equal_range(key);
268 return std::make_pair(const_iterator(result.first),
269 const_iterator(result.second));
270 }
271
275 iterator lower_bound(const Key& key) {
276 return iterator(tree_.lower_bound(key));
277 }
278 const_iterator lower_bound(const Key& key) const {
279 return const_iterator(tree_.lower_bound(key));
280 }
281
285 iterator upper_bound(const Key& key) {
286 return iterator(tree_.upper_bound(key));
287 }
288 const_iterator upper_bound(const Key& key) const {
289 return const_iterator(tree_.upper_bound(key));
290 }
291
292 private:
293 // Check that T is an Item in a function, since the class T will not be fully
294 // defined when the IntrusiveList<T> class is instantiated.
295 static constexpr void CheckItemType() {
296 using ItemBase = containers::internal::AATreeItem;
297 using IntrusiveItemType =
298 typename containers::internal::IntrusiveItem<ItemBase, T>::Type;
299 static_assert(
300 std::is_base_of<IntrusiveItemType, T>(),
301 "IntrusiveMultiMap items must be derived from "
302 "IntrusiveMultiMap<Key, T>::Item, where T is the item or one of its "
303 "bases.");
304 }
305
306 // Allow maps to access the tree for `merge`.
307 template <typename, typename>
308 friend class IntrusiveMap;
309
310 // The AA tree that stores the map.
311 //
312 // This field is mutable so that it doesn't need const overloads.
313 mutable Tree tree_;
314};
315
316} // namespace pw
Definition: intrusive_multimap.h:102
Definition: intrusive_multimap.h:91
Definition: intrusive_multimap.h:65
iterator erase_range(AATreeItem &first, AATreeItem &last)
iterator erase_one(AATreeItem &item)
constexpr IntrusiveMultiMap(Comparator &&compare, KeyRetriever &&get_key)
Definition: intrusive_multimap.h:140
iterator insert(T &item)
Adds the given item to the multimap.
Definition: intrusive_multimap.h:209
iterator upper_bound(const Key &key)
Definition: intrusive_multimap.h:285
std::pair< iterator, iterator > equal_range(const Key &key)
Definition: intrusive_multimap.h:262
typename Tree::Item Item
Definition: intrusive_multimap.h:77
constexpr IntrusiveMultiMap()
Constructs an empty map of items.
Definition: intrusive_multimap.h:116
void merge(MapType &other)
Splices items from the other map into this one.
Definition: intrusive_multimap.h:244
iterator lower_bound(const Key &key)
Definition: intrusive_multimap.h:275
IntrusiveMultiMap(Iterator first, Iterator last, Functors &&... functors)
Definition: intrusive_multimap.h:152
IntrusiveMultiMap(std::initializer_list< T * > items, Functors &&... functors)
Definition: intrusive_multimap.h:160
void swap(IntrusiveMultiMap< Key, T > &other)
Exchanges this multimap's items with the other multimap's items.
Definition: intrusive_multimap.h:240
iterator find(const Key &key)
Definition: intrusive_multimap.h:253
size_t count(const Key &key) const
Returns the number of items in the multimap with the given key.
Definition: intrusive_multimap.h:249
size_t size() const
Returns the number of items in the multimap.
Definition: intrusive_multimap.h:194
bool empty() const noexcept
Returns whether the multimap has zero items or not.
Definition: intrusive_multimap.h:191
iterator erase(T &item)
Definition: intrusive_multimap.h:229
void clear()
Definition: intrusive_multimap.h:206
constexpr IntrusiveMultiMap(Comparator compare)
Definition: intrusive_multimap.h:129
constexpr size_t max_size() const noexcept
Definition: intrusive_multimap.h:199
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