Vector.pkg 11 KB

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