Melon
A C++ library designed for RadishOS.
Loading...
Searching...
No Matches
Vector.hpp
1#pragma once
2
3#include "Internal/Memory/Buffer.hpp"
4#include "Typing.hpp"
5
7namespace Melon::Vector
8{
11 template<typename T>
12 class Vector
13 {
15 Typing::USize obj_count;
16 Typing::USize __capacity;
17
18 void extend(this Vector<T> &self)
19 {
20 self.__capacity *= 2;
21 self.buf.resize(self.__capacity);
22 }
23
24 public:
29 : obj_count(0), __capacity(1)
30 {
31 this->buf = Memory::Buffer<T>(new T[this->__capacity], this->__capacity);
32 }
33
36 template<Typing::USize N>
37 Vector(const T (&objects)[N])
38 {
39 this->__capacity = N;
40 this->buf = Memory::Buffer<T>(new T[this->__capacity], this->__capacity);
41
42 Typing::USize i = 0;
43 for (; i < N; i++)
44 this->buf[i] = objects[i];
45 this->obj_count = i;
46 }
47
50 Vector(const Vector<T> &other)
51 {
52 this->buf = other.buf;
53 this->__capacity = other.__capacity;
54 this->obj_count = other.obj_count;
55 }
56
60 {
61 this->buf = std::move(other.buf);
62 this->__capacity = other.__capacity;
63 this->obj_count = other.obj_count;
64
65 other.__capacity = 0;
66 other.obj_count = 0;
67 }
68
71 const T *begin(this const Vector<T> &self)
72 {
73 return self.buf.begin();
74 }
75
78 const T *end(this const Vector<T> &self)
79 {
80 return self.buf.begin() + self.obj_count;
81 }
82
88 void pushBack(this Vector<T> &self, const T &object)
89 {
90 if (self.obj_count >= self.__capacity)
91 self.extend();
92
93 self.buf[self.obj_count++] = object;
94 }
95
101 template<typename... ARGS>
102 void emplaceBack(this Vector<T> &self, ARGS &&...args)
103 {
104 if (self.obj_count >= self.__capacity)
105 self.extend();
106
107 self.buf[self.obj_count] = T(args...);
108 ++self.obj_count;
109 }
110
116 T &popBack(this Vector<T> &self)
117 {
118 if (self.obj_count > 0) {
119 --self.obj_count;
120 T &obj = self.buf[self.obj_count];
121 return obj;
122 } else {
123 throw Exceptions::OutOfRange(self.obj_count, 0);
124 }
125 }
126
129 void erase(this Vector<T> &self, Typing::USize index)
130 {
131 if (index >= self.obj_count)
132 throw Exceptions::OutOfRange(index, self.obj_count);
133
134 for (Typing::USize i = index; i < self.obj_count - 1; i++)
135 self.buf[i] = self.buf[i + 1];
136
137 --self.obj_count;
138 }
139
141 void clear(this Vector<T> &self)
142 {
143 self.buf.set(0, self.buf.size());
144 self.obj_count = 0;
145 }
146
149 const T *data(this const Vector<T> &self)
150 {
151 return self.buf.get();
152 }
153
157 {
158 Memory::Buffer<T> result(self.buf.get(), self.buf.size());
159 return result;
160 }
161
165 {
166 return self.obj_count;
167 }
168
172 {
173 return self.__capacity;
174 }
175
178 bool isEmpty(this const Vector<T> &self)
179 {
180 return self.obj_count == 0;
181 }
182
188 bool operator ==(this const Vector<T> &self, const Vector<T> &other)
189 {
190 return self.buf == other.buf;
191 }
192
195 T &operator [](this Vector<T> &self, Typing::USize index)
196 {
197 return self.buf[index];
198 }
199
202 const T &operator [](this const Vector<T> &self, Typing::USize index)
203 {
204 return self.buf[index];
205 }
206
210 Vector<T> &operator =(this Vector<T> &self, const Vector<T> &other)
211 {
212 if (&self != &other) {
213 self.buf = other.buf;
214 self.__capacity = other.__capacity;
215 self.obj_count = other.obj_count;
216 }
217
218 return self;
219 }
220
225 {
226 if (&self != &other) {
227 self.buf = std::move(other.buf);
228 self.__capacity = other.__capacity;
229 self.obj_count = other.obj_count;
230
231 other.__capacity = 0;
232 other.obj_count = 0;
233 }
234
235 return self;
236 }
237 };
238} // namespace Melon::Vector
Thrown when an index is out of range.
Definition OutOfRange.hpp:11
Container for raw memory.
Definition Buffer.hpp:18
Vector(const Vector< T > &other)
Copy constructor.
Definition Vector.hpp:50
void erase(this Vector< T > &self, Typing::USize index)
Erases object at a given position in the vector.
Definition Vector.hpp:129
Vector(const T(&objects)[N])
Constructs the vector from existing objects.
Definition Vector.hpp:37
const T * data(this const Vector< T > &self)
Gets a pointer to the internal buffer storage.
Definition Vector.hpp:149
Vector< T > & operator=(this Vector< T > &self, const Vector< T > &other)
Copy assignment operator.
Definition Vector.hpp:210
Vector(Vector< T > &&other)
Move constructor.
Definition Vector.hpp:59
Memory::Buffer< T > toBuffer(this const Vector< T > &self)
Gets a copy of the internal buffer.
Definition Vector.hpp:156
void emplaceBack(this Vector< T > &self, ARGS &&...args)
Constructs an object and add it at the end of the vector.
Definition Vector.hpp:102
Vector()
Default constructor.
Definition Vector.hpp:28
const T * begin(this const Vector< T > &self)
Gets a pointer to the base of the data.
Definition Vector.hpp:71
T & operator[](this Vector< T > &self, Typing::USize index)
Gets an object from the vector.
Definition Vector.hpp:195
void pushBack(this Vector< T > &self, const T &object)
Add an object to the end of the vector.
Definition Vector.hpp:88
Typing::USize length(this const Vector< T > &self)
Gets the length of the vector.
Definition Vector.hpp:164
bool isEmpty(this const Vector< T > &self)
Checks if the vector is empty.
Definition Vector.hpp:178
const T * end(this const Vector< T > &self)
Gets a pointer to the end of the data.
Definition Vector.hpp:78
bool operator==(this const Vector< T > &self, const Vector< T > &other)
Checks if both buffers are the same.
Definition Vector.hpp:188
Typing::USize capacity(this const Vector< T > &self)
Gets the capacity of the vector.
Definition Vector.hpp:171
T & popBack(this Vector< T > &self)
Removes the last object from the vector.
Definition Vector.hpp:116
void clear(this Vector< T > &self)
Clears the vector.
Definition Vector.hpp:141
size_t USize
Biggest unsigned type.
Definition Typing.hpp:38
Contains dynamic vector manipulation features.
Definition Vector.hpp:8