libhatchet
Loading...
Searching...
No Matches
hxconstexpr_list.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
11
12#include "libhatchet.h"
13
14// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
15#if HX_USE_MACROS_WITH_MODULE
16#error Header does not provide macros alone.
17#endif
18
19#include "hxptr.hpp"
20#include "hxutility.h"
21
22HX_NS_BEGIN_
23
32public:
34 hxconstexpr hxconstexpr_list_node(void) : m_list_prev_(hxnull), m_list_next_(hxnull) { }
35
38
41 hxassertmsg(this != &other_, "self_assignment"); (void)other_;
42 return *this;
43 }
44
45private:
47 template<typename, typename> friend class hxconstexpr_list;
48 hxconstexpr_list_node* m_list_prev_;
49 hxconstexpr_list_node* m_list_next_;
51};
52
80template<typename T_, typename deleter_t_=hxdefault_delete>
81class hxconstexpr_list : private deleter_t_ {
82public:
85 using node_t = T_;
86
89 public:
90 // GCOVR_EXCL_START
92 hxconstexpr const_iterator(void) : m_current_node_(hxnull)
94 , m_sentinel_(hxnull)
95#endif
96 { }
97 // GCOVR_EXCL_STOP
110 hxconstexpr bool operator==(const const_iterator& x_) const;
111#if HX_CPLUSPLUS < 202002L // C++20 defaults != from ==.
114 hxconstexpr bool operator!=(const const_iterator& x_) const { return !(*this == x_); }
115#endif
117 hxconstexpr const T_& operator*(void) const {
118 hxassertmsg(this->m_current_node_ != hxnull
119 && this->m_current_node_ != this->m_sentinel_, "invalid_iterator");
120 return *static_cast<const T_*>(m_current_node_);
121 }
122
123 hxconstexpr const T_* operator->(void) const {
124 hxassertmsg(this->m_current_node_ != hxnull
125 && this->m_current_node_ != this->m_sentinel_, "invalid_iterator");
126 return static_cast<const T_*>(m_current_node_);
127 }
128 protected:
130 template<typename, typename> friend class hxconstexpr_list;
132 : m_current_node_(current_)
134 , m_sentinel_(sentinel_)
135#endif
136 { (void)sentinel_; }
137 hxconstexpr_list_node* m_current_node_;
138#if (HX_HARDENING_MODE) == HX_HARDENING_MODE_DEBUG
139 const hxconstexpr_list_node* m_sentinel_;
140#endif
142 };
143
145 class iterator : public const_iterator {
146 public:
147 // GCOVR_EXCL_START
150 // GCOVR_EXCL_STOP
160 hxconstexpr T_& operator*(void) const {
161 hxassertmsg(this->m_current_node_ != hxnull
162 && this->m_current_node_ != this->m_sentinel_, "invalid_iterator");
163 return *static_cast<T_*>(this->m_current_node_);
164 }
165
166 hxconstexpr T_* operator->(void) const {
167 hxassertmsg(this->m_current_node_ != hxnull
168 && this->m_current_node_ != this->m_sentinel_, "invalid_iterator");
169 return static_cast<T_*>(this->m_current_node_);
170 }
171 private:
173 template<typename, typename> friend class hxconstexpr_list;
175 : const_iterator(current_, sentinel_) { }
177 };
178
181 hxconstexpr explicit hxconstexpr_list(deleter_t_ deleter_=deleter_t_());
182
185 hxconstexpr ~hxconstexpr_list(void) { this->clear(this->deleter()); }
186
189
191 hxattr_nodiscard hxconstexpr const T_& back(void) const;
192
194 hxconstexpr iterator begin(void) { return iterator(m_sentinel_.m_list_next_, &m_sentinel_); }
195
198
200 hxconstexpr const_iterator cbegin(void) const { return this->begin(); }
201
203 hxconstexpr const_iterator cend(void) const { return this->end(); }
204
208 template<typename deleter_u_>
209 hxconstexpr void clear(deleter_u_&& deleter_) noexcept;
210
212 hxinline hxconstexpr void clear(void) { this->clear(this->deleter()); }
213
216
219 hxconstexpr iterator end(void) { return iterator(&m_sentinel_, &m_sentinel_); }
220
224
229 template<typename deleter_u_>
230 hxconstexpr void erase(const_iterator pos_, deleter_u_&& deleter_) noexcept;
231
235 hxconstexpr void erase(const_iterator pos_) noexcept;
236
241
245 template<typename callable_t_>
246 hxattr_nodiscard hxconstexpr const_iterator find_if(callable_t_&& callable_) const;
247
249 template<typename callable_t_>
251
254 template<typename callable_t_>
255 hxconstexpr void for_each(callable_t_&& callable_) const;
256
258 template<typename callable_t_>
259 hxconstexpr void for_each(callable_t_&& callable_);
260
262 hxattr_nodiscard hxconstexpr const deleter_t_& deleter(void) const;
263
266
269
271 hxattr_nodiscard hxconstexpr const T_& front(void) const;
272
277 template<typename deleter_u_>
279
285
290 template<typename deleter_u_>
292
298
302
306
310 template<typename deleter_u_>
312
317
321 template<typename deleter_u_>
323
328
333
339 template<typename callable_t_, typename deleter_u_>
340 hxconstexpr hxsize_t remove_if(callable_t_&& callable_, deleter_u_&& deleter_) noexcept;
341
345 template<typename callable_t_>
346 hxconstexpr hxsize_t remove_if(callable_t_&& callable_) noexcept;
347
350
352 hxattr_nodiscard hxconstexpr hxsize_t size(void) const { return m_size_; }
353
359
360private:
361 hxconstexpr_list(const hxconstexpr_list&) = delete;
363 hxconstexpr_list& operator=(const hxconstexpr_list&) = delete;
364 hxconstexpr_list& operator=(hxconstexpr_list&&) = delete;
365
366 hxconstexpr void insert_(hxconstexpr_list_node* prev_, hxconstexpr_list_node* next_,
368 hxconstexpr void extract_(hxconstexpr_list_node* ptr_);
369
370 hxsize_t m_size_;
371 hxconstexpr_list_node m_sentinel_;
372};
373
374#include "detail/hxconstexpr_list.inl"
375HX_NS_END_
const_iterator - Bidirectional iterator over const nodes.
Definition hxconstexpr_list.hpp:88
constexpr bool operator==(const const_iterator &x_) const
Returns true if both iterators point to the same node.
constexpr const_iterator(void)
Constructs an iterator that must not be incremented or dereferenced.
Definition hxconstexpr_list.hpp:92
constexpr const_iterator & operator--(void)
Retreats to the previous node and returns this iterator.
constexpr const_iterator & operator++(void)
Advances to the next node and returns this iterator.
constexpr const T & operator*(void) const
Returns a const reference to the current node.
Definition hxconstexpr_list.hpp:117
constexpr const_iterator operator--(int)
Post-decrement: retreats to the previous node and returns the prior position.
constexpr const T * operator->(void) const
Returns a const pointer to the current node.
Definition hxconstexpr_list.hpp:123
constexpr const_iterator operator++(int)
Post-increment: advances to the next node and returns the prior position.
iterator - Bidirectional iterator over mutable nodes.
Definition hxconstexpr_list.hpp:145
constexpr iterator operator--(int)
Post-decrement: retreats to the previous node and returns the prior position.
constexpr iterator & operator++(void)
Advances to the next node and returns this iterator.
Definition hxconstexpr_list.hpp:152
constexpr iterator & operator--(void)
Retreats to the previous node and returns this iterator.
Definition hxconstexpr_list.hpp:156
constexpr T & operator*(void) const
Returns a mutable reference to the current node.
Definition hxconstexpr_list.hpp:160
constexpr iterator operator++(int)
Post-increment: advances to the next node and returns the prior position.
constexpr iterator(void)
Constructs an iterator that must not be incremented or dereferenced.
Definition hxconstexpr_list.hpp:149
constexpr T * operator->(void) const
Returns a mutable pointer to the current node.
Definition hxconstexpr_list.hpp:166
hxconstexpr_list_node - Intrusive doubly linked list node base.
Definition hxconstexpr_list.hpp:31
constexpr hxconstexpr_list_node(void)
Constructs an unlinked node.
Definition hxconstexpr_list.hpp:34
constexpr hxconstexpr_list_node(const hxconstexpr_list_node &)
Constructs an unlinked node.
Definition hxconstexpr_list.hpp:37
constexpr hxconstexpr_list_node & operator=(const hxconstexpr_list_node &other_)
Assigns nothing. List linkage of either node is not affected.
Definition hxconstexpr_list.hpp:40
hxconstexpr_list - An intrusive doubly linked list that takes ownership of nodes via a deleter_t call...
Definition hxconstexpr_list.hpp:81
constexpr const_iterator find_if(callable_t_ &&callable_) const
Finds the first node for which the predicate returns true.
constexpr iterator insert(const_iterator pos_, hxptr< T, deleter_u_ > &&ptr_)
insert - Inserts the node owned by ptr immediately before pos.
constexpr hxsize_t remove_if(callable_t_ &&callable_) noexcept
Removes all nodes for which predicate returns true, invoking the stored deleter on each removed node.
constexpr hxconstexpr_list(deleter_t_ deleter_=deleter_t_())
Constructs an empty list with an optional deleter instance.
constexpr hxsize_t remove_if(callable_t_ &&callable_, deleter_u_ &&deleter_) noexcept
Removes all nodes for which predicate returns true, invoking deleter on each removed node.
constexpr void for_each(callable_t_ &&callable_) const
Calls a function, lambda, or std::function on each node.
constexpr const T & front(void) const
Returns a const reference to the first node. The list must not be empty.
constexpr bool empty(void) const
Returns true if the list contains no nodes.
constexpr iterator insert(const_iterator pos_, T *ptr_)
insert - Inserts ptr immediately before pos.
constexpr const_iterator begin(void) const
Returns a const iterator to the first node, or end() if the list is empty.
constexpr void reverse(void)
Reverses the order of nodes in the list in-place.
constexpr const_iterator cbegin(void) const
Returns a const iterator to the first node, or cend() if the list is empty.
Definition hxconstexpr_list.hpp:200
constexpr deleter_t_ & deleter(void)
Returns a reference to the stored deleter.
constexpr void erase(const_iterator pos_) noexcept
Unlinks and deletes the node at pos if deleter is true then calls deleter on it.
constexpr iterator find_if(callable_t_ &&callable_)
Non-const version of find_if.
constexpr void for_each(callable_t_ &&callable_)
Non-const version of for_each.
constexpr iterator end(void)
Returns an iterator to the sentinel, representing one past the last node.
Definition hxconstexpr_list.hpp:219
constexpr void release_all(void)
Resets the list to empty without invoking the deleter on any node.
constexpr iterator push_front(T *ptr_)
push_front - Inserts ptr at the front of the list.
constexpr ~hxconstexpr_list(void)
Destroys the list by calling clear(), which invokes the deleter on every remaining node.
Definition hxconstexpr_list.hpp:185
constexpr hxsize_t size(void) const
Returns the number of nodes currently in the list.
Definition hxconstexpr_list.hpp:352
constexpr void clear(void)
Removes all nodes using the stored deleter.
Definition hxconstexpr_list.hpp:212
constexpr const_iterator cend(void) const
Returns a const iterator to the sentinel, representing one past the last node.
Definition hxconstexpr_list.hpp:203
constexpr void clear(deleter_u_ &&deleter_) noexcept
Removes all nodes, invoking deleter on each.
constexpr iterator insert_after(const_iterator pos_, T *ptr_)
insert_after - Inserts ptr immediately after pos.
constexpr iterator push_back(hxptr< T, deleter_u_ > &&ptr_)
push_back - Inserts the node owned by ptr at the back of the list.
T node_t
T - The node type stored in the list.
Definition hxconstexpr_list.hpp:85
constexpr hxptr< T, deleter_t_ > extract(const_iterator pos_)
extract - Returns an hxptr owning the node at pos after unlinking it from the list.
constexpr const_iterator end(void) const
Returns a const iterator to the sentinel, representing one past the last node.
constexpr iterator push_front(hxptr< T, deleter_u_ > &&ptr_)
push_front - Inserts the node owned by ptr at the front of the list.
constexpr T & back(void)
Returns a reference to the last node. The list must not be empty.
constexpr void erase(const_iterator pos_, deleter_u_ &&deleter_) noexcept
Unlinks the node at pos and if deleter is true then calls deleter on it.
constexpr const deleter_t_ & deleter(void) const
Returns a const reference to the stored deleter.
constexpr iterator push_back(T *ptr_)
push_back - Inserts ptr at the back of the list.
constexpr hxptr< T, deleter_t_ > pop_back(void)
pop_back - Returns an hxptr owning the last node.
constexpr const T & back(void) const
Returns a const reference to the last node. The list must not be empty.
constexpr void splice(const_iterator pos_, hxconstexpr_list &other_)
Transfers all nodes from other and inserts them before pos, taking ownership.
constexpr iterator insert_after(const_iterator pos_, hxptr< T, deleter_u_ > &&ptr_)
insert_after - Inserts the node owned by ptr immediately after pos.
constexpr iterator begin(void)
Returns an iterator to the first node, or end() if the list is empty.
Definition hxconstexpr_list.hpp:194
constexpr hxptr< T, deleter_t_ > pop_front(void)
pop_front - Returns an hxptr owning the first node.
constexpr T & front(void)
Returns a reference to the first node. The list must not be empty.
hxptr<T, deleter_t> - A unique owning pointer.
Definition hxptr.hpp:30
A unique owning pointer.
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:120
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:89
#define hxconstexpr
hxconstexpr - Enables C++23 compatable constexpr usage for functions as that has support for destruct...
Definition hxsettings.h:263
A few utility functions and most standard C++ meta programming functions.
Provides core macros, memory management and feature detection.
#define hxassertmsg(x_,...)
hxassertmsg(bool x, ...) - Logs an error and terminates execution if x is false.
Definition libhatchet.h:101
#define hxnull
hxnull - The null pointer value for a given pointer type represented by the numeric constant 0.
Definition libhatchet.h:277
#define HX_HARDENING_MODE
HX_HARDENING_MODE - Library hardening level. See the README.md for levels.
Definition libhatchet.h:51
#define HX_HARDENING_MODE_DEBUG
HX_HARDENING_MODE_DEBUG - Provides comprehensive asserts and verbose output.
Definition libhatchet.h:64
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:209