momo  3.9
vector.h
Go to the documentation of this file.
1 /**********************************************************\
2 
3  This file is part of the
4  https://github.com/morzhovets/momo
5  project, distributed under the MIT License. See
6  https://github.com/morzhovets/momo/blob/branch_cpp11/LICENSE
7  for details.
8 
9  momo/stdish/vector.h
10 
11  namespace momo::stdish:
12  class vector
13  class vector_intcap
14 
15 \**********************************************************/
16 
17 #ifndef MOMO_INCLUDE_GUARD_STDISH_VECTOR
18 #define MOMO_INCLUDE_GUARD_STDISH_VECTOR
19 
20 #include "../Array.h"
21 
22 namespace momo
23 {
24 
25 namespace stdish
26 {
27 
39 template<typename TValue,
40  typename TAllocator = std::allocator<TValue>,
41  typename TArray = Array<TValue, MemManagerStd<TAllocator>>>
42 class vector
43 {
44 private:
45  typedef TArray Array;
46  typedef typename Array::MemManager MemManager;
47 
49 
50 public:
51  typedef TValue value_type;
52  typedef TAllocator allocator_type;
53 
54  typedef Array nested_container_type;
55 
56  typedef size_t size_type;
57  typedef ptrdiff_t difference_type;
58 
59  typedef typename Array::Iterator iterator;
61 
63  typedef const value_type& const_reference;
64 
65  typedef value_type* pointer;
66  typedef const value_type* const_pointer;
67  //typedef typename std::allocator_traits<allocator_type>::pointer pointer;
68  //typedef typename std::allocator_traits<allocator_type>::const_pointer const_pointer;
69 
70  typedef std::reverse_iterator<iterator> reverse_iterator;
71  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
72 
73 public:
74  vector() noexcept(noexcept(Array()))
75  {
76  }
77 
78  explicit vector(const allocator_type& alloc) noexcept
79  : mArray(MemManager(alloc))
80  {
81  }
82 
83  explicit vector(size_type count, const allocator_type& alloc = allocator_type())
84  : mArray(count, MemManager(alloc))
85  {
86  }
87 
88  vector(size_type count, const value_type& value, const allocator_type& alloc = allocator_type())
89  : mArray(count, value, MemManager(alloc))
90  {
91  }
92 
93  template<typename Iterator,
94  typename = typename std::iterator_traits<Iterator>::iterator_category>
95  vector(Iterator first, Iterator last, const allocator_type& alloc = allocator_type())
96  : mArray(first, last, MemManager(alloc))
97  {
98  }
99 
100  vector(std::initializer_list<value_type> values, const allocator_type& alloc = allocator_type())
101  : mArray(values, MemManager(alloc))
102  {
103  }
104 
105  vector(vector&& right) noexcept
106  : mArray(std::move(right.mArray))
107  {
108  }
109 
111  noexcept(std::is_empty<allocator_type>::value)
112  : mArray(pvCreateArray(std::move(right), alloc))
113  {
114  }
115 
116  vector(const vector& right)
117  : mArray(right.mArray)
118  {
119  }
120 
122  : mArray(right.mArray, MemManager(alloc))
123  {
124  }
125 
126  ~vector() = default;
127 
129  noexcept(std::is_empty<allocator_type>::value ||
130  std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
131  {
132  if (this != &right)
133  {
134  bool propagate = std::is_empty<allocator_type>::value ||
135  std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value;
136  allocator_type alloc = (propagate ? &right : this)->get_allocator();
137  mArray = pvCreateArray(std::move(right), alloc);
138  }
139  return *this;
140  }
141 
142  vector& operator=(const vector& right)
143  {
144  if (this != &right)
145  {
146  bool propagate = std::is_empty<allocator_type>::value ||
147  std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value;
148  allocator_type alloc = (propagate ? &right : this)->get_allocator();
149  mArray = Array(right.mArray, MemManager(alloc));
150  }
151  return *this;
152  }
153 
154  vector& operator=(std::initializer_list<value_type> values)
155  {
156  assign(values);
157  return *this;
158  }
159 
160  void swap(vector& right) noexcept
161  {
162  MOMO_ASSERT(std::allocator_traits<allocator_type>::propagate_on_container_swap::value
163  || get_allocator() == right.get_allocator());
164  mArray.Swap(right.mArray);
165  }
166 
167  friend void swap(vector& left, vector& right) noexcept
168  {
169  left.swap(right);
170  }
171 
173  {
174  return mArray;
175  }
176 
178  {
179  return mArray;
180  }
181 
182  const_iterator begin() const noexcept
183  {
184  return mArray.GetBegin();
185  }
186 
187  iterator begin() noexcept
188  {
189  return mArray.GetBegin();
190  }
191 
192  const_iterator end() const noexcept
193  {
194  return mArray.GetEnd();
195  }
196 
197  iterator end() noexcept
198  {
199  return mArray.GetEnd();
200  }
201 
203  {
204  return const_reverse_iterator(end());
205  }
206 
208  {
209  return reverse_iterator(end());
210  }
211 
212  const_reverse_iterator rend() const noexcept
213  {
214  return const_reverse_iterator(begin());
215  }
216 
218  {
219  return reverse_iterator(begin());
220  }
221 
222  const_iterator cbegin() const noexcept
223  {
224  return begin();
225  }
226 
227  const_iterator cend() const noexcept
228  {
229  return end();
230  }
231 
233  {
234  return rbegin();
235  }
236 
237  const_reverse_iterator crend() const noexcept
238  {
239  return rend();
240  }
241 
242  value_type* data() noexcept
243  {
244  return mArray.GetItems();
245  }
246 
247  const value_type* data() const noexcept
248  {
249  return mArray.GetItems();
250  }
251 
252  allocator_type get_allocator() const noexcept
253  {
254  return allocator_type(mArray.GetMemManager().GetByteAllocator());
255  }
256 
257  size_type max_size() const noexcept
258  {
259  return std::allocator_traits<allocator_type>::max_size(get_allocator());
260  }
261 
262  size_type size() const noexcept
263  {
264  return mArray.GetCount();
265  }
266 
268  {
269  mArray.SetCount(size);
270  }
271 
272  void resize(size_type size, const value_type& value)
273  {
274  mArray.SetCount(size, value);
275  }
276 
277  MOMO_NODISCARD bool empty() const noexcept
278  {
279  return mArray.IsEmpty();
280  }
281 
282  void clear() noexcept
283  {
284  mArray.Clear();
285  }
286 
287  size_type capacity() const noexcept
288  {
289  return mArray.GetCapacity();
290  }
291 
292  void reserve(size_type count)
293  {
294  mArray.Reserve(count);
295  }
296 
298  {
299  mArray.Shrink();
300  }
301 
303  {
304  return mArray[index];
305  }
306 
308  {
309  return mArray[index];
310  }
311 
313  {
314  if (index >= size())
315  throw std::out_of_range("invalid vector subscript");
316  return mArray[index];
317  }
318 
320  {
321  if (index >= size())
322  throw std::out_of_range("invalid vector subscript");
323  return mArray[index];
324  }
325 
327  {
328  return mArray[0];
329  }
330 
332  {
333  return mArray[0];
334  }
335 
337  {
338  return mArray.GetBackItem();
339  }
340 
342  {
343  return mArray.GetBackItem();
344  }
345 
346  void push_back(value_type&& value)
347  {
348  mArray.AddBack(std::move(value));
349  }
350 
351  void push_back(const value_type& value)
352  {
353  mArray.AddBack(value);
354  }
355 
357  {
358  size_t index = SMath::Dist(cbegin(), where);
359  mArray.Insert(index, std::move(value));
360  return SMath::Next(begin(), index);
361  }
362 
364  {
365  size_t index = SMath::Dist(cbegin(), where);
366  mArray.Insert(index, value);
367  return SMath::Next(begin(), index);
368  }
369 
370  iterator insert(const_iterator where, size_type count, const value_type& value)
371  {
372  size_t index = SMath::Dist(cbegin(), where);
373  mArray.Insert(index, count, value);
374  return SMath::Next(begin(), index);
375  }
376 
377  template<typename Iterator,
378  typename = typename std::iterator_traits<Iterator>::iterator_category>
379  iterator insert(const_iterator where, Iterator first, Iterator last)
380  {
381  size_t index = SMath::Dist(cbegin(), where);
382  mArray.Insert(index, first, last);
383  return SMath::Next(begin(), index);
384  }
385 
386  iterator insert(const_iterator where, std::initializer_list<value_type> values)
387  {
388  size_t index = SMath::Dist(cbegin(), where);
389  mArray.Insert(index, values);
390  return SMath::Next(begin(), index);
391  }
392 
393  template<typename... ValueArgs>
394  reference emplace_back(ValueArgs&&... valueArgs)
395  {
396  mArray.AddBackVar(std::forward<ValueArgs>(valueArgs)...);
397  return back();
398  }
399 
400  template<typename... ValueArgs>
401  iterator emplace(const_iterator where, ValueArgs&&... valueArgs)
402  {
403  size_t index = SMath::Dist(cbegin(), where);
404  mArray.InsertVar(index, std::forward<ValueArgs>(valueArgs)...);
405  return SMath::Next(begin(), index);
406  }
407 
408  void pop_back()
409  {
410  mArray.RemoveBack();
411  }
412 
414  {
415  return erase(where, where + 1);
416  }
417 
419  {
420  size_t index = SMath::Dist(cbegin(), first);
421  mArray.Remove(index, SMath::Dist(first, last));
422  return SMath::Next(begin(), index);
423  }
424 
425  template<typename ValueArg>
426  friend size_type erase(vector& cont, const ValueArg& valueArg)
427  {
428  auto valueFilter = [&valueArg] (const value_type& value)
429  { return value == valueArg; };
430  return cont.mArray.Remove(valueFilter);
431  }
432 
433  template<typename ValueFilter>
434  friend size_type erase_if(vector& cont, const ValueFilter& valueFilter)
435  {
436  return cont.mArray.Remove(valueFilter);
437  }
438 
439  void assign(size_type count, const value_type& value)
440  {
441  mArray = Array(count, value, MemManager(get_allocator()));
442  }
443 
444  template<typename Iterator,
445  typename = typename std::iterator_traits<Iterator>::iterator_category>
446  void assign(Iterator first, Iterator last)
447  {
448  mArray = Array(first, last, MemManager(get_allocator()));
449  }
450 
451  void assign(std::initializer_list<value_type> values)
452  {
453  assign(values.begin(), values.end());
454  }
455 
456  bool operator==(const vector& right) const
457  {
458  return mArray.IsEqual(right.mArray);
459  }
460 
461  bool operator!=(const vector& right) const
462  {
463  return !(*this == right);
464  }
465 
466 #ifdef MOMO_HAS_THREE_WAY_COMPARISON
467  auto operator<=>(const vector& right) const
468  requires requires (const_reference ref) { std::tie(ref) <=> std::tie(ref); }
469  {
470  auto comp = [] (const value_type& value1, const value_type& value2)
471  { return std::tie(value1) <=> std::tie(value2); };
472  return std::lexicographical_compare_three_way(begin(), end(),
473  right.begin(), right.end(), comp);
474  }
475 #else
476  bool operator<(const vector& right) const
477  {
478  return std::lexicographical_compare(begin(), end(), right.begin(), right.end());
479  }
480 
481  bool operator>(const vector& right) const
482  {
483  return right < *this;
484  }
485 
486  bool operator<=(const vector& right) const
487  {
488  return !(right < *this);
489  }
490 
491  bool operator>=(const vector& right) const
492  {
493  return right <= *this;
494  }
495 #endif
496 
497 private:
498  static Array pvCreateArray(vector&& right, const allocator_type& alloc)
499  {
500  if (right.get_allocator() == alloc)
501  return std::move(right.mArray);
502  Array array(std::make_move_iterator(right.begin()),
503  std::make_move_iterator(right.end()), MemManager(alloc));
504  right.clear();
505  return array;
506  }
507 
508 private:
509  Array mArray;
510 };
511 
512 #ifdef MOMO_HAS_DEDUCTION_GUIDES
513 template<typename Iterator,
514  typename Allocator = std::allocator<typename std::iterator_traits<Iterator>::value_type>>
515 vector(Iterator, Iterator, Allocator = Allocator())
517 #endif
518 
528 template<size_t tInternalCapacity, typename TValue,
529  typename TAllocator = std::allocator<TValue>>
530 using vector_intcap = vector<TValue, TAllocator,
532 
533 } // namespace stdish
534 
535 } // namespace momo
536 
537 #endif // MOMO_INCLUDE_GUARD_STDISH_VECTOR
momo::stdish::vector::shrink_to_fit
void shrink_to_fit()
Definition: vector.h:297
momo::stdish::vector::back
const_reference back() const
Definition: vector.h:341
momo::stdish::vector::swap
friend void swap(vector &left, vector &right) noexcept
Definition: vector.h:167
momo::stdish::vector::front
reference front()
Definition: vector.h:326
momo::stdish::vector::vector
vector(size_type count, const allocator_type &alloc=allocator_type())
Definition: vector.h:83
momo::stdish::vector::operator==
bool operator==(const vector &right) const
Definition: vector.h:456
momo::stdish::vector::vector
vector(const allocator_type &alloc) noexcept
Definition: vector.h:78
momo::stdish::vector::rbegin
reverse_iterator rbegin() noexcept
Definition: vector.h:207
momo::stdish::vector::crend
const_reverse_iterator crend() const noexcept
Definition: vector.h:237
momo::stdish::vector::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: vector.h:70
momo::stdish::vector::rend
const_reverse_iterator rend() const noexcept
Definition: vector.h:212
momo::stdish::vector::cbegin
const_iterator cbegin() const noexcept
Definition: vector.h:222
momo::stdish::vector::nested_container_type
Array nested_container_type
Definition: vector.h:54
momo::stdish::vector::back
reference back()
Definition: vector.h:336
momo::stdish::vector::front
const_reference front() const
Definition: vector.h:331
momo::Array::Iterator
IteratorSelector::Iterator Iterator
Definition: Array.h:504
momo::stdish::vector::push_back
void push_back(value_type &&value)
Definition: vector.h:346
momo::stdish::vector::vector
vector(std::initializer_list< value_type > values, const allocator_type &alloc=allocator_type())
Definition: vector.h:100
momo::stdish::vector::assign
void assign(Iterator first, Iterator last)
Definition: vector.h:446
momo::stdish::vector::difference_type
ptrdiff_t difference_type
Definition: vector.h:57
momo::stdish::vector::erase_if
friend size_type erase_if(vector &cont, const ValueFilter &valueFilter)
Definition: vector.h:434
momo::stdish::vector::data
const value_type * data() const noexcept
Definition: vector.h:247
momo::stdish::vector::iterator
Array::Iterator iterator
Definition: vector.h:59
momo::stdish::vector::vector
vector(Iterator first, Iterator last, const allocator_type &alloc=allocator_type())
Definition: vector.h:95
momo::stdish::vector::at
const_reference at(size_type index) const
Definition: vector.h:312
momo::stdish::vector::~vector
~vector()=default
momo::stdish::vector::max_size
size_type max_size() const noexcept
Definition: vector.h:257
momo::stdish::vector::operator=
vector & operator=(std::initializer_list< value_type > values)
Definition: vector.h:154
momo::stdish::vector::get_nested_container
const nested_container_type & get_nested_container() const noexcept
Definition: vector.h:172
momo::stdish::vector::size
size_type size() const noexcept
Definition: vector.h:262
momo::stdish::vector::rbegin
const_reverse_iterator rbegin() const noexcept
Definition: vector.h:202
momo::stdish::vector::end
const_iterator end() const noexcept
Definition: vector.h:192
momo::internal::UIntMath::Dist
static UInt Dist(Iterator begin, Iterator end)
Definition: Utility.h:271
momo::stdish::vector::emplace_back
reference emplace_back(ValueArgs &&... valueArgs)
Definition: vector.h:394
momo::stdish::vector
momo::stdish::vector is similar to std::vector.
Definition: vector.h:43
momo::stdish::vector::vector
vector(const vector &right, const momo::internal::Identity< allocator_type > &alloc)
Definition: vector.h:121
momo::stdish::vector::crbegin
const_reverse_iterator crbegin() const noexcept
Definition: vector.h:232
momo::stdish::vector::resize
void resize(size_type size, const value_type &value)
Definition: vector.h:272
momo::stdish::vector::operator=
vector & operator=(vector &&right) noexcept(std::is_empty< allocator_type >::value||std::allocator_traits< allocator_type >::propagate_on_container_move_assignment::value)
Definition: vector.h:128
momo::stdish::vector::operator[]
reference operator[](size_type index)
Definition: vector.h:307
momo::stdish::vector::resize
void resize(size_type size)
Definition: vector.h:267
momo::stdish::vector::get_nested_container
nested_container_type & get_nested_container() noexcept
Definition: vector.h:177
momo::stdish::vector::data
value_type * data() noexcept
Definition: vector.h:242
momo
Definition: Array.h:26
momo::stdish::vector::insert
iterator insert(const_iterator where, size_type count, const value_type &value)
Definition: vector.h:370
momo::stdish::vector::reference
value_type & reference
Definition: vector.h:62
momo::stdish::vector::vector
vector(vector &&right, const momo::internal::Identity< allocator_type > &alloc) noexcept(std::is_empty< allocator_type >::value)
Definition: vector.h:110
momo::stdish::vector::pop_back
void pop_back()
Definition: vector.h:408
momo::stdish::vector::emplace
iterator emplace(const_iterator where, ValueArgs &&... valueArgs)
Definition: vector.h:401
momo::stdish::vector::insert
iterator insert(const_iterator where, Iterator first, Iterator last)
Definition: vector.h:379
momo::stdish::vector::size_type
size_t size_type
Definition: vector.h:56
momo::internal::Identity
EnableIf< true, Type > Identity
Definition: Utility.h:177
momo::stdish::vector::rend
reverse_iterator rend() noexcept
Definition: vector.h:217
momo::stdish::vector::erase
friend size_type erase(vector &cont, const ValueArg &valueArg)
Definition: vector.h:426
momo::Array::MemManager
TMemManager MemManager
Definition: Array.h:196
momo::stdish::vector::push_back
void push_back(const value_type &value)
Definition: vector.h:351
momo::stdish::vector::swap
void swap(vector &right) noexcept
Definition: vector.h:160
momo::stdish::vector::operator>=
bool operator>=(const vector &right) const
Definition: vector.h:491
momo::Array::ConstIterator
IteratorSelector::ConstIterator ConstIterator
Definition: Array.h:503
momo::stdish::vector::operator=
vector & operator=(const vector &right)
Definition: vector.h:142
momo::stdish::vector::operator!=
bool operator!=(const vector &right) const
Definition: vector.h:461
momo::stdish::vector::erase
iterator erase(const_iterator first, const_iterator last)
Definition: vector.h:418
momo::stdish::vector::capacity
size_type capacity() const noexcept
Definition: vector.h:287
momo::stdish::vector::operator<=
bool operator<=(const vector &right) const
Definition: vector.h:486
momo::stdish::vector::begin
iterator begin() noexcept
Definition: vector.h:187
momo::stdish::vector::assign
void assign(std::initializer_list< value_type > values)
Definition: vector.h:451
momo::stdish::vector::clear
void clear() noexcept
Definition: vector.h:282
momo::stdish::vector::assign
void assign(size_type count, const value_type &value)
Definition: vector.h:439
momo::stdish::vector::vector
vector() noexcept(noexcept(Array()))
Definition: vector.h:74
momo::stdish::vector::cend
const_iterator cend() const noexcept
Definition: vector.h:227
momo::stdish::vector::const_iterator
Array::ConstIterator const_iterator
Definition: vector.h:60
momo::stdish::vector::const_reference
const value_type & const_reference
Definition: vector.h:63
momo::stdish::vector::allocator_type
TAllocator allocator_type
Definition: vector.h:52
momo::stdish::vector::operator>
bool operator>(const vector &right) const
Definition: vector.h:481
std
Definition: ArrayUtility.h:299
momo::stdish::vector::operator<
bool operator<(const vector &right) const
Definition: vector.h:476
momo::stdish::vector::vector
vector(size_type count, const value_type &value, const allocator_type &alloc=allocator_type())
Definition: vector.h:88
momo::stdish::vector::vector
vector(vector &&right) noexcept
Definition: vector.h:105
momo::stdish::vector::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: vector.h:71
momo::internal::UIntMath::Next
static Iterator Next(Iterator iter, UInt dist)
Definition: Utility.h:277
momo::stdish::vector::reserve
void reserve(size_type count)
Definition: vector.h:292
momo::stdish::vector::erase
iterator erase(const_iterator where)
Definition: vector.h:413
momo::stdish::vector::get_allocator
allocator_type get_allocator() const noexcept
Definition: vector.h:252
momo::stdish::vector::begin
const_iterator begin() const noexcept
Definition: vector.h:182
momo::stdish::vector::vector
vector(const vector &right)
Definition: vector.h:116
momo::stdish::vector::pointer
value_type * pointer
Definition: vector.h:65
momo::stdish::vector::insert
iterator insert(const_iterator where, std::initializer_list< value_type > values)
Definition: vector.h:386
momo::stdish::vector::value_type
TValue value_type
Definition: vector.h:51
momo::stdish::vector::const_pointer
const value_type * const_pointer
Definition: vector.h:66
momo::Array
Definition: Array.h:193
momo::stdish::vector::insert
iterator insert(const_iterator where, value_type &&value)
Definition: vector.h:356
momo::internal::UIntMath
Definition: Utility.h:254
MOMO_ASSERT
#define MOMO_ASSERT(expr)
Definition: UserSettings.h:162
momo::stdish::vector::operator[]
const_reference operator[](size_type index) const
Definition: vector.h:302
momo::stdish::vector::insert
iterator insert(const_iterator where, const value_type &value)
Definition: vector.h:363
momo::stdish::vector::empty
MOMO_NODISCARD bool empty() const noexcept
Definition: vector.h:277
MOMO_NODISCARD
#define MOMO_NODISCARD
Definition: UserSettings.h:192
momo::stdish::vector::at
reference at(size_type index)
Definition: vector.h:319
momo::stdish::vector::end
iterator end() noexcept
Definition: vector.h:197