IdTable.h 4.1 KB

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