HashMap.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/util/Allocator.h>
  7. #include <anki/util/Functions.h>
  8. #include <anki/util/NonCopyable.h>
  9. #include <anki/util/SparseArray.h>
  10. namespace anki
  11. {
  12. /// @addtogroup util_containers
  13. /// @{
  14. /// Default hasher.
  15. template<typename TKey>
  16. class DefaultHasher
  17. {
  18. public:
  19. U64 operator()(const TKey& a) const
  20. {
  21. return a.computeHash();
  22. }
  23. };
  24. /// Specialization for U64 keys.
  25. template<>
  26. class DefaultHasher<U64>
  27. {
  28. public:
  29. U64 operator()(const U64 a) const
  30. {
  31. return a;
  32. }
  33. };
  34. /// Specialization for I64 keys.
  35. template<>
  36. class DefaultHasher<I64>
  37. {
  38. public:
  39. U64 operator()(const I64 a) const
  40. {
  41. union
  42. {
  43. U64 u64;
  44. I64 i64;
  45. };
  46. i64 = a;
  47. return u64;
  48. }
  49. };
  50. template<>
  51. class DefaultHasher<U32>
  52. {
  53. public:
  54. U64 operator()(const U32 a) const
  55. {
  56. return computeHash(&a, sizeof(a));
  57. }
  58. };
  59. /// Hash map template.
  60. template<typename TKey, typename TValue, typename THasher = DefaultHasher<TKey>>
  61. class HashMap
  62. {
  63. public:
  64. // Typedefs
  65. using SparseArrayType = SparseArray<TValue, U64>;
  66. using Value = TValue;
  67. using Key = TKey;
  68. using Hasher = THasher;
  69. using Iterator = typename SparseArrayType::Iterator;
  70. using ConstIterator = typename SparseArrayType::ConstIterator;
  71. // Consts
  72. /// @see SparseArray::INITIAL_STORAGE_SIZE
  73. static constexpr U32 INITIAL_STORAGE_SIZE = SparseArrayType::INITIAL_STORAGE_SIZE;
  74. /// @see SparseArray::LINEAR_PROBING_COUNT
  75. static constexpr U32 LINEAR_PROBING_COUNT = SparseArrayType::LINEAR_PROBING_COUNT;
  76. /// @see SparseArray::MAX_LOAD_FACTOR
  77. static constexpr F32 MAX_LOAD_FACTOR = SparseArrayType::MAX_LOAD_FACTOR;
  78. /// Default constructor.
  79. /// @copy doc SparseArray::SparseArray
  80. HashMap(U32 initialStorageSize = INITIAL_STORAGE_SIZE, U32 probeCount = LINEAR_PROBING_COUNT,
  81. F32 maxLoadFactor = MAX_LOAD_FACTOR)
  82. : m_sparseArr(initialStorageSize, probeCount, maxLoadFactor)
  83. {
  84. }
  85. /// Move.
  86. HashMap(HashMap&& b)
  87. {
  88. *this = std::move(b);
  89. }
  90. /// You need to manually destroy the map.
  91. /// @see HashMap::destroy
  92. ~HashMap()
  93. {
  94. }
  95. /// Move.
  96. HashMap& operator=(HashMap&& b)
  97. {
  98. m_sparseArr = std::move(b.m_sparseArr);
  99. return *this;
  100. }
  101. /// Get begin.
  102. Iterator getBegin()
  103. {
  104. return m_sparseArr.getBegin();
  105. }
  106. /// Get begin.
  107. ConstIterator getBegin() const
  108. {
  109. return m_sparseArr.getBegin();
  110. }
  111. /// Get end.
  112. Iterator getEnd()
  113. {
  114. return m_sparseArr.getEnd();
  115. }
  116. /// Get end.
  117. ConstIterator getEnd() const
  118. {
  119. return m_sparseArr.getEnd();
  120. }
  121. /// Get begin.
  122. Iterator begin()
  123. {
  124. return getBegin();
  125. }
  126. /// Get begin.
  127. ConstIterator begin() const
  128. {
  129. return getBegin();
  130. }
  131. /// Get end.
  132. Iterator end()
  133. {
  134. return getEnd();
  135. }
  136. /// Get end.
  137. ConstIterator end() const
  138. {
  139. return getEnd();
  140. }
  141. /// Return true if map is empty.
  142. Bool isEmpty() const
  143. {
  144. return m_sparseArr.isEmpty();
  145. }
  146. /// Destroy the list.
  147. template<typename TAllocator>
  148. void destroy(TAllocator alloc)
  149. {
  150. m_sparseArr.destroy(alloc);
  151. }
  152. /// Construct an element inside the map.
  153. template<typename TAllocator, typename... TArgs>
  154. Iterator emplace(TAllocator alloc, const TKey& key, TArgs&&... args)
  155. {
  156. const U64 hash = THasher()(key);
  157. return m_sparseArr.emplace(alloc, hash, std::forward<TArgs>(args)...);
  158. }
  159. /// Erase element.
  160. template<typename TAllocator>
  161. void erase(TAllocator alloc, Iterator it)
  162. {
  163. m_sparseArr.erase(alloc, it);
  164. }
  165. /// Find a value using a key.
  166. Iterator find(const Key& key)
  167. {
  168. const U64 hash = THasher()(key);
  169. return m_sparseArr.find(hash);
  170. }
  171. /// Find a value using a key.
  172. ConstIterator find(const Key& key) const
  173. {
  174. const U64 hash = THasher()(key);
  175. return m_sparseArr.find(hash);
  176. }
  177. protected:
  178. SparseArrayType m_sparseArr;
  179. };
  180. /// Hash map template with automatic cleanup.
  181. template<typename TKey, typename TValue, typename THasher = DefaultHasher<TKey>>
  182. class HashMapAuto : public HashMap<TKey, TValue, THasher>
  183. {
  184. public:
  185. using Base = HashMap<TKey, TValue, THasher>;
  186. /// Default constructor.
  187. /// @copy doc SparseArray::SparseArray
  188. HashMapAuto(const GenericMemoryPoolAllocator<U8>& alloc, U32 initialStorageSize = Base::INITIAL_STORAGE_SIZE,
  189. U32 probeCount = Base::LINEAR_PROBING_COUNT, F32 maxLoadFactor = Base::MAX_LOAD_FACTOR)
  190. : Base(initialStorageSize, probeCount, maxLoadFactor)
  191. , m_alloc(alloc)
  192. {
  193. }
  194. /// Move.
  195. HashMapAuto(HashMapAuto&& b)
  196. {
  197. *this = std::move(b);
  198. }
  199. /// Copy.
  200. HashMapAuto(const HashMapAuto& b)
  201. : Base()
  202. {
  203. copy(b);
  204. }
  205. /// Destructor.
  206. ~HashMapAuto()
  207. {
  208. destroy();
  209. }
  210. /// Move.
  211. HashMapAuto& operator=(HashMapAuto&& b)
  212. {
  213. std::move(*static_cast<HashMapAuto>(this));
  214. m_alloc = std::move(b.m_alloc);
  215. return *this;
  216. }
  217. /// Copy.
  218. HashMapAuto& operator=(const HashMapAuto& b)
  219. {
  220. copy(b);
  221. return *this;
  222. }
  223. /// Construct an element inside the map.
  224. template<typename... TArgs>
  225. typename Base::Iterator emplace(const TKey& key, TArgs&&... args)
  226. {
  227. return Base::emplace(m_alloc, key, std::forward<TArgs>(args)...);
  228. }
  229. /// Erase element.
  230. void erase(typename Base::Iterator it)
  231. {
  232. Base::erase(m_alloc, it);
  233. }
  234. /// Clean up the map.
  235. void destroy()
  236. {
  237. Base::destroy(m_alloc);
  238. }
  239. private:
  240. GenericMemoryPoolAllocator<U8> m_alloc;
  241. void copy(const HashMapAuto& b)
  242. {
  243. destroy();
  244. m_alloc = b.m_alloc;
  245. b.m_sparseArr.clone(m_alloc, Base::m_sparseArr);
  246. }
  247. };
  248. /// @}
  249. } // end namespace anki