HashMap.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include <algorithm>
  12. using namespace anki;
  13. class Hasher
  14. {
  15. public:
  16. U64 operator()(int x)
  17. {
  18. return x;
  19. }
  20. };
  21. class Compare
  22. {
  23. public:
  24. Bool operator()(int a, int b)
  25. {
  26. return a == b;
  27. }
  28. };
  29. ANKI_TEST(Util, HashMap)
  30. {
  31. HeapAllocator<U8> alloc(allocAligned, nullptr);
  32. int vals[] = {20, 15, 5, 1, 10, 0, 18, 6, 7, 11, 13, 3};
  33. U valsSize = sizeof(vals) / sizeof(vals[0]);
  34. // Simple
  35. {
  36. HashMap<int, int, Hasher, Compare> map;
  37. map.pushBack(alloc, 20, 1);
  38. map.pushBack(alloc, 21, 1);
  39. map.destroy(alloc);
  40. }
  41. // Add more and iterate
  42. {
  43. HashMap<int, int, Hasher, Compare> map;
  44. for(U i = 0; i < valsSize; ++i)
  45. {
  46. map.pushBack(alloc, vals[i], vals[i] * 10);
  47. }
  48. U count = 0;
  49. auto it = map.getBegin();
  50. for(; it != map.getEnd(); ++it)
  51. {
  52. ++count;
  53. }
  54. ANKI_TEST_EXPECT_EQ(count, valsSize);
  55. map.destroy(alloc);
  56. }
  57. // Erase
  58. {
  59. HashMap<int, int, Hasher, Compare> map;
  60. for(U i = 0; i < valsSize; ++i)
  61. {
  62. map.pushBack(alloc, vals[i], vals[i] * 10);
  63. }
  64. for(U i = valsSize - 1; i != 0; --i)
  65. {
  66. HashMap<int, int, Hasher, Compare>::Iterator it = map.find(vals[i]);
  67. ANKI_TEST_EXPECT_NEQ(it, map.getEnd());
  68. map.erase(alloc, it);
  69. map.pushBack(alloc, vals[i], vals[i] * 10);
  70. }
  71. map.destroy(alloc);
  72. }
  73. // Find
  74. {
  75. HashMap<int, int, Hasher, Compare> map;
  76. for(U i = 0; i < valsSize; ++i)
  77. {
  78. map.pushBack(alloc, vals[i], vals[i] * 10);
  79. }
  80. for(U i = valsSize - 1; i != 0; --i)
  81. {
  82. HashMap<int, int, Hasher, Compare>::Iterator it = map.find(vals[i]);
  83. ANKI_TEST_EXPECT_NEQ(it, map.getEnd());
  84. ANKI_TEST_EXPECT_EQ(*it, vals[i] * 10);
  85. }
  86. map.destroy(alloc);
  87. }
  88. // Fuzzy test
  89. {
  90. const U MAX = 1000;
  91. HashMap<int, int, Hasher, Compare> akMap;
  92. std::vector<int> numbers;
  93. // Inserd random
  94. for(U i = 0; i < MAX; ++i)
  95. {
  96. U num;
  97. while(1)
  98. {
  99. num = rand();
  100. if(std::find(numbers.begin(), numbers.end(), int(num))
  101. == numbers.end())
  102. {
  103. // Not found
  104. ANKI_TEST_EXPECT_EQ(akMap.find(num), akMap.getEnd());
  105. akMap.pushBack(alloc, num, num);
  106. numbers.push_back(num);
  107. break;
  108. }
  109. else
  110. {
  111. // Found
  112. ANKI_TEST_EXPECT_NEQ(akMap.find(num), akMap.getEnd());
  113. }
  114. }
  115. }
  116. // Remove randomly
  117. U count = MAX;
  118. while(count--)
  119. {
  120. U idx = rand() % (count + 1);
  121. int num = numbers[idx];
  122. numbers.erase(numbers.begin() + idx);
  123. auto it = akMap.find(num);
  124. akMap.erase(alloc, it);
  125. }
  126. akMap.destroy(alloc);
  127. }
  128. // Bench it
  129. {
  130. HashMap<int, int, Hasher, Compare> akMap;
  131. std::unordered_map<int,
  132. int,
  133. std::hash<int>,
  134. std::equal_to<int>,
  135. HeapAllocator<std::pair<int, int>>>
  136. stdMap(10, std::hash<int>(), std::equal_to<int>(), alloc);
  137. std::unordered_map<int, int> tmpMap;
  138. HighRezTimer timer;
  139. I64 count = 0;
  140. // Create a huge set
  141. const U COUNT = 1024 * 100;
  142. DynamicArrayAuto<int> vals(alloc);
  143. vals.create(COUNT);
  144. for(U i = 0; i < COUNT; ++i)
  145. {
  146. // Put unique keys
  147. int v;
  148. do
  149. {
  150. v = rand();
  151. } while(tmpMap.find(v) != tmpMap.end());
  152. tmpMap[v] = 1;
  153. vals[i] = v;
  154. }
  155. // Put the vals AnKi
  156. timer.start();
  157. for(U i = 0; i < COUNT; ++i)
  158. {
  159. akMap.pushBack(alloc, vals[i], vals[i]);
  160. }
  161. timer.stop();
  162. HighRezTimer::Scalar akTime = timer.getElapsedTime();
  163. // Put the vals STL
  164. timer.start();
  165. for(U i = 0; i < COUNT; ++i)
  166. {
  167. stdMap[vals[i]] = vals[i];
  168. }
  169. timer.stop();
  170. HighRezTimer::Scalar stlTime = timer.getElapsedTime();
  171. printf("Inserting bench: STL %f AnKi %f | %f%%\n",
  172. stlTime,
  173. akTime,
  174. stlTime / akTime * 100.0);
  175. // Find values AnKi
  176. timer.start();
  177. for(U i = 0; i < COUNT; ++i)
  178. {
  179. auto it = akMap.find(vals[i]);
  180. count += *it;
  181. }
  182. timer.stop();
  183. akTime = timer.getElapsedTime();
  184. // Find values STL
  185. timer.start();
  186. for(U i = 0; i < COUNT; ++i)
  187. {
  188. count += stdMap[vals[i]];
  189. }
  190. timer.stop();
  191. stlTime = timer.getElapsedTime();
  192. printf("Find bench: STL %f AnKi %f | %f%%\n",
  193. stlTime,
  194. akTime,
  195. stlTime / akTime * 100.0);
  196. akMap.destroy(alloc);
  197. }
  198. }
  199. class Hashable : public IntrusiveHashMapEnabled<Hashable>
  200. {
  201. public:
  202. Hashable(int x)
  203. : m_x(x)
  204. {
  205. }
  206. int m_x;
  207. };
  208. ANKI_TEST(Util, IntrusiveHashMap)
  209. {
  210. Hashable a(1);
  211. Hashable b(2);
  212. Hashable c(10);
  213. IntrusiveHashMap<int, Hashable, Hasher, Compare> map;
  214. // Add vals
  215. map.pushBack(1, &a);
  216. map.pushBack(2, &b);
  217. map.pushBack(10, &c);
  218. // Find
  219. ANKI_TEST_EXPECT_EQ(map.find(1)->m_x, 1);
  220. ANKI_TEST_EXPECT_EQ(map.find(10)->m_x, 10);
  221. // Erase
  222. map.erase(map.find(10));
  223. ANKI_TEST_EXPECT_EQ(map.find(10), map.getEnd());
  224. // Put back
  225. map.pushBack(10, &c);
  226. ANKI_TEST_EXPECT_NEQ(map.find(10), map.getEnd());
  227. }