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.7KB

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