11#include "DSCEngine/debug/assert.hpp"
13#define callstack_call ;
14#define callstack_ret return
28 void container_resize(
int new_cap);
34 template <
typename... Args>
35 Vector(T _first, Args... args)
36 : container(
new T[
sizeof...(args) + 1]{_first, args...}),
37 _size(
sizeof...(args) + 1),
38 capacity(
sizeof...(args) + 1)
152 int cap = size == 0 ? 1 : size;
155 cap>>=1; cap++; cap<<=1;
158 container =
new T[cap]();
164 _size = vector._size;
165 capacity = vector.capacity;
166 container = vector.container;
167 vector.container =
nullptr;
175 _size = vector.
size();
176 capacity = vector.capacity;
177 container = vector.container;
178 vector.container =
nullptr;
187 _size = vector._size;
188 capacity = vector.capacity;
189 container =
new T[capacity];
191 for(
int i=0;i<_size;i++)
192 container[i] = vector.container[i];
203 _size = vector._size;
204 capacity = vector.capacity;
206 container =
new T[capacity];
208 for(
int i=0;i<_size;i++)
209 container[i] = vector.container[i];
235 new_cap>>=1; new_cap++; new_cap<<=1;
237 T* new_container =
new T[new_cap];
238 int min_cap = new_cap < _size ? new_cap : _size;
242 for(
int i=0;i<min_cap;i++)
244 new_container[i] = (T&&)container[i];
248 container = new_container;
255 nds_assert(new_size>=0);
256 container_resize(new_size);
265 container_resize(capacity<<1);
267 container[_size++]=item;
275 nds_assert(index>=0,
"Index out of range");
276 nds_assert(index<_size,
"Index out of range");
278 callstack_ret container[index];
286 nds_assert(index>=0,
"Index out of range");
287 nds_assert(index<_size,
"Index out of range");
289 callstack_ret container[index];
297 nds_assert(index>=0,
"Index out of range");
298 nds_assert(index<_size,
"Index out of range");
300 callstack_ret container[index];
306 for(
int i=0;i<_size;i++)
308 if(container[i]==item)
317 for(
int i=0;i<_size;i++)
319 if(container[i]==item)
322 for(
int j=i;j<_size;j++)
324 container[j] = container[j+1];
337 nds_assert(index>=0,
"Index out of range");
338 nds_assert(index<_size,
"Index out of range");
341 for(
int j=index;j<_size;j++)
343 container[j] = container[j+1];
364 nds_assert(size()>0);
365 callstack_ret container[size()-1];
Generic dynamic vector.
Definition: vector.hpp:23
int index_of(const T &item) const
Finds the position of an item in the vector.
Definition: vector.hpp:304
void clear()
Sets all the elements in the vector to their default value determined by their type.
Definition: vector.hpp:218
void reset()
Sets the vector to its initial state
Definition: vector.hpp:224
void push_back(const T &item)
Adds new element to the end of the vector.
Definition: vector.hpp:261
T & back()
Gets the last element in vector.
Definition: vector.hpp:361
const T & get_at(int index) const
Gets element at a certain index.
Definition: vector.hpp:293
bool remove(const T &item)
Removes the first occurence of an element from the vector.
Definition: vector.hpp:315
void resize(int new_size)
Changes vector's number of elements.
Definition: vector.hpp:253
int size() const
Gets the vector length.
Definition: vector.hpp:349
void remove_at(int index)
Removes the element at a given position.
Definition: vector.hpp:333
T & operator[](int index)
Random access index operator.
Definition: vector.hpp:271
send debug messages to the emulator