Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Vector.h 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*!
  2. * \file include/Vector.h
  3. * \brief Template Vector
  4. *
  5. * \author Mongoose
  6. */
  7. #ifndef _VECTOR_H_
  8. #define _VECTOR_H_
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #ifdef DEBUG_MEMORY
  12. #include <memory_test.h>
  13. #endif
  14. /*!
  15. * \brief Template class for a (pretty strange) Vector
  16. * \tparam Object datatype the Vector can store
  17. */
  18. template <class Object> class Vector {
  19. public:
  20. /*!
  21. * \brief Construct an empty Vector
  22. */
  23. Vector() {
  24. mData = 0x0;
  25. mError = 0x0;
  26. mReserve = 0;
  27. mStart = 0;
  28. mEnd = 0;
  29. mIndex = 0;
  30. }
  31. /*!
  32. * \brief Construct a Vector from another one
  33. * \param vector Vector to copy from
  34. */
  35. Vector(Vector &vector) {
  36. mData = 0x0;
  37. mError = 0x0;
  38. mReserve = 0;
  39. mStart = 0;
  40. mEnd = 0;
  41. copy(vector);
  42. }
  43. /*!
  44. * \brief Construct a Vector with a specific size
  45. * \param size initial size
  46. */
  47. Vector(unsigned int size) {
  48. mData = 0x0;
  49. mError = 0x0;
  50. mReserve = 0;
  51. mStart = 0;
  52. mEnd = 0;
  53. mIndex = 0;
  54. resize(size);
  55. }
  56. /*!
  57. * \brief Deconstruct a Vector
  58. */
  59. ~Vector() {
  60. if (!empty() && mData)
  61. delete [] mData;
  62. clear();
  63. mReserve = 0;
  64. }
  65. /*!
  66. * \brief Clears the Vector, but deletes nothing
  67. * \sa Vector::erase()
  68. */
  69. void clear() {
  70. mStart = 0;
  71. mEnd = 0;
  72. mIndex = 0;
  73. }
  74. /*!
  75. * \brief Clears the vector and deletes everything contained
  76. * \sa Vector::clear()
  77. */
  78. void erase() {
  79. for (start(); forward(); next()) {
  80. if (current())
  81. delete current();
  82. }
  83. clear();
  84. }
  85. /*!
  86. * \brief Reserve more memory
  87. * \param count new maximum size
  88. */
  89. void reserve(unsigned int count) {
  90. Object *swap = 0x0;
  91. if (count > mReserve) {
  92. swap = mData;
  93. mReserve = count;
  94. mData = new Object[count];
  95. }
  96. if (swap) {
  97. for (unsigned int i = begin(); i < end(); ++i)
  98. mData[i] = swap[i];
  99. delete [] swap;
  100. }
  101. }
  102. /*!
  103. * \brief Resize the Vector
  104. * \param count new size
  105. */
  106. void resize(unsigned int count) {
  107. resize(count, 0x0);
  108. }
  109. /*!
  110. * \brief Resize the Vector
  111. * \param count new size
  112. * \param object what to put into blank spaces
  113. */
  114. void resize(unsigned int count, Object object) {
  115. reserve(count);
  116. for (unsigned int i = 0; i < count; ++i) {
  117. if (i < begin() || i >= end())
  118. mData[i] = object;
  119. }
  120. mEnd = count;
  121. }
  122. /*!
  123. * \brief Increase size by 1
  124. */
  125. void pushBack() {
  126. pushBack(0x0);
  127. }
  128. /*!
  129. * \brief Increase size by 1
  130. * \param object what to put into new space
  131. */
  132. void pushBack(Object object) {
  133. resize(size() + 1);
  134. mData[size()-1] = object;
  135. }
  136. /*!
  137. * \brief Check if empty
  138. * \returns true if begin() equals end()
  139. */
  140. bool empty() {
  141. return (begin() == end());
  142. }
  143. /*!
  144. * \brief Check maximum capacity
  145. * \returns reserved memory
  146. */
  147. unsigned int capacity() {
  148. return mReserve;
  149. }
  150. /*!
  151. * \brief Get start index
  152. * \returns start index
  153. */
  154. unsigned int begin() {
  155. return mStart;
  156. }
  157. /*!
  158. * \brief Get end index
  159. * \returns end index
  160. */
  161. unsigned int end() {
  162. return mEnd;
  163. }
  164. /*!
  165. * \brief Get size
  166. * \returns end index
  167. */
  168. unsigned int size() {
  169. return mEnd;
  170. }
  171. /*!
  172. * \brief Copy a Vector into this one.
  173. * May increase size.
  174. * \param vector Vector to copy from
  175. */
  176. void copy(Vector<Object> &vector) {
  177. unsigned int i;
  178. if (vector.capacity() > capacity()) {
  179. resize(vector.capacity());
  180. }
  181. mStart = vector.begin();
  182. mEnd = vector.end();
  183. for (i = vector.begin(); i < vector.end(); ++i) {
  184. mData[i] = vector[i];
  185. }
  186. }
  187. /*!
  188. * \brief Sort the Vector
  189. * \param compareFunc comparison function for qsort
  190. */
  191. void qSort(int (*compareFunc)(const void *, const void *)) {
  192. qsort(mData, end(), sizeof(Object), compareFunc);
  193. }
  194. /*!
  195. * \brief Swap two items
  196. * \param index first index
  197. * \param index2 second index
  198. */
  199. void swap(unsigned int index, unsigned int index2) {
  200. if (index < begin() || index > end())
  201. return;
  202. if (index2 < begin() || index2 > end())
  203. return;
  204. Object swap = mData[index];
  205. mData[index] = mData[index2];
  206. mData[index2] = swap;
  207. }
  208. /*!
  209. * \brief Set an index to a value
  210. * \param index index to set
  211. * \param object object to set it to
  212. */
  213. void assign(unsigned int index, Object object) {
  214. mData[index] = object;
  215. }
  216. /*!
  217. * \brief Get value at index
  218. * \param index index to look at
  219. * \returns data for index, or error object
  220. */
  221. Object operator [] (unsigned int index) {
  222. if (mData == 0x0 || index < begin() || index > end() || index >= size() || empty())
  223. return mError;
  224. return mData[index];
  225. }
  226. /*!
  227. * \brief Print the Vector
  228. * \param print_func function that can print Objects
  229. */
  230. void print(void (*print_func)(Object)) {
  231. for (unsigned int i = begin(); i < end(); ++i) {
  232. if (print_func)
  233. (*print_func)(mData[i]);
  234. fflush(stdout);
  235. }
  236. printf("\n");
  237. }
  238. /*!
  239. * \brief Start Iterator at specific index
  240. * \param index where to start
  241. */
  242. void start(unsigned int index) {
  243. if (mData == 0x0 || index < begin() || index > end() ||
  244. index >= size() || empty())
  245. return;
  246. mIndex = index;
  247. }
  248. /*!
  249. * \brief Set Iterator to the first element
  250. */
  251. void start() {
  252. mIndex = begin();
  253. }
  254. /*!
  255. * \brief Set Iterator to the last element
  256. */
  257. void finish() {
  258. mIndex = end() - 1;
  259. }
  260. /*!
  261. * \brief Check if the Iterator can go forward
  262. * \returns true if Iterator is still in range
  263. */
  264. bool forward() {
  265. return (mIndex < end());
  266. }
  267. /*!
  268. * \brief Check if the Iterator can go backwards
  269. * \returns true if Iterator is already in range
  270. */
  271. bool backward() {
  272. return (mIndex + 1 > begin());
  273. }
  274. /*!
  275. * \brief Increment the Iterator
  276. */
  277. void next() {
  278. if (mIndex < end())
  279. ++mIndex;
  280. }
  281. /*!
  282. * \brief Decrement the Iterator
  283. */
  284. void prev() {
  285. --mIndex;
  286. }
  287. /*!
  288. * \brief Set the error object
  289. * \param object new error object
  290. */
  291. void setError(Object object) {
  292. mError = object;
  293. }
  294. /*!
  295. * \brief Get Iterator index
  296. * \returns current Iterator index
  297. */
  298. unsigned int getCurrentIndex() {
  299. return mIndex;
  300. }
  301. /*!
  302. * \brief Set Iterator index
  303. * \param index new Iterator index
  304. */
  305. void setCurrentIndex(unsigned int index) {
  306. if (index < end())
  307. mIndex = index;
  308. }
  309. /*!
  310. * \brief Get current element
  311. * \returns element at Iterator index
  312. */
  313. Object current() {
  314. return mData[mIndex];
  315. }
  316. /*!
  317. * \brief Check if an object is in the Vector.
  318. * Requires objects to support `==`.
  319. * \param object object to find
  320. * \returns true if found, false if not.
  321. */
  322. bool find(Object object) {
  323. for (start(); forward(); next()) {
  324. if (object == current())
  325. return true;
  326. }
  327. return false;
  328. }
  329. //
  330. /*!
  331. * \brief Add an object.
  332. * Instead of appending objects this attempts replacing 'removed' objects.
  333. * \param object object to add
  334. * \returns index of added object
  335. */
  336. unsigned int add(Object object) {
  337. if (begin() > 0) {
  338. mData[begin() - 1] = object;
  339. --mStart;
  340. return begin();
  341. }
  342. pushBack(object);
  343. return size();
  344. }
  345. /*!
  346. * \brief Remove an object. This will change the index of another element!
  347. * \param index index to "remove".
  348. */
  349. void remove(unsigned int index) {
  350. mData[index] = mData[begin()];
  351. ++mStart;
  352. }
  353. private:
  354. Object *mData; //!< Data array
  355. Object mError; //!< Error object
  356. unsigned int mReserve; //!< Index for reserved space
  357. unsigned int mStart; //!< Start index
  358. unsigned int mEnd; //!< End index
  359. unsigned int mIndex; //!< Iterator index
  360. };
  361. #endif