1
0

Vectors.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #ifndef NULLSOFT_VECTOR_H
  2. #define NULLSOFT_VECTOR_H
  3. #include <assert.h>
  4. #include <bfc/platform/types.h>
  5. template <class Type, int INCREMENT = 32, int MULTIPLIER=1>
  6. class Vector
  7. {
  8. public:
  9. typedef Type *iterator;
  10. typedef const Type *const_iterator;
  11. public:
  12. Vector() {}
  13. virtual ~Vector()
  14. {
  15. delete [] values;
  16. }
  17. Vector(const Vector<Type, INCREMENT, MULTIPLIER> &copy)
  18. {
  19. if (copy.numPtrs)
  20. {
  21. values = new Type[copy.numPtrs];
  22. allocSize = copy.numPtrs;
  23. numPtrs = copy.numPtrs;
  24. for (size_t i = 0;i != numPtrs;i++)
  25. {
  26. values[i] = copy.values[i];
  27. }
  28. }
  29. }
  30. void operator=(const Vector<Type, INCREMENT, MULTIPLIER> &copy)
  31. {
  32. Reset();
  33. if (copy.numPtrs)
  34. {
  35. values = new Type[copy.numPtrs];
  36. allocSize = copy.numPtrs;
  37. numPtrs = copy.numPtrs;
  38. for (size_t i = 0;i != numPtrs;i++)
  39. {
  40. values[i] = copy.values[i];
  41. }
  42. }
  43. }
  44. Type &operator[](size_t index)
  45. {
  46. return values[index];
  47. }
  48. Type *data()
  49. {
  50. return values;
  51. }
  52. Type *begin() const
  53. {
  54. return values;
  55. }
  56. Type *end() const
  57. {
  58. if (values) return values + numPtrs; else return 0;
  59. }
  60. void Reset()
  61. {
  62. delete [] values; values = 0; numPtrs = 0; allocSize=0;
  63. }
  64. void clear()
  65. {
  66. numPtrs = 0;
  67. }
  68. size_t size() const
  69. {
  70. return numPtrs;
  71. }
  72. size_t capacity()
  73. {
  74. return allocSize;
  75. }
  76. Type &back()
  77. {
  78. return values[numPtrs-1];
  79. }
  80. Type &at(size_t index) const
  81. {
  82. return values[index];
  83. }
  84. void reserve(size_t size)
  85. {
  86. if (size <= numPtrs)
  87. return;
  88. Type *newTable = new Type[size];
  89. for (size_t i = 0;i != numPtrs;i++)
  90. {
  91. newTable[i] = values[i];
  92. }
  93. allocSize = size;
  94. delete[] values;
  95. values = newTable;
  96. }
  97. void push_back(Type t)
  98. {
  99. if (numPtrs == allocSize)
  100. reserve(allocSize*MULTIPLIER + INCREMENT);
  101. values[numPtrs++] = t;
  102. }
  103. void insert(size_t index, const Type &value)
  104. {
  105. if (numPtrs == allocSize)
  106. reserve(allocSize*MULTIPLIER + INCREMENT);
  107. for (size_t i = numPtrs;i != index;i--)
  108. {
  109. values[i] = values[i-1];
  110. }
  111. values[index] = value;
  112. numPtrs++;
  113. }
  114. void append(size_t size, Type *t)
  115. {
  116. reserve(numPtrs + size + INCREMENT);
  117. for (size_t i = 0;i != size;i++)
  118. {
  119. push_back(t[i]);
  120. }
  121. }
  122. void pop_back()
  123. {
  124. if (numPtrs)
  125. {
  126. numPtrs--;
  127. // next line removed to allow structs and classes
  128. // values[numPtrs] = 0; // TODO: an inplace delete might be better?
  129. }
  130. }
  131. void erase(iterator itr)
  132. {
  133. size_t index = itr - values;
  134. eraseAt(index);
  135. }
  136. void eraseAt(size_t index)
  137. {
  138. if (numPtrs > index)
  139. {
  140. for (size_t k = index + 1; k < numPtrs; k++)
  141. values[k-1] = values[k];
  142. --numPtrs;
  143. }
  144. }
  145. /* Removes an item by swapping it with the last item in the list. faster but can ruin order */
  146. void eraseAtFast(size_t index)
  147. {
  148. if (index < numPtrs)
  149. {
  150. values[index] = values[--numPtrs];
  151. // if (numPtrs != index)
  152. // values[numPtrs]=0;
  153. }
  154. }
  155. bool empty() const
  156. {
  157. return numPtrs == 0;
  158. }
  159. void resize(size_t newSize, Type val)
  160. {
  161. if (newSize < numPtrs)
  162. {
  163. numPtrs = newSize;
  164. }
  165. else if (newSize > numPtrs)
  166. {
  167. reserve(allocSize + (newSize - numPtrs) + INCREMENT);
  168. while(numPtrs < newSize)
  169. {
  170. values[numPtrs] = val;
  171. numPtrs++;
  172. }
  173. }
  174. }
  175. void resize(size_t newSize)
  176. {
  177. if (newSize < numPtrs)
  178. {
  179. numPtrs = newSize;
  180. }
  181. else if (newSize > numPtrs)
  182. {
  183. reserve(allocSize + (newSize - numPtrs) + INCREMENT);
  184. numPtrs = newSize;
  185. }
  186. }
  187. void set(Type *ptr, size_t num)
  188. {
  189. delete [] values;
  190. values=ptr;
  191. numPtrs=num;
  192. }
  193. private:
  194. size_t numPtrs = 0;
  195. size_t allocSize = 0;
  196. Type *values = 0;
  197. };
  198. #endif