2 #ifdef USE_INITIALIZER_LIST
3 #include "InitializerList.h"
67 return iterator(ptr + offset, offset);
69 bool operator==(
iterator it) {
return ptr == it.getPtr(); }
70 bool operator<(
iterator it) {
return ptr < it.getPtr(); }
71 bool operator<=(
iterator it) {
return ptr <= it.getPtr(); }
72 bool operator>(
iterator it) {
return ptr > it.getPtr(); }
73 bool operator>=(
iterator it) {
return ptr >= it.getPtr(); }
74 bool operator!=(
iterator it) {
return ptr != it.getPtr(); }
75 T &operator*() {
return *ptr; }
76 T *operator->() {
return ptr; }
77 T *getPtr() {
return ptr; }
78 size_t pos() {
return pos_; }
79 size_t operator-(
iterator it) {
return (ptr - it.getPtr()); }
82 #ifdef USE_INITIALIZER_LIST
85 Vector(std::initializer_list<T> iniList) {
86 resize(iniList.size());
88 for (
auto &obj : iniList) {
97 setAllocator(allocator);
98 resize_internal(len,
false,
false);
104 setAllocator(allocator);
109 setAllocator(allocator);
111 for (
int j = 0; j < size; j++) {
131 this->p_allocator = copyFrom.p_allocator;
132 resize_internal(copyFrom.size(),
false);
133 for (
int j = 0; j < copyFrom.size(); j++) {
134 p_data[j] = copyFrom[j];
136 this->len = copyFrom.size();
140 template <
typename TT,
int N>
142 resize_internal(N,
false);
143 for (
int j = 0; j < N; j++) {
151 resize_internal(copyFrom.size(),
false);
152 for (
int j = 0; j < copyFrom.size(); j++) {
153 p_data[j] = copyFrom[j];
155 this->len = copyFrom.size();
161 this->p_allocator = &allocator;
162 this->len = to - from;
163 resize_internal(this->len,
false);
164 for (
size_t j = 0; j < this->len; j++) {
172 void setAllocator(
Allocator &allocator) {
173 p_allocator = &allocator;
176 void clear() { len = 0; }
178 int size() {
return len; }
180 bool empty() {
return size() == 0; }
182 void push_back(T &&value) {
183 resize_internal(len + 1,
true);
188 void push_back(T &value) {
189 resize_internal(len + 1,
true);
194 void push_front(T &value) {
195 resize_internal(len + 1,
true);
197 for (
int j = len; j >= 0; j--) {
198 p_data[j + 1] = p_data[j];
204 void push_front(T &&value) {
205 resize_internal(len + 1,
true);
207 for (
int j = len; j >= 0; j--) {
208 p_data[j + 1] = p_data[j];
220 void pop_front() { erase(0); }
222 void assign(iterator v1, iterator v2) {
223 size_t newLen = v2 - v1;
224 resize_internal(newLen,
false);
227 for (
auto ptr = v1; ptr != v2; ptr++) {
228 p_data[pos++] = *ptr;
232 void assign(
size_t number, T value) {
233 resize_internal(number,
false);
235 for (
int j = 0; j < number; j++) {
240 void swap(Vector<T> &in) {
243 int bufferLenCpy = bufferLen;
248 bufferLen = in.bufferLen;
252 in.bufferLen = bufferLenCpy;
255 T &operator[](
int index) {
256 assert(p_data !=
nullptr);
257 return p_data[index];
260 T &operator[](
const int index)
const {
return p_data[index]; }
262 bool resize(
int newSize, T value) {
263 if (resize(newSize)) {
264 for (
int j = 0; j < newSize; j++) {
272 void shrink_to_fit() { resize_internal(this->len,
true,
true); }
274 int capacity() {
return this->bufferLen; }
276 bool resize(
int newSize) {
277 int oldSize = this->len;
278 resize_internal(newSize,
true);
280 return this->len != oldSize;
283 iterator begin() {
return iterator(p_data, 0); }
285 T &back() {
return *iterator(p_data + (len - 1), len - 1); }
287 iterator end() {
return iterator(p_data + len, len); }
290 void erase(iterator it) {
return erase(it.pos()); }
293 void erase(
int pos) {
295 int lenToEnd = len - pos - 1;
299 memmove((
void *)&p_data[pos], (
void *)(&p_data[pos + 1]),
300 lenToEnd *
sizeof(T));
303 #if defined(NO_INPLACE_INIT_SUPPORT)
304 p_data[len - 1] = T();
306 new (&(p_data[len - 1])) T();
312 T *data() {
return p_data; }
314 operator bool()
const {
return p_data !=
nullptr; }
317 for (
int j = 0; j < size(); j++) {
318 if (p_data[j] == obj)
return j;
323 bool contains(T obj){
324 return indexOf(obj) >= 0;
328 void swap(T &other) {
330 int temp_blen = bufferLen;
332 T *temp_data = p_data;
334 bufferLen = other.bufferLen;
336 p_data = other.p_data;
338 other.bufferLen = temp_blen;
339 other.len = temp_len;
340 other.p_data = temp_data;
346 deleteArray(p_data, size());
354 Allocator *p_allocator = &DefaultAllocator;
356 void resize_internal(
int newSize,
bool copy,
bool shrink =
false) {
357 if (newSize <= 0)
return;
358 if (newSize > bufferLen || this->p_data ==
nullptr || shrink) {
360 int oldBufferLen = this->bufferLen;
361 p_data = newArray(newSize);
362 assert(p_data !=
nullptr);
363 this->bufferLen = newSize;
364 if (oldData !=
nullptr) {
365 if (copy && this->len > 0) {
367 memmove((
void*)p_data, (
void*)oldData, len *
sizeof(T));
369 memset((
void*)oldData, 0, len *
sizeof(T));
372 cleanup(oldData, newSize, oldBufferLen);
374 deleteArray(oldData, oldBufferLen);
379 T *newArray(
int newSize) {
382 data = p_allocator->createArray<T>(newSize);
384 data =
new T[newSize];
389 void deleteArray(T *oldData,
int oldBufferLen) {
391 p_allocator->removeArray(oldData, oldBufferLen);
398 void cleanup(T *data,
int from,
int to) {
399 for (
int j = from; j < to; j++) {