Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
intrusive_set.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 <type_traits>
17
18#include "pw_containers/internal/aa_tree.h"
19#include "pw_containers/internal/aa_tree_item.h"
20
21namespace pw {
22
60template <typename T>
62 private:
63 using GenericIterator = containers::internal::GenericAATree::iterator;
65 using Compare = typename Tree::Compare;
66 using GetKey = typename Tree::GetKey;
67
68 public:
70 using Item = typename Tree::Item;
71
72 using key_type = T;
73 using value_type = T;
74 using size_type = std::size_t;
75 using difference_type = std::ptrdiff_t;
76 using key_compare = Compare;
77 using value_compare = Compare;
78 using reference = value_type&;
79 using const_reference = const value_type&;
80 using pointer = value_type*;
81 using const_pointer = const value_type*;
82
83 public:
84 class iterator : public containers::internal::AATreeIterator<T> {
85 public:
86 constexpr iterator() = default;
87
88 private:
89 friend IntrusiveSet;
90 constexpr explicit iterator(GenericIterator iter)
91 : containers::internal::AATreeIterator<T>(iter) {}
92 };
93
95 : public containers::internal::AATreeIterator<std::add_const_t<T>> {
96 public:
97 constexpr const_iterator() = default;
98
99 private:
100 friend IntrusiveSet;
101 constexpr explicit const_iterator(GenericIterator iter)
102 : containers::internal::AATreeIterator<std::add_const_t<T>>(iter) {}
103 };
104
105 using reverse_iterator = std::reverse_iterator<iterator>;
106 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
107
109 constexpr explicit IntrusiveSet() : IntrusiveSet(std::less<T>()) {}
110
118 template <typename Comparator>
119 constexpr explicit IntrusiveSet(Comparator compare)
120 : tree_(true, std::move(compare), [](const T& t) -> const T& {
121 return t;
122 }) {
123 CheckItemType();
124 }
125
130 template <typename Iterator, typename... Functors>
131 IntrusiveSet(Iterator first, Iterator last, Functors&&... functors)
132 : IntrusiveSet(std::forward<Functors>(functors)...) {
133 tree_.insert(first, last);
134 }
135
138 template <typename... Functors>
139 IntrusiveSet(std::initializer_list<T*> items, Functors&&... functors)
140 : IntrusiveSet(
141 items.begin(), items.end(), std::forward<Functors>(functors)...) {}
142
143 // Iterators
144
145 iterator begin() noexcept { return iterator(tree_.begin()); }
146 const_iterator begin() const noexcept {
147 return const_iterator(tree_.begin());
148 }
149 const_iterator cbegin() const noexcept { return begin(); }
150
151 iterator end() noexcept { return iterator(tree_.end()); }
152 const_iterator end() const noexcept { return const_iterator(tree_.end()); }
153 const_iterator cend() const noexcept { return end(); }
154
155 reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
156 const_reverse_iterator rbegin() const noexcept {
157 return const_reverse_iterator(end());
158 }
159 const_reverse_iterator crbegin() const noexcept { return rbegin(); }
160
161 reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
162 const_reverse_iterator rend() const noexcept {
163 return const_reverse_iterator(begin());
164 }
165 const_reverse_iterator crend() const noexcept { return rend(); }
166
167 // Capacity
168
170 [[nodiscard]] bool empty() const noexcept { return tree_.empty(); }
171
173 size_t size() const { return tree_.size(); }
174
178 constexpr size_t max_size() const noexcept { return tree_.max_size(); }
179
180 // Modifiers
181
185 void clear() { tree_.clear(); }
186
194 std::pair<iterator, bool> insert(T& item) {
195 auto result = tree_.insert(item);
196 return std::make_pair(iterator(result.first), result.second);
197 }
198
199 iterator insert(iterator, T& item) {
200 // Disregard the hint.
201 return insert(item).first;
202 }
203
204 template <class Iterator>
205 void insert(Iterator first, Iterator last) {
206 tree_.insert(first, last);
207 }
208
209 void insert(std::initializer_list<T*> ilist) {
210 tree_.insert(ilist.begin(), ilist.end());
211 }
212
217 iterator erase(iterator pos) { return iterator(tree_.erase_one(*pos)); }
218
219 iterator erase(iterator first, iterator last) {
220 return iterator(tree_.erase_range(*first, *last));
221 }
222
223 size_t erase(const T& item) { return tree_.erase_all(item); }
224
226 void swap(IntrusiveSet<T>& other) { tree_.swap(other.tree_); }
227
231 template <typename MapType>
232 void merge(MapType& other) {
233 tree_.merge(other.tree_);
234 }
235
239 size_t count(const T& item) const { return tree_.count(item); }
240
243 iterator find(const T& item) { return iterator(tree_.find(item)); }
244
245 const_iterator find(const T& item) const {
246 return const_iterator(tree_.find(item));
247 }
248
252 std::pair<iterator, iterator> equal_range(const T& item) {
253 auto result = tree_.equal_range(item);
254 return std::make_pair(iterator(result.first), iterator(result.second));
255 }
256 std::pair<const_iterator, const_iterator> equal_range(const T& item) const {
257 auto result = tree_.equal_range(item);
258 return std::make_pair(const_iterator(result.first),
259 const_iterator(result.second));
260 }
261
264 iterator lower_bound(const T& item) {
265 return iterator(tree_.lower_bound(item));
266 }
267 const_iterator lower_bound(const T& item) const {
268 return const_iterator(tree_.lower_bound(item));
269 }
270
273 iterator upper_bound(const T& item) {
274 return iterator(tree_.upper_bound(item));
275 }
276 const_iterator upper_bound(const T& item) const {
277 return const_iterator(tree_.upper_bound(item));
278 }
279
280 private:
281 // Check that T is an Item in a function, since the class T will not be fully
282 // defined when the IntrusiveList<T> class is instantiated.
283 static constexpr void CheckItemType() {
284 using ItemBase = containers::internal::AATreeItem;
285 using IntrusiveItemType =
286 typename containers::internal::IntrusiveItem<ItemBase, T>::Type;
287 static_assert(
288 std::is_base_of<IntrusiveItemType, T>(),
289 "IntrusiveSet items must be derived from IntrusiveSet<T>::Item, where "
290 "T is the item or one of its bases.");
291 }
292
293 // Allow sets to access the tree for `merge`.
294 template <typename>
295 friend class IntrusiveMultiSet;
296
297 // The AA tree that stores the set.
298 //
299 // This field is mutable so that it doesn't need const overloads.
300 mutable Tree tree_;
301};
302
303} // namespace pw
Definition: intrusive_set.h:95
Definition: intrusive_set.h:84
Definition: intrusive_set.h:61
IntrusiveSet(Iterator first, Iterator last, Functors &&... functors)
Definition: intrusive_set.h:131
void merge(MapType &other)
Definition: intrusive_set.h:232
void swap(IntrusiveSet< T > &other)
Exchanges this set's items with the other set's items.
Definition: intrusive_set.h:226
typename Tree::Item Item
IntrusiveSet items must derive from Item.
Definition: intrusive_set.h:70
iterator lower_bound(const T &item)
Definition: intrusive_set.h:264
std::pair< iterator, bool > insert(T &item)
Definition: intrusive_set.h:194
iterator find(const T &item)
Definition: intrusive_set.h:243
IntrusiveSet(std::initializer_list< T * > items, Functors &&... functors)
Definition: intrusive_set.h:139
iterator upper_bound(const T &item)
Definition: intrusive_set.h:273
void clear()
Definition: intrusive_set.h:185
size_t size() const
Returns the number of items in the set.
Definition: intrusive_set.h:173
constexpr size_t max_size() const noexcept
Definition: intrusive_set.h:178
std::pair< iterator, iterator > equal_range(const T &item)
Definition: intrusive_set.h:252
size_t count(const T &item) const
Definition: intrusive_set.h:239
constexpr IntrusiveSet()
Constructs an empty set of items.
Definition: intrusive_set.h:109
iterator erase(iterator pos)
Definition: intrusive_set.h:217
bool empty() const noexcept
Returns whether the set has zero items or not.
Definition: intrusive_set.h:170
constexpr IntrusiveSet(Comparator compare)
Definition: intrusive_set.h:119
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