flat_hash_map.h 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  1. // Taken from
  2. // https://github.com/skarupke/flat_hash_map/blob/2c4687431f978f02a3780e24b8b701d22aa32d9c/flat_hash_map.hpp
  3. // with fixes applied:
  4. // - https://github.com/skarupke/flat_hash_map/pull/25
  5. // - https://github.com/skarupke/flat_hash_map/pull/26
  6. // - replace size_t with uint64_t to fix it for 32bit
  7. // - add "GCC diagnostic" pragma to ignore -Wshadow
  8. // - make sherwood_v3_table::convertible_to_iterator public because GCC5 seems
  9. // to have issues with it otherwise
  10. // - fix compiler warnings in operator templated_iterator<const value_type>
  11. // Copyright Malte Skarupke 2017.
  12. // Distributed under the Boost Software License, Version 1.0.
  13. // (See http://www.boost.org/LICENSE_1_0.txt)
  14. #pragma once
  15. #include <c10/macros/Macros.h>
  16. #include <algorithm>
  17. #include <cmath>
  18. #include <cstddef>
  19. #include <cstdint>
  20. #include <functional>
  21. #include <iterator>
  22. #include <stdexcept>
  23. #include <type_traits>
  24. #include <utility>
  25. C10_CLANG_DIAGNOSTIC_PUSH()
  26. #if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
  27. C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
  28. #endif
  29. #ifdef _MSC_VER
  30. #define SKA_NOINLINE(...) __declspec(noinline) __VA_ARGS__
  31. #else
  32. #define SKA_NOINLINE(...) __VA_ARGS__ __attribute__((noinline))
  33. #endif
  34. namespace ska {
  35. struct prime_number_hash_policy;
  36. struct power_of_two_hash_policy;
  37. struct fibonacci_hash_policy;
  38. namespace detailv3 {
  39. template <typename Result, typename Functor>
  40. struct functor_storage : Functor {
  41. functor_storage() = default;
  42. functor_storage(const Functor& functor) : Functor(functor) {}
  43. template <typename... Args>
  44. Result operator()(Args&&... args) {
  45. return static_cast<Functor&>(*this)(std::forward<Args>(args)...);
  46. }
  47. template <typename... Args>
  48. Result operator()(Args&&... args) const {
  49. return static_cast<const Functor&>(*this)(std::forward<Args>(args)...);
  50. }
  51. };
  52. template <typename Result, typename... Args>
  53. struct functor_storage<Result, Result (*)(Args...)> {
  54. typedef Result (*function_ptr)(Args...);
  55. function_ptr function;
  56. functor_storage(function_ptr function) : function(function) {}
  57. Result operator()(Args... args) const {
  58. return function(std::forward<Args>(args)...);
  59. }
  60. operator function_ptr&() {
  61. return function;
  62. }
  63. operator const function_ptr&() {
  64. return function;
  65. }
  66. };
  67. template <typename key_type, typename value_type, typename hasher>
  68. struct KeyOrValueHasher : functor_storage<uint64_t, hasher> {
  69. typedef functor_storage<uint64_t, hasher> hasher_storage;
  70. KeyOrValueHasher() = default;
  71. KeyOrValueHasher(const hasher& hash) : hasher_storage(hash) {}
  72. uint64_t operator()(const key_type& key) {
  73. return static_cast<hasher_storage&>(*this)(key);
  74. }
  75. uint64_t operator()(const key_type& key) const {
  76. return static_cast<const hasher_storage&>(*this)(key);
  77. }
  78. uint64_t operator()(const value_type& value) {
  79. return static_cast<hasher_storage&>(*this)(value.first);
  80. }
  81. uint64_t operator()(const value_type& value) const {
  82. return static_cast<const hasher_storage&>(*this)(value.first);
  83. }
  84. template <typename F, typename S>
  85. uint64_t operator()(const std::pair<F, S>& value) {
  86. return static_cast<hasher_storage&>(*this)(value.first);
  87. }
  88. template <typename F, typename S>
  89. uint64_t operator()(const std::pair<F, S>& value) const {
  90. return static_cast<const hasher_storage&>(*this)(value.first);
  91. }
  92. };
  93. template <typename key_type, typename value_type, typename key_equal>
  94. struct KeyOrValueEquality : functor_storage<bool, key_equal> {
  95. typedef functor_storage<bool, key_equal> equality_storage;
  96. KeyOrValueEquality() = default;
  97. KeyOrValueEquality(const key_equal& equality) : equality_storage(equality) {}
  98. bool operator()(const key_type& lhs, const key_type& rhs) {
  99. return static_cast<equality_storage&>(*this)(lhs, rhs);
  100. }
  101. bool operator()(const key_type& lhs, const value_type& rhs) {
  102. return static_cast<equality_storage&>(*this)(lhs, rhs.first);
  103. }
  104. bool operator()(const value_type& lhs, const key_type& rhs) {
  105. return static_cast<equality_storage&>(*this)(lhs.first, rhs);
  106. }
  107. bool operator()(const value_type& lhs, const value_type& rhs) {
  108. return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
  109. }
  110. template <typename F, typename S>
  111. bool operator()(const key_type& lhs, const std::pair<F, S>& rhs) {
  112. return static_cast<equality_storage&>(*this)(lhs, rhs.first);
  113. }
  114. template <typename F, typename S>
  115. bool operator()(const std::pair<F, S>& lhs, const key_type& rhs) {
  116. return static_cast<equality_storage&>(*this)(lhs.first, rhs);
  117. }
  118. template <typename F, typename S>
  119. bool operator()(const value_type& lhs, const std::pair<F, S>& rhs) {
  120. return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
  121. }
  122. template <typename F, typename S>
  123. bool operator()(const std::pair<F, S>& lhs, const value_type& rhs) {
  124. return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
  125. }
  126. template <typename FL, typename SL, typename FR, typename SR>
  127. bool operator()(const std::pair<FL, SL>& lhs, const std::pair<FR, SR>& rhs) {
  128. return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
  129. }
  130. };
  131. static constexpr int8_t min_lookups = 4;
  132. template <typename T>
  133. struct sherwood_v3_entry {
  134. sherwood_v3_entry() = default;
  135. sherwood_v3_entry(int8_t distance_from_desired)
  136. : distance_from_desired(distance_from_desired) {}
  137. ~sherwood_v3_entry() = default;
  138. bool has_value() const {
  139. return distance_from_desired >= 0;
  140. }
  141. bool is_empty() const {
  142. return distance_from_desired < 0;
  143. }
  144. bool is_at_desired_position() const {
  145. return distance_from_desired <= 0;
  146. }
  147. template <typename... Args>
  148. void emplace(int8_t distance, Args&&... args) {
  149. new (std::addressof(value)) T(std::forward<Args>(args)...);
  150. distance_from_desired = distance;
  151. }
  152. void destroy_value() {
  153. value.~T();
  154. distance_from_desired = -1;
  155. }
  156. int8_t distance_from_desired = -1;
  157. static constexpr int8_t special_end_value = 0;
  158. union {
  159. T value;
  160. };
  161. };
  162. inline int8_t log2(uint64_t value) {
  163. static constexpr int8_t table[64] = {
  164. 63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3,
  165. 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4,
  166. 62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21,
  167. 56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5};
  168. value |= value >> 1;
  169. value |= value >> 2;
  170. value |= value >> 4;
  171. value |= value >> 8;
  172. value |= value >> 16;
  173. value |= value >> 32;
  174. return table[((value - (value >> 1)) * 0x07EDD5E59A4E28C2) >> 58];
  175. }
  176. template <typename T, bool>
  177. struct AssignIfTrue {
  178. void operator()(T& lhs, const T& rhs) {
  179. lhs = rhs;
  180. }
  181. void operator()(T& lhs, T&& rhs) {
  182. lhs = std::move(rhs);
  183. }
  184. };
  185. template <typename T>
  186. struct AssignIfTrue<T, false> {
  187. void operator()(T&, const T&) {}
  188. void operator()(T&, T&&) {}
  189. };
  190. inline uint64_t next_power_of_two(uint64_t i) {
  191. --i;
  192. i |= i >> 1;
  193. i |= i >> 2;
  194. i |= i >> 4;
  195. i |= i >> 8;
  196. i |= i >> 16;
  197. i |= i >> 32;
  198. ++i;
  199. return i;
  200. }
  201. // Implementation taken from http://en.cppreference.com/w/cpp/types/void_t
  202. // (it takes CWG1558 into account and also works for older compilers)
  203. template <typename... Ts>
  204. struct make_void {
  205. typedef void type;
  206. };
  207. template <typename... Ts>
  208. using void_t = typename make_void<Ts...>::type;
  209. template <typename T, typename = void>
  210. struct HashPolicySelector {
  211. typedef fibonacci_hash_policy type;
  212. };
  213. template <typename T>
  214. struct HashPolicySelector<T, void_t<typename T::hash_policy>> {
  215. typedef typename T::hash_policy type;
  216. };
  217. template <
  218. typename T,
  219. typename FindKey,
  220. typename ArgumentHash,
  221. typename DetailHasher,
  222. typename ArgumentEqual,
  223. typename Equal,
  224. typename ArgumentAlloc,
  225. typename EntryAlloc>
  226. class sherwood_v3_table : private EntryAlloc,
  227. private DetailHasher,
  228. private Equal {
  229. using Entry = detailv3::sherwood_v3_entry<T>;
  230. using AllocatorTraits = std::allocator_traits<EntryAlloc>;
  231. using EntryPointer = typename AllocatorTraits::pointer;
  232. public:
  233. struct convertible_to_iterator;
  234. using value_type = T;
  235. using size_type = uint64_t;
  236. using difference_type = std::ptrdiff_t;
  237. using hasher = ArgumentHash;
  238. using key_equal = ArgumentEqual;
  239. using allocator_type = EntryAlloc;
  240. using reference = value_type&;
  241. using const_reference = const value_type&;
  242. using pointer = value_type*;
  243. using const_pointer = const value_type*;
  244. sherwood_v3_table() = default;
  245. explicit sherwood_v3_table(
  246. size_type bucket_count,
  247. const ArgumentHash& hash = ArgumentHash(),
  248. const ArgumentEqual& equal = ArgumentEqual(),
  249. const ArgumentAlloc& alloc = ArgumentAlloc())
  250. : EntryAlloc(alloc), DetailHasher(hash), Equal(equal) {
  251. rehash(bucket_count);
  252. }
  253. sherwood_v3_table(size_type bucket_count, const ArgumentAlloc& alloc)
  254. : sherwood_v3_table(
  255. bucket_count,
  256. ArgumentHash(),
  257. ArgumentEqual(),
  258. alloc) {}
  259. sherwood_v3_table(
  260. size_type bucket_count,
  261. const ArgumentHash& hash,
  262. const ArgumentAlloc& alloc)
  263. : sherwood_v3_table(bucket_count, hash, ArgumentEqual(), alloc) {}
  264. explicit sherwood_v3_table(const ArgumentAlloc& alloc) : EntryAlloc(alloc) {}
  265. template <typename It>
  266. sherwood_v3_table(
  267. It first,
  268. It last,
  269. size_type bucket_count = 0,
  270. const ArgumentHash& hash = ArgumentHash(),
  271. const ArgumentEqual& equal = ArgumentEqual(),
  272. const ArgumentAlloc& alloc = ArgumentAlloc())
  273. : sherwood_v3_table(bucket_count, hash, equal, alloc) {
  274. insert(first, last);
  275. }
  276. template <typename It>
  277. sherwood_v3_table(
  278. It first,
  279. It last,
  280. size_type bucket_count,
  281. const ArgumentAlloc& alloc)
  282. : sherwood_v3_table(
  283. first,
  284. last,
  285. bucket_count,
  286. ArgumentHash(),
  287. ArgumentEqual(),
  288. alloc) {}
  289. template <typename It>
  290. sherwood_v3_table(
  291. It first,
  292. It last,
  293. size_type bucket_count,
  294. const ArgumentHash& hash,
  295. const ArgumentAlloc& alloc)
  296. : sherwood_v3_table(
  297. first,
  298. last,
  299. bucket_count,
  300. hash,
  301. ArgumentEqual(),
  302. alloc) {}
  303. sherwood_v3_table(
  304. std::initializer_list<T> il,
  305. size_type bucket_count = 0,
  306. const ArgumentHash& hash = ArgumentHash(),
  307. const ArgumentEqual& equal = ArgumentEqual(),
  308. const ArgumentAlloc& alloc = ArgumentAlloc())
  309. : sherwood_v3_table(bucket_count, hash, equal, alloc) {
  310. if (bucket_count == 0)
  311. rehash(il.size());
  312. insert(il.begin(), il.end());
  313. }
  314. sherwood_v3_table(
  315. std::initializer_list<T> il,
  316. size_type bucket_count,
  317. const ArgumentAlloc& alloc)
  318. : sherwood_v3_table(
  319. il,
  320. bucket_count,
  321. ArgumentHash(),
  322. ArgumentEqual(),
  323. alloc) {}
  324. sherwood_v3_table(
  325. std::initializer_list<T> il,
  326. size_type bucket_count,
  327. const ArgumentHash& hash,
  328. const ArgumentAlloc& alloc)
  329. : sherwood_v3_table(il, bucket_count, hash, ArgumentEqual(), alloc) {}
  330. sherwood_v3_table(const sherwood_v3_table& other)
  331. : sherwood_v3_table(
  332. other,
  333. AllocatorTraits::select_on_container_copy_construction(
  334. other.get_allocator())) {}
  335. sherwood_v3_table(const sherwood_v3_table& other, const ArgumentAlloc& alloc)
  336. : EntryAlloc(alloc),
  337. DetailHasher(other),
  338. Equal(other),
  339. _max_load_factor(other._max_load_factor) {
  340. rehash_for_other_container(other);
  341. try {
  342. insert(other.begin(), other.end());
  343. } catch (...) {
  344. clear();
  345. deallocate_data(entries, num_slots_minus_one, max_lookups);
  346. throw;
  347. }
  348. }
  349. sherwood_v3_table(sherwood_v3_table&& other) noexcept
  350. : EntryAlloc(std::move(other)),
  351. DetailHasher(std::move(other)),
  352. Equal(std::move(other)) {
  353. swap_pointers(other);
  354. }
  355. sherwood_v3_table(
  356. sherwood_v3_table&& other,
  357. const ArgumentAlloc& alloc) noexcept
  358. : EntryAlloc(alloc),
  359. DetailHasher(std::move(other)),
  360. Equal(std::move(other)) {
  361. swap_pointers(other);
  362. }
  363. sherwood_v3_table& operator=(const sherwood_v3_table& other) {
  364. if (this == std::addressof(other))
  365. return *this;
  366. clear();
  367. if (AllocatorTraits::propagate_on_container_copy_assignment::value) {
  368. if (static_cast<EntryAlloc&>(*this) !=
  369. static_cast<const EntryAlloc&>(other)) {
  370. reset_to_empty_state();
  371. }
  372. AssignIfTrue<
  373. EntryAlloc,
  374. AllocatorTraits::propagate_on_container_copy_assignment::value>()(
  375. *this, other);
  376. }
  377. _max_load_factor = other._max_load_factor;
  378. static_cast<DetailHasher&>(*this) = other;
  379. static_cast<Equal&>(*this) = other;
  380. rehash_for_other_container(other);
  381. insert(other.begin(), other.end());
  382. return *this;
  383. }
  384. sherwood_v3_table& operator=(sherwood_v3_table&& other) noexcept {
  385. if (this == std::addressof(other))
  386. return *this;
  387. else if (AllocatorTraits::propagate_on_container_move_assignment::value) {
  388. clear();
  389. reset_to_empty_state();
  390. AssignIfTrue<
  391. EntryAlloc,
  392. AllocatorTraits::propagate_on_container_move_assignment::value>()(
  393. *this, std::move(other));
  394. swap_pointers(other);
  395. } else if (
  396. static_cast<EntryAlloc&>(*this) == static_cast<EntryAlloc&>(other)) {
  397. swap_pointers(other);
  398. } else {
  399. clear();
  400. _max_load_factor = other._max_load_factor;
  401. rehash_for_other_container(other);
  402. for (T& elem : other)
  403. emplace(std::move(elem));
  404. other.clear();
  405. }
  406. static_cast<DetailHasher&>(*this) = std::move(other);
  407. static_cast<Equal&>(*this) = std::move(other);
  408. return *this;
  409. }
  410. ~sherwood_v3_table() {
  411. clear();
  412. deallocate_data(entries, num_slots_minus_one, max_lookups);
  413. }
  414. const allocator_type& get_allocator() const {
  415. return static_cast<const allocator_type&>(*this);
  416. }
  417. const ArgumentEqual& key_eq() const {
  418. return static_cast<const ArgumentEqual&>(*this);
  419. }
  420. const ArgumentHash& hash_function() const {
  421. return static_cast<const ArgumentHash&>(*this);
  422. }
  423. template <typename ValueType>
  424. struct templated_iterator {
  425. templated_iterator() = default;
  426. templated_iterator(EntryPointer current) : current(current) {}
  427. EntryPointer current = EntryPointer();
  428. using iterator_category = std::forward_iterator_tag;
  429. using value_type = ValueType;
  430. using difference_type = ptrdiff_t;
  431. using pointer = ValueType*;
  432. using reference = ValueType&;
  433. friend bool operator==(
  434. const templated_iterator& lhs,
  435. const templated_iterator& rhs) {
  436. return lhs.current == rhs.current;
  437. }
  438. friend bool operator!=(
  439. const templated_iterator& lhs,
  440. const templated_iterator& rhs) {
  441. return !(lhs == rhs);
  442. }
  443. templated_iterator& operator++() {
  444. do {
  445. ++current;
  446. } while (current->is_empty());
  447. return *this;
  448. }
  449. templated_iterator operator++(int) {
  450. templated_iterator copy(*this);
  451. ++*this;
  452. return copy;
  453. }
  454. ValueType& operator*() const {
  455. return current->value;
  456. }
  457. ValueType* operator->() const {
  458. return std::addressof(current->value);
  459. }
  460. // the template automatically disables the operator when value_type is
  461. // already const, because that would cause a lot of compiler warnings
  462. // otherwise.
  463. template <
  464. class target_type = const value_type,
  465. class = typename std::enable_if<
  466. std::is_same<target_type, const value_type>::value &&
  467. !std::is_same<target_type, value_type>::value>::type>
  468. operator templated_iterator<target_type>() const {
  469. return {current};
  470. }
  471. };
  472. using iterator = templated_iterator<value_type>;
  473. using const_iterator = templated_iterator<const value_type>;
  474. iterator begin() {
  475. for (EntryPointer it = entries;; ++it) {
  476. if (it->has_value())
  477. return {it};
  478. }
  479. }
  480. const_iterator begin() const {
  481. for (EntryPointer it = entries;; ++it) {
  482. if (it->has_value())
  483. return {it};
  484. }
  485. }
  486. const_iterator cbegin() const {
  487. return begin();
  488. }
  489. iterator end() {
  490. return {
  491. entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups)};
  492. }
  493. const_iterator end() const {
  494. return {
  495. entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups)};
  496. }
  497. const_iterator cend() const {
  498. return end();
  499. }
  500. iterator find(const FindKey& key) {
  501. uint64_t index =
  502. hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
  503. EntryPointer it = entries + ptrdiff_t(index);
  504. for (int8_t distance = 0; it->distance_from_desired >= distance;
  505. ++distance, ++it) {
  506. if (compares_equal(key, it->value))
  507. return {it};
  508. }
  509. return end();
  510. }
  511. const_iterator find(const FindKey& key) const {
  512. return const_cast<sherwood_v3_table*>(this)->find(key);
  513. }
  514. uint64_t count(const FindKey& key) const {
  515. return find(key) == end() ? 0 : 1;
  516. }
  517. std::pair<iterator, iterator> equal_range(const FindKey& key) {
  518. iterator found = find(key);
  519. if (found == end())
  520. return {found, found};
  521. else
  522. return {found, std::next(found)};
  523. }
  524. std::pair<const_iterator, const_iterator> equal_range(
  525. const FindKey& key) const {
  526. const_iterator found = find(key);
  527. if (found == end())
  528. return {found, found};
  529. else
  530. return {found, std::next(found)};
  531. }
  532. template <typename Key, typename... Args>
  533. std::pair<iterator, bool> emplace(Key&& key, Args&&... args) {
  534. uint64_t index =
  535. hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
  536. EntryPointer current_entry = entries + ptrdiff_t(index);
  537. int8_t distance_from_desired = 0;
  538. for (; current_entry->distance_from_desired >= distance_from_desired;
  539. ++current_entry, ++distance_from_desired) {
  540. if (compares_equal(key, current_entry->value))
  541. return {{current_entry}, false};
  542. }
  543. return emplace_new_key(
  544. distance_from_desired,
  545. current_entry,
  546. std::forward<Key>(key),
  547. std::forward<Args>(args)...);
  548. }
  549. std::pair<iterator, bool> insert(const value_type& value) {
  550. return emplace(value);
  551. }
  552. std::pair<iterator, bool> insert(value_type&& value) {
  553. return emplace(std::move(value));
  554. }
  555. template <typename... Args>
  556. iterator emplace_hint(const_iterator, Args&&... args) {
  557. return emplace(std::forward<Args>(args)...).first;
  558. }
  559. iterator insert(const_iterator, const value_type& value) {
  560. return emplace(value).first;
  561. }
  562. iterator insert(const_iterator, value_type&& value) {
  563. return emplace(std::move(value)).first;
  564. }
  565. template <typename It>
  566. void insert(It begin, It end) {
  567. for (; begin != end; ++begin) {
  568. emplace(*begin);
  569. }
  570. }
  571. void insert(std::initializer_list<value_type> il) {
  572. insert(il.begin(), il.end());
  573. }
  574. void rehash(uint64_t num_buckets) {
  575. num_buckets = std::max(
  576. num_buckets,
  577. static_cast<uint64_t>(
  578. std::ceil(num_elements / static_cast<double>(_max_load_factor))));
  579. if (num_buckets == 0) {
  580. reset_to_empty_state();
  581. return;
  582. }
  583. auto new_prime_index = hash_policy.next_size_over(num_buckets);
  584. if (num_buckets == bucket_count())
  585. return;
  586. int8_t new_max_lookups = compute_max_lookups(num_buckets);
  587. EntryPointer new_buckets(
  588. AllocatorTraits::allocate(*this, num_buckets + new_max_lookups));
  589. EntryPointer special_end_item =
  590. new_buckets + static_cast<ptrdiff_t>(num_buckets + new_max_lookups - 1);
  591. for (EntryPointer it = new_buckets; it != special_end_item; ++it)
  592. it->distance_from_desired = -1;
  593. special_end_item->distance_from_desired = Entry::special_end_value;
  594. std::swap(entries, new_buckets);
  595. std::swap(num_slots_minus_one, num_buckets);
  596. --num_slots_minus_one;
  597. hash_policy.commit(new_prime_index);
  598. int8_t old_max_lookups = max_lookups;
  599. max_lookups = new_max_lookups;
  600. num_elements = 0;
  601. for (EntryPointer
  602. it = new_buckets,
  603. end = it + static_cast<ptrdiff_t>(num_buckets + old_max_lookups);
  604. it != end;
  605. ++it) {
  606. if (it->has_value()) {
  607. emplace(std::move(it->value));
  608. it->destroy_value();
  609. }
  610. }
  611. deallocate_data(new_buckets, num_buckets, old_max_lookups);
  612. }
  613. void reserve(uint64_t num_elements_) {
  614. uint64_t required_buckets = num_buckets_for_reserve(num_elements_);
  615. if (required_buckets > bucket_count())
  616. rehash(required_buckets);
  617. }
  618. // the return value is a type that can be converted to an iterator
  619. // the reason for doing this is that it's not free to find the
  620. // iterator pointing at the next element. if you care about the
  621. // next iterator, turn the return value into an iterator
  622. convertible_to_iterator erase(const_iterator to_erase) {
  623. EntryPointer current = to_erase.current;
  624. current->destroy_value();
  625. --num_elements;
  626. for (EntryPointer next = current + ptrdiff_t(1);
  627. !next->is_at_desired_position();
  628. ++current, ++next) {
  629. current->emplace(next->distance_from_desired - 1, std::move(next->value));
  630. next->destroy_value();
  631. }
  632. return {to_erase.current};
  633. }
  634. iterator erase(const_iterator begin_it, const_iterator end_it) {
  635. if (begin_it == end_it)
  636. return {begin_it.current};
  637. for (EntryPointer it = begin_it.current, end = end_it.current; it != end;
  638. ++it) {
  639. if (it->has_value()) {
  640. it->destroy_value();
  641. --num_elements;
  642. }
  643. }
  644. if (end_it == this->end())
  645. return this->end();
  646. ptrdiff_t num_to_move = std::min(
  647. static_cast<ptrdiff_t>(end_it.current->distance_from_desired),
  648. end_it.current - begin_it.current);
  649. EntryPointer to_return = end_it.current - num_to_move;
  650. for (EntryPointer it = end_it.current; !it->is_at_desired_position();) {
  651. EntryPointer target = it - num_to_move;
  652. target->emplace(
  653. it->distance_from_desired - num_to_move, std::move(it->value));
  654. it->destroy_value();
  655. ++it;
  656. num_to_move = std::min(
  657. static_cast<ptrdiff_t>(it->distance_from_desired), num_to_move);
  658. }
  659. return {to_return};
  660. }
  661. uint64_t erase(const FindKey& key) {
  662. auto found = find(key);
  663. if (found == end())
  664. return 0;
  665. else {
  666. erase(found);
  667. return 1;
  668. }
  669. }
  670. void clear() {
  671. for (EntryPointer it = entries,
  672. end = it +
  673. static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups);
  674. it != end;
  675. ++it) {
  676. if (it->has_value())
  677. it->destroy_value();
  678. }
  679. num_elements = 0;
  680. }
  681. void shrink_to_fit() {
  682. rehash_for_other_container(*this);
  683. }
  684. void swap(sherwood_v3_table& other) {
  685. using std::swap;
  686. swap_pointers(other);
  687. swap(static_cast<ArgumentHash&>(*this), static_cast<ArgumentHash&>(other));
  688. swap(
  689. static_cast<ArgumentEqual&>(*this), static_cast<ArgumentEqual&>(other));
  690. if (AllocatorTraits::propagate_on_container_swap::value)
  691. swap(static_cast<EntryAlloc&>(*this), static_cast<EntryAlloc&>(other));
  692. }
  693. uint64_t size() const {
  694. return num_elements;
  695. }
  696. uint64_t max_size() const {
  697. return (AllocatorTraits::max_size(*this)) / sizeof(Entry);
  698. }
  699. uint64_t bucket_count() const {
  700. return num_slots_minus_one ? num_slots_minus_one + 1 : 0;
  701. }
  702. size_type max_bucket_count() const {
  703. return (AllocatorTraits::max_size(*this) - min_lookups) / sizeof(Entry);
  704. }
  705. uint64_t bucket(const FindKey& key) const {
  706. return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
  707. }
  708. float load_factor() const {
  709. uint64_t buckets = bucket_count();
  710. if (buckets)
  711. return static_cast<float>(num_elements) / bucket_count();
  712. else
  713. return 0;
  714. }
  715. void max_load_factor(float value) {
  716. _max_load_factor = value;
  717. }
  718. float max_load_factor() const {
  719. return _max_load_factor;
  720. }
  721. bool empty() const {
  722. return num_elements == 0;
  723. }
  724. private:
  725. EntryPointer entries = empty_default_table();
  726. uint64_t num_slots_minus_one = 0;
  727. typename HashPolicySelector<ArgumentHash>::type hash_policy;
  728. int8_t max_lookups = detailv3::min_lookups - 1;
  729. float _max_load_factor = 0.5f;
  730. uint64_t num_elements = 0;
  731. EntryPointer empty_default_table() {
  732. EntryPointer result =
  733. AllocatorTraits::allocate(*this, detailv3::min_lookups);
  734. EntryPointer special_end_item =
  735. result + static_cast<ptrdiff_t>(detailv3::min_lookups - 1);
  736. for (EntryPointer it = result; it != special_end_item; ++it)
  737. it->distance_from_desired = -1;
  738. special_end_item->distance_from_desired = Entry::special_end_value;
  739. return result;
  740. }
  741. static int8_t compute_max_lookups(uint64_t num_buckets) {
  742. int8_t desired = detailv3::log2(num_buckets);
  743. return std::max(detailv3::min_lookups, desired);
  744. }
  745. uint64_t num_buckets_for_reserve(uint64_t num_elements_) const {
  746. return static_cast<uint64_t>(std::ceil(
  747. num_elements_ / std::min(0.5, static_cast<double>(_max_load_factor))));
  748. }
  749. void rehash_for_other_container(const sherwood_v3_table& other) {
  750. rehash(
  751. std::min(num_buckets_for_reserve(other.size()), other.bucket_count()));
  752. }
  753. void swap_pointers(sherwood_v3_table& other) {
  754. using std::swap;
  755. swap(hash_policy, other.hash_policy);
  756. swap(entries, other.entries);
  757. swap(num_slots_minus_one, other.num_slots_minus_one);
  758. swap(num_elements, other.num_elements);
  759. swap(max_lookups, other.max_lookups);
  760. swap(_max_load_factor, other._max_load_factor);
  761. }
  762. template <typename Key, typename... Args>
  763. SKA_NOINLINE(std::pair<iterator, bool>)
  764. emplace_new_key(
  765. int8_t distance_from_desired,
  766. EntryPointer current_entry,
  767. Key&& key,
  768. Args&&... args) {
  769. using std::swap;
  770. if (num_slots_minus_one == 0 || distance_from_desired == max_lookups ||
  771. num_elements + 1 >
  772. (num_slots_minus_one + 1) * static_cast<double>(_max_load_factor)) {
  773. grow();
  774. return emplace(std::forward<Key>(key), std::forward<Args>(args)...);
  775. } else if (current_entry->is_empty()) {
  776. current_entry->emplace(
  777. distance_from_desired,
  778. std::forward<Key>(key),
  779. std::forward<Args>(args)...);
  780. ++num_elements;
  781. return {{current_entry}, true};
  782. }
  783. value_type to_insert(std::forward<Key>(key), std::forward<Args>(args)...);
  784. swap(distance_from_desired, current_entry->distance_from_desired);
  785. swap(to_insert, current_entry->value);
  786. iterator result = {current_entry};
  787. for (++distance_from_desired, ++current_entry;; ++current_entry) {
  788. if (current_entry->is_empty()) {
  789. current_entry->emplace(distance_from_desired, std::move(to_insert));
  790. ++num_elements;
  791. return {result, true};
  792. } else if (current_entry->distance_from_desired < distance_from_desired) {
  793. swap(distance_from_desired, current_entry->distance_from_desired);
  794. swap(to_insert, current_entry->value);
  795. ++distance_from_desired;
  796. } else {
  797. ++distance_from_desired;
  798. if (distance_from_desired == max_lookups) {
  799. swap(to_insert, result.current->value);
  800. grow();
  801. return emplace(std::move(to_insert));
  802. }
  803. }
  804. }
  805. }
  806. void grow() {
  807. rehash(std::max(uint64_t(4), 2 * bucket_count()));
  808. }
  809. void deallocate_data(
  810. EntryPointer begin,
  811. uint64_t num_slots_minus_one_,
  812. int8_t max_lookups_) {
  813. AllocatorTraits::deallocate(
  814. *this, begin, num_slots_minus_one_ + max_lookups_ + 1);
  815. }
  816. void reset_to_empty_state() {
  817. deallocate_data(entries, num_slots_minus_one, max_lookups);
  818. entries = empty_default_table();
  819. num_slots_minus_one = 0;
  820. hash_policy.reset();
  821. max_lookups = detailv3::min_lookups - 1;
  822. }
  823. template <typename U>
  824. uint64_t hash_object(const U& key) {
  825. return static_cast<DetailHasher&>(*this)(key);
  826. }
  827. template <typename U>
  828. uint64_t hash_object(const U& key) const {
  829. return static_cast<const DetailHasher&>(*this)(key);
  830. }
  831. template <typename L, typename R>
  832. bool compares_equal(const L& lhs, const R& rhs) {
  833. return static_cast<Equal&>(*this)(lhs, rhs);
  834. }
  835. public:
  836. struct convertible_to_iterator {
  837. EntryPointer it;
  838. operator iterator() {
  839. if (it->has_value())
  840. return {it};
  841. else
  842. return ++iterator{it};
  843. }
  844. operator const_iterator() {
  845. if (it->has_value())
  846. return {it};
  847. else
  848. return ++const_iterator{it};
  849. }
  850. };
  851. };
  852. } // namespace detailv3
  853. struct prime_number_hash_policy {
  854. static uint64_t mod0(uint64_t) {
  855. return 0llu;
  856. }
  857. static uint64_t mod2(uint64_t hash) {
  858. return hash % 2llu;
  859. }
  860. static uint64_t mod3(uint64_t hash) {
  861. return hash % 3llu;
  862. }
  863. static uint64_t mod5(uint64_t hash) {
  864. return hash % 5llu;
  865. }
  866. static uint64_t mod7(uint64_t hash) {
  867. return hash % 7llu;
  868. }
  869. static uint64_t mod11(uint64_t hash) {
  870. return hash % 11llu;
  871. }
  872. static uint64_t mod13(uint64_t hash) {
  873. return hash % 13llu;
  874. }
  875. static uint64_t mod17(uint64_t hash) {
  876. return hash % 17llu;
  877. }
  878. static uint64_t mod23(uint64_t hash) {
  879. return hash % 23llu;
  880. }
  881. static uint64_t mod29(uint64_t hash) {
  882. return hash % 29llu;
  883. }
  884. static uint64_t mod37(uint64_t hash) {
  885. return hash % 37llu;
  886. }
  887. static uint64_t mod47(uint64_t hash) {
  888. return hash % 47llu;
  889. }
  890. static uint64_t mod59(uint64_t hash) {
  891. return hash % 59llu;
  892. }
  893. static uint64_t mod73(uint64_t hash) {
  894. return hash % 73llu;
  895. }
  896. static uint64_t mod97(uint64_t hash) {
  897. return hash % 97llu;
  898. }
  899. static uint64_t mod127(uint64_t hash) {
  900. return hash % 127llu;
  901. }
  902. static uint64_t mod151(uint64_t hash) {
  903. return hash % 151llu;
  904. }
  905. static uint64_t mod197(uint64_t hash) {
  906. return hash % 197llu;
  907. }
  908. static uint64_t mod251(uint64_t hash) {
  909. return hash % 251llu;
  910. }
  911. static uint64_t mod313(uint64_t hash) {
  912. return hash % 313llu;
  913. }
  914. static uint64_t mod397(uint64_t hash) {
  915. return hash % 397llu;
  916. }
  917. static uint64_t mod499(uint64_t hash) {
  918. return hash % 499llu;
  919. }
  920. static uint64_t mod631(uint64_t hash) {
  921. return hash % 631llu;
  922. }
  923. static uint64_t mod797(uint64_t hash) {
  924. return hash % 797llu;
  925. }
  926. static uint64_t mod1009(uint64_t hash) {
  927. return hash % 1009llu;
  928. }
  929. static uint64_t mod1259(uint64_t hash) {
  930. return hash % 1259llu;
  931. }
  932. static uint64_t mod1597(uint64_t hash) {
  933. return hash % 1597llu;
  934. }
  935. static uint64_t mod2011(uint64_t hash) {
  936. return hash % 2011llu;
  937. }
  938. static uint64_t mod2539(uint64_t hash) {
  939. return hash % 2539llu;
  940. }
  941. static uint64_t mod3203(uint64_t hash) {
  942. return hash % 3203llu;
  943. }
  944. static uint64_t mod4027(uint64_t hash) {
  945. return hash % 4027llu;
  946. }
  947. static uint64_t mod5087(uint64_t hash) {
  948. return hash % 5087llu;
  949. }
  950. static uint64_t mod6421(uint64_t hash) {
  951. return hash % 6421llu;
  952. }
  953. static uint64_t mod8089(uint64_t hash) {
  954. return hash % 8089llu;
  955. }
  956. static uint64_t mod10193(uint64_t hash) {
  957. return hash % 10193llu;
  958. }
  959. static uint64_t mod12853(uint64_t hash) {
  960. return hash % 12853llu;
  961. }
  962. static uint64_t mod16193(uint64_t hash) {
  963. return hash % 16193llu;
  964. }
  965. static uint64_t mod20399(uint64_t hash) {
  966. return hash % 20399llu;
  967. }
  968. static uint64_t mod25717(uint64_t hash) {
  969. return hash % 25717llu;
  970. }
  971. static uint64_t mod32401(uint64_t hash) {
  972. return hash % 32401llu;
  973. }
  974. static uint64_t mod40823(uint64_t hash) {
  975. return hash % 40823llu;
  976. }
  977. static uint64_t mod51437(uint64_t hash) {
  978. return hash % 51437llu;
  979. }
  980. static uint64_t mod64811(uint64_t hash) {
  981. return hash % 64811llu;
  982. }
  983. static uint64_t mod81649(uint64_t hash) {
  984. return hash % 81649llu;
  985. }
  986. static uint64_t mod102877(uint64_t hash) {
  987. return hash % 102877llu;
  988. }
  989. static uint64_t mod129607(uint64_t hash) {
  990. return hash % 129607llu;
  991. }
  992. static uint64_t mod163307(uint64_t hash) {
  993. return hash % 163307llu;
  994. }
  995. static uint64_t mod205759(uint64_t hash) {
  996. return hash % 205759llu;
  997. }
  998. static uint64_t mod259229(uint64_t hash) {
  999. return hash % 259229llu;
  1000. }
  1001. static uint64_t mod326617(uint64_t hash) {
  1002. return hash % 326617llu;
  1003. }
  1004. static uint64_t mod411527(uint64_t hash) {
  1005. return hash % 411527llu;
  1006. }
  1007. static uint64_t mod518509(uint64_t hash) {
  1008. return hash % 518509llu;
  1009. }
  1010. static uint64_t mod653267(uint64_t hash) {
  1011. return hash % 653267llu;
  1012. }
  1013. static uint64_t mod823117(uint64_t hash) {
  1014. return hash % 823117llu;
  1015. }
  1016. static uint64_t mod1037059(uint64_t hash) {
  1017. return hash % 1037059llu;
  1018. }
  1019. static uint64_t mod1306601(uint64_t hash) {
  1020. return hash % 1306601llu;
  1021. }
  1022. static uint64_t mod1646237(uint64_t hash) {
  1023. return hash % 1646237llu;
  1024. }
  1025. static uint64_t mod2074129(uint64_t hash) {
  1026. return hash % 2074129llu;
  1027. }
  1028. static uint64_t mod2613229(uint64_t hash) {
  1029. return hash % 2613229llu;
  1030. }
  1031. static uint64_t mod3292489(uint64_t hash) {
  1032. return hash % 3292489llu;
  1033. }
  1034. static uint64_t mod4148279(uint64_t hash) {
  1035. return hash % 4148279llu;
  1036. }
  1037. static uint64_t mod5226491(uint64_t hash) {
  1038. return hash % 5226491llu;
  1039. }
  1040. static uint64_t mod6584983(uint64_t hash) {
  1041. return hash % 6584983llu;
  1042. }
  1043. static uint64_t mod8296553(uint64_t hash) {
  1044. return hash % 8296553llu;
  1045. }
  1046. static uint64_t mod10453007(uint64_t hash) {
  1047. return hash % 10453007llu;
  1048. }
  1049. static uint64_t mod13169977(uint64_t hash) {
  1050. return hash % 13169977llu;
  1051. }
  1052. static uint64_t mod16593127(uint64_t hash) {
  1053. return hash % 16593127llu;
  1054. }
  1055. static uint64_t mod20906033(uint64_t hash) {
  1056. return hash % 20906033llu;
  1057. }
  1058. static uint64_t mod26339969(uint64_t hash) {
  1059. return hash % 26339969llu;
  1060. }
  1061. static uint64_t mod33186281(uint64_t hash) {
  1062. return hash % 33186281llu;
  1063. }
  1064. static uint64_t mod41812097(uint64_t hash) {
  1065. return hash % 41812097llu;
  1066. }
  1067. static uint64_t mod52679969(uint64_t hash) {
  1068. return hash % 52679969llu;
  1069. }
  1070. static uint64_t mod66372617(uint64_t hash) {
  1071. return hash % 66372617llu;
  1072. }
  1073. static uint64_t mod83624237(uint64_t hash) {
  1074. return hash % 83624237llu;
  1075. }
  1076. static uint64_t mod105359939(uint64_t hash) {
  1077. return hash % 105359939llu;
  1078. }
  1079. static uint64_t mod132745199(uint64_t hash) {
  1080. return hash % 132745199llu;
  1081. }
  1082. static uint64_t mod167248483(uint64_t hash) {
  1083. return hash % 167248483llu;
  1084. }
  1085. static uint64_t mod210719881(uint64_t hash) {
  1086. return hash % 210719881llu;
  1087. }
  1088. static uint64_t mod265490441(uint64_t hash) {
  1089. return hash % 265490441llu;
  1090. }
  1091. static uint64_t mod334496971(uint64_t hash) {
  1092. return hash % 334496971llu;
  1093. }
  1094. static uint64_t mod421439783(uint64_t hash) {
  1095. return hash % 421439783llu;
  1096. }
  1097. static uint64_t mod530980861(uint64_t hash) {
  1098. return hash % 530980861llu;
  1099. }
  1100. static uint64_t mod668993977(uint64_t hash) {
  1101. return hash % 668993977llu;
  1102. }
  1103. static uint64_t mod842879579(uint64_t hash) {
  1104. return hash % 842879579llu;
  1105. }
  1106. static uint64_t mod1061961721(uint64_t hash) {
  1107. return hash % 1061961721llu;
  1108. }
  1109. static uint64_t mod1337987929(uint64_t hash) {
  1110. return hash % 1337987929llu;
  1111. }
  1112. static uint64_t mod1685759167(uint64_t hash) {
  1113. return hash % 1685759167llu;
  1114. }
  1115. static uint64_t mod2123923447(uint64_t hash) {
  1116. return hash % 2123923447llu;
  1117. }
  1118. static uint64_t mod2675975881(uint64_t hash) {
  1119. return hash % 2675975881llu;
  1120. }
  1121. static uint64_t mod3371518343(uint64_t hash) {
  1122. return hash % 3371518343llu;
  1123. }
  1124. static uint64_t mod4247846927(uint64_t hash) {
  1125. return hash % 4247846927llu;
  1126. }
  1127. static uint64_t mod5351951779(uint64_t hash) {
  1128. return hash % 5351951779llu;
  1129. }
  1130. static uint64_t mod6743036717(uint64_t hash) {
  1131. return hash % 6743036717llu;
  1132. }
  1133. static uint64_t mod8495693897(uint64_t hash) {
  1134. return hash % 8495693897llu;
  1135. }
  1136. static uint64_t mod10703903591(uint64_t hash) {
  1137. return hash % 10703903591llu;
  1138. }
  1139. static uint64_t mod13486073473(uint64_t hash) {
  1140. return hash % 13486073473llu;
  1141. }
  1142. static uint64_t mod16991387857(uint64_t hash) {
  1143. return hash % 16991387857llu;
  1144. }
  1145. static uint64_t mod21407807219(uint64_t hash) {
  1146. return hash % 21407807219llu;
  1147. }
  1148. static uint64_t mod26972146961(uint64_t hash) {
  1149. return hash % 26972146961llu;
  1150. }
  1151. static uint64_t mod33982775741(uint64_t hash) {
  1152. return hash % 33982775741llu;
  1153. }
  1154. static uint64_t mod42815614441(uint64_t hash) {
  1155. return hash % 42815614441llu;
  1156. }
  1157. static uint64_t mod53944293929(uint64_t hash) {
  1158. return hash % 53944293929llu;
  1159. }
  1160. static uint64_t mod67965551447(uint64_t hash) {
  1161. return hash % 67965551447llu;
  1162. }
  1163. static uint64_t mod85631228929(uint64_t hash) {
  1164. return hash % 85631228929llu;
  1165. }
  1166. static uint64_t mod107888587883(uint64_t hash) {
  1167. return hash % 107888587883llu;
  1168. }
  1169. static uint64_t mod135931102921(uint64_t hash) {
  1170. return hash % 135931102921llu;
  1171. }
  1172. static uint64_t mod171262457903(uint64_t hash) {
  1173. return hash % 171262457903llu;
  1174. }
  1175. static uint64_t mod215777175787(uint64_t hash) {
  1176. return hash % 215777175787llu;
  1177. }
  1178. static uint64_t mod271862205833(uint64_t hash) {
  1179. return hash % 271862205833llu;
  1180. }
  1181. static uint64_t mod342524915839(uint64_t hash) {
  1182. return hash % 342524915839llu;
  1183. }
  1184. static uint64_t mod431554351609(uint64_t hash) {
  1185. return hash % 431554351609llu;
  1186. }
  1187. static uint64_t mod543724411781(uint64_t hash) {
  1188. return hash % 543724411781llu;
  1189. }
  1190. static uint64_t mod685049831731(uint64_t hash) {
  1191. return hash % 685049831731llu;
  1192. }
  1193. static uint64_t mod863108703229(uint64_t hash) {
  1194. return hash % 863108703229llu;
  1195. }
  1196. static uint64_t mod1087448823553(uint64_t hash) {
  1197. return hash % 1087448823553llu;
  1198. }
  1199. static uint64_t mod1370099663459(uint64_t hash) {
  1200. return hash % 1370099663459llu;
  1201. }
  1202. static uint64_t mod1726217406467(uint64_t hash) {
  1203. return hash % 1726217406467llu;
  1204. }
  1205. static uint64_t mod2174897647073(uint64_t hash) {
  1206. return hash % 2174897647073llu;
  1207. }
  1208. static uint64_t mod2740199326961(uint64_t hash) {
  1209. return hash % 2740199326961llu;
  1210. }
  1211. static uint64_t mod3452434812973(uint64_t hash) {
  1212. return hash % 3452434812973llu;
  1213. }
  1214. static uint64_t mod4349795294267(uint64_t hash) {
  1215. return hash % 4349795294267llu;
  1216. }
  1217. static uint64_t mod5480398654009(uint64_t hash) {
  1218. return hash % 5480398654009llu;
  1219. }
  1220. static uint64_t mod6904869625999(uint64_t hash) {
  1221. return hash % 6904869625999llu;
  1222. }
  1223. static uint64_t mod8699590588571(uint64_t hash) {
  1224. return hash % 8699590588571llu;
  1225. }
  1226. static uint64_t mod10960797308051(uint64_t hash) {
  1227. return hash % 10960797308051llu;
  1228. }
  1229. static uint64_t mod13809739252051(uint64_t hash) {
  1230. return hash % 13809739252051llu;
  1231. }
  1232. static uint64_t mod17399181177241(uint64_t hash) {
  1233. return hash % 17399181177241llu;
  1234. }
  1235. static uint64_t mod21921594616111(uint64_t hash) {
  1236. return hash % 21921594616111llu;
  1237. }
  1238. static uint64_t mod27619478504183(uint64_t hash) {
  1239. return hash % 27619478504183llu;
  1240. }
  1241. static uint64_t mod34798362354533(uint64_t hash) {
  1242. return hash % 34798362354533llu;
  1243. }
  1244. static uint64_t mod43843189232363(uint64_t hash) {
  1245. return hash % 43843189232363llu;
  1246. }
  1247. static uint64_t mod55238957008387(uint64_t hash) {
  1248. return hash % 55238957008387llu;
  1249. }
  1250. static uint64_t mod69596724709081(uint64_t hash) {
  1251. return hash % 69596724709081llu;
  1252. }
  1253. static uint64_t mod87686378464759(uint64_t hash) {
  1254. return hash % 87686378464759llu;
  1255. }
  1256. static uint64_t mod110477914016779(uint64_t hash) {
  1257. return hash % 110477914016779llu;
  1258. }
  1259. static uint64_t mod139193449418173(uint64_t hash) {
  1260. return hash % 139193449418173llu;
  1261. }
  1262. static uint64_t mod175372756929481(uint64_t hash) {
  1263. return hash % 175372756929481llu;
  1264. }
  1265. static uint64_t mod220955828033581(uint64_t hash) {
  1266. return hash % 220955828033581llu;
  1267. }
  1268. static uint64_t mod278386898836457(uint64_t hash) {
  1269. return hash % 278386898836457llu;
  1270. }
  1271. static uint64_t mod350745513859007(uint64_t hash) {
  1272. return hash % 350745513859007llu;
  1273. }
  1274. static uint64_t mod441911656067171(uint64_t hash) {
  1275. return hash % 441911656067171llu;
  1276. }
  1277. static uint64_t mod556773797672909(uint64_t hash) {
  1278. return hash % 556773797672909llu;
  1279. }
  1280. static uint64_t mod701491027718027(uint64_t hash) {
  1281. return hash % 701491027718027llu;
  1282. }
  1283. static uint64_t mod883823312134381(uint64_t hash) {
  1284. return hash % 883823312134381llu;
  1285. }
  1286. static uint64_t mod1113547595345903(uint64_t hash) {
  1287. return hash % 1113547595345903llu;
  1288. }
  1289. static uint64_t mod1402982055436147(uint64_t hash) {
  1290. return hash % 1402982055436147llu;
  1291. }
  1292. static uint64_t mod1767646624268779(uint64_t hash) {
  1293. return hash % 1767646624268779llu;
  1294. }
  1295. static uint64_t mod2227095190691797(uint64_t hash) {
  1296. return hash % 2227095190691797llu;
  1297. }
  1298. static uint64_t mod2805964110872297(uint64_t hash) {
  1299. return hash % 2805964110872297llu;
  1300. }
  1301. static uint64_t mod3535293248537579(uint64_t hash) {
  1302. return hash % 3535293248537579llu;
  1303. }
  1304. static uint64_t mod4454190381383713(uint64_t hash) {
  1305. return hash % 4454190381383713llu;
  1306. }
  1307. static uint64_t mod5611928221744609(uint64_t hash) {
  1308. return hash % 5611928221744609llu;
  1309. }
  1310. static uint64_t mod7070586497075177(uint64_t hash) {
  1311. return hash % 7070586497075177llu;
  1312. }
  1313. static uint64_t mod8908380762767489(uint64_t hash) {
  1314. return hash % 8908380762767489llu;
  1315. }
  1316. static uint64_t mod11223856443489329(uint64_t hash) {
  1317. return hash % 11223856443489329llu;
  1318. }
  1319. static uint64_t mod14141172994150357(uint64_t hash) {
  1320. return hash % 14141172994150357llu;
  1321. }
  1322. static uint64_t mod17816761525534927(uint64_t hash) {
  1323. return hash % 17816761525534927llu;
  1324. }
  1325. static uint64_t mod22447712886978529(uint64_t hash) {
  1326. return hash % 22447712886978529llu;
  1327. }
  1328. static uint64_t mod28282345988300791(uint64_t hash) {
  1329. return hash % 28282345988300791llu;
  1330. }
  1331. static uint64_t mod35633523051069991(uint64_t hash) {
  1332. return hash % 35633523051069991llu;
  1333. }
  1334. static uint64_t mod44895425773957261(uint64_t hash) {
  1335. return hash % 44895425773957261llu;
  1336. }
  1337. static uint64_t mod56564691976601587(uint64_t hash) {
  1338. return hash % 56564691976601587llu;
  1339. }
  1340. static uint64_t mod71267046102139967(uint64_t hash) {
  1341. return hash % 71267046102139967llu;
  1342. }
  1343. static uint64_t mod89790851547914507(uint64_t hash) {
  1344. return hash % 89790851547914507llu;
  1345. }
  1346. static uint64_t mod113129383953203213(uint64_t hash) {
  1347. return hash % 113129383953203213llu;
  1348. }
  1349. static uint64_t mod142534092204280003(uint64_t hash) {
  1350. return hash % 142534092204280003llu;
  1351. }
  1352. static uint64_t mod179581703095829107(uint64_t hash) {
  1353. return hash % 179581703095829107llu;
  1354. }
  1355. static uint64_t mod226258767906406483(uint64_t hash) {
  1356. return hash % 226258767906406483llu;
  1357. }
  1358. static uint64_t mod285068184408560057(uint64_t hash) {
  1359. return hash % 285068184408560057llu;
  1360. }
  1361. static uint64_t mod359163406191658253(uint64_t hash) {
  1362. return hash % 359163406191658253llu;
  1363. }
  1364. static uint64_t mod452517535812813007(uint64_t hash) {
  1365. return hash % 452517535812813007llu;
  1366. }
  1367. static uint64_t mod570136368817120201(uint64_t hash) {
  1368. return hash % 570136368817120201llu;
  1369. }
  1370. static uint64_t mod718326812383316683(uint64_t hash) {
  1371. return hash % 718326812383316683llu;
  1372. }
  1373. static uint64_t mod905035071625626043(uint64_t hash) {
  1374. return hash % 905035071625626043llu;
  1375. }
  1376. static uint64_t mod1140272737634240411(uint64_t hash) {
  1377. return hash % 1140272737634240411llu;
  1378. }
  1379. static uint64_t mod1436653624766633509(uint64_t hash) {
  1380. return hash % 1436653624766633509llu;
  1381. }
  1382. static uint64_t mod1810070143251252131(uint64_t hash) {
  1383. return hash % 1810070143251252131llu;
  1384. }
  1385. static uint64_t mod2280545475268481167(uint64_t hash) {
  1386. return hash % 2280545475268481167llu;
  1387. }
  1388. static uint64_t mod2873307249533267101(uint64_t hash) {
  1389. return hash % 2873307249533267101llu;
  1390. }
  1391. static uint64_t mod3620140286502504283(uint64_t hash) {
  1392. return hash % 3620140286502504283llu;
  1393. }
  1394. static uint64_t mod4561090950536962147(uint64_t hash) {
  1395. return hash % 4561090950536962147llu;
  1396. }
  1397. static uint64_t mod5746614499066534157(uint64_t hash) {
  1398. return hash % 5746614499066534157llu;
  1399. }
  1400. static uint64_t mod7240280573005008577(uint64_t hash) {
  1401. return hash % 7240280573005008577llu;
  1402. }
  1403. static uint64_t mod9122181901073924329(uint64_t hash) {
  1404. return hash % 9122181901073924329llu;
  1405. }
  1406. static uint64_t mod11493228998133068689(uint64_t hash) {
  1407. return hash % 11493228998133068689llu;
  1408. }
  1409. static uint64_t mod14480561146010017169(uint64_t hash) {
  1410. return hash % 14480561146010017169llu;
  1411. }
  1412. static uint64_t mod18446744073709551557(uint64_t hash) {
  1413. return hash % 18446744073709551557llu;
  1414. }
  1415. using mod_function = uint64_t (*)(uint64_t);
  1416. mod_function next_size_over(uint64_t& size) const {
  1417. // prime numbers generated by the following method:
  1418. // 1. start with a prime p = 2
  1419. // 2. go to wolfram alpha and get p = NextPrime(2 * p)
  1420. // 3. repeat 2. until you overflow 64 bits
  1421. // you now have large gaps which you would hit if somebody called reserve()
  1422. // with an unlucky number.
  1423. // 4. to fill the gaps for every prime p go to wolfram alpha and get
  1424. // ClosestPrime(p * 2^(1/3)) and ClosestPrime(p * 2^(2/3)) and put those in
  1425. // the gaps
  1426. // 5. get PrevPrime(2^64) and put it at the end
  1427. static constexpr const uint64_t prime_list[] = {
  1428. 2llu,
  1429. 3llu,
  1430. 5llu,
  1431. 7llu,
  1432. 11llu,
  1433. 13llu,
  1434. 17llu,
  1435. 23llu,
  1436. 29llu,
  1437. 37llu,
  1438. 47llu,
  1439. 59llu,
  1440. 73llu,
  1441. 97llu,
  1442. 127llu,
  1443. 151llu,
  1444. 197llu,
  1445. 251llu,
  1446. 313llu,
  1447. 397llu,
  1448. 499llu,
  1449. 631llu,
  1450. 797llu,
  1451. 1009llu,
  1452. 1259llu,
  1453. 1597llu,
  1454. 2011llu,
  1455. 2539llu,
  1456. 3203llu,
  1457. 4027llu,
  1458. 5087llu,
  1459. 6421llu,
  1460. 8089llu,
  1461. 10193llu,
  1462. 12853llu,
  1463. 16193llu,
  1464. 20399llu,
  1465. 25717llu,
  1466. 32401llu,
  1467. 40823llu,
  1468. 51437llu,
  1469. 64811llu,
  1470. 81649llu,
  1471. 102877llu,
  1472. 129607llu,
  1473. 163307llu,
  1474. 205759llu,
  1475. 259229llu,
  1476. 326617llu,
  1477. 411527llu,
  1478. 518509llu,
  1479. 653267llu,
  1480. 823117llu,
  1481. 1037059llu,
  1482. 1306601llu,
  1483. 1646237llu,
  1484. 2074129llu,
  1485. 2613229llu,
  1486. 3292489llu,
  1487. 4148279llu,
  1488. 5226491llu,
  1489. 6584983llu,
  1490. 8296553llu,
  1491. 10453007llu,
  1492. 13169977llu,
  1493. 16593127llu,
  1494. 20906033llu,
  1495. 26339969llu,
  1496. 33186281llu,
  1497. 41812097llu,
  1498. 52679969llu,
  1499. 66372617llu,
  1500. 83624237llu,
  1501. 105359939llu,
  1502. 132745199llu,
  1503. 167248483llu,
  1504. 210719881llu,
  1505. 265490441llu,
  1506. 334496971llu,
  1507. 421439783llu,
  1508. 530980861llu,
  1509. 668993977llu,
  1510. 842879579llu,
  1511. 1061961721llu,
  1512. 1337987929llu,
  1513. 1685759167llu,
  1514. 2123923447llu,
  1515. 2675975881llu,
  1516. 3371518343llu,
  1517. 4247846927llu,
  1518. 5351951779llu,
  1519. 6743036717llu,
  1520. 8495693897llu,
  1521. 10703903591llu,
  1522. 13486073473llu,
  1523. 16991387857llu,
  1524. 21407807219llu,
  1525. 26972146961llu,
  1526. 33982775741llu,
  1527. 42815614441llu,
  1528. 53944293929llu,
  1529. 67965551447llu,
  1530. 85631228929llu,
  1531. 107888587883llu,
  1532. 135931102921llu,
  1533. 171262457903llu,
  1534. 215777175787llu,
  1535. 271862205833llu,
  1536. 342524915839llu,
  1537. 431554351609llu,
  1538. 543724411781llu,
  1539. 685049831731llu,
  1540. 863108703229llu,
  1541. 1087448823553llu,
  1542. 1370099663459llu,
  1543. 1726217406467llu,
  1544. 2174897647073llu,
  1545. 2740199326961llu,
  1546. 3452434812973llu,
  1547. 4349795294267llu,
  1548. 5480398654009llu,
  1549. 6904869625999llu,
  1550. 8699590588571llu,
  1551. 10960797308051llu,
  1552. 13809739252051llu,
  1553. 17399181177241llu,
  1554. 21921594616111llu,
  1555. 27619478504183llu,
  1556. 34798362354533llu,
  1557. 43843189232363llu,
  1558. 55238957008387llu,
  1559. 69596724709081llu,
  1560. 87686378464759llu,
  1561. 110477914016779llu,
  1562. 139193449418173llu,
  1563. 175372756929481llu,
  1564. 220955828033581llu,
  1565. 278386898836457llu,
  1566. 350745513859007llu,
  1567. 441911656067171llu,
  1568. 556773797672909llu,
  1569. 701491027718027llu,
  1570. 883823312134381llu,
  1571. 1113547595345903llu,
  1572. 1402982055436147llu,
  1573. 1767646624268779llu,
  1574. 2227095190691797llu,
  1575. 2805964110872297llu,
  1576. 3535293248537579llu,
  1577. 4454190381383713llu,
  1578. 5611928221744609llu,
  1579. 7070586497075177llu,
  1580. 8908380762767489llu,
  1581. 11223856443489329llu,
  1582. 14141172994150357llu,
  1583. 17816761525534927llu,
  1584. 22447712886978529llu,
  1585. 28282345988300791llu,
  1586. 35633523051069991llu,
  1587. 44895425773957261llu,
  1588. 56564691976601587llu,
  1589. 71267046102139967llu,
  1590. 89790851547914507llu,
  1591. 113129383953203213llu,
  1592. 142534092204280003llu,
  1593. 179581703095829107llu,
  1594. 226258767906406483llu,
  1595. 285068184408560057llu,
  1596. 359163406191658253llu,
  1597. 452517535812813007llu,
  1598. 570136368817120201llu,
  1599. 718326812383316683llu,
  1600. 905035071625626043llu,
  1601. 1140272737634240411llu,
  1602. 1436653624766633509llu,
  1603. 1810070143251252131llu,
  1604. 2280545475268481167llu,
  1605. 2873307249533267101llu,
  1606. 3620140286502504283llu,
  1607. 4561090950536962147llu,
  1608. 5746614499066534157llu,
  1609. 7240280573005008577llu,
  1610. 9122181901073924329llu,
  1611. 11493228998133068689llu,
  1612. 14480561146010017169llu,
  1613. 18446744073709551557llu};
  1614. static constexpr uint64_t (*const mod_functions[])(uint64_t) = {
  1615. &mod0,
  1616. &mod2,
  1617. &mod3,
  1618. &mod5,
  1619. &mod7,
  1620. &mod11,
  1621. &mod13,
  1622. &mod17,
  1623. &mod23,
  1624. &mod29,
  1625. &mod37,
  1626. &mod47,
  1627. &mod59,
  1628. &mod73,
  1629. &mod97,
  1630. &mod127,
  1631. &mod151,
  1632. &mod197,
  1633. &mod251,
  1634. &mod313,
  1635. &mod397,
  1636. &mod499,
  1637. &mod631,
  1638. &mod797,
  1639. &mod1009,
  1640. &mod1259,
  1641. &mod1597,
  1642. &mod2011,
  1643. &mod2539,
  1644. &mod3203,
  1645. &mod4027,
  1646. &mod5087,
  1647. &mod6421,
  1648. &mod8089,
  1649. &mod10193,
  1650. &mod12853,
  1651. &mod16193,
  1652. &mod20399,
  1653. &mod25717,
  1654. &mod32401,
  1655. &mod40823,
  1656. &mod51437,
  1657. &mod64811,
  1658. &mod81649,
  1659. &mod102877,
  1660. &mod129607,
  1661. &mod163307,
  1662. &mod205759,
  1663. &mod259229,
  1664. &mod326617,
  1665. &mod411527,
  1666. &mod518509,
  1667. &mod653267,
  1668. &mod823117,
  1669. &mod1037059,
  1670. &mod1306601,
  1671. &mod1646237,
  1672. &mod2074129,
  1673. &mod2613229,
  1674. &mod3292489,
  1675. &mod4148279,
  1676. &mod5226491,
  1677. &mod6584983,
  1678. &mod8296553,
  1679. &mod10453007,
  1680. &mod13169977,
  1681. &mod16593127,
  1682. &mod20906033,
  1683. &mod26339969,
  1684. &mod33186281,
  1685. &mod41812097,
  1686. &mod52679969,
  1687. &mod66372617,
  1688. &mod83624237,
  1689. &mod105359939,
  1690. &mod132745199,
  1691. &mod167248483,
  1692. &mod210719881,
  1693. &mod265490441,
  1694. &mod334496971,
  1695. &mod421439783,
  1696. &mod530980861,
  1697. &mod668993977,
  1698. &mod842879579,
  1699. &mod1061961721,
  1700. &mod1337987929,
  1701. &mod1685759167,
  1702. &mod2123923447,
  1703. &mod2675975881,
  1704. &mod3371518343,
  1705. &mod4247846927,
  1706. &mod5351951779,
  1707. &mod6743036717,
  1708. &mod8495693897,
  1709. &mod10703903591,
  1710. &mod13486073473,
  1711. &mod16991387857,
  1712. &mod21407807219,
  1713. &mod26972146961,
  1714. &mod33982775741,
  1715. &mod42815614441,
  1716. &mod53944293929,
  1717. &mod67965551447,
  1718. &mod85631228929,
  1719. &mod107888587883,
  1720. &mod135931102921,
  1721. &mod171262457903,
  1722. &mod215777175787,
  1723. &mod271862205833,
  1724. &mod342524915839,
  1725. &mod431554351609,
  1726. &mod543724411781,
  1727. &mod685049831731,
  1728. &mod863108703229,
  1729. &mod1087448823553,
  1730. &mod1370099663459,
  1731. &mod1726217406467,
  1732. &mod2174897647073,
  1733. &mod2740199326961,
  1734. &mod3452434812973,
  1735. &mod4349795294267,
  1736. &mod5480398654009,
  1737. &mod6904869625999,
  1738. &mod8699590588571,
  1739. &mod10960797308051,
  1740. &mod13809739252051,
  1741. &mod17399181177241,
  1742. &mod21921594616111,
  1743. &mod27619478504183,
  1744. &mod34798362354533,
  1745. &mod43843189232363,
  1746. &mod55238957008387,
  1747. &mod69596724709081,
  1748. &mod87686378464759,
  1749. &mod110477914016779,
  1750. &mod139193449418173,
  1751. &mod175372756929481,
  1752. &mod220955828033581,
  1753. &mod278386898836457,
  1754. &mod350745513859007,
  1755. &mod441911656067171,
  1756. &mod556773797672909,
  1757. &mod701491027718027,
  1758. &mod883823312134381,
  1759. &mod1113547595345903,
  1760. &mod1402982055436147,
  1761. &mod1767646624268779,
  1762. &mod2227095190691797,
  1763. &mod2805964110872297,
  1764. &mod3535293248537579,
  1765. &mod4454190381383713,
  1766. &mod5611928221744609,
  1767. &mod7070586497075177,
  1768. &mod8908380762767489,
  1769. &mod11223856443489329,
  1770. &mod14141172994150357,
  1771. &mod17816761525534927,
  1772. &mod22447712886978529,
  1773. &mod28282345988300791,
  1774. &mod35633523051069991,
  1775. &mod44895425773957261,
  1776. &mod56564691976601587,
  1777. &mod71267046102139967,
  1778. &mod89790851547914507,
  1779. &mod113129383953203213,
  1780. &mod142534092204280003,
  1781. &mod179581703095829107,
  1782. &mod226258767906406483,
  1783. &mod285068184408560057,
  1784. &mod359163406191658253,
  1785. &mod452517535812813007,
  1786. &mod570136368817120201,
  1787. &mod718326812383316683,
  1788. &mod905035071625626043,
  1789. &mod1140272737634240411,
  1790. &mod1436653624766633509,
  1791. &mod1810070143251252131,
  1792. &mod2280545475268481167,
  1793. &mod2873307249533267101,
  1794. &mod3620140286502504283,
  1795. &mod4561090950536962147,
  1796. &mod5746614499066534157,
  1797. &mod7240280573005008577,
  1798. &mod9122181901073924329,
  1799. &mod11493228998133068689,
  1800. &mod14480561146010017169,
  1801. &mod18446744073709551557};
  1802. const uint64_t* found = std::lower_bound(
  1803. std::begin(prime_list), std::end(prime_list) - 1, size);
  1804. size = *found;
  1805. return mod_functions[1 + found - prime_list];
  1806. }
  1807. void commit(mod_function new_mod_function) {
  1808. current_mod_function = new_mod_function;
  1809. }
  1810. void reset() {
  1811. current_mod_function = &mod0;
  1812. }
  1813. uint64_t index_for_hash(uint64_t hash, uint64_t /*num_slots_minus_one*/)
  1814. const {
  1815. return current_mod_function(hash);
  1816. }
  1817. uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
  1818. return index > num_slots_minus_one ? current_mod_function(index) : index;
  1819. }
  1820. private:
  1821. mod_function current_mod_function = &mod0;
  1822. };
  1823. struct power_of_two_hash_policy {
  1824. uint64_t index_for_hash(uint64_t hash, uint64_t num_slots_minus_one) const {
  1825. return hash & num_slots_minus_one;
  1826. }
  1827. uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
  1828. return index_for_hash(index, num_slots_minus_one);
  1829. }
  1830. int8_t next_size_over(uint64_t& size) const {
  1831. size = detailv3::next_power_of_two(size);
  1832. return 0;
  1833. }
  1834. void commit(int8_t) {}
  1835. void reset() {}
  1836. };
  1837. struct fibonacci_hash_policy {
  1838. uint64_t index_for_hash(uint64_t hash, uint64_t /*num_slots_minus_one*/)
  1839. const {
  1840. return (11400714819323198485ull * hash) >> shift;
  1841. }
  1842. uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
  1843. return index & num_slots_minus_one;
  1844. }
  1845. int8_t next_size_over(uint64_t& size) const {
  1846. size = std::max(uint64_t(2), detailv3::next_power_of_two(size));
  1847. return 64 - detailv3::log2(size);
  1848. }
  1849. void commit(int8_t shift_) {
  1850. shift = shift_;
  1851. }
  1852. void reset() {
  1853. shift = 63;
  1854. }
  1855. private:
  1856. int8_t shift = 63;
  1857. };
  1858. template <
  1859. typename K,
  1860. typename V,
  1861. typename H = std::hash<K>,
  1862. typename E = std::equal_to<K>,
  1863. typename A = std::allocator<std::pair<K, V>>>
  1864. class flat_hash_map
  1865. : public detailv3::sherwood_v3_table<
  1866. std::pair<K, V>,
  1867. K,
  1868. H,
  1869. detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
  1870. E,
  1871. detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
  1872. A,
  1873. typename std::allocator_traits<A>::template rebind_alloc<
  1874. detailv3::sherwood_v3_entry<std::pair<K, V>>>> {
  1875. using Table = detailv3::sherwood_v3_table<
  1876. std::pair<K, V>,
  1877. K,
  1878. H,
  1879. detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
  1880. E,
  1881. detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
  1882. A,
  1883. typename std::allocator_traits<A>::template rebind_alloc<
  1884. detailv3::sherwood_v3_entry<std::pair<K, V>>>>;
  1885. public:
  1886. using key_type = K;
  1887. using mapped_type = V;
  1888. using Table::Table;
  1889. flat_hash_map() = default;
  1890. inline V& operator[](const K& key) {
  1891. return emplace(key, convertible_to_value()).first->second;
  1892. }
  1893. inline V& operator[](K&& key) {
  1894. return emplace(std::move(key), convertible_to_value()).first->second;
  1895. }
  1896. V& at(const K& key) {
  1897. auto found = this->find(key);
  1898. if (found == this->end())
  1899. throw std::out_of_range("Argument passed to at() was not in the map.");
  1900. return found->second;
  1901. }
  1902. const V& at(const K& key) const {
  1903. auto found = this->find(key);
  1904. if (found == this->end())
  1905. throw std::out_of_range("Argument passed to at() was not in the map.");
  1906. return found->second;
  1907. }
  1908. using Table::emplace;
  1909. std::pair<typename Table::iterator, bool> emplace() {
  1910. return emplace(key_type(), convertible_to_value());
  1911. }
  1912. template <typename M>
  1913. std::pair<typename Table::iterator, bool> insert_or_assign(
  1914. const key_type& key,
  1915. M&& m) {
  1916. auto emplace_result = emplace(key, std::forward<M>(m));
  1917. if (!emplace_result.second)
  1918. emplace_result.first->second = std::forward<M>(m);
  1919. return emplace_result;
  1920. }
  1921. template <typename M>
  1922. std::pair<typename Table::iterator, bool> insert_or_assign(
  1923. key_type&& key,
  1924. M&& m) {
  1925. auto emplace_result = emplace(std::move(key), std::forward<M>(m));
  1926. if (!emplace_result.second)
  1927. emplace_result.first->second = std::forward<M>(m);
  1928. return emplace_result;
  1929. }
  1930. template <typename M>
  1931. typename Table::iterator insert_or_assign(
  1932. typename Table::const_iterator,
  1933. const key_type& key,
  1934. M&& m) {
  1935. return insert_or_assign(key, std::forward<M>(m)).first;
  1936. }
  1937. template <typename M>
  1938. typename Table::iterator insert_or_assign(
  1939. typename Table::const_iterator,
  1940. key_type&& key,
  1941. M&& m) {
  1942. return insert_or_assign(std::move(key), std::forward<M>(m)).first;
  1943. }
  1944. friend bool operator==(const flat_hash_map& lhs, const flat_hash_map& rhs) {
  1945. if (lhs.size() != rhs.size())
  1946. return false;
  1947. for (const typename Table::value_type& value : lhs) {
  1948. auto found = rhs.find(value.first);
  1949. if (found == rhs.end())
  1950. return false;
  1951. else if (value.second != found->second)
  1952. return false;
  1953. }
  1954. return true;
  1955. }
  1956. friend bool operator!=(const flat_hash_map& lhs, const flat_hash_map& rhs) {
  1957. return !(lhs == rhs);
  1958. }
  1959. private:
  1960. struct convertible_to_value {
  1961. operator V() const {
  1962. return V();
  1963. }
  1964. };
  1965. };
  1966. template <
  1967. typename T,
  1968. typename H = std::hash<T>,
  1969. typename E = std::equal_to<T>,
  1970. typename A = std::allocator<T>>
  1971. class flat_hash_set
  1972. : public detailv3::sherwood_v3_table<
  1973. T,
  1974. T,
  1975. H,
  1976. detailv3::functor_storage<uint64_t, H>,
  1977. E,
  1978. detailv3::functor_storage<bool, E>,
  1979. A,
  1980. typename std::allocator_traits<A>::template rebind_alloc<
  1981. detailv3::sherwood_v3_entry<T>>> {
  1982. using Table = detailv3::sherwood_v3_table<
  1983. T,
  1984. T,
  1985. H,
  1986. detailv3::functor_storage<uint64_t, H>,
  1987. E,
  1988. detailv3::functor_storage<bool, E>,
  1989. A,
  1990. typename std::allocator_traits<A>::template rebind_alloc<
  1991. detailv3::sherwood_v3_entry<T>>>;
  1992. public:
  1993. using key_type = T;
  1994. using Table::Table;
  1995. flat_hash_set() = default;
  1996. template <typename... Args>
  1997. std::pair<typename Table::iterator, bool> emplace(Args&&... args) {
  1998. return Table::emplace(T(std::forward<Args>(args)...));
  1999. }
  2000. std::pair<typename Table::iterator, bool> emplace(const key_type& arg) {
  2001. return Table::emplace(arg);
  2002. }
  2003. std::pair<typename Table::iterator, bool> emplace(key_type& arg) {
  2004. return Table::emplace(arg);
  2005. }
  2006. std::pair<typename Table::iterator, bool> emplace(const key_type&& arg) {
  2007. return Table::emplace(std::move(arg));
  2008. }
  2009. std::pair<typename Table::iterator, bool> emplace(key_type&& arg) {
  2010. return Table::emplace(std::move(arg));
  2011. }
  2012. friend bool operator==(const flat_hash_set& lhs, const flat_hash_set& rhs) {
  2013. if (lhs.size() != rhs.size())
  2014. return false;
  2015. for (const T& value : lhs) {
  2016. if (rhs.find(value) == rhs.end())
  2017. return false;
  2018. }
  2019. return true;
  2020. }
  2021. friend bool operator!=(const flat_hash_set& lhs, const flat_hash_set& rhs) {
  2022. return !(lhs == rhs);
  2023. }
  2024. };
  2025. template <typename T>
  2026. struct power_of_two_std_hash : std::hash<T> {
  2027. typedef ska::power_of_two_hash_policy hash_policy;
  2028. };
  2029. } // end namespace ska
  2030. C10_CLANG_DIAGNOSTIC_POP()