Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dnode.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
88# ifndef TPL_DNODE_H
89# define TPL_DNODE_H
90
91# include <type_traits>
92# include <ahFunction.H>
93# include <dlink.H>
94 # include <ah-errors.H>
95
96using namespace Aleph;
97
98namespace Aleph {
99
105template <typename T> class Dnode : public Dlink
106{
107private:
108
110
111public:
112
114 Dnode<T> *& get_next() const noexcept { return (Dnode<T>*&) next; }
115
117 Dnode<T> *& get_prev() const noexcept { return (Dnode<T>*&) prev; }
118
120 Dnode<T>* remove_prev() noexcept { return (Dnode<T>*) Dlink::remove_prev(); }
121
123 Dnode<T>* remove_next() noexcept { return (Dnode<T>*) Dlink::remove_next(); }
124
126 Dnode<T> *& get_first_ne() const noexcept { return get_next(); }
127
129 Dnode<T> *& get_last_ne() const noexcept { return get_prev(); }
130
133 {
134 ah_underflow_error_if(this->is_empty()) << "Dnode is empty";
135 return get_first_ne();
136 }
137
140 {
141 ah_underflow_error_if(this->is_empty()) << "Dnode is empty";
142 return get_last_ne();
143 }
144
147 {
148 return static_cast<Dnode<T>*>(Dlink::remove_prev());
149 }
150
153 {
154 return static_cast<Dnode<T>*>(Dlink::remove_next());
155 }
156
159 {
160 ah_underflow_error_if(this->is_empty()) << "Dnode is empty";
161 return remove_last_ne();
162 }
163
166 {
167 ah_underflow_error_if(this->is_empty()) << "Dnode is empty";
168 return remove_first_ne();
169 }
170
182 Dnode() noexcept(std::is_nothrow_default_constructible_v<T>)
183 : data()
184 {
185 static_assert(std::is_default_constructible<T>::value,
186 "No default constructor for T");
187 }
188
190 Dnode(const T & item) : data(item)
191 {
192 static_assert(std::is_copy_constructible<T>::value,
193 "No copy constructor for T");
194 }
195
197 Dnode(T && item) : data(std::move(item))
198 {
199 static_assert(std::is_move_constructible<T>::value,
200 "No move constructor for T");
201 }
202
205 {
206
207 Dlink::swap(p);
208 std::swap(data, p.data);
209 return *this;
210 }
211
213 Dnode(const Dnode & node)
214 : Dlink(node), data(node.data)
215 {
216 static_assert(std::is_copy_constructible<T>::value,
217 "No copy constructor for T");
218 }
219
225 Dnode(Dnode && node) noexcept requires std::is_nothrow_move_constructible_v<T>
226 : Dlink(std::move(node)), data(std::move(node.data))
227 {
228 /* empty */
229 }
230
243 Dnode(Dnode && node) requires (not std::is_nothrow_move_constructible_v<T>)
244 : data(std::move(node.data))
245 {
246 Dlink::swap(node);
247 }
248
250 Dnode & operator = (const Dnode & p)
251 {
252 static_assert(std::is_copy_assignable<T>::value,
253 "No copy assign for T");
254 data = p.data;
255 return *this;
256 }
257
260 {
261 std::swap(data, p.data);
262 return *this;
263 }
264
266 T & get_data() noexcept { return data; }
267
269 const T & get_data() const noexcept { return data; }
270
272 using key_type = T;
273
275 T & get_key() noexcept { return data; }
276
278 const T & get_key() const noexcept { return data; }
279
282 static Dnode * data_to_node(T & data) noexcept
283 {
284 const size_t offset = __builtin_offsetof(Dnode<T>, data);
285 char * addr = reinterpret_cast<char *>(&data);
286 return reinterpret_cast<Dnode *>(addr - offset);
287 }
288
294 {
295 public:
296
299
302
304
305 using Base::Base;
306
308 Dnode<T> * get_curr_ne() const noexcept
309 {
310 return static_cast<Dnode<T>*>(this->Dlink::Iterator::get_curr_ne());
311 }
312
319 {
320 return static_cast<Dnode<T>*>(this->Dlink::Iterator::get_curr());
321 }
322
332 { return static_cast<Dnode*>(Dlink::Iterator::del()); }
333
335 Dnode * del_ne() noexcept
336 { return static_cast<Dnode*>(Dlink::Iterator::del_ne()); }
337 };
338};
339
340template <typename T> Dnode<T> * Dlink::to_dnode() noexcept
341{
342 return static_cast<Dnode<T>*>(this);
343}
344
345template <typename T> const Dnode<T> * Dlink::to_dnode() const noexcept
346{
347 return static_cast<const Dnode<T>*>(this);
348}
349
350template <typename T> T & Dlink::to_data() noexcept
351{
352 return to_dnode<T>()->get_data();
353}
354
355template <typename T> const T & Dlink::to_data() const noexcept
356{
357 return to_dnode<T>()->get_data();
358}
359
360
361}
362
363# endif /* TPL_DNODE_H */
Exception handling system with formatted messages for Aleph-w.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
Standard functor implementations and comparison objects.
Iterator on a list of Dnode objects.
Definition tpl_dnode.H:294
Dnode< T > * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Definition tpl_dnode.H:308
Dnode * del_ne() noexcept
Same as del() but without exception check.
Definition tpl_dnode.H:335
Dnode * del()
Remove the current node of the list a return a pointer to the removed node.
Definition tpl_dnode.H:331
Dnode< T > * get_curr() const
Return a pointer to the current node.
Definition tpl_dnode.H:318
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
Dnode< T > *& get_next() const noexcept
Return the next node to this
Definition tpl_dnode.H:114
Dnode< T > *& get_first() const
Get the first node.
Definition tpl_dnode.H:132
Dnode< T > * remove_first()
Remove the first node and return its address.
Definition tpl_dnode.H:165
T key_type
The data type.
Definition tpl_dnode.H:272
Dnode< T > * remove_next() noexcept
Remove the next node to this; return its address.
Definition tpl_dnode.H:123
Dnode< T > * remove_first_ne() noexcept
Remove the first node and return its address.
Definition tpl_dnode.H:152
Dnode(Dnode &&node) noexcept
Move constructor: takes the place of node in its list and moves its item, which cannot throw.
Definition tpl_dnode.H:225
Dnode< T > * remove_last()
Remove the last node and return its address.
Definition tpl_dnode.H:158
const T & get_data() const noexcept
Return a modifiable reference to the data contained in the node.
Definition tpl_dnode.H:269
Dnode(Dnode &&node)
Move constructor for an item whose move may throw: moves the item of node and then takes its place in...
Definition tpl_dnode.H:243
Dnode & operator=(const Dnode &p)
Copy assigment.
Definition tpl_dnode.H:250
Dnode & swap(Dnode &p)
Swap this with p
Definition tpl_dnode.H:204
Dnode(T &&item)
Construct a new node with the item moved.
Definition tpl_dnode.H:197
Dnode(const Dnode &node)
Copy constructor.
Definition tpl_dnode.H:213
T & get_key() noexcept
See get_data().
Definition tpl_dnode.H:275
Dnode< T > *& get_first_ne() const noexcept
Get the first node.
Definition tpl_dnode.H:126
Dnode() noexcept(std::is_nothrow_default_constructible_v< T >)
Construct a node, or the header of an empty list, whose item is T(): zero for an arithmetic type or a...
Definition tpl_dnode.H:182
Dnode(const T &item)
Construct a node with a copy of item
Definition tpl_dnode.H:190
Dnode< T > *& get_prev() const noexcept
Return the previous node to this
Definition tpl_dnode.H:117
Dnode< T > * remove_last_ne() noexcept
Remove the last node and return its address.
Definition tpl_dnode.H:146
Dnode< T > *& get_last_ne() const noexcept
Get the last node.
Definition tpl_dnode.H:129
static Dnode * data_to_node(T &data) noexcept
Given an reference to the data in the node, returns a pointer to the Dnode object that contains it.
Definition tpl_dnode.H:282
T & get_data() noexcept
Return a modifiable reference to the data contained in the node.
Definition tpl_dnode.H:266
Dnode< T > * remove_prev() noexcept
Remove the previous node to this; return its address.
Definition tpl_dnode.H:120
Dnode< T > *& get_last() const
Get the last node.
Definition tpl_dnode.H:139
const T & get_key() const noexcept
See get_data().
Definition tpl_dnode.H:278
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
STL namespace.