IdTable.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #define INVALID_ID 65535
  30. struct Id
  31. {
  32. uint16_t id;
  33. uint16_t index;
  34. };
  35. /// Table of Ids.
  36. template <uint32_t MAX_NUM_ID>
  37. class IdTable
  38. {
  39. public:
  40. /// Creates the table for tracking exactly @a MAX_NUM_ID - 1 unique Ids.
  41. IdTable();
  42. /// Returns a new Id.
  43. Id create();
  44. /// Destroys the specified @a id.
  45. void destroy(Id id);
  46. /// Returns whether the table has the specified @a id
  47. bool has(Id id) const;
  48. private:
  49. // Returns the next available unique id.
  50. uint16_t next_id();
  51. private:
  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. // Table of ids.
  59. // The last valid id is reserved and cannot be used to
  60. // refer to Ids from the outside.
  61. Id m_ids[MAX_NUM_ID];
  62. };
  63. //-----------------------------------------------------------------------------
  64. template <uint32_t MAX_NUM_ID>
  65. inline IdTable<MAX_NUM_ID>::IdTable()
  66. : m_freelist(MAX_NUM_ID), m_last_index(0), m_next_id(0)
  67. {
  68. }
  69. //-----------------------------------------------------------------------------
  70. template <uint32_t MAX_NUM_ID>
  71. inline Id IdTable<MAX_NUM_ID>::create()
  72. {
  73. // Obtain a new id
  74. Id id;
  75. id.id = next_id();
  76. // Recycle slot if there are any
  77. if (m_freelist != MAX_NUM_ID)
  78. {
  79. id.index = m_freelist;
  80. m_freelist = m_ids[m_freelist].id;
  81. }
  82. else
  83. {
  84. id.index = m_last_index++;
  85. }
  86. m_ids[id.index] = id;
  87. return id;
  88. }
  89. //-----------------------------------------------------------------------------
  90. template <uint32_t MAX_NUM_ID>
  91. inline void IdTable<MAX_NUM_ID>::destroy(Id id)
  92. {
  93. CE_ASSERT(has(id), "IdTable does not have ID: %d,%d", id.id, id.index);
  94. m_ids[id.index].id = INVALID_ID;
  95. m_ids[id.index].index = m_freelist;
  96. m_freelist = id.index;
  97. }
  98. //-----------------------------------------------------------------------------
  99. template <uint32_t MAX_NUM_ID>
  100. inline bool IdTable<MAX_NUM_ID>::has(Id id) const
  101. {
  102. return id.index < MAX_NUM_ID && m_ids[id.index].id == id.id;
  103. }
  104. //-----------------------------------------------------------------------------
  105. template <uint32_t MAX_NUM_ID>
  106. inline uint16_t IdTable<MAX_NUM_ID>::next_id()
  107. {
  108. CE_ASSERT(m_next_id < MAX_NUM_ID, "Maximum number of IDs reached");
  109. return m_next_id++;
  110. }
  111. } // namespace crown