HashMap.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2009-present, 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/MemoryPool.h>
  7. #include <AnKi/Util/Hash.h>
  8. #include <AnKi/Util/Functions.h>
  9. #include <AnKi/Util/SparseArray.h>
  10. namespace anki {
  11. /// @addtogroup util_containers
  12. /// @{
  13. /// Default hasher.
  14. template<typename TKey>
  15. class DefaultHasher
  16. {
  17. public:
  18. U64 operator()(const TKey& a) const
  19. {
  20. return a.computeHash();
  21. }
  22. };
  23. /// Specialization for some fundamental types.
  24. template<typename T>
  25. concept IsFundamentalOrPointer = std::is_fundamental_v<T> || std::is_pointer_v<T>;
  26. template<IsFundamentalOrPointer TKey>
  27. class DefaultHasher<TKey>
  28. {
  29. public:
  30. U64 operator()(const TKey a) const
  31. {
  32. return computeHash(&a, sizeof(a));
  33. }
  34. };
  35. /// SparseArray configuration. See SparseArray docs for details.
  36. /// @memberof HashMap
  37. class HashMapSparseArrayConfig
  38. {
  39. public:
  40. using Index = U64;
  41. static constexpr Index getInitialStorageSize()
  42. {
  43. return 64;
  44. }
  45. static constexpr U32 getLinearProbingCount()
  46. {
  47. return 8;
  48. }
  49. static constexpr F32 getMaxLoadFactor()
  50. {
  51. return 0.8f;
  52. }
  53. };
  54. /// Hash map template.
  55. template<typename TKey, typename TValue, typename THasher = DefaultHasher<TKey>, typename TMemoryPool = SingletonMemoryPoolWrapper<DefaultMemoryPool>,
  56. typename TSparseArrayConfig = HashMapSparseArrayConfig>
  57. class HashMap
  58. {
  59. public:
  60. // Typedefs
  61. using SparseArrayType = SparseArray<TValue, TMemoryPool, TSparseArrayConfig>;
  62. using Value = TValue;
  63. using Key = TKey;
  64. using Hasher = THasher;
  65. using Iterator = typename SparseArrayType::Iterator;
  66. using ConstIterator = typename SparseArrayType::ConstIterator;
  67. /// Default constructor.
  68. HashMap(const TMemoryPool& pool = TMemoryPool())
  69. : m_sparseArr(pool)
  70. {
  71. }
  72. /// Move.
  73. HashMap(HashMap&& b)
  74. {
  75. *this = std::move(b);
  76. }
  77. /// Copy.
  78. HashMap(const HashMap& b)
  79. : m_sparseArr(b.m_sparseArr)
  80. {
  81. }
  82. ~HashMap() = default;
  83. /// Move.
  84. HashMap& operator=(HashMap&& b)
  85. {
  86. m_sparseArr = std::move(b.m_sparseArr);
  87. return *this;
  88. }
  89. /// Move.
  90. HashMap& operator=(const HashMap& b)
  91. {
  92. m_sparseArr = b.m_sparseArr;
  93. return *this;
  94. }
  95. /// Get begin.
  96. Iterator getBegin()
  97. {
  98. return m_sparseArr.getBegin();
  99. }
  100. /// Get begin.
  101. ConstIterator getBegin() const
  102. {
  103. return m_sparseArr.getBegin();
  104. }
  105. /// Get end.
  106. Iterator getEnd()
  107. {
  108. return m_sparseArr.getEnd();
  109. }
  110. /// Get end.
  111. ConstIterator getEnd() const
  112. {
  113. return m_sparseArr.getEnd();
  114. }
  115. /// Get begin.
  116. Iterator begin()
  117. {
  118. return getBegin();
  119. }
  120. /// Get begin.
  121. ConstIterator begin() const
  122. {
  123. return getBegin();
  124. }
  125. /// Get end.
  126. Iterator end()
  127. {
  128. return getEnd();
  129. }
  130. /// Get end.
  131. ConstIterator end() const
  132. {
  133. return getEnd();
  134. }
  135. /// Return true if map is empty.
  136. Bool isEmpty() const
  137. {
  138. return m_sparseArr.isEmpty();
  139. }
  140. PtrSize getSize() const
  141. {
  142. return m_sparseArr.getSize();
  143. }
  144. /// Destroy the list.
  145. void destroy()
  146. {
  147. m_sparseArr.destroy();
  148. }
  149. /// Construct an element inside the map.
  150. template<typename... TArgs>
  151. Iterator emplace(const TKey& key, TArgs&&... args)
  152. {
  153. const U64 hash = THasher()(key);
  154. return m_sparseArr.emplace(hash, std::forward<TArgs>(args)...);
  155. }
  156. /// Erase element.
  157. void erase(Iterator it)
  158. {
  159. m_sparseArr.erase(it);
  160. }
  161. /// Find a value using a key.
  162. Iterator find(const Key& key)
  163. {
  164. const U64 hash = THasher()(key);
  165. return m_sparseArr.find(hash);
  166. }
  167. /// Find a value using a key.
  168. ConstIterator find(const Key& key) const
  169. {
  170. const U64 hash = THasher()(key);
  171. return m_sparseArr.find(hash);
  172. }
  173. SparseArrayType& getSparseArray()
  174. {
  175. return m_sparseArr;
  176. }
  177. protected:
  178. SparseArrayType m_sparseArr;
  179. };
  180. /// @}
  181. } // end namespace anki