id_array.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "container_types.h"
  27. namespace crown
  28. {
  29. /// Packed array of objects with lookup table.
  30. ///
  31. /// @ingroup Containers
  32. template <uint32_t MAX, typename T>
  33. struct IdArray
  34. {
  35. /// Creates the table for tracking exactly @a MAX - 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. // The index of the first unused id
  42. uint16_t m_freelist;
  43. // Next available unique id
  44. uint16_t m_next_id;
  45. uint16_t m_size;
  46. // The last valid id is reserved and cannot be used to
  47. // refer to Ids from the outside
  48. Id m_sparse[MAX];
  49. uint16_t m_sparse_to_dense[MAX];
  50. uint16_t m_dense_to_sparse[MAX];
  51. T m_objects[MAX];
  52. };
  53. /// Functions to manipulate IdArray.
  54. ///
  55. /// @ingroup Containers
  56. namespace id_array
  57. {
  58. /// Creates a new @a object in the array @a a and returns its id.
  59. template <uint32_t MAX, typename T> Id create(IdArray<MAX, T>& a, const T& object);
  60. /// Destroys the object with the given @a id.
  61. template <uint32_t MAX, typename T> void destroy(IdArray<MAX, T>& a, Id id);
  62. /// Returns whether the table has the object with the given @a id.
  63. template <uint32_t MAX, typename T> bool has(const IdArray<MAX, T>& a, Id id);
  64. /// Returns the number of objects in the array.
  65. template <uint32_t MAX, typename T> uint32_t size(const IdArray<MAX, T>& a);
  66. /// Returns the object with the given @a id.
  67. template <uint32_t MAX, typename T> T& get(const Id& id);
  68. template <uint32_t MAX, typename T> T* begin(IdArray<MAX, T>& a);
  69. template <uint32_t MAX, typename T> const T* begin(const IdArray<MAX, T>& a);
  70. template <uint32_t MAX, typename T> T* end(IdArray<MAX, T>& a);
  71. template <uint32_t MAX, typename T> const T* end(const IdArray<MAX, T>& a);
  72. } // namespace id_array
  73. namespace id_array
  74. {
  75. //-----------------------------------------------------------------------------
  76. template <uint32_t MAX, typename T>
  77. inline Id create(IdArray<MAX, T>& a, const T& object)
  78. {
  79. CE_ASSERT(a.m_size < MAX, "Object list full");
  80. // Obtain a new id
  81. Id id;
  82. id.id = a.m_next_id++;
  83. // Recycle slot if there are any
  84. if (a.m_freelist != INVALID_ID)
  85. {
  86. id.index = a.m_freelist;
  87. a.m_freelist = a.m_sparse[a.m_freelist].index;
  88. }
  89. else
  90. {
  91. id.index = a.m_size;
  92. }
  93. a.m_sparse[id.index] = id;
  94. a.m_sparse_to_dense[id.index] = a.m_size;
  95. a.m_dense_to_sparse[a.m_size] = id.index;
  96. a.m_objects[a.m_size] = object;
  97. a.m_size++;
  98. return id;
  99. }
  100. //-----------------------------------------------------------------------------
  101. template <uint32_t MAX, typename T>
  102. inline void destroy(IdArray<MAX, T>& a, Id id)
  103. {
  104. CE_ASSERT(has(a, id), "IdArray does not have ID: %d,%d", id.id, id.index);
  105. a.m_sparse[id.index].id = INVALID_ID;
  106. a.m_sparse[id.index].index = a.m_freelist;
  107. a.m_freelist = id.index;
  108. // Swap with last element
  109. const uint32_t last = a.m_size - 1;
  110. CE_ASSERT(last >= a.m_sparse_to_dense[id.index], "Swapping with previous item");
  111. a.m_objects[a.m_sparse_to_dense[id.index]] = a.m_objects[last];
  112. // Update tables
  113. uint16_t std = a.m_sparse_to_dense[id.index];
  114. uint16_t dts = a.m_dense_to_sparse[last];
  115. a.m_sparse_to_dense[dts] = std;
  116. a.m_dense_to_sparse[std] = dts;
  117. a.m_size--;
  118. }
  119. //-----------------------------------------------------------------------------
  120. template <uint32_t MAX, typename T>
  121. inline T& get(IdArray<MAX, T>& a, const Id& id)
  122. {
  123. CE_ASSERT(has(a, id), "IdArray does not have ID: %d,%d", id.id, id.index);
  124. return a.m_objects[a.m_sparse_to_dense[id.index]];
  125. }
  126. //-----------------------------------------------------------------------------
  127. template <uint32_t MAX, typename T>
  128. inline bool has(const IdArray<MAX, T>& a, Id id)
  129. {
  130. return id.index < MAX && a.m_sparse[id.index].id == id.id;
  131. }
  132. //-----------------------------------------------------------------------------
  133. template <uint32_t MAX, typename T>
  134. inline uint32_t size(const IdArray<MAX, T>& a)
  135. {
  136. return a.m_size;
  137. }
  138. //-----------------------------------------------------------------------------
  139. template <uint32_t MAX, typename T>
  140. inline T* begin(IdArray<MAX, T>& a)
  141. {
  142. return a.m_objects;
  143. }
  144. //-----------------------------------------------------------------------------
  145. template <uint32_t MAX, typename T>
  146. inline const T* begin(const IdArray<MAX, T>& a)
  147. {
  148. return a.m_objects;
  149. }
  150. //-----------------------------------------------------------------------------
  151. template <uint32_t MAX, typename T>
  152. inline T* end(IdArray<MAX, T>& a)
  153. {
  154. return a.m_objects + a.m_size;
  155. }
  156. //-----------------------------------------------------------------------------
  157. template <uint32_t MAX, typename T>
  158. inline const T* end(const IdArray<MAX, T>& a)
  159. {
  160. return a.m_objects + a.m_size;
  161. }
  162. } // namespace id_array
  163. //-----------------------------------------------------------------------------
  164. template <uint32_t MAX, typename T>
  165. inline IdArray<MAX, T>::IdArray()
  166. : m_freelist(INVALID_ID)
  167. , m_next_id(0)
  168. , m_size(0)
  169. {
  170. for (uint32_t i = 0; i < MAX; i++)
  171. {
  172. m_sparse[i].id = INVALID_ID;
  173. }
  174. }
  175. //-----------------------------------------------------------------------------
  176. template <uint32_t MAX, typename T>
  177. inline T& IdArray<MAX, T>::operator[](uint32_t i)
  178. {
  179. CE_ASSERT(i < m_size, "Index out of bounds");
  180. return m_objects[i];
  181. }
  182. //-----------------------------------------------------------------------------
  183. template <uint32_t MAX, typename T>
  184. inline const T& IdArray<MAX, T>::operator[](uint32_t i) const
  185. {
  186. CE_ASSERT(i < m_size, "Index out of bounds");
  187. return m_objects[i];
  188. }
  189. } // namespace crown