IceContainer.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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) Resize();
  46. // Add new entry
  47. mEntries[mCurNbEntries++] = entry;
  48. return *this;
  49. }
  50. inline_ Container& Add(const uword* entries, udword nb)
  51. {
  52. // Resize if needed
  53. if(mCurNbEntries+nb>mMaxNbEntries) Resize(nb);
  54. // Add new entry
  55. CopyMemory(&mEntries[mCurNbEntries], entries, nb*sizeof(uword));
  56. mCurNbEntries+=nb;
  57. return *this;
  58. }
  59. inline_ Container& Add(const udword* entries, udword nb)
  60. {
  61. // Resize if needed
  62. if(mCurNbEntries+nb>mMaxNbEntries) Resize(nb);
  63. // Add new entry
  64. CopyMemory(&mEntries[mCurNbEntries], entries, nb*sizeof(udword));
  65. mCurNbEntries+=nb;
  66. return *this;
  67. }
  68. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. /**
  70. * A O(1) method to add a value in the container. The container is automatically resized if needed.
  71. * The method is inline, not the resize. The call overhead happens on resizes only, which is not a problem since the resizing operation
  72. * costs a lot more than the call overhead...
  73. *
  74. * \param entry [in] a float to store in the container
  75. * \see Add(udword entry)
  76. * \see Empty()
  77. * \see Contains(udword entry)
  78. * \return Self-Reference
  79. */
  80. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. inline_ Container& Add(float entry)
  82. {
  83. // Resize if needed
  84. if(mCurNbEntries==mMaxNbEntries) Resize();
  85. // Add new entry
  86. mEntries[mCurNbEntries++] = IR(entry);
  87. return *this;
  88. }
  89. inline_ Container& Add(const float* entries, udword nb)
  90. {
  91. // Resize if needed
  92. if(mCurNbEntries+nb>mMaxNbEntries) Resize(nb);
  93. // Add new entry
  94. CopyMemory(&mEntries[mCurNbEntries], entries, nb*sizeof(float));
  95. mCurNbEntries+=nb;
  96. return *this;
  97. }
  98. //! Add unique [slow]
  99. inline_ Container& AddUnique(udword entry)
  100. {
  101. if(!Contains(entry)) Add(entry);
  102. return *this;
  103. }
  104. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  105. /**
  106. * Clears the container. All stored values are deleted, and it frees used ram.
  107. * \see Reset()
  108. * \return Self-Reference
  109. */
  110. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  111. Container& Empty();
  112. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113. /**
  114. * Resets the container. Stored values are discarded but the buffer is kept so that further calls don't need resizing again.
  115. * That's a kind of temporal coherence.
  116. * \see Empty()
  117. */
  118. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  119. inline_ void Reset()
  120. {
  121. // Avoid the write if possible
  122. // ### CMOV
  123. if(mCurNbEntries) mCurNbEntries = 0;
  124. }
  125. // HANDLE WITH CARE
  126. inline_ void ForceSize(udword size)
  127. {
  128. mCurNbEntries = size;
  129. }
  130. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  131. /**
  132. * Sets the initial size of the container. If it already contains something, it's discarded.
  133. * \param nb [in] Number of entries
  134. * \return true if success
  135. */
  136. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  137. bool SetSize(udword nb);
  138. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  139. /**
  140. * Refits the container and get rid of unused bytes.
  141. * \return true if success
  142. */
  143. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  144. bool Refit();
  145. // Checks whether the container already contains a given value.
  146. bool Contains(udword entry, udword* location=null) const;
  147. // Deletes an entry - doesn't preserve insertion order.
  148. bool Delete(udword entry);
  149. // Deletes an entry - does preserve insertion order.
  150. bool DeleteKeepingOrder(udword entry);
  151. //! Deletes the very last entry.
  152. inline_ void DeleteLastEntry() { if(mCurNbEntries) mCurNbEntries--; }
  153. //! Deletes the entry whose index is given
  154. inline_ void DeleteIndex(udword index) { mEntries[index] = mEntries[--mCurNbEntries]; }
  155. // Helpers
  156. Container& FindNext(udword& entry, FindMode find_mode=FIND_CLAMP);
  157. Container& FindPrev(udword& entry, FindMode find_mode=FIND_CLAMP);
  158. // Data access.
  159. inline_ udword GetNbEntries() const { return mCurNbEntries; } //!< Returns the current number of entries.
  160. inline_ udword GetEntry(udword i) const { return mEntries[i]; } //!< Returns ith entry
  161. inline_ udword* GetEntries() const { return mEntries; } //!< Returns the list of entries.
  162. inline_ udword GetFirst() const { return mEntries[0]; }
  163. inline_ udword GetLast() const { return mEntries[mCurNbEntries-1]; }
  164. // Growth control
  165. inline_ float GetGrowthFactor() const { return mGrowthFactor; } //!< Returns the growth factor
  166. inline_ void SetGrowthFactor(float growth) { mGrowthFactor = growth; } //!< Sets the growth factor
  167. inline_ bool IsFull() const { return mCurNbEntries==mMaxNbEntries; } //!< Checks the container is full
  168. inline_ BOOL IsNotEmpty() const { return mCurNbEntries; } //!< Checks the container is empty
  169. //! Read-access as an array
  170. inline_ udword operator[](udword i) const { ASSERT(i>=0 && i<mCurNbEntries); return mEntries[i]; }
  171. //! Write-access as an array
  172. inline_ udword& operator[](udword i) { ASSERT(i>=0 && i<mCurNbEntries); return mEntries[i]; }
  173. // Stats
  174. udword GetUsedRam() const;
  175. //! Operator for "Container A = Container B"
  176. //void operator = (const Container& object);
  177. #ifdef CONTAINER_STATS
  178. inline_ udword GetNbContainers() const { return mNbContainers; }
  179. inline_ udword GetTotalBytes() const { return mUsedRam; }
  180. private:
  181. static udword mNbContainers; //!< Number of containers around
  182. static udword mUsedRam; //!< Amount of bytes used by containers in the system
  183. #endif
  184. private:
  185. // Resizing
  186. bool Resize(udword needed=1);
  187. // Data
  188. udword mMaxNbEntries; //!< Maximum possible number of entries
  189. udword mCurNbEntries; //!< Current number of entries
  190. udword* mEntries; //!< List of entries
  191. float mGrowthFactor; //!< Resize: new number of entries = old number * mGrowthFactor
  192. };
  193. #endif // __ICECONTAINER_H__