Arduino DLNA Server
Vector.h
Go to the documentation of this file.
1 #pragma once
2 #ifdef USE_INITIALIZER_LIST
3 #include "InitializerList.h"
4 #endif
5 #include <assert.h>
6 
7 #include "Allocator.h"
8 
9 namespace tiny_dlna {
10 
20 template <class T>
21 class Vector {
22  public:
29  class iterator {
30  protected:
31  T *ptr;
32  size_t pos_;
33 
34  public:
35  iterator() {}
36  iterator(T *parPtr, size_t pos) {
37  this->ptr = parPtr;
38  this->pos_ = pos;
39  }
40  // copy constructor
41  iterator(const iterator &copyFrom) {
42  ptr = copyFrom.ptr;
43  pos_ = copyFrom.pos_;
44  }
46  ptr++;
47  pos_++;
48  return *this;
49  }
51  ptr++;
52  pos_++;
53  return *this;
54  }
56  ptr--;
57  pos_--;
58  return *this;
59  }
61  ptr--;
62  pos_--;
63  return *this;
64  }
65  iterator operator+(int offset) {
66  pos_ += offset;
67  return iterator(ptr + offset, offset);
68  }
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()); }
80  };
81 
82 #ifdef USE_INITIALIZER_LIST
83 
85  Vector(std::initializer_list<T> iniList) {
86  resize(iniList.size());
87  int pos = 0;
88  for (auto &obj : iniList) {
89  p_data[pos++] = obj;
90  }
91  }
92 
93 #endif
94 
96  Vector(size_t len = 0, Allocator &allocator = DefaultAllocator) {
97  setAllocator(allocator);
98  resize_internal(len, false);
99  }
100 
102  Vector(Allocator &allocator) { setAllocator(allocator); }
103 
105  Vector(int size, T value, Allocator &allocator = DefaultAllocator) {
106  setAllocator(allocator);
107  resize(size);
108  for (int j = 0; j < size; j++) {
109  p_data[j] = value;
110  }
111  }
112 
114  Vector(Vector<T> &&moveFrom) {
115  swap(moveFrom);
116  moveFrom.clear();
117  };
118 
120  Vector &operator=(Vector &&moveFrom) {
121  swap(moveFrom);
122  moveFrom.clear();
123  return *this;
124  }
125 
127  Vector(Vector<T> &copyFrom) {
128  this->p_allocator = copyFrom.p_allocator;
129  resize_internal(copyFrom.size(), false);
130  for (int j = 0; j < copyFrom.size(); j++) {
131  p_data[j] = copyFrom[j];
132  }
133  this->len = copyFrom.size();
134  }
135 
138  resize_internal(copyFrom.size(), false);
139  for (int j = 0; j < copyFrom.size(); j++) {
140  p_data[j] = copyFrom[j];
141  }
142  this->len = copyFrom.size();
143  return *this;
144  }
145 
147  Vector(T *from, T *to, Allocator &allocator = DefaultAllocator) {
148  this->p_allocator = &allocator;
149  this->len = to - from;
150  resize_internal(this->len, false);
151  for (size_t j = 0; j < this->len; j++) {
152  p_data[j] = from[j];
153  }
154  }
155 
157  virtual ~Vector() { reset(); }
158 
159  void setAllocator(Allocator &allocator) { p_allocator = &allocator; }
160 
161  void clear() { len = 0; }
162 
163  int size() { return len; }
164 
165  bool empty() { return size() == 0; }
166 
167  void push_back(T &&value) {
168  resize_internal(len + 1, true);
169  p_data[len] = value;
170  len++;
171  }
172 
173  void push_back(T &value) {
174  resize_internal(len + 1, true);
175  p_data[len] = value;
176  len++;
177  }
178 
179  void push_front(T &value) {
180  resize_internal(len + 1, true);
181  // memmove(p_data,p_data+1,len*sizeof(T));
182  for (int j = len; j >= 0; j--) {
183  p_data[j + 1] = p_data[j];
184  }
185  p_data[0] = value;
186  len++;
187  }
188 
189  void push_front(T &&value) {
190  resize_internal(len + 1, true);
191  // memmove(p_data,p_data+1,len*sizeof(T));
192  for (int j = len; j >= 0; j--) {
193  p_data[j + 1] = p_data[j];
194  }
195  p_data[0] = value;
196  len++;
197  }
198 
199  void pop_back() {
200  if (len > 0) {
201  len--;
202  }
203  }
204 
205  void pop_front() { erase(0); }
206 
207  void assign(iterator v1, iterator v2) {
208  size_t newLen = v2 - v1;
209  resize_internal(newLen, false);
210  this->len = newLen;
211  int pos = 0;
212  for (auto ptr = v1; ptr != v2; ptr++) {
213  p_data[pos++] = *ptr;
214  }
215  }
216 
217  void assign(size_t number, T value) {
218  resize_internal(number, false);
219  this->len = number;
220  for (int j = 0; j < number; j++) {
221  p_data[j] = value;
222  }
223  }
224 
225  void swap(Vector<T> &in) {
226  // save data
227  T *dataCpy = p_data;
228  int bufferLenCpy = max_capacity;
229  int lenCpy = len;
230  // swap this
231  p_data = in.p_data;
232  len = in.len;
234  // swp in
235  in.p_data = dataCpy;
236  in.len = lenCpy;
237  in.max_capacity = bufferLenCpy;
238  }
239 
240  T &operator[](int index) {
241  assert(p_data != nullptr);
242  return p_data[index];
243  }
244 
245  T &operator[](const int index) const { return p_data[index]; }
246 
247  bool resize(int newSize, T value) {
248  if (resize(newSize)) {
249  for (int j = 0; j < newSize; j++) {
250  p_data[j] = value;
251  }
252  return true;
253  }
254  return false;
255  }
256 
257  void shrink_to_fit() { resize_internal(this->len, true, true); }
258 
259  int capacity() { return this->max_capacity; }
260 
261  bool resize(int newSize) {
262  int oldSize = this->len;
263  resize_internal(newSize, true);
264  this->len = newSize;
265  return this->len != oldSize;
266  }
267 
268  iterator begin() { return iterator(p_data, 0); }
269 
270  T &back() { return *iterator(p_data + (len - 1), len - 1); }
271 
272  iterator end() { return iterator(p_data + len, len); }
273 
274  // removes a single element
275  void erase(iterator it) { return erase(it.pos()); }
276 
277  // removes a single element
278  void erase(int pos) {
279  if (pos < len) {
280  int lenToEnd = len - pos - 1;
281  // call destructor on data to be erased
282  p_data[pos].~T();
283  // shift values by 1 position
284  memmove((void *)&p_data[pos], (void *)(&p_data[pos + 1]),
285  lenToEnd * sizeof(T));
286 
287  // make sure that we have a valid object at the end
288  // p_data[len - 1] = T();
289  new (&(p_data[len - 1])) T();
290  len--;
291  }
292  }
293 
294  T *data() { return p_data; }
295 
296  operator bool() const { return p_data != nullptr; }
297 
298  int indexOf(T obj) {
299  for (int j = 0; j < size(); j++) {
300  if (p_data[j] == obj) return j;
301  }
302  return -1;
303  }
304 
305  bool contains(T obj) { return indexOf(obj) >= 0; }
306 
307  void swap(T &other) {
308  // save values
309  int temp_blen = max_capacity;
310  int temp_len = len;
311  T *temp_data = p_data;
312  // swap from other
313  max_capacity = other.bufferLen;
314  len = other.len;
315  p_data = other.p_data;
316  // set other
317  other.bufferLen = temp_blen;
318  other.len = temp_len;
319  other.p_data = temp_data;
320  }
321 
322  void reset() {
323  clear();
324  shrink_to_fit();
325  deleteArray(p_data, size()); // delete [] this->p_data;
326  p_data = nullptr;
327  }
328 
329  protected:
330  int max_capacity = 0;
331  int len = 0;
332  T *p_data = nullptr;
333  Allocator *p_allocator = &DefaultAllocator;
334 
335  void resize_internal(int newSize, bool copy, bool shrink = false) {
336  if (newSize <= 0) return;
337  if (newSize > max_capacity || this->p_data == nullptr || shrink) {
338  T *oldData = p_data;
339  int oldBufferLen = this->max_capacity;
340  p_data = newArray(newSize); // new T[newSize+1];
341  assert(p_data != nullptr);
342  this->max_capacity = newSize;
343  if (oldData != nullptr) {
344  if (copy && this->len > 0) {
345  // save existing data
346  memmove((void *)p_data, (void *)oldData, len * sizeof(T));
347  // clear to prevent double release
348  memset((void *)oldData, 0, len * sizeof(T));
349  }
350  if (shrink) {
351  cleanup(oldData, newSize, oldBufferLen);
352  }
353  deleteArray(oldData, oldBufferLen); // delete [] oldData;
354  }
355  }
356  }
357 
358  T *newArray(int newSize) {
359  T *data;
360 #if USE_ALLOCATOR
361  data = p_allocator->createArray<T>(newSize); // new T[newSize+1];
362 #else
363  data = new T[newSize];
364 #endif
365  return data;
366  }
367 
368  void deleteArray(T *oldData, int oldBufferLen) {
369 #if USE_ALLOCATOR
370  p_allocator->removeArray(oldData, oldBufferLen); // delete [] oldData;
371 #else
372  delete[] oldData;
373 #endif
374  }
375 
376  void cleanup(T *data, int from, int to) {
377  for (int j = from; j < to; j++) {
378  data[j].~T();
379  }
380  }
381 };
382 
383 } // namespace tiny_dlna
Memory allocateator which uses malloc.
Definition: Allocator.h:21
void removeArray(T *obj, int len)
Definition: Allocator.h:52
T * createArray(int len)
Definition: Allocator.h:42
Iterator for the Vector class.
Definition: Vector.h:29
iterator operator--(int n)
Definition: Vector.h:55
iterator(const iterator &copyFrom)
Definition: Vector.h:41
T * operator->()
Definition: Vector.h:76
T & operator*()
Definition: Vector.h:75
bool operator>(iterator it)
Definition: Vector.h:72
size_t pos_
Definition: Vector.h:32
size_t pos()
Definition: Vector.h:78
T * getPtr()
Definition: Vector.h:77
iterator operator+(int offset)
Definition: Vector.h:65
size_t operator-(iterator it)
Definition: Vector.h:79
bool operator>=(iterator it)
Definition: Vector.h:73
iterator operator++(int n)
Definition: Vector.h:45
bool operator<(iterator it)
Definition: Vector.h:70
bool operator!=(iterator it)
Definition: Vector.h:74
iterator(T *parPtr, size_t pos)
Definition: Vector.h:36
iterator()
Definition: Vector.h:35
bool operator<=(iterator it)
Definition: Vector.h:71
T * ptr
Definition: Vector.h:31
bool operator==(iterator it)
Definition: Vector.h:69
iterator operator--()
Definition: Vector.h:60
iterator operator++()
Definition: Vector.h:50
Vector implementation which provides the most important methods as defined by std::vector....
Definition: Vector.h:21
virtual ~Vector()
Destructor.
Definition: Vector.h:157
void assign(iterator v1, iterator v2)
Definition: Vector.h:207
T & operator[](int index)
Definition: Vector.h:240
void erase(int pos)
Definition: Vector.h:278
void resize_internal(int newSize, bool copy, bool shrink=false)
Definition: Vector.h:335
void swap(T &other)
Definition: Vector.h:307
Allocator * p_allocator
Definition: Vector.h:333
Vector & operator=(Vector &&moveFrom)
Move operator.
Definition: Vector.h:120
Vector< T > & operator=(Vector< T > &copyFrom)
copy operator
Definition: Vector.h:137
void clear()
Definition: Vector.h:161
void cleanup(T *data, int from, int to)
Definition: Vector.h:376
bool resize(int newSize, T value)
Definition: Vector.h:247
bool empty()
Definition: Vector.h:165
void assign(size_t number, T value)
Definition: Vector.h:217
Vector(Vector< T > &&moveFrom)
Move constructor.
Definition: Vector.h:114
Vector(T *from, T *to, Allocator &allocator=DefaultAllocator)
legacy constructor with pointer range
Definition: Vector.h:147
Vector(Allocator &allocator)
Constructor with only allocator.
Definition: Vector.h:102
void reset()
Definition: Vector.h:322
iterator end()
Definition: Vector.h:272
int size()
Definition: Vector.h:163
bool contains(T obj)
Definition: Vector.h:305
void push_back(T &&value)
Definition: Vector.h:167
T & back()
Definition: Vector.h:270
T & operator[](const int index) const
Definition: Vector.h:245
iterator begin()
Definition: Vector.h:268
bool resize(int newSize)
Definition: Vector.h:261
void swap(Vector< T > &in)
Definition: Vector.h:225
void pop_front()
Definition: Vector.h:205
int indexOf(T obj)
Definition: Vector.h:298
void push_front(T &value)
Definition: Vector.h:179
void push_front(T &&value)
Definition: Vector.h:189
int max_capacity
Definition: Vector.h:330
int len
Definition: Vector.h:331
void shrink_to_fit()
Definition: Vector.h:257
T * p_data
Definition: Vector.h:332
T * data()
Definition: Vector.h:294
void deleteArray(T *oldData, int oldBufferLen)
Definition: Vector.h:368
void push_back(T &value)
Definition: Vector.h:173
Vector(int size, T value, Allocator &allocator=DefaultAllocator)
Allocate size and initialize array.
Definition: Vector.h:105
void pop_back()
Definition: Vector.h:199
Vector(Vector< T > &copyFrom)
copy constructor
Definition: Vector.h:127
void erase(iterator it)
Definition: Vector.h:275
T * newArray(int newSize)
Definition: Vector.h:358
int capacity()
Definition: Vector.h:259
Vector(size_t len=0, Allocator &allocator=DefaultAllocator)
Default constructor: size 0 with DefaultAllocator.
Definition: Vector.h:96
void setAllocator(Allocator &allocator)
Definition: Vector.h:159
Definition: Allocator.h:6