Max OS 0.3
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1
9#ifndef MAXOS_COMMON_VECTOR_H
10#define MAXOS_COMMON_VECTOR_H
11
12#include <cstdint>
13#include <cstddef>
14
15
16namespace MaxOS::common {
17
24 template<class Type> class VectorIterationHandler {
25 public:
27 ~VectorIterationHandler() = default;
28
29 virtual void on_read(Type);
30 virtual void on_end_of_stream();
31 };
32
39 template<class Type> class Vector {
40 protected:
44
45 public:
46 typedef Type* iterator;
47
53
54 Type& operator [](uint32_t index) const;
57
58 [[nodiscard]] bool empty() const;
59 [[nodiscard]] uint32_t size() const;
60
61 iterator begin() const;
62 iterator end() const;
64
67
70
71 void erase(Type);
72 void erase(typename Vector<Type>::iterator position);
73 void clear();
74
75 void reserve(size_t amount);
77
79 void iterate(void callback(Type&));
80 };
81
83
88 template<class Type> Vector<Type>::Vector() {
89
90 // Allocate space for the array
91 m_elements = new Type[m_capacity];
92
93 }
94
102 template<class Type> Vector<Type>::Vector(int size, Type element) {
103
104 // Allocate space for the array
105 m_elements = new Type[size];
106 m_capacity = size > 0 ? size : 1;
107 m_size = 0;
108
109 // Push all the elements to the Vector
110 for(int i = 0; i < size; ++i)
111 push_back(element);
112 }
113
120 template<class Type> Vector<Type>::Vector(const Vector<Type>& other)
121 : m_size(other.m_size),
122 m_capacity(other.m_capacity) {
123 // Copy each element into a new array
125 for(uint32_t i = 0; i < m_size; ++i)
126 m_elements[i] = other.m_elements[i];
127 }
128
136 : m_elements(other.m_elements),
137 m_size(other.m_size),
138 m_capacity(other.m_capacity) {
139
140 // Clear the other Vector
141 other.m_elements = nullptr;
142 other.m_size = 0;
143 other.m_capacity = 0;
144
145 }
146
152 template<class Type> Vector<Type>::~Vector() {
153
154 // De-allocate the array
155 delete[] m_elements;
156
157 }
158
164 template<class Type> void Vector<Type>::increase_size() {
165 reserve(m_capacity * 2);
166 }
167
174 template<class Type> void Vector<Type>::reserve(size_t amount) {
175
176
177 // Increase the capacity of the Vector
178 if(m_capacity < amount)
179 m_capacity = amount;
180
181 // Allocate more space for the array
183
184 // Copy the elements to the new array
185 for(uint32_t i = 0; i < m_size; ++i)
186 new_elements[i] = m_elements[i];
187
188 // De-allocate the old array
189 delete[] m_elements;
190
191 // Set the new array
192 m_elements = new_elements;
193 }
194
195
203 template<class Type> Type& Vector<Type>::operator [](uint32_t index) const {
204
205 // If the index is in the Vector
206 if(index < m_size)
207 return m_elements[index];
208
209 // Return the last element of the Vector
210 return m_elements[m_size - 1];
211
212 }
213
222
223 // Setting to itself?
224 if(this == &other)
225 return *this;
226
227 // Create a new buffer to store the elements
228 delete[] m_elements;
229 m_elements = new Type[other.m_capacity];
230
231 // Copy data
232 m_size = other.m_size;
233 m_capacity = other.m_capacity;
234 for(uint32_t i = 0; i < m_size; ++i)
235 m_elements[i] = other.m_elements[i];
236
237 return *this;
238 }
239
248
249 // Moving to itself?
250 if(this == &other)
251 return *this;
252
253 // Move into this vector
254 delete[] m_elements;
255 m_elements = other.m_elements;
256 m_size = other.m_size;
257 m_capacity = other.m_capacity;
258
259 // Remove from other vector
260 other.m_elements = nullptr;
261 other.m_size = 0;
262 other.m_capacity = 0;
263
264 return *this;
265 }
266
273 template<class Type> uint32_t Vector<Type>::size() const {
274 return m_size;
275 }
276
283 template<class Type> typename Vector<Type>::iterator Vector<Type>::begin() const {
284 return &m_elements[0];
285 }
286
293 template<class Type> typename Vector<Type>::iterator Vector<Type>::end() const {
294 return &m_elements[0] + m_size;
295 }
296
304 template<class Type> typename Vector<Type>::iterator Vector<Type>::find(Type element) const {
305
306 // Find the element
307 for(iterator i = begin(); i != end(); ++i)
308 if(*i == element)
309 return i;
310
311 // The element must not be in the Vector
312 return end();
313 }
314
321 template<class Type> bool Vector<Type>::empty() const {
322 return begin() == end();
323 }
324
325
334
335 // Check if we need to allocate more space for the array
336 if(m_size == m_capacity)
337 increase_size();
338
339 // Add the element to the Vector and return the iterator of the element
340 m_elements[m_size++] = element;
341 return end() - 1;
342 }
343
350 template<class Type> Type Vector<Type>::pop_back() {
351
352 // Remove the last element from the Vector
353 if(m_size > 0)
354 --m_size;
355
356 return m_elements[m_size];
357 }
358
367
368 // Check if we need to allocate more space for the array
369 if(m_size == m_capacity)
370 increase_size();
371
372 // Move all elements one index to the right
373 for(iterator i = end(); i > begin(); --i)
374 *i = *(i - 1);
375
376 // Add the element to the front of the Vector
377 m_elements[0] = element;
378 ++m_size;
379
380 // Return the iterator of the element
381 return begin();
382 }
383
390 template<class Type> Type Vector<Type>::pop_front() {
391
392 // Make sure the Vector is not empty
393 if(m_size == 0)
394 return Type();
395
396 // Store the element to return
397
398 Type element = m_elements[0];
399
400 // Move all elements one index to the left
401 for(uint32_t i = 0; i < m_size - 1; ++i)
402 m_elements[i] = m_elements[i + 1];
403
404 // Decrease the size of the Vector
405 --m_size;
406 return element;
407 }
408
415 template<class Type> void Vector<Type>::erase(Type element) {
416
417 // Search for the element
418 int hits = 0;
419 for(iterator i = begin(); i != end(); ++i) {
420 // If it is the element we are looking for
421 if(*i == element) {
422 ++hits;
423 } else {
424
425 // If there are hits move the element to the left
426 if(hits > 0)
427 *(i - hits) = *i;
428 }
429
430 }
431
432 // Decrease the size of the Vector
433 m_size -= hits;
434 }
435
442 template<class Type> void Vector<Type>::erase(typename Vector<Type>::iterator position) {
443
444 // If the m_position is not in the Vector
445 if(position < begin() || position >= end())
446 return;
447
448 // Move all elements one index to the left
449 for(++position; position != end(); ++position)
450 *(position - 1) = *position;
451
452 // Decrease the size of the Vector
453 --m_size;
454 }
455
461 template<class Type> void Vector<Type>::clear() {
462 m_size = 0;
463 }
464
472
473 // Call the OnRead function of the handler for each element
474 for(auto& element : m_elements)
476
477 // Call the OnEndOfStream function of the handler
478 vector_iteration_handler->on_end_of_stream();
479 }
480
481
488 template<class Type> void Vector<Type>::iterate(void callback(Type&)) {
489
490 // Call the callback function for each element
491 for(auto& element : m_elements)
493 }
494
495 template<class Type> VectorIterationHandler<Type>::VectorIterationHandler() = default;
496
497
498
499
506
507 }
508
514 template<class Type> void VectorIterationHandler<Type>::on_read(Type) {
515
516 }
517}
518
519
520#endif //MAXOS_COMMON_VECTOR_H
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Rectangle()
_______________________________________________TEMPLATES_____________________________________________...
Handles the iteration of a Vector providing read and end of stream functions.
Definition vector.h:24
virtual void on_read(Type)
Called when an element is read from the Vector (overridden by subclasses, no default behavior)
Definition vector.h:514
virtual void on_end_of_stream()
Called when the end of the stream is reached (overridden by subclasses, no default behavior)
Definition vector.h:505
Dynamically stores an array of elements.
Definition vector.h:39
iterator end() const
Returns the last element of the Vector.
Definition vector.h:293
Type & operator[](uint32_t index) const
Overloads the [] operator to return the element at the index.
Definition vector.h:203
uint32_t size() const
Returns the number of elements in the Vector.
Definition vector.h:273
Vector< Type > & operator=(const Vector< Type > &other)
Assignment by copy, data is copied into a new buffer stored in this vector.
Definition vector.h:221
uint32_t m_capacity
How many elements can be stored without resizing.
Definition vector.h:43
Type * m_elements
The array of elements.
Definition vector.h:41
void erase(typename Vector< Type >::iterator position)
Removes the element at the m_position.
Definition vector.h:442
void reserve(size_t amount)
Reserves space in the Vector for a certain amount of elements.
Definition vector.h:174
iterator push_front(Type)
Adds an element to the front of the Vector and returns the iterator of the element.
Definition vector.h:366
void clear()
Removes all elements from the Vector.
Definition vector.h:461
void iterate(void callback(Type &))
Iterates over the Vector and calls the callback function for each element.
Definition vector.h:488
void increase_size()
Increases the size of the Vector by doubling the capacity.
Definition vector.h:164
Vector(int size, Type element)
Constructor for Vector.
Definition vector.h:102
~Vector()
Destructor for Vector, de-allocates the array.
Definition vector.h:152
void iterate(VectorIterationHandler< Type > *)
Iterates over the Vector and calls the OnRead function of the handler for each element.
Definition vector.h:471
Type * iterator
The iterator type for the Vector.
Definition vector.h:46
uint32_t m_size
How many elements are currently stored.
Definition vector.h:42
Vector(Vector< Type > &&other)
Move constructor for Vector.
Definition vector.h:135
iterator find(Type) const
Finds an element in the Vector and returns the iterator of the element.
Definition vector.h:304
iterator push_back(Type)
Adds an element to the end of the vector and returns the iterator of the element.
Definition vector.h:333
Vector()
______________________________________Implementation_________________________________________________...
Definition vector.h:88
Type pop_back()
Removes the last element from the Vector.
Definition vector.h:350
Vector(const Vector< Type > &other)
Copy constructor for Vector.
Definition vector.h:120
iterator begin() const
Returns the first element of the Vector.
Definition vector.h:283
Type pop_front()
Removes the m_first_memory_chunk element from the Vector.
Definition vector.h:390
bool empty() const
Checks if the Vector is empty.
Definition vector.h:321
void erase(Type)
Removes all elements from the Vector that are equal to the element.
Definition vector.h:415