Vector.pkg 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. $#include "Vector.h"
  2. $#include "Vector3.h"
  3. /// %Vector template class.
  4. class Vector : public VectorBase
  5. {
  6. TOLUA_TEMPLATE_BIND(T, String)
  7. public:
  8. /// Construct empty.
  9. Vector()
  10. {
  11. }
  12. /// Construct with initial size.
  13. explicit Vector(unsigned size)
  14. {
  15. Resize(size, 0);
  16. }
  17. /// Construct with initial data.
  18. Vector(const T* data, unsigned size)
  19. {
  20. Resize(size, data);
  21. }
  22. /// Construct from another vector.
  23. Vector(const Vector<T>& vector)
  24. {
  25. *this = vector;
  26. }
  27. /// Destruct.
  28. ~Vector()
  29. {
  30. Clear();
  31. delete[] buffer_;
  32. }
  33. /// Add an element.
  34. Vector<T> operator + (const T& rhs) const
  35. {
  36. Vector<T> ret(*this);
  37. ret.Push(rhs);
  38. return ret;
  39. }
  40. /// Add another vector.
  41. Vector<T> operator + (const Vector<T>& rhs) const
  42. {
  43. Vector<T> ret(*this);
  44. ret.Push(rhs);
  45. return ret;
  46. }
  47. /// Test for equality with another vector.
  48. bool operator == (const Vector<T>& rhs) const
  49. {
  50. if (rhs.size_ != size_)
  51. return false;
  52. T* buffer = Buffer();
  53. T* rhsBuffer = rhs.Buffer();
  54. for (unsigned i = 0; i < size_; ++i)
  55. {
  56. if (buffer[i] != rhsBuffer[i])
  57. return false;
  58. }
  59. return true;
  60. }
  61. /// Return element at index.
  62. T& operator [] (unsigned index) { assert(index < size_); return Buffer()[index]; }
  63. /// Return const element at index.
  64. const T& operator [] (unsigned index) const { assert(index < size_); return Buffer()[index]; }
  65. /// Return element at index.
  66. T& At(unsigned index) { assert(index < size_); return Buffer()[index]; }
  67. /// Return const element at index.
  68. const T& At(unsigned index) const { assert(index < size_); return Buffer()[index]; }
  69. /// Add an element at the end.
  70. void Push(const T& value) { Resize(size_ + 1, &value); }
  71. /// Add another vector at the end.
  72. void Push(const Vector<T>& vector) { Resize(size_ + vector.size_, vector.Buffer()); }
  73. /// Remove the last element.
  74. void Pop()
  75. {
  76. if (size_)
  77. Resize(size_ - 1, 0);
  78. }
  79. /// Insert an element at position.
  80. void Insert(unsigned pos, const T& value)
  81. {
  82. if (pos > size_)
  83. pos = size_;
  84. unsigned oldSize = size_;
  85. Resize(size_ + 1, 0);
  86. MoveRange(pos + 1, pos, oldSize - pos);
  87. Buffer()[pos] = value;
  88. }
  89. /// Insert another vector at position.
  90. void Insert(unsigned pos, const Vector<T>& vector)
  91. {
  92. if (pos > size_)
  93. pos = size_;
  94. unsigned oldSize = size_;
  95. Resize(size_ + vector.size_, 0);
  96. MoveRange(pos + vector.size_, pos, oldSize - pos);
  97. CopyElements(Buffer() + pos, vector.Buffer(), vector.size_);
  98. }
  99. /// Erase a range of elements.
  100. void Erase(unsigned pos, unsigned length = 1)
  101. {
  102. // Return if the range is illegal
  103. if (pos + length > size_ || !length)
  104. return;
  105. MoveRange(pos, pos + length, size_ - pos - length);
  106. Resize(size_ - length, 0);
  107. }
  108. /// Erase an element if found.
  109. bool Remove(const T& value)
  110. {
  111. Iterator i = Find(value);
  112. if (i != End())
  113. {
  114. Erase(i);
  115. return true;
  116. }
  117. else
  118. return false;
  119. }
  120. /// Clear the vector.
  121. void Clear() { Resize(0); }
  122. /// Resize the vector.
  123. void Resize(unsigned newSize) { Resize(newSize, 0); }
  124. /// Set new capacity.
  125. void Reserve(unsigned newCapacity)
  126. {
  127. if (newCapacity < size_)
  128. newCapacity = size_;
  129. if (newCapacity != capacity_)
  130. {
  131. T* newBuffer = 0;
  132. capacity_ = newCapacity;
  133. if (capacity_)
  134. {
  135. newBuffer = reinterpret_cast<T*>(AllocateBuffer(capacity_ * sizeof(T)));
  136. // Move the data into the new buffer
  137. ConstructElements(newBuffer, Buffer(), size_);
  138. }
  139. // Delete the old buffer
  140. DestructElements(Buffer(), size_);
  141. delete[] buffer_;
  142. buffer_ = reinterpret_cast<unsigned char*>(newBuffer);
  143. }
  144. }
  145. /// Reallocate so that no extra memory is used.
  146. void Compact() { Reserve(size_); }
  147. /// Return whether contains a specific value.
  148. bool Contains(const T& value) const { return Find(value) != End(); }
  149. /// Return first element.
  150. T& Front() { assert(size_); return Buffer()[0]; }
  151. /// Return const first element.
  152. const T& Front() const { assert(size_); return Buffer()[0]; }
  153. /// Return last element.
  154. T& Back() { assert(size_); return Buffer()[size_ - 1]; }
  155. /// Return const last element.
  156. const T& Back() const { assert(size_); return Buffer()[size_ - 1]; }
  157. /// Return size of vector.
  158. unsigned Size() const { return size_; }
  159. /// Return capacity of vector.
  160. unsigned Capacity() const { return capacity_; }
  161. /// Return whether vector is empty.
  162. bool Empty() const { return size_ == 0; }
  163. };
  164. /// %Vector template class for POD types. Does not call constructors or destructors and uses block move.
  165. class PODVector
  166. {
  167. TOLUA_TEMPLATE_BIND(T, Vector3)
  168. public:
  169. /// Construct empty.
  170. PODVector()
  171. {
  172. }
  173. /// Construct with initial size.
  174. explicit PODVector(unsigned size)
  175. {
  176. Resize(size);
  177. }
  178. /// Construct with initial data.
  179. PODVector(const T* data, unsigned size)
  180. {
  181. Resize(size);
  182. CopyElements(Buffer(), data, size);
  183. }
  184. /// Destruct.
  185. ~PODVector()
  186. {
  187. delete[] buffer_;
  188. }
  189. /// Add an element.
  190. PODVector<T> operator + (const T& rhs) const
  191. {
  192. PODVector<T> ret(*this);
  193. ret.Push(rhs);
  194. return ret;
  195. }
  196. /// Add another vector.
  197. PODVector<T> operator + (const PODVector<T>& rhs) const
  198. {
  199. PODVector<T> ret(*this);
  200. ret.Push(rhs);
  201. return ret;
  202. }
  203. /// Test for equality with another vector.
  204. bool operator == (const PODVector<T>& rhs) const
  205. {
  206. if (rhs.size_ != size_)
  207. return false;
  208. T* buffer = Buffer();
  209. T* rhsBuffer = rhs.Buffer();
  210. for (unsigned i = 0; i < size_; ++i)
  211. {
  212. if (buffer[i] != rhsBuffer[i])
  213. return false;
  214. }
  215. return true;
  216. }
  217. /// Return element at index.
  218. T& operator [] (unsigned index) { assert(index < size_); return Buffer()[index]; }
  219. /// Return const element at index.
  220. const T& operator [] (unsigned index) const { assert(index < size_); return Buffer()[index]; }
  221. /// Return element at index.
  222. T& At(unsigned index) { assert(index < size_); return Buffer()[index]; }
  223. /// Return const element at index.
  224. const T& At(unsigned index) const { assert(index < size_); return Buffer()[index]; }
  225. /// Add an element at the end.
  226. void Push(const T& value)
  227. {
  228. if (size_ < capacity_)
  229. ++size_;
  230. else
  231. Resize(size_ + 1);
  232. Back() = value;
  233. }
  234. /// Add another vector at the end.
  235. void Push(const PODVector<T>& vector)
  236. {
  237. unsigned oldSize = size_;
  238. Resize(size_ + vector.size_);
  239. CopyElements(Buffer() + oldSize, vector.Buffer(), vector.size_);
  240. }
  241. /// Remove the last element.
  242. void Pop()
  243. {
  244. if (size_)
  245. Resize(size_ - 1);
  246. }
  247. /// Insert an element at position.
  248. void Insert(unsigned pos, const T& value)
  249. {
  250. if (pos > size_)
  251. pos = size_;
  252. unsigned oldSize = size_;
  253. Resize(size_ + 1);
  254. MoveRange(pos + 1, pos, oldSize - pos);
  255. Buffer()[pos] = value;
  256. }
  257. /// Insert another vector at position.
  258. void Insert(unsigned pos, const PODVector<T>& vector)
  259. {
  260. if (pos > size_)
  261. pos = size_;
  262. unsigned oldSize = size_;
  263. Resize(size_ + vector.size_);
  264. MoveRange(pos + vector.size_, pos, oldSize - pos);
  265. CopyElements(Buffer() + pos, vector.Buffer(), vector.size_);
  266. }
  267. /// Erase a range of elements.
  268. void Erase(unsigned pos, unsigned length = 1)
  269. {
  270. // Return if the range is illegal
  271. if (!length || pos + length > size_)
  272. return;
  273. MoveRange(pos, pos + length, size_ - pos - length);
  274. Resize(size_ - length);
  275. }
  276. /// Erase an element if found.
  277. bool Remove(const T& value)
  278. {
  279. Iterator i = Find(value);
  280. if (i != End())
  281. {
  282. Erase(i);
  283. return true;
  284. }
  285. else
  286. return false;
  287. }
  288. /// Clear the vector.
  289. void Clear() { Resize(0); }
  290. /// Resize the vector.
  291. void Resize(unsigned newSize)
  292. {
  293. if (newSize > capacity_)
  294. {
  295. if (!capacity_)
  296. capacity_ = newSize;
  297. else
  298. {
  299. while (capacity_ < newSize)
  300. capacity_ += (capacity_ + 1) >> 1;
  301. }
  302. unsigned char* newBuffer = AllocateBuffer(capacity_ * sizeof(T));
  303. // Move the data into the new buffer and delete the old
  304. if (buffer_)
  305. {
  306. CopyElements(reinterpret_cast<T*>(newBuffer), Buffer(), size_);
  307. delete[] buffer_;
  308. }
  309. buffer_ = newBuffer;
  310. }
  311. size_ = newSize;
  312. }
  313. /// Set new capacity.
  314. void Reserve(unsigned newCapacity)
  315. {
  316. if (newCapacity < size_)
  317. newCapacity = size_;
  318. if (newCapacity != capacity_)
  319. {
  320. unsigned char* newBuffer = 0;
  321. capacity_ = newCapacity;
  322. if (capacity_)
  323. {
  324. newBuffer = AllocateBuffer(capacity_ * sizeof(T));
  325. // Move the data into the new buffer
  326. CopyElements(reinterpret_cast<T*>(newBuffer), Buffer(), size_);
  327. }
  328. // Delete the old buffer
  329. delete[] buffer_;
  330. buffer_ = newBuffer;
  331. }
  332. }
  333. /// Reallocate so that no extra memory is used.
  334. void Compact() { Reserve(size_); }
  335. /// Return whether contains a specific value.
  336. bool Contains(const T& value) const { return Find(value) != End(); }
  337. /// Return first element.
  338. T& Front() { return Buffer()[0]; }
  339. /// Return const first element.
  340. const T& Front() const { return Buffer()[0]; }
  341. /// Return last element.
  342. T& Back() { assert(size_); return Buffer()[size_ - 1]; }
  343. /// Return const last element.
  344. const T& Back() const { assert(size_); return Buffer()[size_ - 1]; }
  345. /// Return number of elements.
  346. unsigned Size() const { return size_; }
  347. /// Return capacity of vector.
  348. unsigned Capacity() const { return capacity_; }
  349. /// Return whether vector is empty.
  350. bool Empty() const { return size_ == 0; }
  351. };