id_table.h 4.2 KB

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