Open Source Tomb Raider Engine
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Vector.h 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*!
  2. * \file include/templates/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. /*!
  12. * \brief Template class for a (pretty strange) Vector
  13. * \tparam Object datatype the Vector can store
  14. */
  15. template <class Object> class Vector {
  16. public:
  17. /*!
  18. * \brief Construct an empty Vector
  19. */
  20. Vector() {
  21. mData = 0x0;
  22. mError = 0x0;
  23. mReserve = 0;
  24. mStart = 0;
  25. mEnd = 0;
  26. mIndex = 0;
  27. }
  28. /*!
  29. * \brief Construct a Vector from another one
  30. * \param vector Vector to copy from
  31. */
  32. Vector(Vector &vector) {
  33. mData = 0x0;
  34. mError = 0x0;
  35. mReserve = 0;
  36. mStart = 0;
  37. mEnd = 0;
  38. mIndex = 0;
  39. copy(vector);
  40. }
  41. /*!
  42. * \brief Construct a Vector with a specific size
  43. * \param size initial size
  44. */
  45. Vector(unsigned int size) {
  46. mData = 0x0;
  47. mError = 0x0;
  48. mReserve = 0;
  49. mStart = 0;
  50. mEnd = 0;
  51. mIndex = 0;
  52. resize(size);
  53. }
  54. /*!
  55. * \brief Deconstruct a Vector
  56. */
  57. ~Vector() {
  58. if (!empty() && mData)
  59. delete [] mData;
  60. clear();
  61. mReserve = 0;
  62. }
  63. /*!
  64. * \brief Clears the Vector, but deletes nothing
  65. * \sa Vector::erase()
  66. */
  67. void clear() {
  68. mStart = 0;
  69. mEnd = 0;
  70. mIndex = 0;
  71. }
  72. /*!
  73. * \brief Clears the vector and deletes everything contained
  74. * \sa Vector::clear()
  75. */
  76. void erase() {
  77. for (start(); forward(); next()) {
  78. if (current())
  79. delete current();
  80. }
  81. clear();
  82. }
  83. /*!
  84. * \brief Reserve more memory
  85. * \param count new maximum size
  86. */
  87. void reserve(unsigned int count) {
  88. Object *swap = 0x0;
  89. if (count > mReserve) {
  90. swap = mData;
  91. mReserve = count;
  92. mData = new Object[count];
  93. }
  94. if (swap) {
  95. for (unsigned int i = begin(); i < end(); ++i)
  96. mData[i] = swap[i];
  97. delete [] swap;
  98. }
  99. }
  100. /*!
  101. * \brief Resize the Vector
  102. * \param count new size
  103. */
  104. void resize(unsigned int count) {
  105. resize(count, 0x0);
  106. }
  107. /*!
  108. * \brief Resize the Vector
  109. * \param count new size
  110. * \param object what to put into blank spaces
  111. */
  112. void resize(unsigned int count, Object object) {
  113. reserve(count);
  114. for (unsigned int i = 0; i < count; ++i) {
  115. if (i < begin() || i >= end())
  116. mData[i] = object;
  117. }
  118. mEnd = count;
  119. }
  120. /*!
  121. * \brief Increase size by 1
  122. */
  123. void pushBack() {
  124. pushBack(0x0);
  125. }
  126. /*!
  127. * \brief Increase size by 1
  128. * \param object what to put into new space
  129. */
  130. void pushBack(Object object) {
  131. resize(size() + 1);
  132. mData[size()-1] = object;
  133. }
  134. /*!
  135. * \brief Check if empty
  136. * \returns true if begin() equals end()
  137. */
  138. bool empty() {
  139. return (begin() == end());
  140. }
  141. /*!
  142. * \brief Check maximum capacity
  143. * \returns reserved memory
  144. */
  145. unsigned int capacity() {
  146. return mReserve;
  147. }
  148. /*!
  149. * \brief Get start index
  150. * \returns start index
  151. */
  152. unsigned int begin() {
  153. return mStart;
  154. }
  155. /*!
  156. * \brief Get end index
  157. * \returns end index
  158. */
  159. unsigned int end() {
  160. return mEnd;
  161. }
  162. /*!
  163. * \brief Get size
  164. * \returns end index
  165. */
  166. unsigned int size() {
  167. return mEnd;
  168. }
  169. /*!
  170. * \brief Copy a Vector into this one.
  171. * May increase size.
  172. * \param vector Vector to copy from
  173. */
  174. void copy(Vector<Object> &vector) {
  175. unsigned int i;
  176. if (vector.capacity() > capacity()) {
  177. resize(vector.capacity());
  178. }
  179. mStart = vector.begin();
  180. mEnd = vector.end();
  181. mError = vector.Error();
  182. mIndex = vector.getCurrentIndex();
  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