DSC Engine
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1
6// Known flaw: this code does not deal with empty vectors (capacity 0, container nullptr)
7
8#pragma once
9
11#include "DSCEngine/debug/assert.hpp"
12
13#define callstack_call ;
14#define callstack_ret return
15
16namespace DSC
17{
22 template<typename T> class Vector
23 {
24 private:
25 T *container;
26 int _size;
27 int capacity;
28 void container_resize(int new_cap);
29 public:
30 Vector(int size=0);
31 Vector(const Vector<T>& vector);
32 Vector(Vector<T>&& vector);
33
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)
39 {}
40
41
42 Vector<T>& operator = (Vector<T>&& vector);
43 Vector<T>& operator = (const Vector<T>& vector);
44
49 void clear();
50
54 void reset();
55
56
64 void resize(int new_size);
65
71 void push_back(const T& item);
72
79 T& back();
80
87 int index_of(const T& item) const;
88
95 bool remove(const T& item);
96
104 void remove_at(int index);
105
111 int size() const;
112
121 T& operator[] (int index);
122
132 const T& operator[] (int index) const;
133
142 const T& get_at(int index) const;
143
144 ~Vector();
145 };
146}
147
148template<typename T>
150{
151 _size = size;
152 int cap = size == 0 ? 1 : size;
153 if(sizeof(T)&1)
154 {
155 cap>>=1; cap++; cap<<=1; // assure capacity is 16-bit aligned
156 }
157 capacity = cap;
158 container = new T[cap]();
159}
160
161template<typename T>
162DSC::Vector<T>::Vector(Vector<T>&& vector)
163{
164 _size = vector._size;
165 capacity = vector.capacity;
166 container = vector.container;
167 vector.container = nullptr;
168 vector._size = 0;
169 vector.capacity = 0;
170}
171
172template<typename T>
174{
175 _size = vector.size();
176 capacity = vector.capacity;
177 container = vector.container;
178 vector.container = nullptr;
179 vector._size = 0;
180 vector.capacity = 0;
181 return *this;
182}
183
184template<typename T>
185DSC::Vector<T>::Vector(const Vector<T>& vector)
186{
187 _size = vector._size;
188 capacity = vector.capacity;
189 container = new T[capacity];
190
191 for(int i=0;i<_size;i++)
192 container[i] = vector.container[i];
193
194 // uncomment this to see where the vector copy is called
195 //#warning vector copy constructor
196
197 //dmaCopy(vector.container, container, _size * sizeof(T));
198}
199
200template<typename T>
202{
203 _size = vector._size;
204 capacity = vector.capacity;
205 delete[] container;
206 container = new T[capacity];
207
208 for(int i=0;i<_size;i++)
209 container[i] = vector.container[i];
210
211 //#warning vector copy =
212
213 //dmaCopy(vector.container, container, _size * sizeof(T));
214 return *this;
215}
216
217template<typename T>
219{
220 _size=0;
221}
222
223template<typename T>
225{
226 container_resize(1);
227 _size = 0;
228}
229
230template<typename T>
231void DSC::Vector<T>:: container_resize(int new_cap)
232{
233 if(sizeof(T)&1)
234 {
235 new_cap>>=1; new_cap++; new_cap<<=1;
236 }
237 T* new_container = new T[new_cap];
238 int min_cap = new_cap < _size ? new_cap : _size;
239 if(min_cap>0)
240 {
241 //dmaCopy(container, new_container, min_cap * sizeof(T));
242 for(int i=0;i<min_cap;i++)
243 {
244 new_container[i] = (T&&)container[i];
245 }
246 }
247 delete[] container;
248 container = new_container;
249 capacity = new_cap;
250}
251
252template<typename T>
253void DSC::Vector<T>::resize(int new_size)
254{
255 nds_assert(new_size>=0);
256 container_resize(new_size);
257 _size = new_size;
258}
259
260template<typename T>
261void DSC::Vector<T>::push_back(const T& item)
262{
263 if(_size==capacity)
264 {
265 container_resize(capacity<<1);
266 }
267 container[_size++]=item;
268}
269
270template<typename T>
272{
273 callstack_call
274
275 nds_assert(index>=0, "Index out of range");
276 nds_assert(index<_size, "Index out of range");
277
278 callstack_ret container[index];
279}
280
281template<typename T>
282const T& DSC::Vector<T>::operator[] (int index) const
283{
284 callstack_call
285
286 nds_assert(index>=0, "Index out of range");
287 nds_assert(index<_size, "Index out of range");
288
289 callstack_ret container[index];
290}
291
292template<typename T>
293const T& DSC::Vector<T>::get_at(int index) const
294{
295 callstack_call
296
297 nds_assert(index>=0, "Index out of range");
298 nds_assert(index<_size, "Index out of range");
299
300 callstack_ret container[index];
301}
302
303template<typename T>
304int DSC::Vector<T>::index_of(const T& item) const
305{
306 for(int i=0;i<_size;i++)
307 {
308 if(container[i]==item)
309 return i;
310 }
311 return -1;
312}
313
314template<typename T>
315bool DSC::Vector<T>::remove(const T& item)
316{
317 for(int i=0;i<_size;i++)
318 {
319 if(container[i]==item)
320 {
321 _size--;
322 for(int j=i;j<_size;j++)
323 {
324 container[j] = container[j+1];
325 }
326 return true;
327 }
328 }
329 return false;
330}
331
332template<typename T>
334{
335 callstack_call
336
337 nds_assert(index>=0, "Index out of range");
338 nds_assert(index<_size, "Index out of range");
339
340 _size--;
341 for(int j=index;j<_size;j++)
342 {
343 container[j] = container[j+1];
344 }
345 callstack_ret;
346}
347
348template<typename T>
350{
351 return _size;
352}
353
354template<typename T>
356{
357 delete[] container;
358}
359
360template<typename T>
362{
363 callstack_call
364 nds_assert(size()>0);
365 callstack_ret container[size()-1];
366}
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