mn_node.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2020 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef MINLIB_d80db0c1_69c6_478d_b3a8_364a8bc23f0b_H_
19 #define MINLIB_d80db0c1_69c6_478d_b3a8_364a8bc23f0b_H_
20 
21 #include "../mn_iterator.hpp"
22 #include "../mn_allocator.hpp"
23 #include "../mn_deleter.hpp"
24 
25 namespace mn {
26  namespace container {
27 
28  template <class TNODE>
30  public:
32  using value_type = typename TNODE::value_type;
33  using pointer = value_type*;
37  using node_type = TNODE;
38 
40  basic_node_itarrator(node_type _pNode) : m_pNode(_pNode) { }
41  basic_node_itarrator(const self_type& _pOther ) : m_pNode(_pOther.m_pNode) { }
42 
44  if(m_pNode) m_pNode = m_pNode->Next;
45  return *this;
46  }
48  if(m_pNode) m_pNode = m_pNode->Prev;
49  return *this;
50  }
51 
52  reference operator*() const { return m_pNode->get(); }
53  pointer operator->() const { return &m_pNode->get(); }
54 
56  self_type copy(*this); ++(*this); return copy; }
57 
59  self_type copy(*this); --(*this); return copy; }
60 
61  bool operator == (const self_type& rhs) const {
62  return rhs.m_pNode == m_pNode; }
63 
64  bool operator != (const self_type& rhs) const {
65  return !(rhs == *this); }
66  private:
68  };
69 
70  template <class TNODE>
72  public:
74  using value_type = typename TNODE::value_type;
75  using pointer = value_type*;
79  using node_type = TNODE;
80 
83  basic_node_itarrator_res(const self_type& _pOther ) : m_pNode(_pOther.m_pNode) { }
84 
86  if(m_pNode) m_pNode = m_pNode->Prev;
87  return *this;
88  }
90  if(m_pNode) m_pNode = m_pNode->Next;
91  return *this;
92  }
93 
94  reference operator*() const { return m_pNode->get(); }
95  pointer operator->() const { return &m_pNode->get(); }
96 
98  self_type copy(*this); ++(*this); return copy; }
99 
101  self_type copy(*this); --(*this); return copy; }
102 
103  bool operator == (const self_type& rhs) const {
104  return rhs.m_pNode == m_pNode; }
105 
106  bool operator != (const self_type& rhs) const {
107  return !(rhs == *this); }
108  private:
110  };
111 
112  template <typename T>
113  class basic_node {
114 
117  using value_type = T;
118  using pointer = value_type*;
119  using const_pointer = const pointer;
121  using const_reference = const reference;
122 
125 
131  explicit basic_node(value_type value )
132  : Next(0), Prev(0), m_tValue(value) { }
133 
134  basic_node(self_type* _pNext, self_type* _pPrev)
135  : Next(_pNext), Prev(_pPrev), m_tValue( value_type() ) { }
136  basic_node(const self_type& _pOther)
137  : Next(_pOther.Next), Prev(_pOther.Prev), m_tValue(_pOther.m_tValue) { }
138 
139  iterator begin() { return iterator( root() ); }
141  const_iterator cbegin() const { return const_iterator( root() ); }
142 
143  iterator end() { return iterator( last() ); }
145  const_iterator cend() const { return const_iterator( last() ); }
146 
148  return (Prev != 0) ? Prev->root() : this;
149  }
150  const self_type* root() const {
151  return (Prev != 0) ? Prev->root() : this;
152  }
154  return (Next != 0) ? Next->last() : this;
155  }
156  const self_type* last() const {
157  return (Next != 0) ? Next->last() : this;
158  }
159 
163  void insert(self_type* pNext) {
164  Next = pNext;
165  Prev = pNext->Prev;
166  pNext->Prev->Next = this;
167  pNext->Prev = this;
168  }
172  void remove() {
173  Next->Prev = Prev;
174  Prev->Next = Next;
175  }
176 
177  bool is() const { return this != Next; }
178 
182  void splice(self_type* first, self_type* last) {
183  last->Prev->Next = this;
184  first->Prev->Next = last;
185  this->Prev->Next = first;
186 
187  self_type* pTemp = this->Prev;
188  this->Prev = last->Prev;
189  last->Prev = first->Prev;
190  first->Prev = pTemp;
191  }
195  void reverse() {
196  self_type* pNode = this;
197 
198  do {
199  if(pNode != NULL) {
200  self_type* pTemp = pNode->Next;
201  pNode->Next = pNode->Prev;
202  pNode->Prev = pTemp;
203  pNode = pNode->Prev;
204  }
205  } while(pNode != this);
206  }
210  void insert_range(self_type* pFirst, self_type* pFinal) {
211  Prev->Next = pFirst; pFirst->Prev = Prev;
212  Prev = pFinal; pFinal->Next = this;
213  }
214  void swap(self_type& other) {
215  const basic_node temp(this);
216  this = other; other = temp;
217 
218  if(this.Next == &other) this.Next = this.Prev = this;
219  else this.Next->Prev = this.Prev->Next = this;
220 
221  if(other.Next == this) other.Next = other.Prev = &other;
222  else other.Next->Prev = other.Prev->Next = &other;
223 
224  }
228  static void remove_range(self_type* pFirst, self_type* pFinal) {
229  pFinal->Next->Prev = pFirst->Prev;
230  pFirst->Prev->Next = pFinal->Next;
231  }
232 
233  value_type get() { return m_tValue; }
234 
235  bool operator == (const self_type& rhs) const {
236  if( rhs.Next != Next) return false;
237  if( rhs.Prev != Prev) return false;
238  return (rhs.m_tValue == m_tValue);
239  }
240 
241  bool operator != (const self_type& rhs) const {
242  return !(rhs == *this);
243  }
244 
253  private:
255  };
256 
257 
258  template <typename T>
259  inline void swap(basic_node<T>& a, basic_node<T>& b) {
260  a.swap(b);
261  }
265  template<typename T>
267  }
268 }
269 
270 #endif
Definition: mn_node.hpp:71
pointer operator->() const
Definition: mn_node.hpp:95
self_type operator--(int)
Definition: mn_node.hpp:100
value_type * pointer
Definition: mn_node.hpp:75
self_type operator++(int)
Definition: mn_node.hpp:97
reference operator*() const
Definition: mn_node.hpp:94
self_type & operator++()
Definition: mn_node.hpp:85
self_type & operator--()
Definition: mn_node.hpp:89
basic_node_itarrator_res(node_type _pNode)
Definition: mn_node.hpp:82
basic_node_itarrator_res(const self_type &_pOther)
Definition: mn_node.hpp:83
node_type m_pNode
Definition: mn_node.hpp:109
TNODE node_type
Definition: mn_node.hpp:79
bool operator==(const self_type &rhs) const
Definition: mn_node.hpp:103
typename TNODE::value_type value_type
Definition: mn_node.hpp:74
value_type & reference
Definition: mn_node.hpp:76
bool operator!=(const self_type &rhs) const
Definition: mn_node.hpp:106
basic_node_itarrator_res()
Definition: mn_node.hpp:81
ptrdiff_t difference_type
Definition: mn_node.hpp:78
Definition: mn_node.hpp:29
reference operator*() const
Definition: mn_node.hpp:52
self_type operator--(int)
Definition: mn_node.hpp:58
pointer operator->() const
Definition: mn_node.hpp:53
self_type & operator++()
Definition: mn_node.hpp:43
basic_node_itarrator(const self_type &_pOther)
Definition: mn_node.hpp:41
value_type & reference
Definition: mn_node.hpp:34
self_type & operator--()
Definition: mn_node.hpp:47
ptrdiff_t difference_type
Definition: mn_node.hpp:36
basic_node_itarrator()
Definition: mn_node.hpp:39
TNODE node_type
Definition: mn_node.hpp:37
bool operator!=(const self_type &rhs) const
Definition: mn_node.hpp:64
typename TNODE::value_type value_type
Definition: mn_node.hpp:32
bool operator==(const self_type &rhs) const
Definition: mn_node.hpp:61
self_type operator++(int)
Definition: mn_node.hpp:55
value_type * pointer
Definition: mn_node.hpp:33
node_type m_pNode
Definition: mn_node.hpp:67
basic_node_itarrator(node_type _pNode)
Definition: mn_node.hpp:40
Definition: mn_node.hpp:113
self_type * root()
Definition: mn_node.hpp:147
basic_node_itarrator< self_type > iterator
Definition: mn_node.hpp:123
T value_type
Definition: mn_node.hpp:117
basic_node(const self_type &_pOther)
Definition: mn_node.hpp:136
void insert_range(self_type *pFirst, self_type *pFinal)
Add a range of elements.
Definition: mn_node.hpp:210
const basic_node_itarrator< const self_type > const_iterator
Definition: mn_node.hpp:124
bool operator!=(const self_type &rhs) const
Definition: mn_node.hpp:241
value_type m_tValue
Definition: mn_node.hpp:254
basic_node(self_type *_pNext, self_type *_pPrev)
Definition: mn_node.hpp:134
void reverse()
Reverses the order of nodes in the circular this node is a part of.
Definition: mn_node.hpp:195
void insert(self_type *pNext)
Inserts this standalone node before the node pNext in pNext's .
Definition: mn_node.hpp:163
basic_node_itarrator_res< self_type > reverse_iterator
Definition: mn_node.hpp:126
self_type * Next
The pointer to the next node.
Definition: mn_node.hpp:248
reverse_iterator rbegin()
Definition: mn_node.hpp:140
value_type get()
Definition: mn_node.hpp:233
bool operator==(const self_type &rhs) const
Definition: mn_node.hpp:235
void swap(self_type &other)
Definition: mn_node.hpp:214
self_type * Prev
The pointer to the prev node.
Definition: mn_node.hpp:252
bool is() const
Definition: mn_node.hpp:177
const_iterator cend() const
Definition: mn_node.hpp:145
self_type * last()
Definition: mn_node.hpp:153
iterator begin()
Definition: mn_node.hpp:139
void splice(self_type *first, self_type *last)
Removes [pFirst,pLast) from the it's in and inserts it before this in this node's .
Definition: mn_node.hpp:182
const self_type * last() const
Definition: mn_node.hpp:156
const reference const_reference
Definition: mn_node.hpp:121
reverse_iterator rend()
Definition: mn_node.hpp:144
value_type * pointer
Definition: mn_node.hpp:118
const_iterator cbegin() const
Definition: mn_node.hpp:141
value_type & reference
Definition: mn_node.hpp:120
iterator end()
Definition: mn_node.hpp:143
void remove()
Removes this node from the it's in.
Definition: mn_node.hpp:172
const self_type * root() const
Definition: mn_node.hpp:150
const pointer const_pointer
Definition: mn_node.hpp:119
static void remove_range(self_type *pFirst, self_type *pFinal)
remove a range of elements
Definition: mn_node.hpp:228
basic_node(value_type value)
Construct a new base node object.
Definition: mn_node.hpp:131
void swap(basic_any &x, basic_any &y) noexcept
Definition: mn_any.hpp:272
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25
long ptrdiff_t
Definition: mn_def.hpp:49
void copy(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:79
Definition: mn_iterator.hpp:35