IdArray.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Assert.h"
  25. #include "Types.h"
  26. #include "List.h"
  27. #include "Log.h"
  28. namespace crown
  29. {
  30. /// Packed array of objects with lookup table.
  31. template <uint32_t MAX_NUM_ID, typename T>
  32. class IdArray
  33. {
  34. public:
  35. /// Creates the table for tracking exactly @a MAX_NUM_ID - 1 unique Ids.
  36. IdArray();
  37. /// Random access by index.
  38. T& operator[](uint32_t i);
  39. /// Random access by index.
  40. const T& operator[](uint32_t i) const;
  41. /// Returns a new Id.
  42. Id create(const T& object);
  43. /// Destroys the object with the given @a id.
  44. void destroy(Id id);
  45. /// Returns whether the table has the object with the given @a id
  46. bool has(Id id) const;
  47. /// Returns the number of objects in the array.
  48. uint32_t size() const;
  49. /// Returns the object with the given @a id.
  50. T& lookup(const Id& id);
  51. T* begin();
  52. const T* begin() const;
  53. T* end();
  54. const T* end() const;
  55. private:
  56. // Returns the next available unique id.
  57. uint16_t next_id();
  58. public:
  59. // The index of the first unused id
  60. uint16_t m_freelist;
  61. // The index of the last id in the id table
  62. uint16_t m_last_index;
  63. // Next available unique id
  64. uint16_t m_next_id;
  65. uint16_t m_num_objects;
  66. // The last valid id is reserved and cannot be used to
  67. // refer to Ids from the outside
  68. Id m_sparse[MAX_NUM_ID];
  69. uint16_t m_sparse_to_dense[MAX_NUM_ID];
  70. uint16_t m_dense_to_sparse[MAX_NUM_ID];
  71. T m_objects[MAX_NUM_ID];
  72. };
  73. //-----------------------------------------------------------------------------
  74. template <uint32_t MAX_NUM_ID, typename T>
  75. inline IdArray<MAX_NUM_ID, T>::IdArray()
  76. : m_freelist(INVALID_ID)
  77. , m_last_index(0)
  78. , m_next_id(0)
  79. , m_num_objects(0)
  80. {
  81. for (uint32_t i = 0; i < MAX_NUM_ID; i++)
  82. {
  83. m_sparse[i].id = INVALID_ID;
  84. }
  85. }
  86. //-----------------------------------------------------------------------------
  87. template <uint32_t MAX_NUM_ID, typename T>
  88. inline T& IdArray<MAX_NUM_ID, T>::operator[](uint32_t i)
  89. {
  90. CE_ASSERT(i < m_num_objects, "Index out of bounds");
  91. return m_objects[i];
  92. }
  93. //-----------------------------------------------------------------------------
  94. template <uint32_t MAX_NUM_ID, typename T>
  95. inline const T& IdArray<MAX_NUM_ID, T>::operator[](uint32_t i) const
  96. {
  97. CE_ASSERT(i < m_num_objects, "Index out of bounds");
  98. return m_objects[i];
  99. }
  100. //-----------------------------------------------------------------------------
  101. template <uint32_t MAX_NUM_ID, typename T>
  102. inline Id IdArray<MAX_NUM_ID, T>::create(const T& object)
  103. {
  104. CE_ASSERT(m_num_objects < MAX_NUM_ID, "Object list full");
  105. // Obtain a new id
  106. Id id;
  107. id.id = next_id();
  108. // Recycle slot if there are any
  109. if (m_freelist != INVALID_ID)
  110. {
  111. id.index = m_freelist;
  112. m_freelist = m_sparse[m_freelist].index;
  113. }
  114. else
  115. {
  116. id.index = m_last_index++;
  117. }
  118. m_sparse[id.index] = id;
  119. m_sparse_to_dense[id.index] = m_num_objects;
  120. m_dense_to_sparse[m_num_objects] = id.index;
  121. m_objects[m_num_objects] = object;
  122. m_num_objects++;
  123. return id;
  124. }
  125. //-----------------------------------------------------------------------------
  126. template <uint32_t MAX_NUM_ID, typename T>
  127. inline void IdArray<MAX_NUM_ID, T>::destroy(Id id)
  128. {
  129. CE_ASSERT(has(id), "IdArray does not have ID: %d,%d", id.id, id.index);
  130. m_sparse[id.index].id = INVALID_ID;
  131. m_sparse[id.index].index = m_freelist;
  132. m_freelist = id.index;
  133. // Swap with last element
  134. const uint32_t last = m_num_objects - 1;
  135. CE_ASSERT(last >= m_sparse_to_dense[id.index], "Swapping with previous item");
  136. m_objects[m_sparse_to_dense[id.index]] = m_objects[last];
  137. // Update tables
  138. uint16_t std = m_sparse_to_dense[id.index];
  139. uint16_t dts = m_dense_to_sparse[last];
  140. m_sparse_to_dense[dts] = std;
  141. m_dense_to_sparse[std] = dts;
  142. m_num_objects--;
  143. }
  144. //-----------------------------------------------------------------------------
  145. template <uint32_t MAX_NUM_ID, typename T>
  146. inline T& IdArray<MAX_NUM_ID, T>::lookup(const Id& id)
  147. {
  148. CE_ASSERT(has(id), "IdArray does not have ID: %d,%d", id.id, id.index);
  149. return m_objects[m_sparse_to_dense[id.index]];
  150. }
  151. //-----------------------------------------------------------------------------
  152. template <uint32_t MAX_NUM_ID, typename T>
  153. inline bool IdArray<MAX_NUM_ID, T>::has(Id id) const
  154. {
  155. return id.index < MAX_NUM_ID && m_sparse[id.index].id == id.id;
  156. }
  157. //-----------------------------------------------------------------------------
  158. template <uint32_t MAX_NUM_ID, typename T>
  159. inline uint32_t IdArray<MAX_NUM_ID, T>::size() const
  160. {
  161. return m_num_objects;
  162. }
  163. //-----------------------------------------------------------------------------
  164. template <uint32_t MAX_NUM_ID, typename T>
  165. inline uint16_t IdArray<MAX_NUM_ID, T>::next_id()
  166. {
  167. return m_next_id++;
  168. }
  169. //-----------------------------------------------------------------------------
  170. template <uint32_t MAX_NUM_ID, typename T>
  171. inline T* IdArray<MAX_NUM_ID, T>::begin()
  172. {
  173. return m_objects;
  174. }
  175. //-----------------------------------------------------------------------------
  176. template <uint32_t MAX_NUM_ID, typename T>
  177. inline const T* IdArray<MAX_NUM_ID, T>::begin() const
  178. {
  179. return m_objects;
  180. }
  181. //-----------------------------------------------------------------------------
  182. template <uint32_t MAX_NUM_ID, typename T>
  183. inline T* IdArray<MAX_NUM_ID, T>::end()
  184. {
  185. return m_objects + m_num_objects;
  186. }
  187. //-----------------------------------------------------------------------------
  188. template <uint32_t MAX_NUM_ID, typename T>
  189. inline const T* IdArray<MAX_NUM_ID, T>::end() const
  190. {
  191. return m_objects + m_num_objects;
  192. }
  193. } // namespace crown