2
0

HashMap.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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)) == numbers.end())
  101. {
  102. // Not found
  103. ANKI_TEST_EXPECT_EQ(akMap.find(num), akMap.getEnd());
  104. akMap.pushBack(alloc, num, num);
  105. numbers.push_back(num);
  106. break;
  107. }
  108. else
  109. {
  110. // Found
  111. ANKI_TEST_EXPECT_NEQ(akMap.find(num), akMap.getEnd());
  112. }
  113. }
  114. }
  115. // Remove randomly
  116. U count = MAX;
  117. while(count--)
  118. {
  119. U idx = rand() % (count + 1);
  120. int num = numbers[idx];
  121. numbers.erase(numbers.begin() + idx);
  122. auto it = akMap.find(num);
  123. akMap.erase(alloc, it);
  124. }
  125. akMap.destroy(alloc);
  126. }
  127. // Bench it
  128. {
  129. HashMap<int, int, Hasher, Compare> akMap;
  130. std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, HeapAllocator<std::pair<int, int>>> stdMap(
  131. 10, std::hash<int>(), std::equal_to<int>(), alloc);
  132. std::unordered_map<int, int> tmpMap;
  133. HighRezTimer timer;
  134. I64 count = 0;
  135. // Create a huge set
  136. const U COUNT = 1024 * 100;
  137. DynamicArrayAuto<int> vals(alloc);
  138. vals.create(COUNT);
  139. for(U i = 0; i < COUNT; ++i)
  140. {
  141. // Put unique keys
  142. int v;
  143. do
  144. {
  145. v = rand();
  146. } while(tmpMap.find(v) != tmpMap.end());
  147. tmpMap[v] = 1;
  148. vals[i] = v;
  149. }
  150. // Put the vals AnKi
  151. timer.start();
  152. for(U i = 0; i < COUNT; ++i)
  153. {
  154. akMap.pushBack(alloc, vals[i], vals[i]);
  155. }
  156. timer.stop();
  157. HighRezTimer::Scalar akTime = timer.getElapsedTime();
  158. // Put the vals STL
  159. timer.start();
  160. for(U i = 0; i < COUNT; ++i)
  161. {
  162. stdMap[vals[i]] = vals[i];
  163. }
  164. timer.stop();
  165. HighRezTimer::Scalar stlTime = timer.getElapsedTime();
  166. printf("Inserting bench: STL %f AnKi %f | %f%%\n", stlTime, akTime, stlTime / akTime * 100.0);
  167. // Find values AnKi
  168. timer.start();
  169. for(U i = 0; i < COUNT; ++i)
  170. {
  171. auto it = akMap.find(vals[i]);
  172. count += *it;
  173. }
  174. timer.stop();
  175. akTime = timer.getElapsedTime();
  176. // Find values STL
  177. timer.start();
  178. for(U i = 0; i < COUNT; ++i)
  179. {
  180. count += stdMap[vals[i]];
  181. }
  182. timer.stop();
  183. stlTime = timer.getElapsedTime();
  184. printf("Find bench: STL %f AnKi %f | %f%%\n", stlTime, akTime, stlTime / akTime * 100.0);
  185. akMap.destroy(alloc);
  186. }
  187. }
  188. class Hashable : public IntrusiveHashMapEnabled<Hashable>
  189. {
  190. public:
  191. Hashable(int x)
  192. : m_x(x)
  193. {
  194. }
  195. int m_x;
  196. };
  197. ANKI_TEST(Util, IntrusiveHashMap)
  198. {
  199. Hashable a(1);
  200. Hashable b(2);
  201. Hashable c(10);
  202. IntrusiveHashMap<int, Hashable, Hasher, Compare> map;
  203. // Add vals
  204. map.pushBack(1, &a);
  205. map.pushBack(2, &b);
  206. map.pushBack(10, &c);
  207. // Find
  208. ANKI_TEST_EXPECT_EQ(map.find(1)->m_x, 1);
  209. ANKI_TEST_EXPECT_EQ(map.find(10)->m_x, 10);
  210. // Erase
  211. map.erase(map.find(10));
  212. ANKI_TEST_EXPECT_EQ(map.find(10), map.getEnd());
  213. // Put back
  214. map.pushBack(10, &c);
  215. ANKI_TEST_EXPECT_NEQ(map.find(10), map.getEnd());
  216. }