mn_array.hpp
Go to the documentation of this file.
1 
20 #ifndef __MINILIB_CONTAINER_DYNAMIC_ARRAY_H__
21 #define __MINILIB_CONTAINER_DYNAMIC_ARRAY_H__
22 
23 #include "../mn_config.hpp"
24 
25 # include <cassert>
26 # include <typeinfo>
27 
28 #include "mn_algorithm.hpp"
29 #include "../mn_deleter.hpp"
30 #include "../mn_def.hpp"
31 #include "../mn_functional.hpp"
32 #include "../mn_typetraits.hpp"
33 
34 #include "../utils/mn_utils.hpp"
35 #include "../container/mn_pair.hpp"
36 
37 namespace mn {
38  namespace container {
39 
40  template<typename TArray>
41  struct array_traits {
42  using array_type = TArray;
43 
44  using value_type = typename array_type::value_type;
45  using reference = typename array_type::reference;
46  using const_reference = typename array_type::const_reference;
47  using pointer = typename array_type::pointer;
48 
49  using difference_type = typename array_type::difference_type;
50  using size_type = typename array_type::size_type;
51  using allocator = typename array_type::allocator;
52  using deleter = typename array_type::deleter;
53  using iterator = typename array_type::iterator;
54  using const_iterator = typename array_type::const_iterator;
55 
56  static const size_type TypeSize = TArray::TypeSize;
57  };
61  template <typename T>
63  public:
65  using value_type = T;
66  using pointer = T*;
67  using reference = T&;
68  using const_reference = const T&;
71 
73 
74  basic_array_iterator(pointer _valuePtr, uint32_t pos) noexcept
75  : m_valuePtr(_valuePtr), m_iPosition(pos) { }
76 
77  basic_array_iterator(const self_type& other) noexcept
78  : m_valuePtr(other.m_valuePtr), m_iPosition(other.m_iPosition) { }
79 
80  basic_array_iterator(const self_type&& other) noexcept
81  : m_valuePtr(mn::move(other.m_valuePtr)), m_iPosition(mn::move(other.m_iPosition)) { }
82 
83  bool operator != (const self_type& other) const {
84  return !(*this == other);
85  }
86 
87  bool operator == (const self_type& other) const {
88  return m_iPosition == other.m_iPosition;
89  }
90 
92  ++m_iPosition; return *this;
93  }
95  --m_iPosition; return *this;
96  }
97 
99  return *(m_valuePtr + m_iPosition);
100  }
101 
103  return *(m_valuePtr + m_iPosition);
104  }
105 
106  self_type& operator = (const self_type& other) noexcept {
107  m_valuePtr = other.m_valuePtr;
108  m_iPosition = other.m_iPosition;
109  return *this;
110  }
111 
113  return (m_valuePtr + m_iPosition);
114  }
115 
117  return (m_valuePtr + m_iPosition);
118  }
119  private:
121  uint32_t m_iPosition;
122  };
126  template <typename T, class TAllocator = memory::default_allocator,
128  class basic_array {
129  public:
130  using value_type = T;
131  using pointer = T*;
132  using reference = T&;
133  using const_reference = const T&;
136  using allocator_type = TAllocator;
137  using deleter_type = TDeleter;
140 
141  static const size_type TypeSize = sizeof(value_type);
142 
144 
146  : m_arData(nullptr),
147  m_szSize(16),
148  m_szCurPos(0),
149  m_alloCator(),
150  m_dDeleter() {
151 
153  }
154 
155  basic_array(pointer _ptrData, unsigned int _sSize)
156  : m_arData(_ptrData),
157  m_szSize(_sSize),
158  m_szCurPos(0),
159  m_alloCator(),
161 
162  basic_array(const self_type& other)
163  : m_arData(other.m_arData),
164  m_szSize(other.m_szSize),
165  m_szCurPos(other.m_szCurPos),
166  m_alloCator(other.m_alloCator),
167  m_dDeleter(other.m_dDeleter) { }
168 
170  if (m_arData != nullptr) {
172  m_arData = nullptr;
173  }
174  }
175 
176  iterator begin() { return iterator(m_arData, 0); }
177  const_iterator begin() const { return iterator(m_arData, 0); }
178 
181 
182  reference front() noexcept { return (m_arData[0]); }
183  const_reference front() const noexcept { return (m_arData[0]); }
184 
185  reference back() noexcept { return (m_arData[m_szSize-1]); }
186  const_reference back() const noexcept { return (m_arData[m_szSize-1]); }
187 
188  size_type size() const noexcept { return m_szSize; }
189 
190  reference at(size_type pos) { return m_arData[pos]; }
191  const_reference at(size_type pos) const { return m_arData[pos]; }
192 
193  constexpr bool is_empty() const noexcept {
194  return m_szSize == 0; }
195 
196  constexpr bool is_full() const noexcept {
197  return m_szSize == m_szCurPos; }
198 
199 
200  int push(value_type entry) {
201  if (m_szCurPos >= m_szSize)
202  if(!resize(m_szSize + 16)) return -1;
203 
204  m_arData[m_szCurPos] = entry; m_szCurPos++;
205 
206  return m_szCurPos;
207  }
208 
210  assert(is_full());
211  return m_arData[m_szCurPos++];
212  }
213 
214 
215  bool resize(size_type newSize) {
216  pointer _newArData = construct(newSize);
217  if(_newArData == nullptr) return false;
218 
219  size_type _cpysize = (newSize < m_szSize) ? newSize : m_szSize;
220 
221  for(int i = 0; i < _cpysize; i++)
222  _newArData[i] = m_arData[i];
223 
225  m_szSize = newSize;
226  m_arData = _newArData;
227 
229 
230  return true;
231  }
232 
233  bool is_equele(const self_type& other, bool bWithPos = false) {
234  if(m_szSize != other.m_szSize) return false;
235  if(bWithPos && (m_szCurPos != other.m_szCurPos)) return false;
236 
237  for(int i = 0; i < m_szSize; i++)
238  if(m_arData[i] != other.m_arData[i]) return false;
239 
240  return true;
241  }
242 
243  void swap(self_type& other) noexcept {
244  swap_ranges(begin(), end(), other.begin());
245  }
246 
248  assert(pos < m_szSize);
249  return m_arData[pos]; }
250 
251  constexpr const_reference operator[](size_type pos) const noexcept {
252  assert(pos < m_szSize);
253  return m_arData[pos]; }
254 
255  void fill(const value_type& val) {
256  mn::fill_n<value_type>(begin(), size(), val);
257  }
258  private:
260  void* _mem = m_alloCator.allocate(n, TypeSize, mn::alignment_for(n * TypeSize) );
261  //return (pointer*)_val;
262 
263  return new (_mem) value_type[n];
264  }
266  m_alloCator.deallocate(ptr, size, TypeSize, mn::alignment_for(size * TypeSize) );
267  }
268  private:
272 
275  };
276 
277  // Array comparisons.
278  template <typename T, class TAllocator, class TDeleter >
281  return a.is_equele(b);
282  }
283 
284  template <typename T, class TAllocator, class TDeleter >
287  return !(a.is_equele(b));
288  }
289 
290 
291  template <typename T, class TAllocator, class TDeleter >
294  for(int i = 0; i < mn::min(a.size(), b.size()); i++)
295  if(a[i] >= b[i]) return false;
296  return true;
297  }
298 
299  template <typename T, class TAllocator, class TDeleter >
302  for(int i = 0; i < mn::min(a.size(), b.size()); i++)
303  if(a[i] > b[i]) return false;
304  return true;
305  }
306 
307  template <typename T, class TAllocator, class TDeleter >
310  for(int i = 0; i < mn::min(a.size(), b.size()); i++)
311  if(a[i] <= b[i]) return false;
312  return true;
313  }
314 
315  template <typename T, class TAllocator, class TDeleter >
318  for(int i = 0; i < mn::min(a.size(), b.size()); i++)
319  if(a[i] < b[i]) return false;
320  return true;
321  }
322 
323  template <typename T, class TAllocator, class TDeleter >
326  a.swap(b);
327  }
328 
329  template <typename T, class TAllocator, class TDeleter >
330  inline void fill (basic_array<T, TAllocator, TDeleter>& a, const int val) {
331  a.fill(val);
332  }
333 
334  template <typename T, class TAllocator, class TDeleter >
336  a.fill(0);
337  }
338 
339 
340  template <typename T,
341  class TAllocator = memory::default_allocator,
344  }
345 }
346 #endif
Helper class for array iterators.
Definition: mn_array.hpp:62
self_type & operator++()
Definition: mn_array.hpp:91
mn::size_t size_type
Definition: mn_array.hpp:70
reference operator[](size_type index)
Definition: mn_array.hpp:112
basic_array_iterator(pointer _valuePtr, uint32_t pos) noexcept
Definition: mn_array.hpp:74
bool operator!=(const self_type &other) const
Definition: mn_array.hpp:83
self_type & operator=(const self_type &other) noexcept
Definition: mn_array.hpp:106
pointer m_valuePtr
Definition: mn_array.hpp:120
T value_type
Definition: mn_array.hpp:65
T & reference
Definition: mn_array.hpp:67
self_type & operator--()
Definition: mn_array.hpp:94
basic_array_iterator(const self_type &&other) noexcept
Definition: mn_array.hpp:80
reference operator*()
Definition: mn_array.hpp:98
basic_array_iterator(const self_type &other) noexcept
Definition: mn_array.hpp:77
bool operator==(const self_type &other) const
Definition: mn_array.hpp:87
const T & const_reference
Definition: mn_array.hpp:68
uint32_t m_iPosition
Definition: mn_array.hpp:121
mn::ptrdiff_t difference_type
Definition: mn_array.hpp:69
T * pointer
Definition: mn_array.hpp:66
const_reference operator*() const
Definition: mn_array.hpp:102
Lightweight c++11 array implementation.
Definition: mn_array.hpp:128
iterator end()
Definition: mn_array.hpp:179
const_reference back() const noexcept
Definition: mn_array.hpp:186
constexpr bool is_empty() const noexcept
Definition: mn_array.hpp:193
reference operator[](size_type pos) noexcept
Definition: mn_array.hpp:247
allocator_type m_alloCator
Definition: mn_array.hpp:273
T * pointer
Definition: mn_array.hpp:131
mn::size_t size_type
Definition: mn_array.hpp:135
~basic_array()
Definition: mn_array.hpp:169
const T & const_reference
Definition: mn_array.hpp:133
T value_type
Definition: mn_array.hpp:130
const_iterator begin() const
Definition: mn_array.hpp:177
const_iterator end() const
Definition: mn_array.hpp:180
size_type size() const noexcept
Definition: mn_array.hpp:188
TAllocator allocator_type
Definition: mn_array.hpp:136
reference front() noexcept
Definition: mn_array.hpp:182
int push(value_type entry)
Definition: mn_array.hpp:200
void deconstruct(pointer ptr, size_type size)
Definition: mn_array.hpp:265
T & reference
Definition: mn_array.hpp:132
size_type m_szSize
Definition: mn_array.hpp:270
const_reference at(size_type pos) const
Definition: mn_array.hpp:191
void fill(const value_type &val)
Definition: mn_array.hpp:255
basic_array(const self_type &other)
Definition: mn_array.hpp:162
const_reference front() const noexcept
Definition: mn_array.hpp:183
pointer construct(size_type n)
Definition: mn_array.hpp:259
reference back() noexcept
Definition: mn_array.hpp:185
reference at(size_type pos)
Definition: mn_array.hpp:190
reference pop()
Definition: mn_array.hpp:209
bool resize(size_type newSize)
Definition: mn_array.hpp:215
TDeleter deleter_type
Definition: mn_array.hpp:137
iterator begin()
Definition: mn_array.hpp:176
basic_array_iterator< T > iterator
Definition: mn_array.hpp:138
static const size_type TypeSize
Definition: mn_array.hpp:141
basic_array_iterator< const T > const_iterator
Definition: mn_array.hpp:139
pointer m_arData
Definition: mn_array.hpp:269
bool is_equele(const self_type &other, bool bWithPos=false)
Definition: mn_array.hpp:233
void swap(self_type &other) noexcept
Definition: mn_array.hpp:243
constexpr const_reference operator[](size_type pos) const noexcept
Definition: mn_array.hpp:251
deleter_type m_dDeleter
Definition: mn_array.hpp:274
size_type m_szCurPos
Definition: mn_array.hpp:271
basic_array(pointer _ptrData, unsigned int _sSize)
Definition: mn_array.hpp:155
constexpr bool is_full() const noexcept
Definition: mn_array.hpp:196
mn::ptrdiff_t difference_type
Definition: mn_array.hpp:134
basic_array()
Definition: mn_array.hpp:145
A Simple template for a deleter.
Definition: mn_basic_deleter.hpp:37
Basic algorithmens This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniT...
void fill(basic_array< T, TAllocator, TDeleter > &a, const int val)
Definition: mn_array.hpp:330
bool operator<=(const basic_array< T, TAllocator, TDeleter > &a, const basic_array< T, TAllocator, TDeleter > &b)
Definition: mn_array.hpp:300
bool operator<(const basic_array< T, TAllocator, TDeleter > &a, const basic_array< T, TAllocator, TDeleter > &b)
Definition: mn_array.hpp:292
void swap(basic_any &x, basic_any &y) noexcept
Definition: mn_any.hpp:272
bool operator!=(const basic_array< T, TAllocator, TDeleter > &a, const basic_array< T, TAllocator, TDeleter > &b)
Definition: mn_array.hpp:285
bool operator>=(const basic_array< T, TAllocator, TDeleter > &a, const basic_array< T, TAllocator, TDeleter > &b)
Definition: mn_array.hpp:316
bool operator>(const basic_array< T, TAllocator, TDeleter > &a, const basic_array< T, TAllocator, TDeleter > &b)
Definition: mn_array.hpp:308
void zero(basic_array< T, TAllocator, TDeleter > &a)
Definition: mn_array.hpp:335
bool operator==(const basic_array< T, TAllocator, TDeleter > &a, const basic_array< T, TAllocator, TDeleter > &b)
Definition: mn_array.hpp:279
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
T min(const T x, const T y)
Definition: mn_algorithm.hpp:307
fIt2 swap_ranges(fIt1 a, fIt1 b, fIt2 c)
Definition: mn_algorithm.hpp:331
size_t alignment_for(const size_t size) noexcept
Definition: mn_alignment.hpp:178
long ptrdiff_t
Definition: mn_def.hpp:49
void move(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:100
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
Definition: mn_iterator.hpp:35
Definition: mn_array.hpp:41
typename array_type::size_type size_type
Definition: mn_array.hpp:50
typename array_type::pointer pointer
Definition: mn_array.hpp:47
typename array_type::allocator allocator
Definition: mn_array.hpp:51
typename array_type::reference reference
Definition: mn_array.hpp:45
typename array_type::value_type value_type
Definition: mn_array.hpp:44
typename array_type::iterator iterator
Definition: mn_array.hpp:53
typename array_type::deleter deleter
Definition: mn_array.hpp:52
typename array_type::const_iterator const_iterator
Definition: mn_array.hpp:54
TArray array_type
Definition: mn_array.hpp:42
static const size_type TypeSize
Definition: mn_array.hpp:56
typename array_type::const_reference const_reference
Definition: mn_array.hpp:46
typename array_type::difference_type difference_type
Definition: mn_array.hpp:49