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

Vector.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : Freyja
  5. * Author : Mongoose
  6. * Website : http://www.westga.edu/~stu7440/
  7. * Email : stu7440@westga.edu
  8. * Object : Vector
  9. * License : No use w/o permission (C) 2002 Mongoose
  10. * Comments: mtk template vector
  11. *
  12. * UINT_MAX is an error condition, used in place of -1
  13. *
  14. * This file was generated using Mongoose's C++
  15. * template generator script. <stu7440@westga.edu>
  16. *
  17. *-- History ------------------------------------------------
  18. *
  19. * 2002.08.31:
  20. * Mongoose - Created
  21. ================================================================*/
  22. #ifndef _VECTOR_H_
  23. #define _VECTOR_H_
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #ifdef DEBUG_MEMORY
  27. #include <memory_test.h>
  28. #endif
  29. template <class Object> class Vector
  30. {
  31. public:
  32. Vector()
  33. {
  34. mData = 0x0;
  35. mError = 0x0;
  36. mReserve = 0;
  37. mStart = 0;
  38. mEnd = 0;
  39. mIndex = 0;
  40. }
  41. Vector(Vector &vector)
  42. {
  43. mData = 0x0;
  44. mError = 0x0;
  45. mReserve = 0;
  46. mStart = 0;
  47. mEnd = 0;
  48. copy(vector);
  49. }
  50. Vector(unsigned int size)
  51. {
  52. mData = 0x0;
  53. mError = 0x0;
  54. mReserve = 0;
  55. mStart = 0;
  56. mEnd = 0;
  57. mIndex = 0;
  58. resize(size);
  59. }
  60. ~Vector()
  61. {
  62. if (!empty() && mData)
  63. {
  64. delete [] mData;
  65. }
  66. clear();
  67. mReserve = 0;
  68. }
  69. void clear()
  70. {
  71. mStart = 0;
  72. mEnd = 0;
  73. mIndex = 0;
  74. }
  75. void erase()
  76. {
  77. for (start(); forward(); next())
  78. {
  79. if (current())
  80. {
  81. delete current();
  82. }
  83. }
  84. clear();
  85. }
  86. void reserve(unsigned int count)
  87. {
  88. unsigned int i;
  89. Object *swap = 0x0;
  90. if (count > mReserve)
  91. {
  92. swap = mData;
  93. mReserve = count;
  94. mData = new Object[count];
  95. }
  96. if (swap)
  97. {
  98. for (i = begin(); i < end(); ++i)
  99. {
  100. mData[i] = swap[i];
  101. }
  102. delete [] swap;
  103. }
  104. }
  105. void resize(unsigned int count)
  106. {
  107. resize(count, 0x0);
  108. }
  109. void resize(unsigned int count, Object object)
  110. {
  111. unsigned int i;
  112. reserve(count);
  113. for (i = 0; i < count; ++i)
  114. {
  115. if (i < begin() || i >= end())
  116. {
  117. mData[i] = object;
  118. }
  119. }
  120. mEnd = count;
  121. }
  122. void pushBack()
  123. {
  124. pushBack(0x0);
  125. }
  126. void pushBack(Object object)
  127. {
  128. resize(size() + 1);
  129. mData[size()-1] = object;
  130. }
  131. bool empty()
  132. {
  133. return (begin() == end());
  134. }
  135. unsigned int capacity()
  136. {
  137. return mReserve;
  138. }
  139. unsigned int begin()
  140. {
  141. return mStart;
  142. }
  143. unsigned int end()
  144. {
  145. return mEnd;
  146. }
  147. unsigned int size()
  148. {
  149. return mEnd;
  150. }
  151. void copy(Vector<Object> &vector)
  152. {
  153. unsigned int i;
  154. if (vector.capacity() > capacity())
  155. {
  156. resize(vector.capacity());
  157. }
  158. mStart = vector.begin();
  159. mEnd = vector.end();
  160. for (i = vector.begin(); i < vector.end(); ++i)
  161. {
  162. mData[i] = vector[i];
  163. //Add(list->Current()); // WTFBBQ?
  164. }
  165. }
  166. void qSort(int (*compareFunc)(const void *, const void *))
  167. {
  168. qsort(mData, end(), sizeof(Object), compareFunc);
  169. }
  170. void swap(unsigned int index, unsigned int index2)
  171. {
  172. if (index < begin() || index > end())
  173. return;
  174. if (index2 < begin() || index2 > end())
  175. return;
  176. Object swap = mData[index];
  177. mData[index] = mData[index2];
  178. mData[index2] = swap;
  179. }
  180. void assign(unsigned int index, Object object)
  181. {
  182. mData[index] = object;
  183. }
  184. Object operator [] (unsigned int index)
  185. {
  186. if (mData == 0x0 || index < begin() || index > end() ||
  187. index >= size() || empty())
  188. return mError;
  189. return mData[index];
  190. }
  191. void print(void (*print_func)(Object))
  192. {
  193. unsigned int i;
  194. for (i = begin(); i < end(); ++i)
  195. {
  196. if (print_func)
  197. {
  198. (*print_func)(mData[i]);
  199. }
  200. fflush(stdout);
  201. }
  202. printf("\n");
  203. }
  204. // Built-in iterator methods ////////////////////////////////
  205. void start(unsigned int index)
  206. {
  207. if (mData == 0x0 || index < begin() || index > end() ||
  208. index >= size() || empty())
  209. return;
  210. mIndex = index;
  211. }
  212. void start()
  213. {
  214. mIndex = begin();
  215. }
  216. void finish()
  217. {
  218. mIndex = end() - 1;
  219. }
  220. bool forward()
  221. {
  222. return (mIndex < end());
  223. }
  224. bool backward()
  225. {
  226. return (mIndex + 1 > begin());
  227. }
  228. void next()
  229. {
  230. if (mIndex < end())
  231. ++mIndex;
  232. }
  233. void prev()
  234. {
  235. --mIndex;
  236. }
  237. void setError(Object object)
  238. {
  239. mError = object;
  240. }
  241. unsigned int getCurrentIndex()
  242. {
  243. return mIndex;
  244. }
  245. unsigned int setCurrentIndex(unsigned int index)
  246. {
  247. if (index < end())
  248. {
  249. mIndex = index;
  250. }
  251. }
  252. Object current()
  253. {
  254. return mData[mIndex];
  255. }
  256. /* Requires objects to support '==' */
  257. bool find(Object object)
  258. {
  259. for (start(); forward(); next())
  260. {
  261. if (object == current())
  262. {
  263. return true;
  264. }
  265. }
  266. return false;
  267. }
  268. // Instead of appending objects this apptempts replacing 'removed' objects
  269. unsigned int add(Object object)
  270. {
  271. if (begin() > 0)
  272. {
  273. mData[begin() - 1] = object;
  274. --mStart;
  275. return begin();
  276. }
  277. pushBack(object);
  278. return size();
  279. }
  280. // Hhhmm... dangerous and fun - this gets your data out of order
  281. void remove(unsigned int index)
  282. {
  283. mData[index] = mData[begin()];
  284. ++mStart;
  285. }
  286. private:
  287. Object *mData;
  288. Object mError;
  289. unsigned int mReserve;
  290. unsigned int mStart;
  291. unsigned int mEnd;
  292. unsigned int mIndex;
  293. };
  294. #endif