HashMap.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "tests/framework/Framework.h"
  6. #include "tests/util/Foo.h"
  7. #include "anki/util/HashMap.h"
  8. #include "anki/util/DynamicArray.h"
  9. #include "anki/util/HighRezTimer.h"
  10. #include <unordered_map>
  11. using namespace anki;
  12. class Hasher
  13. {
  14. public:
  15. U64 operator()(int x)
  16. {
  17. return x;
  18. }
  19. };
  20. class Compare
  21. {
  22. public:
  23. Bool operator()(int a, int b)
  24. {
  25. return a == b;
  26. }
  27. };
  28. ANKI_TEST(Util, HashMap)
  29. {
  30. HeapAllocator<U8> alloc(allocAligned, nullptr);
  31. int vals[] = {20, 15, 5, 1, 10, 0, 18, 6, 7, 11, 13, 3};
  32. U valsSize = sizeof(vals) / sizeof(vals[0]);
  33. // Simple
  34. {
  35. HashMap<int, int, Hasher, Compare> map;
  36. map.pushBack(alloc, 20, 1);
  37. map.pushBack(alloc, 21, 1);
  38. map.destroy(alloc);
  39. }
  40. // Add more and iterate
  41. {
  42. HashMap<int, int, Hasher, Compare> map;
  43. for(U i = 0; i < valsSize; ++i)
  44. {
  45. map.pushBack(alloc, vals[i], vals[i] * 10);
  46. }
  47. U count = 0;
  48. auto it = map.getBegin();
  49. for(; it != map.getEnd(); ++it)
  50. {
  51. ++count;
  52. }
  53. ANKI_TEST_EXPECT_EQ(count, valsSize);
  54. map.destroy(alloc);
  55. }
  56. // Erase
  57. {
  58. HashMap<int, int, Hasher, Compare> map;
  59. for(U i = 0; i < valsSize; ++i)
  60. {
  61. map.pushBack(alloc, vals[i], vals[i] * 10);
  62. }
  63. for(U i = valsSize - 1; i != 0; --i)
  64. {
  65. HashMap<int, int, Hasher, Compare>::Iterator it = map.find(vals[i]);
  66. ANKI_TEST_EXPECT_NEQ(it, map.getEnd());
  67. map.erase(alloc, it);
  68. map.pushBack(alloc, vals[i], vals[i] * 10);
  69. }
  70. map.destroy(alloc);
  71. }
  72. // Find
  73. {
  74. HashMap<int, int, Hasher, Compare> map;
  75. for(U i = 0; i < valsSize; ++i)
  76. {
  77. map.pushBack(alloc, vals[i], vals[i] * 10);
  78. }
  79. for(U i = valsSize - 1; i != 0; --i)
  80. {
  81. HashMap<int, int, Hasher, Compare>::Iterator it = map.find(vals[i]);
  82. ANKI_TEST_EXPECT_NEQ(it, map.getEnd());
  83. ANKI_TEST_EXPECT_EQ(*it, vals[i] * 10);
  84. }
  85. map.destroy(alloc);
  86. }
  87. // Bench it
  88. {
  89. HashMap<int, int, Hasher, Compare> akMap;
  90. std::unordered_map<int,
  91. int,
  92. std::hash<int>,
  93. std::equal_to<int>,
  94. HeapAllocator<std::pair<int, int>>>
  95. stdMap(10, std::hash<int>(), std::equal_to<int>(), alloc);
  96. std::unordered_map<int, int> tmpMap;
  97. HighRezTimer timer;
  98. I64 count = 0;
  99. // Create a huge set
  100. const U COUNT = 1024 * 100;
  101. DynamicArrayAuto<int> vals(alloc);
  102. vals.create(COUNT);
  103. for(U i = 0; i < COUNT; ++i)
  104. {
  105. // Put unique keys
  106. int v;
  107. do
  108. {
  109. v = rand();
  110. } while(tmpMap.find(v) != tmpMap.end());
  111. tmpMap[v] = 1;
  112. vals[i] = v;
  113. }
  114. // Put the vals AnKi
  115. timer.start();
  116. for(U i = 0; i < COUNT; ++i)
  117. {
  118. akMap.pushBack(alloc, vals[i], vals[i]);
  119. }
  120. timer.stop();
  121. HighRezTimer::Scalar akTime = timer.getElapsedTime();
  122. // Put the vals STL
  123. timer.start();
  124. for(U i = 0; i < COUNT; ++i)
  125. {
  126. stdMap[vals[i]] = vals[i];
  127. }
  128. timer.stop();
  129. HighRezTimer::Scalar stlTime = timer.getElapsedTime();
  130. printf("Inserting bench: STL %f AnKi %f | %f%%\n",
  131. stlTime,
  132. akTime,
  133. stlTime / akTime * 100.0);
  134. // Find values AnKi
  135. timer.start();
  136. for(U i = 0; i < COUNT; ++i)
  137. {
  138. auto it = akMap.find(vals[i]);
  139. count += *it;
  140. }
  141. timer.stop();
  142. akTime = timer.getElapsedTime();
  143. // Find values STL
  144. timer.start();
  145. for(U i = 0; i < COUNT; ++i)
  146. {
  147. count += stdMap[vals[i]];
  148. }
  149. timer.stop();
  150. stlTime = timer.getElapsedTime();
  151. printf("Find bench: STL %f AnKi %f | %f%%\n",
  152. stlTime,
  153. akTime,
  154. stlTime / akTime * 100.0);
  155. akMap.destroy(alloc);
  156. }
  157. }
  158. class Hashable : public IntrusiveHashMapEnabled<Hashable>
  159. {
  160. public:
  161. Hashable(int x)
  162. : m_x(x)
  163. {
  164. }
  165. int m_x;
  166. };
  167. ANKI_TEST(Util, IntrusiveHashMap)
  168. {
  169. Hashable a(1);
  170. Hashable b(2);
  171. Hashable c(10);
  172. IntrusiveHashMap<int, Hashable, Hasher, Compare> map;
  173. // Add vals
  174. map.pushBack(1, &a);
  175. map.pushBack(2, &b);
  176. map.pushBack(10, &c);
  177. // Find
  178. ANKI_TEST_EXPECT_EQ(map.find(1)->m_x, 1);
  179. ANKI_TEST_EXPECT_EQ(map.find(10)->m_x, 10);
  180. // Erase
  181. map.erase(map.find(10));
  182. ANKI_TEST_EXPECT_EQ(map.find(10), map.getEnd());
  183. // Put back
  184. map.pushBack(10, &c);
  185. ANKI_TEST_EXPECT_NEQ(map.find(10), map.getEnd());
  186. }