HashMap.cpp 5.7 KB

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