Atoms Crowd  7.0.0
Array.h
1 #pragma once
2 #include "ToolchefsSTL/Allocators/Allocator.h"
3 #include <utility>
4 #include <cassert>
5 
6 namespace ToolchefsSTL
7 {
8  template<typename T>
9  class Array
10  {
11  public:
12 
13  template <class Iter>
15  {
16  ReverseIterator(Iter* ptr) : m_ptr(ptr) {}
17  ~ReverseIterator() = default;
18  Iter* operator +(size_t value) const { return m_ptr - value; }
19  Iter* operator -(size_t value) const { return m_ptr + value; }
20  Iter* operator ++() { return --m_ptr; }
21  Iter* operator --() { return ++m_ptr; }
22  bool operator ==(const ReverseIterator& rhs) const { return m_ptr == rhs.m_ptr; }
23  bool operator !=(const ReverseIterator& rhs) const { return m_ptr != rhs.m_ptr; }
24  Iter operator *() const { return *m_ptr; }
25  Iter* operator->() const { return m_ptr; }
26  Iter* m_ptr;
27  };
28 
29  typedef T value_type;
30  typedef T& reference_type;
31  typedef T* ptr_type;
32 
33  typedef T* iterator;
34  typedef const T* const_iterator;
37 
38  explicit Array(Allocator* allocator = defaultAllocator()) noexcept;
39 
40  explicit Array(size_t num_elements, Allocator* allocator = defaultAllocator()) noexcept;
41 
42  explicit Array(size_t num_elements, const T& init_value, Allocator* allocator = defaultAllocator()) noexcept;
43 
44  ~Array() noexcept;
45 
46  Array(const Array& rhs) noexcept;
47 
48  Array& operator=(const Array& rhs) noexcept;
49 
50  Array(Array&& rhs) noexcept;
51 
52  Array& operator=(Array&& rhs) noexcept;
53 
54  bool operator==(const Array& rhs) const noexcept;
55 
56  void push_back(const T& element) noexcept;
57 
58  template <typename... Args>
59  T& emplace_back(Args&&... args) {
60  if (m_length == m_capacity) grow();
61  return *new (m_data + m_length++) T(std::forward<Args>(args)...);
62  }
63 
64  void pop_back() noexcept;
65 
66  void insert(size_t index, const T& value) noexcept;
67  void insert(iterator it, const T& value) noexcept;
68 
69  void erase(size_t index) noexcept;
70  iterator erase(iterator it) noexcept;
71 
73  iterator begin() noexcept;
74  iterator end() noexcept;
75  const_iterator cbegin() const noexcept;
76  const_iterator cend() const noexcept;
77 
78  reverse_iterator rbegin() noexcept;
79  reverse_iterator rend() noexcept;
80  const_reverse_iterator crbegin() const noexcept;
81  const_reverse_iterator crend() const noexcept;
82 
84  T& back() noexcept;
85  const T& back() const noexcept;
86 
87  T& front() noexcept;
88  const T& front() const noexcept;
89 
90 #if __cplusplus >= 201703L
91  constexpr T& operator[](const size_t index) noexcept;
92  constexpr const T& operator[](const size_t index) const noexcept;
93 #else
94  inline T& operator[](const size_t index) noexcept { return m_data[index];}
95  inline const T& operator[](const size_t index) const noexcept { return m_data[index];}
96 #endif
97  const T* data() const noexcept;
98 
99  void resize(size_t new_length) noexcept;
100 
101  void reserve(size_t new_capacity) noexcept;
102 
103  void clear() noexcept;
104 
105  size_t size() const noexcept;
106 
107  size_t length() const noexcept;
108 
109  size_t capacity() const noexcept;
110 
111  void shrink_to_fit() noexcept;
112 
113  iterator find(const T& element) noexcept;
114 
115  const_iterator find(const T& element) const noexcept;
116 
117  bool empty() const noexcept;
118 
119  private:
120 
121  void grow() noexcept;
122 
123  private:
124 
125  T* m_data;
126  Allocator* m_allocator;
127  size_t m_length;
128  size_t m_capacity;
129  };
130 }
131 
132 #include "Array.impl.h"
Definition: Allocator.h:10
Definition: Array.h:10
iterator begin() noexcept
Iterators.
Definition: Array.impl.h:172
T & back() noexcept
Direct access.
Definition: Array.impl.h:222