IceContainer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains a simple container class.
  4. * \file IceContainer.cpp
  5. * \author Pierre Terdiman
  6. * \date February, 5, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. /**
  11. * Contains a list of 32-bits values.
  12. * Use this class when you need to store an unknown number of values. The list is automatically
  13. * resized and can contains 32-bits entities (dwords or floats)
  14. *
  15. * \class Container
  16. * \author Pierre Terdiman
  17. * \version 1.0
  18. * \date 08.15.98
  19. */
  20. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. // Precompiled Header
  23. #include "Stdafx.h"
  24. using namespace IceCore;
  25. // Static members
  26. #ifdef CONTAINER_STATS
  27. udword Container::mNbContainers = 0;
  28. udword Container::mUsedRam = 0;
  29. #endif
  30. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. /**
  32. * Constructor. No entries allocated there.
  33. */
  34. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. Container::Container() : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(2.0f)
  36. {
  37. #ifdef CONTAINER_STATS
  38. mNbContainers++;
  39. mUsedRam+=sizeof(Container);
  40. #endif
  41. }
  42. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  43. /**
  44. * Constructor. Also allocates a given number of entries.
  45. */
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. Container::Container(udword size, float growth_factor) : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(growth_factor)
  48. {
  49. #ifdef CONTAINER_STATS
  50. mNbContainers++;
  51. mUsedRam+=sizeof(Container);
  52. #endif
  53. SetSize(size);
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. /**
  57. * Copy constructor.
  58. */
  59. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. Container::Container(const Container& object) : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(2.0f)
  61. {
  62. #ifdef CONTAINER_STATS
  63. mNbContainers++;
  64. mUsedRam+=sizeof(Container);
  65. #endif
  66. *this = object;
  67. }
  68. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. /**
  70. * Destructor. Frees everything and leaves.
  71. */
  72. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. Container::~Container()
  74. {
  75. Empty();
  76. #ifdef CONTAINER_STATS
  77. mNbContainers--;
  78. mUsedRam-=GetUsedRam();
  79. #endif
  80. }
  81. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. /**
  83. * Clears the container. All stored values are deleted, and it frees used ram.
  84. * \see Reset()
  85. * \return Self-Reference
  86. */
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. Container& Container::Empty()
  89. {
  90. #ifdef CONTAINER_STATS
  91. mUsedRam-=mMaxNbEntries*sizeof(udword);
  92. #endif
  93. DELETEARRAY(mEntries);
  94. mCurNbEntries = mMaxNbEntries = 0;
  95. return *this;
  96. }
  97. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  98. /**
  99. * Resizes the container.
  100. * \param needed [in] assume the container can be added at least "needed" values
  101. * \return true if success.
  102. */
  103. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. bool Container::Resize(udword needed)
  105. {
  106. #ifdef CONTAINER_STATS
  107. // Subtract previous amount of bytes
  108. mUsedRam-=mMaxNbEntries*sizeof(udword);
  109. #endif
  110. // Get more entries
  111. mMaxNbEntries = mMaxNbEntries ? udword(float(mMaxNbEntries)*mGrowthFactor) : 2; // Default nb Entries = 2
  112. if(mMaxNbEntries<mCurNbEntries + needed) mMaxNbEntries = mCurNbEntries + needed;
  113. // Get some bytes for new entries
  114. udword* NewEntries = new udword[mMaxNbEntries];
  115. CHECKALLOC(NewEntries);
  116. #ifdef CONTAINER_STATS
  117. // Add current amount of bytes
  118. mUsedRam+=mMaxNbEntries*sizeof(udword);
  119. #endif
  120. // Copy old data if needed
  121. if(mCurNbEntries) CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
  122. // Delete old data
  123. DELETEARRAY(mEntries);
  124. // Assign new pointer
  125. mEntries = NewEntries;
  126. return true;
  127. }
  128. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  129. /**
  130. * Sets the initial size of the container. If it already contains something, it's discarded.
  131. * \param nb [in] Number of entries
  132. * \return true if success
  133. */
  134. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  135. bool Container::SetSize(udword nb)
  136. {
  137. // Make sure it's empty
  138. Empty();
  139. // Checkings
  140. if(!nb) return false;
  141. // Initialize for nb entries
  142. mMaxNbEntries = nb;
  143. // Get some bytes for new entries
  144. mEntries = new udword[mMaxNbEntries];
  145. CHECKALLOC(mEntries);
  146. #ifdef CONTAINER_STATS
  147. // Add current amount of bytes
  148. mUsedRam+=mMaxNbEntries*sizeof(udword);
  149. #endif
  150. return true;
  151. }
  152. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  153. /**
  154. * Refits the container and get rid of unused bytes.
  155. * \return true if success
  156. */
  157. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  158. bool Container::Refit()
  159. {
  160. #ifdef CONTAINER_STATS
  161. // Subtract previous amount of bytes
  162. mUsedRam-=mMaxNbEntries*sizeof(udword);
  163. #endif
  164. // Get just enough entries
  165. mMaxNbEntries = mCurNbEntries;
  166. if(!mMaxNbEntries) return false;
  167. // Get just enough bytes
  168. udword* NewEntries = new udword[mMaxNbEntries];
  169. CHECKALLOC(NewEntries);
  170. #ifdef CONTAINER_STATS
  171. // Add current amount of bytes
  172. mUsedRam+=mMaxNbEntries*sizeof(udword);
  173. #endif
  174. // Copy old data
  175. CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
  176. // Delete old data
  177. DELETEARRAY(mEntries);
  178. // Assign new pointer
  179. mEntries = NewEntries;
  180. return true;
  181. }
  182. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  183. /**
  184. * Checks whether the container already contains a given value.
  185. * \param entry [in] the value to look for in the container
  186. * \param location [out] a possible pointer to store the entry location
  187. * \see Add(udword entry)
  188. * \see Add(float entry)
  189. * \see Empty()
  190. * \return true if the value has been found in the container, else false.
  191. */
  192. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  193. bool Container::Contains(udword entry, udword* location) const
  194. {
  195. // Look for the entry
  196. for(udword i=0;i<mCurNbEntries;i++)
  197. {
  198. if(mEntries[i]==entry)
  199. {
  200. if(location) *location = i;
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  207. /**
  208. * Deletes an entry. If the container contains such an entry, it's removed.
  209. * \param entry [in] the value to delete.
  210. * \return true if the value has been found in the container, else false.
  211. * \warning This method is arbitrary slow (O(n)) and should be used carefully. Insertion order is not preserved.
  212. */
  213. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  214. bool Container::Delete(udword entry)
  215. {
  216. // Look for the entry
  217. for(udword i=0;i<mCurNbEntries;i++)
  218. {
  219. if(mEntries[i]==entry)
  220. {
  221. // Entry has been found at index i. The strategy is to copy the last current entry at index i, and decrement the current number of entries.
  222. DeleteIndex(i);
  223. return true;
  224. }
  225. }
  226. return false;
  227. }
  228. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  229. /**
  230. * Deletes an entry, preserving the insertion order. If the container contains such an entry, it's removed.
  231. * \param entry [in] the value to delete.
  232. * \return true if the value has been found in the container, else false.
  233. * \warning This method is arbitrary slow (O(n)) and should be used carefully.
  234. */
  235. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  236. bool Container::DeleteKeepingOrder(udword entry)
  237. {
  238. // Look for the entry
  239. for(udword i=0;i<mCurNbEntries;i++)
  240. {
  241. if(mEntries[i]==entry)
  242. {
  243. // Entry has been found at index i.
  244. // Shift entries to preserve order. You really should use a linked list instead.
  245. mCurNbEntries--;
  246. for(udword j=i;j<mCurNbEntries;j++)
  247. {
  248. mEntries[j] = mEntries[j+1];
  249. }
  250. return true;
  251. }
  252. }
  253. return false;
  254. }
  255. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  256. /**
  257. * Gets the next entry, starting from input one.
  258. * \param entry [in/out] On input, the entry to look for. On output, the next entry
  259. * \param find_mode [in] wrap/clamp
  260. * \return Self-Reference
  261. */
  262. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  263. Container& Container::FindNext(udword& entry, FindMode find_mode)
  264. {
  265. udword Location;
  266. if(Contains(entry, &Location))
  267. {
  268. Location++;
  269. if(Location==mCurNbEntries) Location = find_mode==FIND_WRAP ? 0 : mCurNbEntries-1;
  270. entry = mEntries[Location];
  271. }
  272. return *this;
  273. }
  274. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  275. /**
  276. * Gets the previous entry, starting from input one.
  277. * \param entry [in/out] On input, the entry to look for. On output, the previous entry
  278. * \param find_mode [in] wrap/clamp
  279. * \return Self-Reference
  280. */
  281. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  282. Container& Container::FindPrev(udword& entry, FindMode find_mode)
  283. {
  284. udword Location;
  285. if(Contains(entry, &Location))
  286. {
  287. Location--;
  288. if(Location==0xffffffff) Location = find_mode==FIND_WRAP ? mCurNbEntries-1 : 0;
  289. entry = mEntries[Location];
  290. }
  291. return *this;
  292. }
  293. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  294. /**
  295. * Gets the ram used by the container.
  296. * \return the ram used in bytes.
  297. */
  298. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  299. udword Container::GetUsedRam() const
  300. {
  301. return sizeof(Container) + mMaxNbEntries * sizeof(udword);
  302. }
  303. /*void Container::operator=(const Container& object)
  304. {
  305. SetSize(object.GetNbEntries());
  306. CopyMemory(mEntries, object.GetEntries(), mMaxNbEntries*sizeof(udword));
  307. mCurNbEntries = mMaxNbEntries;
  308. }*/