handlealloc.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright 2010-2020 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #ifndef BX_HANDLE_ALLOC_H_HEADER_GUARD
  6. #define BX_HANDLE_ALLOC_H_HEADER_GUARD
  7. #include "bx.h"
  8. #include "allocator.h"
  9. #include "uint32_t.h"
  10. namespace bx
  11. {
  12. constexpr uint16_t kInvalidHandle = UINT16_MAX;
  13. ///
  14. class HandleAlloc
  15. {
  16. public:
  17. ///
  18. HandleAlloc(uint16_t _maxHandles);
  19. ///
  20. ~HandleAlloc();
  21. ///
  22. const uint16_t* getHandles() const;
  23. ///
  24. uint16_t getHandleAt(uint16_t _at) const;
  25. ///
  26. uint16_t getNumHandles() const;
  27. ///
  28. uint16_t getMaxHandles() const;
  29. ///
  30. uint16_t alloc();
  31. ///
  32. bool isValid(uint16_t _handle) const;
  33. ///
  34. void free(uint16_t _handle);
  35. ///
  36. void reset();
  37. private:
  38. HandleAlloc();
  39. ///
  40. uint16_t* getDensePtr() const;
  41. ///
  42. uint16_t* getSparsePtr() const;
  43. uint16_t m_numHandles;
  44. uint16_t m_maxHandles;
  45. };
  46. ///
  47. HandleAlloc* createHandleAlloc(AllocatorI* _allocator, uint16_t _maxHandles);
  48. ///
  49. void destroyHandleAlloc(AllocatorI* _allocator, HandleAlloc* _handleAlloc);
  50. ///
  51. template <uint16_t MaxHandlesT>
  52. class HandleAllocT : public HandleAlloc
  53. {
  54. public:
  55. ///
  56. HandleAllocT();
  57. ///
  58. ~HandleAllocT();
  59. private:
  60. uint16_t m_padding[2*MaxHandlesT];
  61. };
  62. ///
  63. template <uint16_t MaxHandlesT>
  64. class HandleListT
  65. {
  66. public:
  67. ///
  68. HandleListT();
  69. ///
  70. void pushBack(uint16_t _handle);
  71. ///
  72. uint16_t popBack();
  73. ///
  74. void pushFront(uint16_t _handle);
  75. ///
  76. uint16_t popFront();
  77. ///
  78. uint16_t getFront() const;
  79. ///
  80. uint16_t getBack() const;
  81. ///
  82. uint16_t getNext(uint16_t _handle) const;
  83. ///
  84. uint16_t getPrev(uint16_t _handle) const;
  85. ///
  86. void remove(uint16_t _handle);
  87. ///
  88. void reset();
  89. private:
  90. ///
  91. void insertBefore(uint16_t _before, uint16_t _handle);
  92. ///
  93. void insertAfter(uint16_t _after, uint16_t _handle);
  94. ///
  95. bool isValid(uint16_t _handle) const;
  96. ///
  97. void updateFrontBack(uint16_t _handle);
  98. uint16_t m_front;
  99. uint16_t m_back;
  100. struct Link
  101. {
  102. uint16_t m_prev;
  103. uint16_t m_next;
  104. };
  105. Link m_links[MaxHandlesT];
  106. };
  107. ///
  108. template <uint16_t MaxHandlesT>
  109. class HandleAllocLruT
  110. {
  111. public:
  112. ///
  113. HandleAllocLruT();
  114. ///
  115. ~HandleAllocLruT();
  116. ///
  117. const uint16_t* getHandles() const;
  118. ///
  119. uint16_t getHandleAt(uint16_t _at) const;
  120. ///
  121. uint16_t getNumHandles() const;
  122. ///
  123. uint16_t getMaxHandles() const;
  124. ///
  125. uint16_t alloc();
  126. ///
  127. bool isValid(uint16_t _handle) const;
  128. ///
  129. void free(uint16_t _handle);
  130. ///
  131. void touch(uint16_t _handle);
  132. ///
  133. uint16_t getFront() const;
  134. ///
  135. uint16_t getBack() const;
  136. ///
  137. uint16_t getNext(uint16_t _handle) const;
  138. ///
  139. uint16_t getPrev(uint16_t _handle) const;
  140. ///
  141. void reset();
  142. private:
  143. HandleListT<MaxHandlesT> m_list;
  144. HandleAllocT<MaxHandlesT> m_alloc;
  145. };
  146. ///
  147. template <uint32_t MaxCapacityT, typename KeyT = uint32_t>
  148. class HandleHashMapT
  149. {
  150. public:
  151. ///
  152. HandleHashMapT();
  153. ///
  154. ~HandleHashMapT();
  155. ///
  156. bool insert(KeyT _key, uint16_t _handle);
  157. ///
  158. bool removeByKey(KeyT _key);
  159. ///
  160. bool removeByHandle(uint16_t _handle);
  161. ///
  162. uint16_t find(KeyT _key) const;
  163. ///
  164. void reset();
  165. ///
  166. uint32_t getNumElements() const;
  167. ///
  168. uint32_t getMaxCapacity() const;
  169. ///
  170. struct Iterator
  171. {
  172. uint16_t handle;
  173. private:
  174. friend class HandleHashMapT<MaxCapacityT, KeyT>;
  175. uint32_t pos;
  176. uint32_t num;
  177. };
  178. ///
  179. Iterator first() const;
  180. ///
  181. bool next(Iterator& _it) const;
  182. private:
  183. ///
  184. uint32_t findIndex(KeyT _key) const;
  185. ///
  186. void removeIndex(uint32_t _idx);
  187. ///
  188. uint32_t mix(uint32_t _x) const;
  189. ///
  190. uint64_t mix(uint64_t _x) const;
  191. uint32_t m_maxCapacity;
  192. uint32_t m_numElements;
  193. KeyT m_key[MaxCapacityT];
  194. uint16_t m_handle[MaxCapacityT];
  195. };
  196. ///
  197. template <uint16_t MaxHandlesT, typename KeyT = uint32_t>
  198. class HandleHashMapAllocT
  199. {
  200. public:
  201. ///
  202. HandleHashMapAllocT();
  203. ///
  204. ~HandleHashMapAllocT();
  205. ///
  206. uint16_t alloc(KeyT _key);
  207. ///
  208. void free(KeyT _key);
  209. ///
  210. void free(uint16_t _handle);
  211. ///
  212. uint16_t find(KeyT _key) const;
  213. ///
  214. const uint16_t* getHandles() const;
  215. ///
  216. uint16_t getHandleAt(uint16_t _at) const;
  217. ///
  218. uint16_t getNumHandles() const;
  219. ///
  220. uint16_t getMaxHandles() const;
  221. ///
  222. bool isValid(uint16_t _handle) const;
  223. ///
  224. void reset();
  225. private:
  226. HandleHashMapT<MaxHandlesT+MaxHandlesT/2, KeyT> m_table;
  227. HandleAllocT<MaxHandlesT> m_alloc;
  228. };
  229. } // namespace bx
  230. #include "inline/handlealloc.inl"
  231. #endif // BX_HANDLE_ALLOC_H_HEADER_GUARD