Golias Engine
A C++ 20 'game engine' built with SDL3 with wide platform support.
vector.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <functional>
5 #include <algorithm>
6 #include <stdexcept>
7 #include <iterator>
8 
9 template<typename T>
10 class TypedArray : public std::vector<T> {
11 public:
12  using std::vector<T>::vector;
13 
14  bool is_empty() const;
15 
16  void push_front(const T& value);
17 
18  void append(const T& value);
19 
20  void append_array(const TypedArray<T>& other);
21 
22  void pop_front();
23 
24  void erase(size_t position);
25 
26  void remove(size_t position);
27 
28  void remove_at(size_t position);
29 
30  T& get(size_t index);
31 
32  const T& get(size_t index) const;
33 
34  int find(const T& what, int from = 0) const;
35 
36  int rfind(const T& what) const;
37 
38  bool has(const T& what) const;
39 
40  int count(const T& what) const;
41 
42  TypedArray<T> duplicate(bool deep = false) const;
43 
44  TypedArray<T> slice(int begin_pos, int end_pos = INT32_MAX) const;
45 
46  void reverse();
47 
48  void sort();
49 
50  template <typename Func>
51  void sort(Func comparator);
52 
53  T min() const;
54 
55  T max() const;
56 
57  template<typename Func>
58  TypedArray<T> filter(Func predicate) const;
59 
60  template <typename Func>
61  auto map(Func func) const;
62 
63  template <typename Func>
64  void for_each(Func func);
65 
66  template <typename U, typename Func>
67  U reduce(Func func, U initial_value) const;
68 
69  bool all(std::function<bool(const T&)> predicate) const;
70 
71  bool any(std::function<bool(const T&)> predicate) const;
72 
73  T& first();
74 
75  const T& first() const;
76 
77  T& last();
78 
79  const T& last() const;
80 };
81 
82 
83 template <typename T>
85  return this->empty();
86 }
87 
88 template <typename T>
89 void TypedArray<T>::push_front(const T& value) {
90  this->insert(this->begin(), value);
91 }
92 
93 template <typename T>
94 void TypedArray<T>::append(const T& value) {
95  this->push_back(value);
96 }
97 
98 template <typename T>
100  this->insert(this->end(), other.begin(), other.end());
101 }
102 
103 template <typename T>
105  if (!this->empty()) {
106  this->erase(this->begin());
107  }
108 }
109 
110 template <typename T>
111 void TypedArray<T>::erase(size_t position) {
112  if (position < this->size()) {
113  std::vector<T>::erase(this->begin() + position);
114  }
115 }
116 
117 template <typename T>
118 void TypedArray<T>::remove(size_t position) {
119  erase(position);
120 }
121 
122 template <typename T>
123 void TypedArray<T>::remove_at(size_t position) {
124  erase(position);
125 }
126 
127 template <typename T>
128 T& TypedArray<T>::get(size_t index) {
129  return (*this)[index];
130 }
131 
132 template <typename T>
133 const T& TypedArray<T>::get(size_t index) const {
134  return (*this)[index];
135 }
136 
137 template <typename T>
138 int TypedArray<T>::find(const T& what, int from) const {
139  for (size_t i = from; i < this->size(); ++i) {
140  if ((*this)[i] == what) {
141  return static_cast<int>(i);
142  }
143  }
144 
145  return -1;
146 }
147 
148 template <typename T>
149 int TypedArray<T>::rfind(const T& what) const {
150  for (int i = this->size() - 1; i >= 0; --i) {
151  if ((*this)[i] == what) {
152  return i;
153  }
154  }
155 
156  return -1;
157 }
158 
159 template <typename T>
160 bool TypedArray<T>::has(const T& what) const {
161  return find(what) != -1;
162 }
163 
164 template <typename T>
165 int TypedArray<T>::count(const T& what) const {
166  int result = 0;
167  for (const auto& item : *this) {
168  if (item == what) {
169  ++result;
170  }
171  }
172 
173  return result;
174 }
175 
176 template <typename T>
178  TypedArray<T> result;
179  result.assign(this->begin(), this->end());
180  return result;
181 }
182 
183 template <typename T>
184 TypedArray<T> TypedArray<T>::slice(int begin_pos, int end_pos) const {
185  TypedArray<T> result;
186  if (begin_pos < 0) {
187  begin_pos = 0;
188  }
189 
190  if (end_pos > static_cast<int>(this->size())) {
191  end_pos = this->size();
192  }
193 
194  if (begin_pos < end_pos) {
195  result.assign(this->begin() + begin_pos, this->begin() + end_pos);
196  }
197 
198  return result;
199 }
200 
201 template <typename T>
203  std::reverse(this->begin(), this->end());
204 }
205 
206 template <typename T>
208  std::sort(this->begin(), this->end());
209 }
210 
211 template <typename T>
212 template <typename Func>
213 void TypedArray<T>::sort(Func comparator) {
214  std::sort(this->begin(), this->end(), comparator);
215 }
216 
217 template <typename T>
219  if (this->empty()) {
220  throw std::runtime_error("Cannot find min of empty array");
221  }
222 
223  return *std::min_element(this->begin(), this->end());
224 }
225 
226 template <typename T>
228  if (this->empty()) {
229  throw std::runtime_error("Cannot find max of empty array");
230  }
231 
232  return *std::max_element(this->begin(), this->end());
233 }
234 
235 template <typename T>
236 template <typename Func>
237 TypedArray<T> TypedArray<T>::filter(Func predicate) const {
238  TypedArray<T> result;
239  std::copy_if(this->begin(), this->end(), std::back_inserter(result), predicate);
240  return result;
241 }
242 
243 template <typename T>
244 template <typename Func>
245 auto TypedArray<T>::map(Func func) const {
246  using ReturnType = decltype(func(std::declval<T>()));
247  TypedArray<ReturnType> result;
248  result.reserve(this->size());
249  std::transform(this->begin(), this->end(), std::back_inserter(result), func);
250  return result;
251 }
252 
253 template <typename T>
254 template <typename Func>
255 void TypedArray<T>::for_each(Func func) {
256  std::for_each(this->begin(), this->end(), func);
257 }
258 
259 template <typename T>
260 template <typename U, typename Func>
261 U TypedArray<T>::reduce(Func func, U initial_value) const {
262  U result = initial_value;
263  for (const auto& item : *this) {
264  result = func(result, item);
265  }
266  return result;
267 }
268 
269 template <typename T>
270 bool TypedArray<T>::all(std::function<bool(const T&)> predicate) const {
271  return std::all_of(this->begin(), this->end(), predicate);
272 }
273 
274 template <typename T>
275 bool TypedArray<T>::any(std::function<bool(const T&)> predicate) const {
276  return std::any_of(this->begin(), this->end(), predicate);
277 }
278 
279 template <typename T>
281  if (this->empty()) {
282  throw std::out_of_range("Array is empty");
283  }
284 
285  return this->front();
286 }
287 
288 template <typename T>
289 const T& TypedArray<T>::first() const {
290  if (this->empty()) {
291  throw std::out_of_range("Array is empty");
292  }
293 
294  return this->front();
295 }
296 
297 template <typename T>
299  if (this->empty()) {
300  throw std::out_of_range("Array is empty");
301  }
302 
303  return this->back();
304 }
305 
306 template <typename T>
307 const T& TypedArray<T>::last() const {
308  if (this->empty()) {
309  throw std::out_of_range("Array is empty");
310  }
311 
312  return this->back();
313 }
Definition: vector.h:10
TypedArray< T > duplicate(bool deep=false) const
Definition: vector.h:177
void erase(size_t position)
Definition: vector.h:111
void remove_at(size_t position)
Definition: vector.h:123
void sort()
Definition: vector.h:207
const T & last() const
Definition: vector.h:307
bool has(const T &what) const
Definition: vector.h:160
void for_each(Func func)
Definition: vector.h:255
bool is_empty() const
Definition: vector.h:84
void remove(size_t position)
Definition: vector.h:118
int rfind(const T &what) const
Definition: vector.h:149
const T & first() const
Definition: vector.h:289
void reverse()
Definition: vector.h:202
T & first()
Definition: vector.h:280
int find(const T &what, int from=0) const
Definition: vector.h:138
int count(const T &what) const
Definition: vector.h:165
void append(const T &value)
Definition: vector.h:94
bool any(std::function< bool(const T &)> predicate) const
Definition: vector.h:275
T & last()
Definition: vector.h:298
T min() const
Definition: vector.h:218
void sort(Func comparator)
Definition: vector.h:213
TypedArray< T > filter(Func predicate) const
Definition: vector.h:237
bool all(std::function< bool(const T &)> predicate) const
Definition: vector.h:270
void pop_front()
Definition: vector.h:104
auto map(Func func) const
Definition: vector.h:245
void push_front(const T &value)
Definition: vector.h:89
TypedArray< T > slice(int begin_pos, int end_pos=INT32_MAX) const
Definition: vector.h:184
void append_array(const TypedArray< T > &other)
Definition: vector.h:99
const T & get(size_t index) const
Definition: vector.h:133
T max() const
Definition: vector.h:227
U reduce(Func func, U initial_value) const
Definition: vector.h:261
T & get(size_t index)
Definition: vector.h:128