mn_list.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_50f2b4fa_c256_450b_9595_0a6ed07718ca_H_
19 #define MINLIB_50f2b4fa_c256_450b_9595_0a6ed07718ca_H_
20 
21 #include "../mn_config.hpp"
22 
23 #include "mn_node.hpp"
24 
25 namespace mn {
26  namespace container {
27 
28  template<typename TNodePtr, typename TPtr, typename TRef>
30  public:
32  using value_type = TNodePtr;
33  using pointer = TPtr;
34  using reference = TRef;
36  using node_type = TNodePtr;
38 
39  explicit list_node_iterator()
40  : m_node(NULL) {}
42  : m_node(node) {}
43 
44  template<typename UNodePtr, typename UPtr, typename URef>
46  : m_node(rhs.node()) { }
47 
48  node_type node() const { return m_node; }
49 
50  reference operator*() const { return m_node->value; }
51  pointer operator->() const { return &m_node->value; }
52 
54  m_node = m_node->Next; return *this; }
55 
57  m_node = m_node->Prev; return *this; }
58 
60  self_type copy(*this); ++(*this); return copy; }
61 
63  self_type copy(*this); --(*this); return copy; }
64 
65  bool operator == (const self_type& rhs) const {
66  return rhs.m_node == m_node; }
67 
68  bool operator != (const self_type& rhs) const {
69  return !(rhs == *this); }
70 
71  private:
73  };
74 
75  template<typename T, class TAllocator = memory::default_allocator,
76  class TDeleter = memory::default_delete<T,TAllocator>>
77  class basic_list {
78  public:
80  using value_type = T;
81  using allocator_type = TAllocator;
84  using deleter = TDeleter;
85 
88 
89  static const size_type NodeSize = sizeof(node_type);
90 
94  explicit basic_list(const allocator_type& allocator = allocator_type() )
95  : m_allocator(allocator), m_deleter(m_allocator) { m_root.reset(); }
96 
100  template<class InputIterator>
101  basic_list(InputIterator first, InputIterator last, const allocator_type& allocator = allocator_type())
102  : m_allocator(allocator), m_deleter(m_allocator) {
103  m_root.reset();
104  assign(first, last);
105  }
109  basic_list(const basic_list& rhs)
111  m_root.reset();
112  assign(rhs.begin(), rhs.end());
113  }
118  clear();
119  }
120 
121  iterator begin() { return iterator((m_root.Next)); }
123 
124  iterator end() { return iterator(&m_root); }
125  const_iterator end() const { return const_iterator(&m_root); }
126 
127  const T& front() const { assert(!empty()); return (m_root.Next)->Value; }
128  T& front() { assert(!empty()); return (m_root.Next)->Value; }
129 
130  const T& back() const { assert(!empty()); return (m_root.Prev)->Value; }
131  T& back() { assert(!empty()); return (m_root.Prev)->Value; }
132 
133  void push_front(const T& value) {
134  node_type* newNode = construct_node(value);
135  newNode->insert(m_root.Next);
136  }
137  inline void pop_front() {
138  assert(!empty());
139  node_type* frontNode = (m_root.Next);
140  frontNode->remove();
141  destruct_node(frontNode);
142  }
143 
144  void push_back(const T& value) {
145  node_type* newNode = construct_node(value);
146  newNode->insert(&m_root);
147  }
148  inline void pop_back() {
149  assert(!empty());
150  node_type* backNode = (m_root.Prev);
151  backNode->remove();
152  destruct_node(backNode);
153  }
154 
155  iterator insert(iterator pos, const T& value) {
156  node_type* newNode = construct_node(value);
157  newNode->insert(pos.node());
158  return iterator(newNode);
159  }
160 
162  assert(it.node()->is());
163  iterator itErase(it);
164  ++it;
165  itErase.node()->remove();
166  destruct_node(itErase.node());
167  return it;
168  }
169 
171  while (first != last) first = erase(first);
172  return first;
173  }
174 
175  template<class InputIterator>
176  void assign(InputIterator first, InputIterator last) {
177  clear();
178  while (first != last) {
179  push_back(*first);
180  ++first;
181  }
182  }
183 
184  bool empty() const {
185  return !m_root.is();
186  }
187 
188  void clear() {
189  node_type* it = (m_root.Next);
190 
191  while (it != &m_root) {
192  node_type* nextIt = (it->Next);
193  destruct_node(it);
194  it = nextIt;
195  }
196  m_root.reset();
197  }
198 
199  size_type size() const {
200  const node_type* it = (m_root.Next);
201  size_type size(0);
202 
203  while (it != &m_root) {
204  ++size;
205  it = (it->Next);
206  }
207  return size;
208  }
209 
211  return m_allocator;
212  }
213  deleter_type& get_deleter() noexcept {
214  return m_deleter;
215  }
216 
217 
218  void set_allocator(const allocator_type& allocator) {
219  if(!empty()) return;
220 
221  m_allocator = allocator;
222  m_deleter.set_deleter(m_allocator);
223  }
224 
226  if (this != &rhs) {
227  assign(rhs.begin(), rhs.end());
228  }
229  return *this;
230  }
231  private:
232  node_type* construct_node(const T& value) {
233  void* mem = m_allocator.allocate(NodeSize, mn::alignment_for(NodeSize) );
234  return new (mem) node_type(value);
235  }
237  if(n == nullptr) return;
238 
239  /*mn::destruct<node_type>(n);
240 
241  m_allocator.deallocate(n, NodeSize, mn::alignment_for(NodeSize));
242  n = nullptr;*/
243 
244  get_deleter()(n);
245  }
246  private:
250  };
251 
257  template<typename T, class TAllocator = memory::default_allocator,
258  class TDeleter = memory::default_delete<T,TAllocator>>
260  }
261 }
262 
263 #endif
Definition: mn_list.hpp:77
bool empty() const
Definition: mn_list.hpp:184
const T & back() const
Definition: mn_list.hpp:130
basic_node< T > node_type
Definition: mn_list.hpp:83
deleter m_deleter
Definition: mn_list.hpp:248
~basic_list()
Destroy the basic list object.
Definition: mn_list.hpp:117
void push_back(const T &value)
Definition: mn_list.hpp:144
basic_list & operator=(const basic_list &rhs)
Definition: mn_list.hpp:225
const_iterator begin() const
Definition: mn_list.hpp:122
T & front()
Definition: mn_list.hpp:128
T & back()
Definition: mn_list.hpp:131
deleter_type & get_deleter() noexcept
Definition: mn_list.hpp:213
static const size_type NodeSize
Definition: mn_list.hpp:89
iterator erase(iterator it)
Definition: mn_list.hpp:161
basic_list(InputIterator first, InputIterator last, const allocator_type &allocator=allocator_type())
Construct a new basic list object.
Definition: mn_list.hpp:101
void destruct_node(node_type *n)
Definition: mn_list.hpp:236
iterator insert(iterator pos, const T &value)
Definition: mn_list.hpp:155
node_type m_root
Definition: mn_list.hpp:249
void assign(InputIterator first, InputIterator last)
Definition: mn_list.hpp:176
size_type size() const
Definition: mn_list.hpp:199
void pop_front()
Definition: mn_list.hpp:137
void set_allocator(const allocator_type &allocator)
Definition: mn_list.hpp:218
basic_list(const basic_list &rhs)
Construct a new basic list object.
Definition: mn_list.hpp:109
TAllocator allocator_type
Definition: mn_list.hpp:81
TDeleter deleter
Definition: mn_list.hpp:84
list_node_iterator< const node_type *, const value_type *, const value_type & > const_iterator
Definition: mn_list.hpp:87
list_node_iterator< node_type *, const value_type *, const value_type & > iterator
Definition: mn_list.hpp:86
mn::size_t size_type
Definition: mn_list.hpp:82
void clear()
Definition: mn_list.hpp:188
iterator end()
Definition: mn_list.hpp:124
allocator_type m_allocator
Definition: mn_list.hpp:247
const_iterator end() const
Definition: mn_list.hpp:125
void push_front(const T &value)
Definition: mn_list.hpp:133
allocator_type & get_allocator() noexcept
Definition: mn_list.hpp:210
iterator erase(iterator first, iterator last)
Definition: mn_list.hpp:170
void pop_back()
Definition: mn_list.hpp:148
const T & front() const
Definition: mn_list.hpp:127
basic_list(const allocator_type &allocator=allocator_type())
Construct a new basic list object.
Definition: mn_list.hpp:94
iterator begin()
Definition: mn_list.hpp:121
T value_type
Definition: mn_list.hpp:80
node_type * construct_node(const T &value)
Definition: mn_list.hpp:232
Definition: mn_node.hpp:113
void insert(self_type *pNext)
Inserts this standalone node before the node pNext in pNext's .
Definition: mn_node.hpp:163
self_type * Next
The pointer to the next node.
Definition: mn_node.hpp:248
self_type * Prev
The pointer to the prev node.
Definition: mn_node.hpp:252
bool is() const
Definition: mn_node.hpp:177
void remove()
Removes this node from the it's in.
Definition: mn_node.hpp:172
Definition: mn_list.hpp:29
bool operator==(const self_type &rhs) const
Definition: mn_list.hpp:65
self_type operator++(int)
Definition: mn_list.hpp:59
pointer operator->() const
Definition: mn_list.hpp:51
self_type & operator--()
Definition: mn_list.hpp:56
self_type & operator++()
Definition: mn_list.hpp:53
TNodePtr value_type
Definition: mn_list.hpp:32
reference operator*() const
Definition: mn_list.hpp:50
bool operator!=(const self_type &rhs) const
Definition: mn_list.hpp:68
ptrdiff_t difference_type
Definition: mn_list.hpp:35
node_type m_node
Definition: mn_list.hpp:72
self_type operator--(int)
Definition: mn_list.hpp:62
list_node_iterator(const list_node_iterator< UNodePtr, UPtr, URef > &rhs)
Definition: mn_list.hpp:45
TRef reference
Definition: mn_list.hpp:34
list_node_iterator()
Definition: mn_list.hpp:39
TPtr pointer
Definition: mn_list.hpp:33
TNodePtr node_type
Definition: mn_list.hpp:36
node_type node() const
Definition: mn_list.hpp:48
list_node_iterator(node_type node)
Definition: mn_list.hpp:41
A Simple template for a deleter.
Definition: mn_basic_deleter.hpp:37
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
malloc_allocator< basic_allocator_filter > default_allocator
Definition: mn_default_allocator.hpp:30
Definition: mn_allocator_typetraits.hpp:25
size_t alignment_for(const size_t size) noexcept
Definition: mn_alignment.hpp:178
long ptrdiff_t
Definition: mn_def.hpp:49
void copy(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:79
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
Definition: mn_iterator.hpp:35