Catos Engine (Source) 0.0.1
Lightweight Game engine
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1//
2// Created by allos on 8/22/2024.
3//
4
5#ifndef CATOS_VECTOR_H
6#define CATOS_VECTOR_H
7
8#include <cstring>
9
11
12 const char* what() {
13 return "Vector out of range";
14 }
15
16};
17
18namespace catos {
19
20
21 template<typename T>
22 class vector {
23
24 public:
25
27 vector(): buf(nullptr), size(0) {};
28
30 vector(const vector& obj) {
31 //Allocate enough mem then copy the item values
32 if (maxSize >= obj.size)
33 reserve(obj.size);
34
35
36 for (int i=0; i<obj.size; i++)
37 buf[i] = obj.buf[i];
38 }
39
41 vector(vector&& obj) {
42 buf = obj.buf;
43 size = obj.size;
44 maxSize = obj.size;
45
46 }
47
49 template<typename ... Args>
50 vector(Args&& ...args): buf(nullptr), size(0) {
51
52 reserve((unsigned int) sizeof...(Args));
53
54 (push_back(args), ...);
55 }
56
59 if (this == obj) return *this; // no self asignment.
60
61 if (maxSize >= obj.size)
62 reserve(obj.size);
63
64
65 for (int i=0; i<obj.size; i++)
66 buf[i] = obj.buf[i];
67
68 return *this;
69 }
70
71
73 void reserve(unsigned int amount) {
74
75 // no need if its the smaller or same size.
76 if (amount <= maxSize) return;
77
78
79 //allocate a new buffer with the desired size.
80 T* temp = new T[amount];
81
82 // copy our current items to that array.
83 for (int i=0; i < size; i++)
84 temp[i] = buf[i];
85
86
87 //destroy our previous allocated memory;
88 delete[] buf;
89
90 // Now our buffer should point to the newly allocated memory.
91 buf = temp;
92 maxSize = amount;
93 }
94
95 void resize(unsigned int newSize) {
96
97 // no need if its the smaller or same size.
98 if (newSize == maxSize) return;
99
100 if (newSize > size) throw out_of_range();
101
102
103 //allocate a new buffer with the desired size.
104 T* temp = new T[newSize];
105
106 // copy our current items to that array.
107 for (int i=0; i < newSize; i++)
108 temp[i] = buf[i];
109
110
111 //destroy our previous allocated memory;
112 delete[] buf;
113
114 // Now our buffer should point to the newly allocated memory.
115 buf = temp;
116 maxSize = newSize;
117 }
118
120 void push_back(T obj) {
121
122 if (size >= maxSize) {
123 // do 8 so that we dont have to allocate every push_back.
124 reserve(maxSize + 8);
125 }
126
127 //add the object to the internal array
128 buf[size] = obj;
129
130 //increase the size;
131 size++;
132
133 }
134
136 void clear() {
137
138
139 if (buf != nullptr) {
140 delete[] buf;
141 }
142
143 maxSize = 0;
144 size = 0;
145
146 };
147
149 void remove(int index) {
150
151 for (int i = index; i < size - 1; i++) {
152 buf[i] = buf[i + 1];
153 }
154
155 size--;
156 };
157
159 T& at(int index) {
160 return buf[index];
161 }
162
164 T& back() {
165 return buf[size - 1];
166 }
167
169 void pop_back() {
170 remove(size - 1);
171 }
172
174 T* data() { return buf; };
175
177 unsigned int length() {
178 return size;
179 }
180
182 bool empty() {
183 return size <= 0;
184 }
185
187 unsigned int maxLength() {
188 return maxSize;
189 }
190
192 T& operator[](int i) {
193 return buf[i];
194 }
195
196
197
200
201 if (buf != nullptr) {
202 delete[] buf;
203 }
204
205 };
206
207
208 /* --- Iterator stuff */
209
210 class iterator;
211
212 iterator begin();
213
214 iterator begin() const;
215
216 iterator end();
217
218 iterator end() const;
219
220 iterator cbegin() const;
221
222 iterator cend() const;
223
224
225
226
227 private:
228
229
230 T* buf = nullptr;
231 // The actual amount of items in the buffer
232 unsigned int size = 0;
233 // The max amount of items in the buffer.
234 unsigned int maxSize = 0;
235 };
236
237
238
239 template<class T> class vector<T>::iterator
240 {
241 public:
243 :_curr(p)
244 {}
245
247 {
248 _curr++;
249 return *this;
250 }
251
253 {
254 _curr--;
255 return *this;
256 }
257
259 {
260 return *_curr;
261 }
262
263 bool operator==(const iterator& b) const
264 {
265 return *_curr == *b._curr;
266 }
267
268 bool operator!=(const iterator& b) const
269 {
270 return _curr != b._curr;
271 }
272
273 private:
274 T* _curr;
275
276
277
278
279
280 };
281
282
283 template<class T>
285 {
286 return vector<T>::iterator(&buf[0]);
287 };
288
289 template<class T>
290 inline typename vector<T>::iterator vector<T>::begin() const {
291 return vector<T>::iterator(&buf[0]);
292 }
293
294 template<class T>
296 {
297 return vector<T>::iterator(&buf[size]);
298 }
299
300 template<class T>
301 inline typename vector<T>::iterator vector<T>::end() const {
302 return vector<T>::iterator(&buf[size]);
303 }
304
305 template<class T>
306 inline typename vector<T>::iterator vector<T>::cbegin() const {
307 return vector<T>::iterator(&buf[0]);
308 }
309
310 template<class T>
311 inline typename vector<T>::iterator vector<T>::cend() const {
312 return vector<T>::iterator(&buf[size]);
313 }
314
315
316
317}
318
319
320
321
322#endif //CATOS_VECTOR_H
Definition application.h:13
Definition vector.h:10
const char * what()
Definition vector.h:12
Definition vector.h:22
vector(vector &&obj)
Move Constructor.
Definition vector.h:41
T & back()
gives the last item.
Definition vector.h:164
~vector()
Deletes the allocated objects.
Definition vector.h:199
void remove(int index)
removes the item at the index
Definition vector.h:149
void clear()
clears the buffer
Definition vector.h:136
iterator end()
Definition vector.h:295
iterator begin() const
Definition vector.h:290
bool empty()
Returns if the size is <= 0.
Definition vector.h:182
T * data()
gives the internal buffer
Definition vector.h:174
void resize(unsigned int newSize)
Definition vector.h:95
vector(Args &&...args)
Initializes and calls push_back for all elements given.
Definition vector.h:50
iterator cend() const
Definition vector.h:311
T & at(int index)
returns the item at the desired index.
Definition vector.h:159
iterator begin()
Definition vector.h:284
vector()
Initializes the vector to empty.
Definition vector.h:27
unsigned int maxLength()
returns the maximum amount of objects in our internal buffer
Definition vector.h:187
void push_back(T obj)
Adds the new item to the array.
Definition vector.h:120
iterator cbegin() const
Definition vector.h:306
vector(const vector &obj)
Copy Constructor.
Definition vector.h:30
vector< T > & operator=(const vector< T > &obj)
Copies the obj.
Definition vector.h:58
unsigned int length()
returns the amount of objects in the internal buffer.
Definition vector.h:177
iterator end() const
Definition vector.h:301
T & operator[](int i)
Returns the obj for the given index.
Definition vector.h:192
void reserve(unsigned int amount)
allocates enough memory for the amount given.
Definition vector.h:73
void pop_back()
Removes the last item.
Definition vector.h:169
Definition vector.h:240
iterator & operator++()
Definition vector.h:246
bool operator==(const iterator &b) const
Definition vector.h:263
iterator & operator--()
Definition vector.h:252
T & operator*()
Definition vector.h:258
bool operator!=(const iterator &b) const
Definition vector.h:268
iterator(T *p)
Definition vector.h:242