Aleph-w  1.9
General library for algorithms and data structures
tpl_dnode.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 
28 # ifndef TPL_DNODE_H
29 # define TPL_DNODE_H
30 
31 # include <type_traits>
32 # include <ahFunction.H>
33 # include <dlink.H>
34 
35 using namespace Aleph;
36 
37 namespace Aleph {
38 
44 template <typename T> class Dnode : public Dlink
45 {
46 private:
47 
48  T data;
49 
50 public:
51 
53  Dnode<T> *& get_next() const noexcept { return (Dnode<T>*&) next; }
54 
56  Dnode<T> *& get_prev() const noexcept { return (Dnode<T>*&) prev; }
57 
59  Dnode<T>* remove_prev() noexcept { return (Dnode<T>*) Dlink::remove_prev(); }
60 
62  Dnode<T>* remove_next() noexcept { return (Dnode<T>*) Dlink::remove_next(); }
63 
65  Dnode<T> *& get_first_ne() const noexcept { return get_next(); }
66 
68  Dnode<T> *& get_last_ne() const noexcept { return get_prev(); }
69 
71  Dnode<T> *& get_first() const
72  {
73  if (this->is_empty())
74  throw std::underflow_error("Dnode is empty");
75  return get_first_ne();
76  }
77 
79  Dnode<T> *& get_last() const
80  {
81  if (this->is_empty())
82  throw std::underflow_error("Dnode is empty");
83  return get_last_ne();
84  }
85 
87  Dnode<T>* remove_last_ne() noexcept
88  {
89  return static_cast<Dnode<T>*>(Dlink::remove_prev());
90  }
91 
94  {
95  return static_cast<Dnode<T>*>(Dlink::remove_next());
96  }
97 
100  {
101  if (this->is_empty())
102  throw std::underflow_error("Dnode is empty");
103  return remove_last_ne();
104  }
105 
108  {
109  if (this->is_empty())
110  throw std::underflow_error("Dnode is empty");
111  return remove_first_ne();
112  }
113 
114  Dnode() noexcept
115  {
116  static_assert(std::is_default_constructible<T>::value,
117  "No default constructor for T");
118  }
119 
121  Dnode(const T & item) noexcept(noexcept(T(item))) : data(item)
122  {
123  static_assert(std::is_copy_constructible<T>::value,
124  "No copy constructor for T");
125  }
126 
128  Dnode(T && item) noexcept(noexcept(std::swap(data, item)))
129  {
130  static_assert(std::is_move_constructible<T>::value,
131  "No move constructor for T");
132  std::swap(data, item);
133  }
134 
136  Dnode & swap(Dnode & p) noexcept(noexcept(std::swap(data, p.data)))
137  {
138 
139  Dlink::swap(p);
140  std::swap(data, p.data);
141  return *this;
142  }
143 
145  Dnode(const Dnode & node) noexcept(std::is_nothrow_copy_assignable<T>::value)
146  : Dlink(node), data(node.data)
147  {
148  static_assert(std::is_copy_constructible<T>::value,
149  "No copy constructor for T");
150  }
151 
153  Dnode(Dnode && node) noexcept(noexcept(std::forward<T>(node.data)))
154  : Dlink(node), data(std::forward<T>(node.data))
155  {
156  /* empty */
157  }
158 
160  Dnode & operator = (const Dnode & p)
161  noexcept(std::is_nothrow_copy_assignable<T>::value)
162  {
163  static_assert(std::is_copy_assignable<T>::value,
164  "No copy assign for T");
165  data = p.data;
166  return *this;
167  }
168 
171  noexcept(std::is_nothrow_move_assignable<T>::value)
172  {
173  std::swap(data, p.data);
174  return *this;
175  }
176 
178  T & get_data() noexcept { return data; }
179 
181  const T & get_data() const noexcept { return data; }
182 
184  using key_type = T;
185 
187  T & get_key() noexcept { return data; }
188 
190  const T & get_key() const noexcept { return data; }
191 
194  static Dnode * data_to_node(T & data) noexcept
195  {
196  Dnode * zero = 0;
197  long offset = (long) &(zero->data);
198  unsigned long addr = (unsigned long) (&data);
199  return (Dnode*) (addr - offset);
200  }
201 
206  class Iterator : public Dlink::Iterator
207  {
208  public:
209 
212 
214  using Item_Type = Dnode<T>*;
215 
216  using Base = Dlink::Iterator;
217 
218  using Base::Base;
219 
221  Dnode<T> * get_curr_ne() const noexcept
222  {
223  return static_cast<Dnode<T>*>(this->Dlink::Iterator::get_curr_ne());
224  }
225 
231  Dnode<T> * get_curr() const
232  {
233  return static_cast<Dnode<T>*>(this->Dlink::Iterator::get_curr());
234  }
235 
244  Dnode * del()
245  { return static_cast<Dnode*>(Dlink::Iterator::del()); }
246  };
247 };
248 
249 template <typename T> Dnode<T> * Dlink::to_dnode() noexcept
250 {
251  return static_cast<Dnode<T>*>(this);
252 }
253 
254 template <typename T> const Dnode<T> * Dlink::to_dnode() const noexcept
255 {
256  return static_cast<const Dnode<T>*>(this);
257 }
258 
259 template <typename T> T & Dlink::to_data() noexcept
260 {
261  return to_dnode<T>()->get_data();
262 }
263 
264 template <typename T> const T & Dlink::to_data() const noexcept
265 {
266  return to_dnode<T>()->get_data();
267 }
268 
269 
270 }
271 
272 # endif /* TPL_DNODE_H */
273 
static Dnode * data_to_node(T &data) noexcept
Definition: tpl_dnode.H:194
Dnode< T > * remove_last_ne() noexcept
Remove the last node and return its address.
Definition: tpl_dnode.H:87
Dnode & operator=(const Dnode &p) noexcept(std::is_nothrow_copy_assignable< T >::value)
Copy assigment.
Definition: tpl_dnode.H:160
Dnode & swap(Dnode &p) noexcept(noexcept(std::swap(data, p.data)))
Swap this with p
Definition: tpl_dnode.H:136
Dnode< T > * remove_first_ne() noexcept
Remove the first node and return its address.
Definition: tpl_dnode.H:93
Dnode(const T &item) noexcept(noexcept(T(item)))
Construct a node with a copy of item
Definition: tpl_dnode.H:121
T & get_key() noexcept
Definition: tpl_dnode.H:187
Dnode(T &&item) noexcept(noexcept(std::swap(data, item)))
Construct a new node with the item moved.
Definition: tpl_dnode.H:128
Dnode< T > * remove_first()
Remove the first node and return its address.
Definition: tpl_dnode.H:107
Dnode< T > * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Definition: tpl_dnode.H:221
Dnode< T > *& get_next() const noexcept
Return the next node to this
Definition: tpl_dnode.H:53
Dnode< T > *& get_last() const
Get the last node.
Definition: tpl_dnode.H:79
Dnode< T > * remove_last()
Remove the last node and return its address.
Definition: tpl_dnode.H:99
const T & get_data() const noexcept
Return a modifiable reference to the data contained in the node.
Definition: tpl_dnode.H:181
Definition: ah-comb.H:35
T & get_data() noexcept
Return a modifiable reference to the data contained in the node.
Definition: tpl_dnode.H:178
const T & get_key() const noexcept
Definition: tpl_dnode.H:190
Dnode< T > * remove_prev() noexcept
Remove the previous node to this; return its address.
Definition: tpl_dnode.H:59
Dnode< T > *& get_prev() const noexcept
Return the previous node to this
Definition: tpl_dnode.H:56
Dnode< T > *& get_first() const
Get the first node.
Definition: tpl_dnode.H:71
Dnode< T > * remove_next() noexcept
Remove the next node to this; return its address.
Definition: tpl_dnode.H:62
Dnode< T > * get_curr() const
Definition: tpl_dnode.H:231
Dnode * del()
Definition: tpl_dnode.H:244
Definition: tpl_dnode.H:206
typename GT::Node * key_type
The data type.
Definition: tpl_dnode.H:184
Dnode< T > *& get_first_ne() const noexcept
Get the first node.
Definition: tpl_dnode.H:65
Dnode(const Dnode &node) noexcept(std::is_nothrow_copy_assignable< T >::value)
Copy constructor.
Definition: tpl_dnode.H:145
Dnode< T > *& get_last_ne() const noexcept
Get the last node.
Definition: tpl_dnode.H:68
Dnode(Dnode &&node) noexcept(noexcept(std::forward< T >(node.data)))
Move constructor.
Definition: tpl_dnode.H:153
Definition: dlink.H:37

Leandro Rabindranath León