Aleph-w 3.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
115
118
121
124
127
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
172 {
173 static_assert(std::is_default_constructible<T>::value,
174 "No default constructor for T");
175 }
176
178 Dnode(const T & item) : data(item)
179 {
180 static_assert(std::is_copy_constructible<T>::value,
181 "No copy constructor for T");
182 }
183
185 Dnode(T && item) : data(std::move(item))
186 {
187 static_assert(std::is_move_constructible<T>::value,
188 "No move constructor for T");
189 }
190
193 {
194
195 Dlink::swap(p);
196 std::swap(data, p.data);
197 return *this;
198 }
199
201 Dnode(const Dnode & node)
202 : Dlink(node), data(node.data)
203 {
204 static_assert(std::is_copy_constructible<T>::value,
205 "No copy constructor for T");
206 }
207
210 : Dlink(std::move(node)), data(std::move(node.data))
211 {
212 /* empty */
213 }
214
216 Dnode & operator = (const Dnode & p)
217 {
218 static_assert(std::is_copy_assignable<T>::value,
219 "No copy assign for T");
220 data = p.data;
221 return *this;
222 }
223
226 {
227 std::swap(data, p.data);
228 return *this;
229 }
230
232 T & get_data() noexcept { return data; }
233
235 const T & get_data() const noexcept { return data; }
236
238 using key_type = T;
239
241 T & get_key() noexcept { return data; }
242
244 const T & get_key() const noexcept { return data; }
245
248 static Dnode * data_to_node(T & data) noexcept
249 {
250 Dnode * zero = 0;
251 long offset = (long) &(zero->data);
252 unsigned long addr = (unsigned long) (&data);
253 return (Dnode*) (addr - offset);
254 }
255
261 {
262 public:
263
266
269
271
272 using Base::Base;
273
276 {
277 return static_cast<Dnode<T>*>(this->Dlink::Iterator::get_curr_ne());
278 }
279
286 {
287 return static_cast<Dnode<T>*>(this->Dlink::Iterator::get_curr());
288 }
289
299 { return static_cast<Dnode*>(Dlink::Iterator::del()); }
300
303 { return static_cast<Dnode*>(Dlink::Iterator::del_ne()); }
304 };
305};
306
307template <typename T> Dnode<T> * Dlink::to_dnode() noexcept
308{
309 return static_cast<Dnode<T>*>(this);
310}
311
312template <typename T> const Dnode<T> * Dlink::to_dnode() const noexcept
313{
314 return static_cast<const Dnode<T>*>(this);
315}
316
317template <typename T> T & Dlink::to_data() noexcept
318{
319 return to_dnode<T>()->get_data();
320}
321
322template <typename T> const T & Dlink::to_data() const noexcept
323{
324 return to_dnode<T>()->get_data();
325}
326
327
328}
329
330# endif /* TPL_DNODE_H */
331
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:368
Standard functor implementations and comparison objects.
Iterator on a list of Dnode objects.
Definition tpl_dnode.H:261
Dnode< T > * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Definition tpl_dnode.H:275
Dnode * del_ne() noexcept
Same as del() but without exception check.
Definition tpl_dnode.H:302
Dnode * del()
Remove the current node of the list a return a pointer to the removed node.
Definition tpl_dnode.H:298
Dnode< T > * get_curr() const
Return a pointer to the current node.
Definition tpl_dnode.H:285
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(Dnode &&node) noexcept
Move constructor.
Definition tpl_dnode.H:209
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:238
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< 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:235
Dnode & operator=(const Dnode &p)
Copy assigment.
Definition tpl_dnode.H:216
T & get_data() noexcept
Return a modifiable reference to the data contained in the node.
Definition tpl_dnode.H:232
Dnode & swap(Dnode &p)
Swap this with p
Definition tpl_dnode.H:192
Dnode(T &&item)
Construct a new node with the item moved.
Definition tpl_dnode.H:185
Dnode(const Dnode &node)
Copy constructor.
Definition tpl_dnode.H:201
T & get_key() noexcept
Definition tpl_dnode.H:241
Dnode< T > *& get_first_ne() const noexcept
Get the first node.
Definition tpl_dnode.H:126
Dnode() noexcept
Definition tpl_dnode.H:171
Dnode(const T &item)
Construct a node with a copy of item
Definition tpl_dnode.H:178
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:248
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
Definition tpl_dnode.H:244
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.