IdArray.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "Allocator.h"
  26. #include "Types.h"
  27. #include "IdTable.h"
  28. #include "List.h"
  29. namespace crown
  30. {
  31. /// Table of Ids.
  32. template <uint32_t MAX_NUM_ID, typename T>
  33. class IdArray
  34. {
  35. public:
  36. /// Creates the table for tracking exactly @a MAX_NUM_ID - 1 unique Ids.
  37. IdArray(Allocator& a);
  38. /// Random access by Id
  39. T& operator[](const Id& id);
  40. const T& operator[](const Id& id) const;
  41. /// Returns a new Id.
  42. Id create(const T& object);
  43. /// Destroys the specified @a id.
  44. void destroy(Id id);
  45. /// Returns whether the table has the specified @a id
  46. bool has(Id id) const;
  47. T& lookup(const Id& id);
  48. private:
  49. // Returns the next available unique id.
  50. uint16_t next_id();
  51. public:
  52. // The index of the first unused id
  53. uint16_t m_freelist;
  54. // The index of the last id in the id table
  55. uint16_t m_last_index;
  56. // Next available unique id
  57. uint16_t m_next_id;
  58. // The last valid id is reserved and cannot be used to
  59. // refer to Ids from the outside
  60. Id m_sparse[MAX_NUM_ID];
  61. uint16_t m_sparse_to_dense[MAX_NUM_ID];
  62. uint16_t m_dense_to_sparse[MAX_NUM_ID];
  63. List<T> m_objects;
  64. };
  65. //-----------------------------------------------------------------------------
  66. template <uint32_t MAX_NUM_ID, typename T>
  67. inline IdArray<MAX_NUM_ID, T>::IdArray(Allocator& a)
  68. : m_freelist(MAX_NUM_ID)
  69. , m_last_index(0)
  70. , m_next_id(0)
  71. , m_objects(a)
  72. {
  73. for (uint32_t i = 0; i < MAX_NUM_ID; i++)
  74. {
  75. m_sparse[i].id = INVALID_ID;
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. template <uint32_t MAX_NUM_ID, typename T>
  80. inline T& IdArray<MAX_NUM_ID, T>::operator[](const Id& id)
  81. {
  82. return lookup(id);
  83. }
  84. //-----------------------------------------------------------------------------
  85. template <uint32_t MAX_NUM_ID, typename T>
  86. inline const T& IdArray<MAX_NUM_ID, T>::operator[](const Id& id) const
  87. {
  88. return lookup(id);
  89. }
  90. //-----------------------------------------------------------------------------
  91. template <uint32_t MAX_NUM_ID, typename T>
  92. inline Id IdArray<MAX_NUM_ID, T>::create(const T& object)
  93. {
  94. // Obtain a new id
  95. Id id;
  96. id.id = next_id();
  97. uint16_t dense_index;
  98. // Recycle slot if there are any
  99. if (m_freelist != MAX_NUM_ID)
  100. {
  101. id.index = m_freelist;
  102. m_freelist = m_sparse[m_freelist].id;
  103. m_objects[id.index] = object;
  104. dense_index = id.index;
  105. }
  106. else
  107. {
  108. id.index = m_last_index++;
  109. dense_index = m_objects.push_back(object);
  110. }
  111. m_sparse[id.index] = id;
  112. m_sparse_to_dense[id.index] = dense_index;
  113. m_dense_to_sparse[dense_index] = id.index;
  114. return id;
  115. }
  116. //-----------------------------------------------------------------------------
  117. template <uint32_t MAX_NUM_ID, typename T>
  118. inline void IdArray<MAX_NUM_ID, T>::destroy(Id id)
  119. {
  120. CE_ASSERT(has(id), "IdArray does not have ID: %d,%d", id.id, id.index);
  121. m_sparse[id.index].id = INVALID_ID;
  122. m_sparse[id.index].index = m_freelist;
  123. m_freelist = id.index;
  124. // Swap with last element
  125. m_objects[m_sparse_to_dense[id.index]] = m_objects.back();
  126. m_objects.pop_back();
  127. // Update conversion tables
  128. //m_sparse_to_dense[m_dense_to_sparse[m_dense.size() - 1]] = id.index;
  129. }
  130. //-----------------------------------------------------------------------------
  131. template <uint32_t MAX_NUM_ID, typename T>
  132. inline T& IdArray<MAX_NUM_ID, T>::lookup(const Id& id)
  133. {
  134. CE_ASSERT(has(id), "IdArray does not have ID: %d,%d", id.id, id.index);
  135. return m_objects[m_sparse_to_dense[id.index]];
  136. }
  137. //-----------------------------------------------------------------------------
  138. template <uint32_t MAX_NUM_ID, typename T>
  139. inline bool IdArray<MAX_NUM_ID, T>::has(Id id) const
  140. {
  141. return id.index < MAX_NUM_ID && m_sparse[id.index].id == id.id;
  142. }
  143. //-----------------------------------------------------------------------------
  144. template <uint32_t MAX_NUM_ID, typename T>
  145. inline uint16_t IdArray<MAX_NUM_ID, T>::next_id()
  146. {
  147. CE_ASSERT(m_next_id < MAX_NUM_ID, "Maximum number of IDs reached");
  148. return m_next_id++;
  149. }
  150. } // namespace crown