2
0

HashMap.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  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/DArray.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, int, std::hash<int>, std::equal_to<int>,
  91. HeapAllocator<std::pair<int, int>>> stdMap(10, std::hash<int>(),
  92. std::equal_to<int>(), alloc);
  93. std::unordered_map<int, int> tmpMap;
  94. HighRezTimer timer;
  95. I64 count = 0;
  96. // Create a huge set
  97. const U COUNT = 1024 * 100;
  98. DArrayAuto<int> vals(alloc);
  99. vals.create(COUNT);
  100. for(U i = 0; i < COUNT; ++i)
  101. {
  102. // Put unique keys
  103. int v;
  104. do
  105. {
  106. v = rand();
  107. } while(tmpMap.find(v) != tmpMap.end());
  108. tmpMap[v] = 1;
  109. vals[i] = v;
  110. }
  111. // Put the vals AnKi
  112. timer.start();
  113. for(U i = 0; i < COUNT; ++i)
  114. {
  115. akMap.pushBack(alloc, vals[i], vals[i]);
  116. }
  117. timer.stop();
  118. HighRezTimer::Scalar akTime = timer.getElapsedTime();
  119. // Put the vals STL
  120. timer.start();
  121. for(U i = 0; i < COUNT; ++i)
  122. {
  123. stdMap[vals[i]] = vals[i];
  124. }
  125. timer.stop();
  126. HighRezTimer::Scalar stlTime = timer.getElapsedTime();
  127. printf("Inserting bench: STL %f AnKi %f | %f%%\n", stlTime, akTime,
  128. stlTime / akTime * 100.0);
  129. // Find values AnKi
  130. timer.start();
  131. for(U i = 0; i < COUNT; ++i)
  132. {
  133. auto it = akMap.find(vals[i]);
  134. count += *it;
  135. }
  136. timer.stop();
  137. akTime = timer.getElapsedTime();
  138. // Find values STL
  139. timer.start();
  140. for(U i = 0; i < COUNT; ++i)
  141. {
  142. count += stdMap[vals[i]];
  143. }
  144. timer.stop();
  145. stlTime = timer.getElapsedTime();
  146. printf("Find bench: STL %f AnKi %f | %f%%\n", stlTime, akTime,
  147. stlTime / akTime * 100.0);
  148. akMap.destroy(alloc);
  149. }
  150. }
  151. class Hashable: public IntrusiveHashMapEnabled<Hashable>
  152. {
  153. public:
  154. Hashable(int x)
  155. : m_x(x)
  156. {}
  157. int m_x;
  158. };
  159. ANKI_TEST(Util, IntrusiveHashMap)
  160. {
  161. Hashable a(1);
  162. Hashable b(2);
  163. Hashable c(10);
  164. IntrusiveHashMap<int, Hashable, Hasher, Compare> map;
  165. // Add vals
  166. map.pushBack(1, &a);
  167. map.pushBack(2, &b);
  168. map.pushBack(10, &c);
  169. // Find
  170. ANKI_TEST_EXPECT_EQ(map.find(1)->m_x, 1);
  171. ANKI_TEST_EXPECT_EQ(map.find(10)->m_x, 10);
  172. // Erase
  173. map.erase(map.find(10));
  174. ANKI_TEST_EXPECT_EQ(map.find(10), map.getEnd());
  175. // Put back
  176. map.pushBack(10, &c);
  177. ANKI_TEST_EXPECT_NEQ(map.find(10), map.getEnd());
  178. }