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
213 size_t size() const {
214 return static_cast<size_type>(std::distance(begin(), end()));
215 }
216
218 constexpr size_type max_size() const noexcept { return list_.max_size(); }
219
220 // Modifiers
221
223 void clear() { list_.clear(); }
224
226 iterator insert(iterator pos, T& item) {
227 return iterator(list_.insert_after((--pos).item_, item));
228 }
229
232 template <typename Iterator>
233 iterator insert(iterator pos, Iterator first, Iterator last) {
234 return iterator(list_.insert_after((--pos).item_, first, last));
235 }
236
239 iterator insert(iterator pos, std::initializer_list<T*> items) {
240 return insert(pos, items.begin(), items.end());
241 }
242
244 iterator erase(T& item) { return erase(iterator(&item)); }
245
248 iterator erase(iterator pos) {
249 return iterator(list_.erase_after((--pos).item_));
250 }
251
253 iterator erase(iterator first, iterator last) {
254 return iterator(list_.erase_after((--first).item_, last.item_));
255 }
256
258 void push_back(T& item) { list_.push_back(item); }
259
261 void pop_back() { list_.pop_back(); }
262
264 void push_front(T& item) { list_.push_front(item); }
265
267 void pop_front() { list_.pop_front(); }
268
272 void swap(IntrusiveList<T>& other) noexcept { list_.swap(other.list_); }
273
274 // Operations
275
279 void merge(IntrusiveList<T>& other) {
280 list_.merge(other.list_, [](const ItemBase& a, const ItemBase& b) {
281 return static_cast<const T&>(a) < static_cast<const T&>(b);
282 });
283 }
284
286 template <typename Compare>
287 void merge(IntrusiveList<T>& other, Compare comp) {
288 list_.merge(other.list_, [comp](const ItemBase& a, const ItemBase& b) {
289 return comp(static_cast<const T&>(a), static_cast<const T&>(b));
290 });
291 }
292
295 void splice(iterator pos, IntrusiveList<T>& other) {
296 splice(pos, other, other.begin(), other.end());
297 }
298
301 void splice(iterator pos, IntrusiveList<T>& other, iterator it) {
302 splice(pos, other, it, std::next(it));
303 }
304
307 void splice(iterator pos,
308 IntrusiveList<T>& other,
309 iterator first,
310 iterator last) {
311 list_.splice_after((--pos).item_, other.list_, (--first).item_, last.item_);
312 }
313
315 bool remove(const T& item) { return list_.remove(item); }
316
318 template <typename UnaryPredicate>
319 size_type remove_if(UnaryPredicate pred) {
320 return list_.remove_if([pred](const ItemBase& item) -> bool {
321 return pred(static_cast<const T&>(item));
322 });
323 }
324
326 void reverse() { std::reverse(begin(), end()); }
327
331 size_type unique() {
332 return list_.unique([](const ItemBase& a, const ItemBase& b) -> bool {
333 return static_cast<const T&>(a) == static_cast<const T&>(b);
334 });
335 }
336
338 template <typename BinaryPredicate>
339 size_type unique(BinaryPredicate pred) {
340 return list_.unique([pred](const ItemBase& a, const ItemBase& b) -> bool {
341 return pred(static_cast<const T&>(a), static_cast<const T&>(b));
342 });
343 }
344
348 void sort() {
349 list_.sort([](const ItemBase& a, const ItemBase& b) -> bool {
350 return static_cast<const T&>(a) < static_cast<const T&>(b);
351 });
352 }
353
355 template <typename Compare>
356 void sort(Compare comp) {
357 list_.sort([comp](const ItemBase& a, const ItemBase& b) -> bool {
358 return comp(static_cast<const T&>(a), static_cast<const T&>(b));
359 });
360 }
361
362 private:
363 // Check that T is an Item in a function, since the class T will not be fully
364 // defined when the IntrusiveList<T> class is instantiated.
365 static constexpr void CheckItemType() {
366 using IntrusiveItemType =
367 typename containers::internal::IntrusiveItem<ItemBase, T>::Type;
368 static_assert(
369 std::is_base_of<IntrusiveItemType, T>(),
370 "IntrusiveList items must be derived from IntrusiveList<T>::Item, "
371 "where T is the item or one of its bases.");
372 }
373
375};
376
377} // namespace containers::future
378
379// See b/362348318 and comments on `PW_CONTAINERS_USE_LEGACY_INTRUSIVE_LIST` and
380// `PW_CONTAINERS_INTRUSIVE_LIST_SUPPRESS_DEPRECATION_WARNING`.
381#if PW_CONTAINERS_USE_LEGACY_INTRUSIVE_LIST
382#if PW_CONTAINERS_INTRUSIVE_LIST_SUPPRESS_DEPRECATION_WARNING
383#define PW_CONTAINERS_INTRUSIVE_LIST_DEPRECATED
384#else
385#define PW_CONTAINERS_INTRUSIVE_LIST_DEPRECATED \
386 [[deprecated("See b/362348318 for background and workarounds.")]]
387#endif // PW_CONTAINERS_INTRUSIVE_LIST_SUPPRESS_DEPRECATION_WARNING
388
390template <typename T>
391using IntrusiveList PW_CONTAINERS_INTRUSIVE_LIST_DEPRECATED =
392 containers::internal::LegacyIntrusiveList<T>;
393
394#else // PW_CONTAINERS_USE_LEGACY_INTRUSIVE_LIST
395
397template <typename T>
399
400#endif // PW_CONTAINERS_USE_LEGACY_INTRUSIVE_LIST
401
402} // 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:248
void swap(IntrusiveList< T > &other) noexcept
Definition: intrusive_list.h:272
size_t unique(BinaryPredicate pred)
Definition: intrusive_list.h:290
void push_front(Item &item)
Inserts an element at the beginning of the list.
Definition: intrusive_list.h:118
void merge(IntrusiveList< T > &other)
Definition: intrusive_list.h:279
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:244
iterator insert(iterator pos, T &item)
Inserts the given item before the given position, pos.
Definition: intrusive_list.h:226
void sort(Compare comp)
Definition: intrusive_list.h:317
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:251
void push_front(T &item)
Inserts an element at the beginning of the list.
Definition: intrusive_list.h:264
void push_back(T &item)
Inserts an element at the end of the list.
Definition: intrusive_list.h:258
size_t size() const
Definition: intrusive_list.h:213
void splice(iterator pos, IntrusiveList< T > &other)
Definition: intrusive_list.h:295
size_type remove_if(UnaryPredicate pred)
Definition: intrusive_list.h:319
void clear()
Removes all items from the list.
Definition: intrusive_list.h:223
bool remove(const T &item)
Definition: intrusive_list.h:315
void pop_back()
Removes the last item in the list. The list must not be empty.
Definition: intrusive_list.h:261
void pop_front()
Removes the first item in the list. The list must not be empty.
Definition: intrusive_list.h:121
void splice(iterator pos, IntrusiveList< T > &other, iterator first, iterator last)
Definition: intrusive_list.h:307
void sort(Compare comp)
Definition: intrusive_list.h:356
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:253
IntrusiveList & operator=(IntrusiveList &&)=default
void reverse()
Reverses the order of items in the list.
Definition: intrusive_list.h:326
iterator insert(iterator pos, std::initializer_list< T * > items)
Definition: intrusive_list.h:239
void merge(GenericIntrusiveList< Item > &other, Compare comp)
Definition: intrusive_list.h:190
iterator insert(iterator pos, Iterator first, Iterator last)
Definition: intrusive_list.h:233
T & back()
Reference to the last element in the list. Undefined behavior if empty().
Definition: intrusive_list.h:170
void pop_back()
Removes the last item in the list. The list must not be empty.
Definition: intrusive_list.h:115
void sort()
Definition: intrusive_list.h:348
bool empty() const noexcept
Definition: intrusive_list.h:207
static Item * insert_after(Item *prev, Item &item)
Definition: intrusive_list.h:132
constexpr size_type max_size() const noexcept
Definition: intrusive_list.h:218
static void splice_after(Item *pos, GenericIntrusiveList< Item > &other, Item *first, Item *last)
Definition: intrusive_list.h:208
void clear()
Removes all items from the list.
Definition: intrusive_list.h:105
size_type unique(BinaryPredicate pred)
Definition: intrusive_list.h:339
size_type unique()
Definition: intrusive_list.h:331
void splice(iterator pos, IntrusiveList< T > &other, iterator it)
Definition: intrusive_list.h:301
void merge(IntrusiveList< T > &other, Compare comp)
Definition: intrusive_list.h:287
void push_back(Item &item)
Inserts an element at the end of the list.
Definition: intrusive_list.h:112
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:267
static Item * erase_after(Item *item)
Definition: intrusive_list.h:160
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:398
The Pigweed namespace.
Definition: alignment.h:27