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
126 template <typename Comparator>
127 constexpr explicit IntrusiveMultiMap(Comparator compare)
128 : IntrusiveMultiMap(std::move(compare),
129 [](const T& t) { return t.key(); }) {}
130
137 template <typename Comparator, typename KeyRetriever>
138 constexpr IntrusiveMultiMap(Comparator&& compare, KeyRetriever&& get_key)
139 : tree_(false,
140 std::forward<Comparator>(compare),
141 std::forward<KeyRetriever>(get_key)) {
142 CheckItemType();
143 }
144
149 template <typename Iterator, typename... Functors>
150 IntrusiveMultiMap(Iterator first, Iterator last, Functors&&... functors)
151 : IntrusiveMultiMap(std::forward<Functors>(functors)...) {
152 tree_.insert(first, last);
153 }
154
157 template <typename... Functors>
158 IntrusiveMultiMap(std::initializer_list<T*> items, Functors&&... functors)
160 items.begin(), items.end(), std::forward<Functors>(functors)...) {}
161
162 // Iterators
163
164 iterator begin() noexcept { return iterator(tree_.begin()); }
165 const_iterator begin() const noexcept {
166 return const_iterator(tree_.begin());
167 }
168 const_iterator cbegin() const noexcept { return begin(); }
169
170 iterator end() noexcept { return iterator(tree_.end()); }
171 const_iterator end() const noexcept { return const_iterator(tree_.end()); }
172 const_iterator cend() const noexcept { return end(); }
173
174 reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
175 const_reverse_iterator rbegin() const noexcept {
176 return const_reverse_iterator(end());
177 }
178 const_reverse_iterator crbegin() const noexcept { return rbegin(); }
179
180 reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
181 const_reverse_iterator rend() const noexcept {
182 return const_reverse_iterator(begin());
183 }
184 const_reverse_iterator crend() const noexcept { return rend(); }
185
186 // Capacity
187
189 [[nodiscard]] bool empty() const noexcept { return tree_.empty(); }
190
192 size_t size() const { return tree_.size(); }
193
197 constexpr size_t max_size() const noexcept { return tree_.max_size(); }
198
199 // Modifiers
200
204 void clear() { tree_.clear(); }
205
207 iterator insert(T& item) { return iterator(tree_.insert(item).first); }
208
209 iterator insert(iterator, T& item) {
210 // Disregard the hint.
211 return iterator(insert(item));
212 }
213
214 template <class Iterator>
215 void insert(Iterator first, Iterator last) {
216 tree_.insert(first, last);
217 }
218
219 void insert(std::initializer_list<T*> ilist) {
220 tree_.insert(ilist.begin(), ilist.end());
221 }
222
227 iterator erase(T& item) { return iterator(tree_.erase_one(item)); }
228
229 iterator erase(iterator pos) { return iterator(tree_.erase_one(*pos)); }
230
231 iterator erase(iterator first, iterator last) {
232 return iterator(tree_.erase_range(first, last));
233 }
234
235 size_t erase(const Key& key) { return tree_.erase_all(key); }
236
238 void swap(IntrusiveMultiMap<Key, T>& other) { tree_.swap(other.tree_); }
239
241 template <typename MapType>
242 void merge(MapType& other) {
243 tree_.merge(other.tree_);
244 }
245
247 size_t count(const Key& key) const { return tree_.count(key); }
248
251 iterator find(const Key& key) { return iterator(tree_.find(key)); }
252
253 const_iterator find(const Key& key) const {
254 return const_iterator(tree_.find(key));
255 }
256
260 std::pair<iterator, iterator> equal_range(const Key& key) {
261 auto result = tree_.equal_range(key);
262 return std::make_pair(iterator(result.first), iterator(result.second));
263 }
264 std::pair<const_iterator, const_iterator> equal_range(const Key& key) const {
265 auto result = tree_.equal_range(key);
266 return std::make_pair(const_iterator(result.first),
267 const_iterator(result.second));
268 }
269
273 iterator lower_bound(const Key& key) {
274 return iterator(tree_.lower_bound(key));
275 }
276 const_iterator lower_bound(const Key& key) const {
277 return const_iterator(tree_.lower_bound(key));
278 }
279
283 iterator upper_bound(const Key& key) {
284 return iterator(tree_.upper_bound(key));
285 }
286 const_iterator upper_bound(const Key& key) const {
287 return const_iterator(tree_.upper_bound(key));
288 }
289
290 private:
291 // Check that T is an Item in a function, since the class T will not be fully
292 // defined when the IntrusiveList<T> class is instantiated.
293 static constexpr void CheckItemType() {
294 using ItemBase = containers::internal::AATreeItem;
295 using IntrusiveItemType =
296 typename containers::internal::IntrusiveItem<ItemBase, T>::Type;
297 static_assert(
298 std::is_base_of<IntrusiveItemType, T>(),
299 "IntrusiveMultiMap items must be derived from "
300 "IntrusiveMultiMap<Key, T>::Item, where T is the item or one of its "
301 "bases.");
302 }
303
304 // Allow maps to access the tree for `merge`.
305 template <typename, typename>
306 friend class IntrusiveMap;
307
308 // The AA tree that stores the map.
309 //
310 // This field is mutable so that it doesn't need const overloads.
311 mutable Tree tree_;
312};
313
314} // 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:138
iterator insert(T &item)
Adds the given item to the multimap.
Definition: intrusive_multimap.h:207
iterator upper_bound(const Key &key)
Definition: intrusive_multimap.h:283
std::pair< iterator, iterator > equal_range(const Key &key)
Definition: intrusive_multimap.h:260
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:242
iterator lower_bound(const Key &key)
Definition: intrusive_multimap.h:273
IntrusiveMultiMap(Iterator first, Iterator last, Functors &&... functors)
Definition: intrusive_multimap.h:150
IntrusiveMultiMap(std::initializer_list< T * > items, Functors &&... functors)
Definition: intrusive_multimap.h:158
void swap(IntrusiveMultiMap< Key, T > &other)
Exchanges this multimap's items with the other multimap's items.
Definition: intrusive_multimap.h:238
iterator find(const Key &key)
Definition: intrusive_multimap.h:251
size_t count(const Key &key) const
Returns the number of items in the multimap with the given key.
Definition: intrusive_multimap.h:247
size_t size() const
Returns the number of items in the multimap.
Definition: intrusive_multimap.h:192
bool empty() const noexcept
Returns whether the multimap has zero items or not.
Definition: intrusive_multimap.h:189
iterator erase(T &item)
Definition: intrusive_multimap.h:227
void clear()
Definition: intrusive_multimap.h:204
constexpr IntrusiveMultiMap(Comparator compare)
Definition: intrusive_multimap.h:127
constexpr size_t max_size() const noexcept
Definition: intrusive_multimap.h:197
iterator lower_bound(Key key)
Definition: aa_tree.h:408
constexpr iterator begin() noexcept
Returns a pointer to the first item, if any.
Definition: aa_tree.h:63
constexpr size_t max_size() const noexcept
Definition: aa_tree.h:84
std::pair< iterator, iterator > equal_range(Key key)
Definition: aa_tree.h:403
iterator upper_bound(Key key)
Definition: aa_tree.h:432
void merge(KeyedAATree< K > &other)
Definition: aa_tree.h:378
iterator find(Key key)
Definition: aa_tree.h:393
std::pair< iterator, bool > insert(AATreeItem &item)
Definition: aa_tree.h:314
size_t erase_all(Key key)
Definition: aa_tree.h:367
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:76
size_t count(Key key)
Definition: aa_tree.h:388
constexpr iterator end() noexcept
Returns a pointer to the last item, if any.
Definition: aa_tree.h:68
The Pigweed namespace.
Definition: alignment.h:27