C/C++ API Reference
Loading...
Searching...
No Matches
intrusive_list.h
1// Copyright 2021 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 <algorithm>
17#include <cstddef>
18#include <cstdio>
19#include <initializer_list>
20#include <iterator>
21#include <limits>
22#include <type_traits>
23
24#include "pw_containers/config.h"
25#include "pw_containers/internal/intrusive_item.h"
26#include "pw_containers/internal/intrusive_list.h"
27#include "pw_containers/internal/intrusive_list_item.h"
28#include "pw_containers/internal/intrusive_list_iterator.h"
29#include "pw_containers/internal/legacy_intrusive_list.h"
30#include "pw_containers/intrusive_forward_list.h"
31
32namespace pw {
33
35
36namespace containers::future {
37
40
87template <typename T>
89 private:
90 using ItemBase = ::pw::containers::internal::IntrusiveListItem;
91
92 public:
93 using element_type = T;
94 using value_type = std::remove_cv_t<element_type>;
95 using size_type = std::size_t;
96 using difference_type = std::ptrdiff_t;
97 using reference = value_type&;
98 using const_reference = const value_type&;
99 using pointer = element_type*;
100 using const_pointer = const element_type*;
101
102 class Item : public ItemBase {
103 protected:
109 constexpr explicit Item() = default;
110
111 private:
112 // IntrusiveItem is used to ensure T inherits from this container's Item
113 // type. See also `CheckItemType`.
114 template <typename, typename, bool>
115 friend struct containers::internal::IntrusiveItem;
116 using ItemType = T;
117 };
118
119 using iterator =
120 typename ::pw::containers::internal::BidirectionalIterator<T, ItemBase>;
121 using const_iterator = typename ::pw::containers::internal::
122 BidirectionalIterator<std::add_const_t<T>, const ItemBase>;
123 using reverse_iterator = std::reverse_iterator<iterator>;
124 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
125
126 constexpr IntrusiveList() { CheckItemType(); }
127
128 // Intrusive lists cannot be copied, since each Item can only be in one list.
129 IntrusiveList(const IntrusiveList&) = delete;
130 IntrusiveList& operator=(const IntrusiveList&) = delete;
131
136
141
142 // Constructs an IntrusiveList from an iterator over Items. The iterator may
143 // dereference as either Item& (e.g. from std::array<Item>) or Item* (e.g.
144 // from std::initializer_list<Item*>).
145 template <typename Iterator>
146 IntrusiveList(Iterator first, Iterator last) {
147 list_.assign(first, last);
148 }
149
150 // Constructs an IntrusiveList from a std::initializer_list of pointers to
151 // items.
152 IntrusiveList(std::initializer_list<Item*> items)
153 : IntrusiveList(items.begin(), items.end()) {}
154
155 template <typename Iterator>
156 void assign(Iterator first, Iterator last) {
157 list_.assign(first, last);
158 }
159
160 void assign(std::initializer_list<T*> items) {
161 list_.assign(items.begin(), items.end());
162 }
163
164 // Element access
165
167 T& front() { return *static_cast<T*>(list_.begin()); }
168
170 T& back() { return *static_cast<T*>(list_.before_end()); }
171
172 // Iterators
173
174 iterator begin() noexcept {
175 return iterator(static_cast<Item*>(list_.begin()));
176 }
177 const_iterator begin() const noexcept {
178 return const_iterator(static_cast<const Item*>(list_.begin()));
179 }
180 const_iterator cbegin() const noexcept { return begin(); }
181
182 iterator end() noexcept { return iterator(static_cast<Item*>(list_.end())); }
183 const_iterator end() const noexcept {
184 return const_iterator(static_cast<const Item*>(list_.end()));
185 }
186 const_iterator cend() const noexcept { return end(); }
187
188 reverse_iterator rbegin() { return reverse_iterator(end()); }
189 const_reverse_iterator rbegin() const {
190 return const_reverse_iterator(end());
191 }
192 const_reverse_iterator crbegin() const {
193 return const_reverse_iterator(end());
194 }
195
196 reverse_iterator rend() { return reverse_iterator(begin()); }
197 const_reverse_iterator rend() const {
198 return const_reverse_iterator(begin());
199 }
200 const_reverse_iterator crend() const {
201 return const_reverse_iterator(begin());
202 }
203
204 // Capacity
205
207 [[nodiscard]] bool empty() const noexcept { return list_.empty(); }
208
210 size_t size() const {
211 return static_cast<size_type>(std::distance(begin(), end()));
212 }
213
215 constexpr size_type max_size() const noexcept { return list_.max_size(); }
216
217 // Modifiers
218
220 void clear() { list_.clear(); }
221
223 iterator insert(iterator pos, T& item) {
224 return iterator(list_.insert_after((--pos).item_, item));
225 }
226
229 template <typename Iterator>
230 iterator insert(iterator pos, Iterator first, Iterator last) {
231 return iterator(list_.insert_after((--pos).item_, first, last));
232 }
233
236 iterator insert(iterator pos, std::initializer_list<T*> items) {
237 return insert(pos, items.begin(), items.end());
238 }
239
241 iterator erase(T& item) { return erase(iterator(&item)); }
242
245 iterator erase(iterator pos) {
246 return iterator(list_.erase_after((--pos).item_));
247 }
248
250 iterator erase(iterator first, iterator last) {
251 return iterator(list_.erase_after((--first).item_, last.item_));
252 }
253
255 void push_back(T& item) { list_.insert_after(list_.before_end(), item); }
256
258 void pop_back() { remove(back()); }
259
261 void push_front(T& item) { list_.insert_after(list_.before_begin(), item); }
262
264 void pop_front() { remove(front()); }
265
269 void swap(IntrusiveList<T>& other) noexcept { list_.swap(other.list_); }
270
271 // Operations
272
276 void merge(IntrusiveList<T>& other) {
277 list_.merge(other.list_, [](const ItemBase& a, const ItemBase& b) {
278 return static_cast<const T&>(a) < static_cast<const T&>(b);
279 });
280 }
281
283 template <typename Compare>
284 void merge(IntrusiveList<T>& other, Compare comp) {
285 list_.merge(other.list_, [comp](const ItemBase& a, const ItemBase& b) {
286 return comp(static_cast<const T&>(a), static_cast<const T&>(b));
287 });
288 }
289
292 void splice(iterator pos, IntrusiveList<T>& other) {
293 splice(pos, other, other.begin(), other.end());
294 }
295
298 void splice(iterator pos, IntrusiveList<T>& other, iterator it) {
299 splice(pos, other, it, std::next(it));
300 }
301
304 void splice(iterator pos,
305 IntrusiveList<T>& other,
306 iterator first,
307 iterator last) {
308 list_.splice_after((--pos).item_, other.list_, (--first).item_, last.item_);
309 }
310
312 bool remove(const T& item) { return list_.remove(item); }
313
315 template <typename UnaryPredicate>
316 size_type remove_if(UnaryPredicate pred) {
317 return list_.remove_if([pred](const ItemBase& item) -> bool {
318 return pred(static_cast<const T&>(item));
319 });
320 }
321
323 void reverse() { std::reverse(begin(), end()); }
324
328 size_type unique() {
329 return list_.unique([](const ItemBase& a, const ItemBase& b) -> bool {
330 return static_cast<const T&>(a) == static_cast<const T&>(b);
331 });
332 }
333
335 template <typename BinaryPredicate>
336 size_type unique(BinaryPredicate pred) {
337 return list_.unique([pred](const ItemBase& a, const ItemBase& b) -> bool {
338 return pred(static_cast<const T&>(a), static_cast<const T&>(b));
339 });
340 }
341
345 void sort() {
346 list_.sort([](const ItemBase& a, const ItemBase& b) -> bool {
347 return static_cast<const T&>(a) < static_cast<const T&>(b);
348 });
349 }
350
352 template <typename Compare>
353 void sort(Compare comp) {
354 list_.sort([comp](const ItemBase& a, const ItemBase& b) -> bool {
355 return comp(static_cast<const T&>(a), static_cast<const T&>(b));
356 });
357 }
358
359 private:
360 // Check that T is an Item in a function, since the class T will not be fully
361 // defined when the IntrusiveList<T> class is instantiated.
362 static constexpr void CheckItemType() {
363 using IntrusiveItemType =
364 typename containers::internal::IntrusiveItem<ItemBase, T>::Type;
365 static_assert(
366 std::is_base_of<IntrusiveItemType, T>(),
367 "IntrusiveList items must be derived from IntrusiveList<T>::Item, "
368 "where T is the item or one of its bases.");
369 }
370
372};
373
374} // namespace containers::future
375
376// See b/362348318 and comments on `PW_CONTAINERS_USE_LEGACY_INTRUSIVE_LIST` and
377// `PW_CONTAINERS_INTRUSIVE_LIST_SUPPRESS_DEPRECATION_WARNING`.
378#if PW_CONTAINERS_USE_LEGACY_INTRUSIVE_LIST
379#if PW_CONTAINERS_INTRUSIVE_LIST_SUPPRESS_DEPRECATION_WARNING
380#define PW_CONTAINERS_INTRUSIVE_LIST_DEPRECATED
381#else
382#define PW_CONTAINERS_INTRUSIVE_LIST_DEPRECATED \
383 [[deprecated("See b/362348318 for background and workarounds.")]]
384#endif // PW_CONTAINERS_INTRUSIVE_LIST_SUPPRESS_DEPRECATION_WARNING
385
387template <typename T>
388using IntrusiveList PW_CONTAINERS_INTRUSIVE_LIST_DEPRECATED =
389 containers::internal::LegacyIntrusiveList<T>;
390
391#else // PW_CONTAINERS_USE_LEGACY_INTRUSIVE_LIST
392
394template <typename T>
396
397#endif // PW_CONTAINERS_USE_LEGACY_INTRUSIVE_LIST
398
399} // namespace pw
Definition: intrusive_list.h:102
Definition: intrusive_list.h:88
Definition: intrusive_list.h:38
iterator erase(iterator pos)
Definition: intrusive_list.h:245
void swap(IntrusiveList< T > &other) noexcept
Definition: intrusive_list.h:269
size_t unique(BinaryPredicate pred)
Definition: intrusive_list.h:278
void merge(IntrusiveList< T > &other)
Definition: intrusive_list.h:276
Item * before_end() noexcept
Returns a pointer to the last item.
Definition: intrusive_list.h:85
iterator erase(T &item)
Removes the given item from the list. The item is not destructed.
Definition: intrusive_list.h:241
iterator insert(iterator pos, T &item)
Inserts the given item before the given position, pos.
Definition: intrusive_list.h:223
void sort(Compare comp)
Definition: intrusive_list.h:305
IntrusiveList(IntrusiveList &&)=default
constexpr size_t max_size() const noexcept
Definition: intrusive_list.h:98
size_t remove_if(UnaryPredicate pred, size_t max=std::numeric_limits< size_t >::max())
Definition: intrusive_list.h:239
void push_front(T &item)
Inserts an element at the beginning of the list.
Definition: intrusive_list.h:261
void push_back(T &item)
Inserts an element at the end of the list.
Definition: intrusive_list.h:255
size_t size() const
Definition: intrusive_list.h:210
void splice(iterator pos, IntrusiveList< T > &other)
Definition: intrusive_list.h:292
size_type remove_if(UnaryPredicate pred)
Definition: intrusive_list.h:316
void clear()
Removes all items from the list.
Definition: intrusive_list.h:220
bool remove(const T &item)
Definition: intrusive_list.h:312
void pop_back()
Removes the last item in the list. The list must not be empty.
Definition: intrusive_list.h:258
constexpr Item * before_begin() noexcept
Returns a pointer to the sentinel item.
Definition: intrusive_list.h:77
void splice(iterator pos, IntrusiveList< T > &other, iterator first, iterator last)
Definition: intrusive_list.h:304
void sort(Compare comp)
Definition: intrusive_list.h:353
constexpr Item * begin() noexcept
Returns a pointer to the first item.
Definition: intrusive_list.h:81
iterator erase(iterator first, iterator last)
Removes the range of items from first (inclusive) to last (exclusive).
Definition: intrusive_list.h:250
IntrusiveList & operator=(IntrusiveList &&)=default
void reverse()
Reverses the order of items in the list.
Definition: intrusive_list.h:323
iterator insert(iterator pos, std::initializer_list< T * > items)
Definition: intrusive_list.h:236
void merge(GenericIntrusiveList< Item > &other, Compare comp)
Definition: intrusive_list.h:178
iterator insert(iterator pos, Iterator first, Iterator last)
Definition: intrusive_list.h:230
T & back()
Reference to the last element in the list. Undefined behavior if empty().
Definition: intrusive_list.h:170
void sort()
Definition: intrusive_list.h:345
bool empty() const noexcept
Definition: intrusive_list.h:207
static Item * insert_after(Item *prev, Item &item)
Definition: intrusive_list.h:120
constexpr size_type max_size() const noexcept
Definition: intrusive_list.h:215
static void splice_after(Item *pos, GenericIntrusiveList< Item > &other, Item *first, Item *last)
Definition: intrusive_list.h:196
void clear()
Removes all items from the list.
Definition: intrusive_list.h:105
size_type unique(BinaryPredicate pred)
Definition: intrusive_list.h:336
size_type unique()
Definition: intrusive_list.h:328
void splice(iterator pos, IntrusiveList< T > &other, iterator it)
Definition: intrusive_list.h:298
void merge(IntrusiveList< T > &other, Compare comp)
Definition: intrusive_list.h:284
constexpr Item * end() noexcept
Returns a pointer to the sentinel item.
Definition: intrusive_list.h:88
void pop_front()
Removes the first item in the list. The list must not be empty.
Definition: intrusive_list.h:264
static Item * erase_after(Item *item)
Definition: intrusive_list.h:148
T & front()
Reference to the first element in the list. Undefined behavior if empty().
Definition: intrusive_list.h:167
containers::future::IntrusiveList< T > IntrusiveList
Definition: intrusive_list.h:395
The Pigweed namespace.
Definition: alignment.h:27