IceContainer.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains a simple container class.
  4. * \file IceContainer.h
  5. * \author Pierre Terdiman
  6. * \date February, 5, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Include Guard
  11. #ifndef __ICECONTAINER_H__
  12. #define __ICECONTAINER_H__
  13. // #define CONTAINER_STATS
  14. enum FindMode
  15. {
  16. FIND_CLAMP,
  17. FIND_WRAP,
  18. FIND_FORCE_DWORD = 0x7fffffff
  19. };
  20. class ICECORE_API Container
  21. {
  22. public:
  23. // Constructor / Destructor
  24. Container();
  25. Container(const Container& object);
  26. Container(udword size, float growth_factor);
  27. ~Container();
  28. // Management
  29. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  30. /**
  31. * A O(1) method to add a value in the container. The container is automatically resized if needed.
  32. * The method is inline, not the resize. The call overhead happens on resizes only, which is not a problem since the resizing operation
  33. * costs a lot more than the call overhead...
  34. *
  35. * \param entry [in] a udword to store in the container
  36. * \see Add(float entry)
  37. * \see Empty()
  38. * \see Contains(udword entry)
  39. * \return Self-Reference
  40. */
  41. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. inline_ Container& Add(udword entry)
  43. {
  44. // Resize if needed
  45. if (mCurNbEntries == mMaxNbEntries
  46. && !Resize())
  47. {
  48. IceAbort();
  49. }
  50. // Add new entry
  51. mEntries[mCurNbEntries++] = entry;
  52. return *this;
  53. }
  54. inline_ Container& Add(const uword* entries, udword nb)
  55. {
  56. // Resize if needed
  57. if (mCurNbEntries + nb > mMaxNbEntries
  58. && !Resize(nb))
  59. {
  60. IceAbort();
  61. }
  62. // Add new entry
  63. CopyMemory(&mEntries[mCurNbEntries], entries, nb*sizeof(uword));
  64. mCurNbEntries+=nb;
  65. return *this;
  66. }
  67. inline_ Container& Add(const udword* entries, udword nb)
  68. {
  69. // Resize if needed
  70. if (mCurNbEntries + nb > mMaxNbEntries
  71. && !Resize(nb))
  72. {
  73. IceAbort();
  74. }
  75. // Add new entry
  76. CopyMemory(&mEntries[mCurNbEntries], entries, nb*sizeof(udword));
  77. mCurNbEntries+=nb;
  78. return *this;
  79. }
  80. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. /**
  82. * A O(1) method to add a value in the container. The container is automatically resized if needed.
  83. * The method is inline, not the resize. The call overhead happens on resizes only, which is not a problem since the resizing operation
  84. * costs a lot more than the call overhead...
  85. *
  86. * \param entry [in] a float to store in the container
  87. * \see Add(udword entry)
  88. * \see Empty()
  89. * \see Contains(udword entry)
  90. * \return Self-Reference
  91. */
  92. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  93. inline_ Container& Add(float entry)
  94. {
  95. // Resize if needed
  96. if (mCurNbEntries == mMaxNbEntries
  97. && !Resize())
  98. {
  99. IceAbort();
  100. }
  101. // Add new entry
  102. mEntries[mCurNbEntries++] = IR(entry);
  103. return *this;
  104. }
  105. inline_ Container& Add(const float* entries, udword nb)
  106. {
  107. // Resize if needed
  108. if (mCurNbEntries + nb > mMaxNbEntries
  109. && !Resize(nb))
  110. {
  111. IceAbort();
  112. }
  113. // Add new entry
  114. CopyMemory(&mEntries[mCurNbEntries], entries, nb*sizeof(float));
  115. mCurNbEntries+=nb;
  116. return *this;
  117. }
  118. //! Add unique [slow]
  119. inline_ Container& AddUnique(udword entry)
  120. {
  121. if(!Contains(entry)) Add(entry);
  122. return *this;
  123. }
  124. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  125. /**
  126. * Clears the container. All stored values are deleted, and it frees used ram.
  127. * \see Reset()
  128. * \return Self-Reference
  129. */
  130. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  131. Container& Empty();
  132. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. /**
  134. * Resets the container. Stored values are discarded but the buffer is kept so that further calls don't need resizing again.
  135. * That's a kind of temporal coherence.
  136. * \see Empty()
  137. */
  138. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  139. inline_ void Reset()
  140. {
  141. // Avoid the write if possible
  142. // ### CMOV
  143. if(mCurNbEntries) mCurNbEntries = 0;
  144. }
  145. // HANDLE WITH CARE
  146. inline_ void ForceSize(udword size)
  147. {
  148. mCurNbEntries = size;
  149. }
  150. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  151. /**
  152. * Sets the initial size of the container. If it already contains something, it's discarded.
  153. * \param nb [in] Number of entries
  154. * \return true if success
  155. */
  156. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  157. bool SetSize(udword nb);
  158. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  159. /**
  160. * Refits the container and get rid of unused bytes.
  161. * \return true if success
  162. */
  163. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  164. bool Refit();
  165. // Checks whether the container already contains a given value.
  166. bool Contains(udword entry, udword* location=null) const;
  167. // Deletes an entry - doesn't preserve insertion order.
  168. bool Delete(udword entry);
  169. // Deletes an entry - does preserve insertion order.
  170. bool DeleteKeepingOrder(udword entry);
  171. //! Deletes the very last entry.
  172. inline_ void DeleteLastEntry() { if(mCurNbEntries) mCurNbEntries--; }
  173. //! Deletes the entry whose index is given
  174. inline_ void DeleteIndex(udword index) { ASSERT(index < mCurNbEntries); mEntries[index] = mEntries[--mCurNbEntries]; }
  175. // Helpers
  176. Container& FindNext(udword& entry, FindMode find_mode=FIND_CLAMP);
  177. Container& FindPrev(udword& entry, FindMode find_mode=FIND_CLAMP);
  178. // Data access.
  179. inline_ udword GetNbEntries() const { return mCurNbEntries; } //!< Returns the current number of entries.
  180. inline_ udword GetEntry(udword i) const { return mEntries[i]; } //!< Returns ith entry
  181. inline_ udword* GetEntries() const { return mEntries; } //!< Returns the list of entries.
  182. inline_ udword GetFirst() const { return mEntries[0]; }
  183. inline_ udword GetLast() const { return mEntries[mCurNbEntries-1]; }
  184. // Growth control
  185. inline_ udword GetGrowthFactor() const { return mGrowthFactor; } //!< Returns the growth factor
  186. inline_ void SetGrowthFactor(udword growth) { mGrowthFactor = growth; } //!< Sets the growth factor
  187. inline_ bool IsFull() const { return mCurNbEntries==mMaxNbEntries; } //!< Checks the container is full
  188. inline_ BOOL IsNotEmpty() const { return mCurNbEntries; } //!< Checks the container is empty
  189. //! Read-access as an array
  190. inline_ udword operator[](udword i) const { ASSERT(i>=0 && i<mCurNbEntries); return mEntries[i]; }
  191. //! Write-access as an array
  192. inline_ udword& operator[](udword i) { ASSERT(i>=0 && i<mCurNbEntries); return mEntries[i]; }
  193. // Stats
  194. udword GetUsedRam() const;
  195. //! Operator for "Container A = Container B"
  196. //void operator = (const Container& object);
  197. #ifdef CONTAINER_STATS
  198. inline_ udword GetNbContainers() const { return mNbContainers; }
  199. inline_ udword GetTotalBytes() const { return mUsedRam; }
  200. private:
  201. static udword mNbContainers; //!< Number of containers around
  202. static udword mUsedRam; //!< Amount of bytes used by containers in the system
  203. #endif
  204. private:
  205. // Resizing
  206. bool Resize(udword needed=1);
  207. // Data
  208. udword mMaxNbEntries; //!< Maximum possible number of entries
  209. udword mCurNbEntries; //!< Current number of entries
  210. udword* mEntries; //!< List of entries
  211. udword mGrowthFactor; //!< Resize: new number of entries = old number * mGrowthFactor
  212. };
  213. #endif // __ICECONTAINER_H__