implementation.hpp 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912
  1. // Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard.
  2. // Copyright (C) 2005-2016 Daniel James
  3. // Copyright (C) 2022-2024 Joaquin M Lopez Munoz.
  4. // Copyright (C) 2022-2023 Christian Mazakas
  5. // Copyright (C) 2024 Braden Ganetsky
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_UNORDERED_DETAIL_IMPLEMENTATION_HPP
  10. #define BOOST_UNORDERED_DETAIL_IMPLEMENTATION_HPP
  11. #include <boost/config.hpp>
  12. #if defined(BOOST_HAS_PRAGMA_ONCE)
  13. #pragma once
  14. #endif
  15. #include <boost/unordered/detail/allocator_constructed.hpp>
  16. #include <boost/unordered/detail/fca.hpp>
  17. #include <boost/unordered/detail/opt_storage.hpp>
  18. #include <boost/unordered/detail/serialize_tracked_address.hpp>
  19. #include <boost/unordered/detail/static_assert.hpp>
  20. #include <boost/unordered/detail/type_traits.hpp>
  21. #include <boost/assert.hpp>
  22. #include <boost/core/allocator_traits.hpp>
  23. #include <boost/core/bit.hpp>
  24. #include <boost/core/invoke_swap.hpp>
  25. #include <boost/core/no_exceptions_support.hpp>
  26. #include <boost/core/pointer_traits.hpp>
  27. #include <boost/core/serialization.hpp>
  28. #include <boost/mp11/algorithm.hpp>
  29. #include <boost/mp11/list.hpp>
  30. #include <boost/throw_exception.hpp>
  31. #include <algorithm>
  32. #include <cmath>
  33. #include <iterator>
  34. #include <limits>
  35. #include <stdexcept>
  36. #include <type_traits>
  37. #include <utility>
  38. #include <tuple> // std::forward_as_tuple
  39. namespace boost {
  40. namespace tuples {
  41. struct null_type;
  42. }
  43. } // namespace boost
  44. // BOOST_UNORDERED_SUPPRESS_DEPRECATED
  45. //
  46. // Define to stop deprecation attributes
  47. #if defined(BOOST_UNORDERED_SUPPRESS_DEPRECATED)
  48. #define BOOST_UNORDERED_DEPRECATED(msg)
  49. #endif
  50. // BOOST_UNORDERED_DEPRECATED
  51. //
  52. // Wrapper around various depreaction attributes.
  53. #if defined(__has_cpp_attribute) && \
  54. (!defined(__cplusplus) || __cplusplus >= 201402)
  55. #if __has_cpp_attribute(deprecated) && !defined(BOOST_UNORDERED_DEPRECATED)
  56. #define BOOST_UNORDERED_DEPRECATED(msg) [[deprecated(msg)]]
  57. #endif
  58. #endif
  59. #if !defined(BOOST_UNORDERED_DEPRECATED)
  60. #if defined(__GNUC__) && __GNUC__ >= 4
  61. #define BOOST_UNORDERED_DEPRECATED(msg) __attribute__((deprecated))
  62. #elif defined(_MSC_VER) && _MSC_VER >= 1400
  63. #define BOOST_UNORDERED_DEPRECATED(msg) __declspec(deprecated(msg))
  64. #elif defined(_MSC_VER) && _MSC_VER >= 1310
  65. #define BOOST_UNORDERED_DEPRECATED(msg) __declspec(deprecated)
  66. #else
  67. #define BOOST_UNORDERED_DEPRECATED(msg)
  68. #endif
  69. #endif
  70. namespace boost {
  71. namespace unordered {
  72. using std::piecewise_construct;
  73. using std::piecewise_construct_t;
  74. namespace detail {
  75. template <typename Types> struct table;
  76. static const float minimum_max_load_factor = 1e-3f;
  77. static const std::size_t default_bucket_count = 0;
  78. struct move_tag
  79. {
  80. };
  81. struct empty_emplace
  82. {
  83. };
  84. struct no_key
  85. {
  86. no_key() {}
  87. template <class T> no_key(T const&) {}
  88. };
  89. struct converting_key
  90. {
  91. };
  92. namespace func {
  93. template <class T> inline void ignore_unused_variable_warning(T const&)
  94. {
  95. }
  96. } // namespace func
  97. //////////////////////////////////////////////////////////////////////////
  98. // iterator SFINAE
  99. template <typename I>
  100. struct is_forward : std::is_base_of<std::forward_iterator_tag,
  101. typename std::iterator_traits<I>::iterator_category>
  102. {
  103. };
  104. template <typename I, typename ReturnType>
  105. struct enable_if_forward
  106. : std::enable_if<boost::unordered::detail::is_forward<I>::value,
  107. ReturnType>
  108. {
  109. };
  110. template <typename I, typename ReturnType>
  111. struct disable_if_forward
  112. : std::enable_if<!boost::unordered::detail::is_forward<I>::value,
  113. ReturnType>
  114. {
  115. };
  116. } // namespace detail
  117. } // namespace unordered
  118. } // namespace boost
  119. namespace boost {
  120. namespace unordered {
  121. namespace detail {
  122. //////////////////////////////////////////////////////////////////////////
  123. // insert_size/initial_size
  124. template <class I>
  125. inline typename boost::unordered::detail::enable_if_forward<I,
  126. std::size_t>::type
  127. insert_size(I i, I j)
  128. {
  129. return static_cast<std::size_t>(std::distance(i, j));
  130. }
  131. template <class I>
  132. inline typename boost::unordered::detail::disable_if_forward<I,
  133. std::size_t>::type
  134. insert_size(I, I)
  135. {
  136. return 1;
  137. }
  138. template <class I>
  139. inline std::size_t initial_size(I i, I j,
  140. std::size_t num_buckets =
  141. boost::unordered::detail::default_bucket_count)
  142. {
  143. return (std::max)(
  144. boost::unordered::detail::insert_size(i, j), num_buckets);
  145. }
  146. //////////////////////////////////////////////////////////////////////////
  147. // compressed
  148. template <typename T, int Index>
  149. struct compressed_base : boost::empty_value<T>
  150. {
  151. compressed_base(T const& x) : empty_value<T>(boost::empty_init_t(), x)
  152. {
  153. }
  154. compressed_base(T& x, move_tag)
  155. : empty_value<T>(boost::empty_init_t(), std::move(x))
  156. {
  157. }
  158. T& get() { return empty_value<T>::get(); }
  159. T const& get() const { return empty_value<T>::get(); }
  160. };
  161. template <typename T, int Index>
  162. struct generate_base : boost::unordered::detail::compressed_base<T, Index>
  163. {
  164. typedef compressed_base<T, Index> type;
  165. generate_base() : type() {}
  166. };
  167. template <typename T1, typename T2>
  168. struct compressed
  169. : private boost::unordered::detail::generate_base<T1, 1>::type,
  170. private boost::unordered::detail::generate_base<T2, 2>::type
  171. {
  172. typedef typename generate_base<T1, 1>::type base1;
  173. typedef typename generate_base<T2, 2>::type base2;
  174. typedef T1 first_type;
  175. typedef T2 second_type;
  176. first_type& first() { return static_cast<base1*>(this)->get(); }
  177. first_type const& first() const
  178. {
  179. return static_cast<base1 const*>(this)->get();
  180. }
  181. second_type& second() { return static_cast<base2*>(this)->get(); }
  182. second_type const& second() const
  183. {
  184. return static_cast<base2 const*>(this)->get();
  185. }
  186. template <typename First, typename Second>
  187. compressed(First const& x1, Second const& x2) : base1(x1), base2(x2)
  188. {
  189. }
  190. compressed(compressed const& x) : base1(x.first()), base2(x.second()) {}
  191. compressed(compressed& x, move_tag m)
  192. : base1(x.first(), m), base2(x.second(), m)
  193. {
  194. }
  195. void assign(compressed const& x)
  196. {
  197. first() = x.first();
  198. second() = x.second();
  199. }
  200. void move_assign(compressed& x)
  201. {
  202. first() = std::move(x.first());
  203. second() = std::move(x.second());
  204. }
  205. void swap(compressed& x)
  206. {
  207. boost::core::invoke_swap(first(), x.first());
  208. boost::core::invoke_swap(second(), x.second());
  209. }
  210. private:
  211. // Prevent assignment just to make use of assign or
  212. // move_assign explicit.
  213. compressed& operator=(compressed const&);
  214. };
  215. //////////////////////////////////////////////////////////////////////////
  216. // pair_traits
  217. //
  218. // Used to get the types from a pair without instantiating it.
  219. template <typename Pair> struct pair_traits
  220. {
  221. typedef typename Pair::first_type first_type;
  222. typedef typename Pair::second_type second_type;
  223. };
  224. template <typename T1, typename T2> struct pair_traits<std::pair<T1, T2> >
  225. {
  226. typedef T1 first_type;
  227. typedef T2 second_type;
  228. };
  229. #if defined(BOOST_MSVC)
  230. #pragma warning(push)
  231. #pragma warning(disable : 4512) // assignment operator could not be generated.
  232. #pragma warning(disable : 4345) // behavior change: an object of POD type
  233. // constructed with an initializer of the form ()
  234. // will be default-initialized.
  235. #endif
  236. //////////////////////////////////////////////////////////////////////////
  237. // Bits and pieces for implementing traits
  238. template <typename T> typename std::add_lvalue_reference<T>::type make();
  239. struct choice2
  240. {
  241. typedef char (&type)[2];
  242. };
  243. struct choice1 : choice2
  244. {
  245. typedef char (&type)[1];
  246. };
  247. choice1 choose();
  248. typedef choice1::type yes_type;
  249. typedef choice2::type no_type;
  250. struct private_type
  251. {
  252. private_type const& operator,(int) const;
  253. };
  254. template <typename T> no_type is_private_type(T const&);
  255. yes_type is_private_type(private_type const&);
  256. struct convert_from_anything
  257. {
  258. template <typename T> convert_from_anything(T const&);
  259. };
  260. } // namespace detail
  261. } // namespace unordered
  262. } // namespace boost
  263. ////////////////////////////////////////////////////////////////////////////////
  264. //
  265. // Some utilities for implementing allocator_traits, but useful elsewhere so
  266. // they're always defined.
  267. namespace boost {
  268. namespace unordered {
  269. namespace detail {
  270. ////////////////////////////////////////////////////////////////////////////
  271. // Explicitly call a destructor
  272. #if defined(BOOST_MSVC)
  273. #pragma warning(push)
  274. #pragma warning(disable : 4100) // unreferenced formal parameter
  275. #endif
  276. namespace func {
  277. template <class T> inline void destroy(T* x) { x->~T(); }
  278. } // namespace func
  279. #if defined(BOOST_MSVC)
  280. #pragma warning(pop)
  281. #endif
  282. //////////////////////////////////////////////////////////////////////////
  283. // value_base
  284. //
  285. // Space used to store values.
  286. template <typename ValueType> struct value_base
  287. {
  288. typedef ValueType value_type;
  289. opt_storage<value_type> data_;
  290. value_base() : data_() {}
  291. void* address() { return this; }
  292. value_type& value() { return *(ValueType*)this; }
  293. value_type const& value() const { return *(ValueType const*)this; }
  294. value_type* value_ptr() { return (ValueType*)this; }
  295. value_type const* value_ptr() const { return (ValueType const*)this; }
  296. private:
  297. value_base& operator=(value_base const&);
  298. };
  299. //////////////////////////////////////////////////////////////////////////
  300. // optional
  301. // TODO: Use std::optional when available.
  302. template <typename T> class optional
  303. {
  304. boost::unordered::detail::value_base<T> value_;
  305. bool has_value_;
  306. void destroy()
  307. {
  308. if (has_value_) {
  309. boost::unordered::detail::func::destroy(value_.value_ptr());
  310. has_value_ = false;
  311. }
  312. }
  313. void move(optional<T>& x)
  314. {
  315. BOOST_ASSERT(!has_value_ && x.has_value_);
  316. new (value_.value_ptr()) T(std::move(x.value_.value()));
  317. boost::unordered::detail::func::destroy(x.value_.value_ptr());
  318. has_value_ = true;
  319. x.has_value_ = false;
  320. }
  321. public:
  322. optional() noexcept : has_value_(false) {}
  323. optional(optional const&) = delete;
  324. optional& operator=(optional const&) = delete;
  325. optional(optional<T>&& x) : has_value_(false)
  326. {
  327. if (x.has_value_) {
  328. move(x);
  329. }
  330. }
  331. explicit optional(T const& x) : has_value_(true)
  332. {
  333. new (value_.value_ptr()) T(x);
  334. }
  335. optional& operator=(optional<T>&& x)
  336. {
  337. destroy();
  338. if (x.has_value_) {
  339. move(x);
  340. }
  341. return *this;
  342. }
  343. ~optional() { destroy(); }
  344. bool has_value() const { return has_value_; }
  345. T& operator*() { return value_.value(); }
  346. T const& operator*() const { return value_.value(); }
  347. T* operator->() { return value_.value_ptr(); }
  348. T const* operator->() const { return value_.value_ptr(); }
  349. bool operator==(optional<T> const& x) const
  350. {
  351. return has_value_ ? x.has_value_ && value_.value() == x.value_.value()
  352. : !x.has_value_;
  353. }
  354. bool operator!=(optional<T> const& x) const { return !((*this) == x); }
  355. void swap(optional<T>& x)
  356. {
  357. if (has_value_ != x.has_value_) {
  358. if (has_value_) {
  359. x.move(*this);
  360. } else {
  361. move(x);
  362. }
  363. } else if (has_value_) {
  364. boost::core::invoke_swap(value_.value(), x.value_.value());
  365. }
  366. }
  367. friend void swap(optional<T>& x, optional<T>& y) { x.swap(y); }
  368. };
  369. } // namespace detail
  370. } // namespace unordered
  371. } // namespace boost
  372. ////////////////////////////////////////////////////////////////////////////////
  373. //
  374. // Allocator traits
  375. //
  376. namespace boost {
  377. namespace unordered {
  378. namespace detail {
  379. template <typename Alloc>
  380. struct allocator_traits : boost::allocator_traits<Alloc>
  381. {
  382. };
  383. template <typename Alloc, typename T>
  384. struct rebind_wrap : boost::allocator_rebind<Alloc, T>
  385. {
  386. };
  387. } // namespace detail
  388. } // namespace unordered
  389. } // namespace boost
  390. namespace boost {
  391. namespace unordered {
  392. namespace detail {
  393. namespace func {
  394. ////////////////////////////////////////////////////////////////////////
  395. // Trait to check for piecewise construction.
  396. template <typename A0> struct use_piecewise
  397. {
  398. static choice1::type test(choice1, std::piecewise_construct_t);
  399. static choice2::type test(choice2, ...);
  400. enum
  401. {
  402. value = sizeof(choice1::type) ==
  403. sizeof(test(choose(), boost::unordered::detail::make<A0>()))
  404. };
  405. };
  406. ////////////////////////////////////////////////////////////////////////
  407. // Construct from variadic parameters
  408. template <typename Alloc, typename T, typename... Args>
  409. inline void construct_from_args(
  410. Alloc& alloc, T* address, Args&&... args)
  411. {
  412. boost::allocator_construct(
  413. alloc, address, std::forward<Args>(args)...);
  414. }
  415. // For backwards compatibility, implement a special case for
  416. // piecewise_construct with boost::tuple
  417. template <typename A0> struct detect_std_tuple
  418. {
  419. template <class... Args>
  420. static choice1::type test(choice1, std::tuple<Args...> const&);
  421. static choice2::type test(choice2, ...);
  422. enum
  423. {
  424. value = sizeof(choice1::type) ==
  425. sizeof(test(choose(), boost::unordered::detail::make<A0>()))
  426. };
  427. };
  428. // Special case for piecewise_construct
  429. template <template <class...> class Tuple, class... Args,
  430. std::size_t... Is, class... TupleArgs>
  431. std::tuple<typename std::add_lvalue_reference<Args>::type...>
  432. to_std_tuple_impl(boost::mp11::mp_list<Args...>,
  433. Tuple<TupleArgs...>& tuple, boost::mp11::index_sequence<Is...>)
  434. {
  435. (void)tuple;
  436. using std::get;
  437. return std::tuple<typename std::add_lvalue_reference<Args>::type...>(
  438. get<Is>(tuple)...);
  439. }
  440. template <class T>
  441. using add_lvalue_reference_t =
  442. typename std::add_lvalue_reference<T>::type;
  443. template <template <class...> class Tuple, class... Args>
  444. boost::mp11::mp_transform<add_lvalue_reference_t,
  445. boost::mp11::mp_remove<std::tuple<Args...>,
  446. boost::tuples::null_type> >
  447. to_std_tuple(Tuple<Args...>& tuple)
  448. {
  449. using list = boost::mp11::mp_remove<boost::mp11::mp_list<Args...>,
  450. boost::tuples::null_type>;
  451. using list_size = boost::mp11::mp_size<list>;
  452. using index_seq = boost::mp11::make_index_sequence<list_size::value>;
  453. return to_std_tuple_impl(list{}, tuple, index_seq{});
  454. }
  455. template <typename Alloc, typename A, typename B, typename A0,
  456. typename A1, typename A2>
  457. inline typename std::enable_if<use_piecewise<A0>::value &&
  458. !detect_std_tuple<A1>::value &&
  459. !detect_std_tuple<A2>::value,
  460. void>::type
  461. construct_from_args(
  462. Alloc& alloc, std::pair<A, B>* address, A0&&, A1&& a1, A2&& a2)
  463. {
  464. boost::allocator_construct(alloc, address, std::piecewise_construct,
  465. to_std_tuple(a1), to_std_tuple(a2));
  466. }
  467. } // namespace func
  468. } // namespace detail
  469. } // namespace unordered
  470. } // namespace boost
  471. namespace boost {
  472. namespace unordered {
  473. namespace detail {
  474. ///////////////////////////////////////////////////////////////////
  475. //
  476. // Node construction
  477. template <typename NodeAlloc> struct node_constructor
  478. {
  479. typedef NodeAlloc node_allocator;
  480. typedef boost::unordered::detail::allocator_traits<NodeAlloc>
  481. node_allocator_traits;
  482. typedef typename node_allocator_traits::value_type node;
  483. typedef typename node_allocator_traits::pointer node_pointer;
  484. typedef typename node::value_type value_type;
  485. node_allocator& alloc_;
  486. node_pointer node_;
  487. node_constructor(node_allocator& n) : alloc_(n), node_() {}
  488. ~node_constructor();
  489. void create_node();
  490. // no throw
  491. node_pointer release()
  492. {
  493. BOOST_ASSERT(node_);
  494. node_pointer p = node_;
  495. node_ = node_pointer();
  496. return p;
  497. }
  498. private:
  499. node_constructor(node_constructor const&);
  500. node_constructor& operator=(node_constructor const&);
  501. };
  502. template <typename Alloc> node_constructor<Alloc>::~node_constructor()
  503. {
  504. if (node_) {
  505. boost::unordered::detail::func::destroy(boost::to_address(node_));
  506. node_allocator_traits::deallocate(alloc_, node_, 1);
  507. }
  508. }
  509. template <typename Alloc> void node_constructor<Alloc>::create_node()
  510. {
  511. BOOST_ASSERT(!node_);
  512. node_ = node_allocator_traits::allocate(alloc_, 1);
  513. new ((void*)boost::to_address(node_)) node();
  514. }
  515. template <typename NodeAlloc> struct node_tmp
  516. {
  517. typedef typename boost::allocator_value_type<NodeAlloc>::type node;
  518. typedef typename boost::allocator_pointer<NodeAlloc>::type node_pointer;
  519. typedef typename node::value_type value_type;
  520. typedef typename boost::allocator_rebind<NodeAlloc, value_type>::type
  521. value_allocator;
  522. NodeAlloc& alloc_;
  523. node_pointer node_;
  524. explicit node_tmp(node_pointer n, NodeAlloc& a) : alloc_(a), node_(n) {}
  525. ~node_tmp();
  526. // no throw
  527. node_pointer release()
  528. {
  529. node_pointer p = node_;
  530. node_ = node_pointer();
  531. return p;
  532. }
  533. };
  534. template <typename Alloc> node_tmp<Alloc>::~node_tmp()
  535. {
  536. if (node_) {
  537. value_allocator val_alloc(alloc_);
  538. boost::allocator_destroy(val_alloc, node_->value_ptr());
  539. boost::allocator_deallocate(alloc_, node_, 1);
  540. }
  541. }
  542. } // namespace detail
  543. } // namespace unordered
  544. } // namespace boost
  545. namespace boost {
  546. namespace unordered {
  547. namespace detail {
  548. namespace func {
  549. // Some nicer construct_node functions, might try to
  550. // improve implementation later.
  551. template <typename Alloc, typename... Args>
  552. inline typename boost::allocator_pointer<Alloc>::type
  553. construct_node_from_args(Alloc& alloc, Args&&... args)
  554. {
  555. typedef typename boost::allocator_value_type<Alloc>::type node;
  556. typedef typename node::value_type value_type;
  557. typedef typename boost::allocator_rebind<Alloc, value_type>::type
  558. value_allocator;
  559. value_allocator val_alloc(alloc);
  560. node_constructor<Alloc> a(alloc);
  561. a.create_node();
  562. construct_from_args(
  563. val_alloc, a.node_->value_ptr(), std::forward<Args>(args)...);
  564. return a.release();
  565. }
  566. template <typename Alloc, typename U>
  567. inline typename boost::allocator_pointer<Alloc>::type construct_node(
  568. Alloc& alloc, U&& x)
  569. {
  570. node_constructor<Alloc> a(alloc);
  571. a.create_node();
  572. typedef typename boost::allocator_value_type<Alloc>::type node;
  573. typedef typename node::value_type value_type;
  574. typedef typename boost::allocator_rebind<Alloc, value_type>::type
  575. value_allocator;
  576. value_allocator val_alloc(alloc);
  577. boost::allocator_construct(
  578. val_alloc, a.node_->value_ptr(), std::forward<U>(x));
  579. return a.release();
  580. }
  581. template <typename Alloc, typename Key>
  582. inline typename boost::allocator_pointer<Alloc>::type
  583. construct_node_pair(Alloc& alloc, Key&& k)
  584. {
  585. node_constructor<Alloc> a(alloc);
  586. a.create_node();
  587. typedef typename boost::allocator_value_type<Alloc>::type node;
  588. typedef typename node::value_type value_type;
  589. typedef typename boost::allocator_rebind<Alloc, value_type>::type
  590. value_allocator;
  591. value_allocator val_alloc(alloc);
  592. boost::allocator_construct(val_alloc, a.node_->value_ptr(),
  593. std::piecewise_construct,
  594. std::forward_as_tuple(std::forward<Key>(k)),
  595. std::forward_as_tuple());
  596. return a.release();
  597. }
  598. template <typename Alloc, typename Key, typename Mapped>
  599. inline typename boost::allocator_pointer<Alloc>::type
  600. construct_node_pair(Alloc& alloc, Key&& k, Mapped&& m)
  601. {
  602. node_constructor<Alloc> a(alloc);
  603. a.create_node();
  604. typedef typename boost::allocator_value_type<Alloc>::type node;
  605. typedef typename node::value_type value_type;
  606. typedef typename boost::allocator_rebind<Alloc, value_type>::type
  607. value_allocator;
  608. value_allocator val_alloc(alloc);
  609. boost::allocator_construct(val_alloc, a.node_->value_ptr(),
  610. std::piecewise_construct,
  611. std::forward_as_tuple(std::forward<Key>(k)),
  612. std::forward_as_tuple(std::forward<Mapped>(m)));
  613. return a.release();
  614. }
  615. template <typename Alloc, typename Key, typename... Args>
  616. inline typename boost::allocator_pointer<Alloc>::type
  617. construct_node_pair_from_args(Alloc& alloc, Key&& k, Args&&... args)
  618. {
  619. node_constructor<Alloc> a(alloc);
  620. a.create_node();
  621. typedef typename boost::allocator_value_type<Alloc>::type node;
  622. typedef typename node::value_type value_type;
  623. typedef typename boost::allocator_rebind<Alloc, value_type>::type
  624. value_allocator;
  625. value_allocator val_alloc(alloc);
  626. boost::allocator_construct(val_alloc, a.node_->value_ptr(),
  627. std::piecewise_construct,
  628. std::forward_as_tuple(std::forward<Key>(k)),
  629. std::forward_as_tuple(std::forward<Args>(args)...));
  630. return a.release();
  631. }
  632. template <typename T, typename Alloc, typename Key>
  633. inline typename boost::allocator_pointer<Alloc>::type
  634. construct_node_from_key(T*, Alloc& alloc, Key&& k)
  635. {
  636. return construct_node(alloc, std::forward<Key>(k));
  637. }
  638. template <typename T, typename V, typename Alloc, typename Key>
  639. inline typename boost::allocator_pointer<Alloc>::type
  640. construct_node_from_key(std::pair<T const, V>*, Alloc& alloc, Key&& k)
  641. {
  642. return construct_node_pair(alloc, std::forward<Key>(k));
  643. }
  644. } // namespace func
  645. } // namespace detail
  646. } // namespace unordered
  647. } // namespace boost
  648. #if defined(BOOST_MSVC)
  649. #pragma warning(pop)
  650. #endif
  651. namespace boost {
  652. namespace unordered {
  653. namespace detail {
  654. //////////////////////////////////////////////////////////////////////////
  655. // Functions
  656. //
  657. // This double buffers the storage for the hash function and key equality
  658. // predicate in order to have exception safe copy/swap. To do so,
  659. // use 'construct_spare' to construct in the spare space, and then when
  660. // ready to use 'switch_functions' to switch to the new functions.
  661. // If an exception is thrown between these two calls, use
  662. // 'cleanup_spare_functions' to destroy the unused constructed functions.
  663. #if defined(_GLIBCXX_HAVE_BUILTIN_LAUNDER)
  664. // gcc-12 warns when accessing the `current_functions` of our `functions`
  665. // class below with `-Wmaybe-unitialized`. By laundering the pointer, we
  666. // silence the warning and assure the compiler that a valid object exists
  667. // in that region of storage. This warning is also generated in C++03
  668. // which does not have `std::launder`. The compiler builtin is always
  669. // available, regardless of the C++ standard used when compiling.
  670. template <class T> T* launder(T* p) noexcept
  671. {
  672. return __builtin_launder(p);
  673. }
  674. #else
  675. template <class T> T* launder(T* p) noexcept { return p; }
  676. #endif
  677. template <class H, class P> class functions
  678. {
  679. public:
  680. static const bool nothrow_move_assignable =
  681. std::is_nothrow_move_assignable<H>::value &&
  682. std::is_nothrow_move_assignable<P>::value;
  683. static const bool nothrow_move_constructible =
  684. std::is_nothrow_move_constructible<H>::value &&
  685. std::is_nothrow_move_constructible<P>::value;
  686. static const bool nothrow_swappable =
  687. boost::unordered::detail::is_nothrow_swappable<H>::value &&
  688. boost::unordered::detail::is_nothrow_swappable<P>::value;
  689. private:
  690. functions& operator=(functions const&);
  691. typedef compressed<H, P> function_pair;
  692. unsigned char current_; // 0/1 - Currently active functions
  693. // +2 - Both constructed
  694. opt_storage<function_pair> funcs_[2];
  695. public:
  696. functions(H const& hf, P const& eq) : current_(0)
  697. {
  698. construct_functions(current_, hf, eq);
  699. }
  700. functions(functions const& bf) : current_(0)
  701. {
  702. construct_functions(current_, bf.current_functions());
  703. }
  704. functions(functions& bf, boost::unordered::detail::move_tag)
  705. : current_(0)
  706. {
  707. construct_functions(current_, bf.current_functions(),
  708. std::integral_constant<bool, nothrow_move_constructible>());
  709. }
  710. ~functions()
  711. {
  712. BOOST_ASSERT(!(current_ & 2));
  713. destroy_functions(current_);
  714. }
  715. H const& hash_function() const { return current_functions().first(); }
  716. P const& key_eq() const { return current_functions().second(); }
  717. function_pair const& current_functions() const
  718. {
  719. return *::boost::unordered::detail::launder(
  720. static_cast<function_pair const*>(
  721. static_cast<void const*>(funcs_[current_ & 1].address())));
  722. }
  723. function_pair& current_functions()
  724. {
  725. return *::boost::unordered::detail::launder(
  726. static_cast<function_pair*>(
  727. static_cast<void*>(funcs_[current_ & 1].address())));
  728. }
  729. void construct_spare_functions(function_pair const& f)
  730. {
  731. BOOST_ASSERT(!(current_ & 2));
  732. construct_functions(current_ ^ 1, f);
  733. current_ |= 2;
  734. }
  735. void cleanup_spare_functions()
  736. {
  737. if (current_ & 2) {
  738. current_ = static_cast<unsigned char>(current_ & 1);
  739. destroy_functions(current_ ^ 1);
  740. }
  741. }
  742. void switch_functions()
  743. {
  744. BOOST_ASSERT(current_ & 2);
  745. destroy_functions(static_cast<unsigned char>(current_ & 1));
  746. current_ ^= 3;
  747. }
  748. private:
  749. void construct_functions(unsigned char which, H const& hf, P const& eq)
  750. {
  751. BOOST_ASSERT(!(which & 2));
  752. new ((void*)&funcs_[which]) function_pair(hf, eq);
  753. }
  754. void construct_functions(
  755. unsigned char which, function_pair const& f, std::false_type = {})
  756. {
  757. BOOST_ASSERT(!(which & 2));
  758. new ((void*)&funcs_[which]) function_pair(f);
  759. }
  760. void construct_functions(
  761. unsigned char which, function_pair& f, std::true_type)
  762. {
  763. BOOST_ASSERT(!(which & 2));
  764. new ((void*)&funcs_[which])
  765. function_pair(f, boost::unordered::detail::move_tag());
  766. }
  767. void destroy_functions(unsigned char which)
  768. {
  769. BOOST_ASSERT(!(which & 2));
  770. boost::unordered::detail::func::destroy(
  771. (function_pair*)(&funcs_[which]));
  772. }
  773. };
  774. #if defined(BOOST_MSVC)
  775. #pragma warning(push)
  776. #pragma warning(disable : 4127) // conditional expression is constant
  777. #endif
  778. //////////////////////////////////////////////////////////////////////////
  779. // convert double to std::size_t
  780. inline std::size_t double_to_size(double f)
  781. {
  782. return f >= static_cast<double>(
  783. (std::numeric_limits<std::size_t>::max)())
  784. ? (std::numeric_limits<std::size_t>::max)()
  785. : static_cast<std::size_t>(f);
  786. }
  787. //////////////////////////////////////////////////////////////////////////
  788. // iterator definitions
  789. namespace iterator_detail {
  790. template <class Node, class Bucket> class c_iterator;
  791. template <class Node, class Bucket> class iterator
  792. {
  793. public:
  794. typedef typename Node::value_type value_type;
  795. typedef value_type element_type;
  796. typedef value_type* pointer;
  797. typedef value_type& reference;
  798. typedef std::ptrdiff_t difference_type;
  799. typedef std::forward_iterator_tag iterator_category;
  800. iterator() : p(), itb() {}
  801. reference operator*() const noexcept { return dereference(); }
  802. pointer operator->() const noexcept
  803. {
  804. pointer x = std::addressof(p->value());
  805. return x;
  806. }
  807. iterator& operator++() noexcept
  808. {
  809. increment();
  810. return *this;
  811. }
  812. iterator operator++(int) noexcept
  813. {
  814. iterator old = *this;
  815. increment();
  816. return old;
  817. }
  818. bool operator==(iterator const& other) const noexcept
  819. {
  820. return equal(other);
  821. }
  822. bool operator!=(iterator const& other) const noexcept
  823. {
  824. return !equal(other);
  825. }
  826. bool operator==(
  827. boost::unordered::detail::iterator_detail::c_iterator<Node,
  828. Bucket> const& other) const noexcept
  829. {
  830. return equal(other);
  831. }
  832. bool operator!=(
  833. boost::unordered::detail::iterator_detail::c_iterator<Node,
  834. Bucket> const& other) const noexcept
  835. {
  836. return !equal(other);
  837. }
  838. private:
  839. typedef typename Node::node_pointer node_pointer;
  840. typedef grouped_bucket_iterator<Bucket> bucket_iterator;
  841. node_pointer p;
  842. bucket_iterator itb;
  843. template <class Types> friend struct boost::unordered::detail::table;
  844. template <class N, class B> friend class c_iterator;
  845. iterator(node_pointer p_, bucket_iterator itb_) : p(p_), itb(itb_) {}
  846. value_type& dereference() const noexcept { return p->value(); }
  847. bool equal(const iterator& x) const noexcept { return (p == x.p); }
  848. bool equal(
  849. const boost::unordered::detail::iterator_detail::c_iterator<Node,
  850. Bucket>& x) const noexcept
  851. {
  852. return (p == x.p);
  853. }
  854. void increment() noexcept
  855. {
  856. p = p->next;
  857. if (!p) {
  858. p = (++itb)->next;
  859. }
  860. }
  861. template <typename Archive>
  862. friend void serialization_track(Archive& ar, const iterator& x)
  863. {
  864. if (x.p) {
  865. track_address(ar, x.p);
  866. serialization_track(ar, x.itb);
  867. }
  868. }
  869. friend class boost::serialization::access;
  870. template <typename Archive> void serialize(Archive& ar, unsigned int)
  871. {
  872. if (!p)
  873. itb = bucket_iterator();
  874. serialize_tracked_address(ar, p);
  875. ar& core::make_nvp("bucket_iterator", itb);
  876. }
  877. };
  878. template <class Node, class Bucket> class c_iterator
  879. {
  880. public:
  881. typedef typename Node::value_type value_type;
  882. typedef value_type const element_type;
  883. typedef value_type const* pointer;
  884. typedef value_type const& reference;
  885. typedef std::ptrdiff_t difference_type;
  886. typedef std::forward_iterator_tag iterator_category;
  887. c_iterator() : p(), itb() {}
  888. c_iterator(iterator<Node, Bucket> it) : p(it.p), itb(it.itb) {}
  889. reference operator*() const noexcept { return dereference(); }
  890. pointer operator->() const noexcept
  891. {
  892. pointer x = std::addressof(p->value());
  893. return x;
  894. }
  895. c_iterator& operator++() noexcept
  896. {
  897. increment();
  898. return *this;
  899. }
  900. c_iterator operator++(int) noexcept
  901. {
  902. c_iterator old = *this;
  903. increment();
  904. return old;
  905. }
  906. bool operator==(c_iterator const& other) const noexcept
  907. {
  908. return equal(other);
  909. }
  910. bool operator!=(c_iterator const& other) const noexcept
  911. {
  912. return !equal(other);
  913. }
  914. bool operator==(
  915. boost::unordered::detail::iterator_detail::iterator<Node,
  916. Bucket> const& other) const noexcept
  917. {
  918. return equal(other);
  919. }
  920. bool operator!=(
  921. boost::unordered::detail::iterator_detail::iterator<Node,
  922. Bucket> const& other) const noexcept
  923. {
  924. return !equal(other);
  925. }
  926. private:
  927. typedef typename Node::node_pointer node_pointer;
  928. typedef grouped_bucket_iterator<Bucket> bucket_iterator;
  929. node_pointer p;
  930. bucket_iterator itb;
  931. template <class Types> friend struct boost::unordered::detail::table;
  932. template <class, class> friend class iterator;
  933. c_iterator(node_pointer p_, bucket_iterator itb_) : p(p_), itb(itb_)
  934. {
  935. }
  936. value_type const& dereference() const noexcept { return p->value(); }
  937. bool equal(const c_iterator& x) const noexcept { return (p == x.p); }
  938. void increment() noexcept
  939. {
  940. p = p->next;
  941. if (!p) {
  942. p = (++itb)->next;
  943. }
  944. }
  945. template <typename Archive>
  946. friend void serialization_track(Archive& ar, const c_iterator& x)
  947. {
  948. if (x.p) {
  949. track_address(ar, x.p);
  950. serialization_track(ar, x.itb);
  951. }
  952. }
  953. friend class boost::serialization::access;
  954. template <typename Archive> void serialize(Archive& ar, unsigned int)
  955. {
  956. if (!p)
  957. itb = bucket_iterator();
  958. serialize_tracked_address(ar, p);
  959. ar& core::make_nvp("bucket_iterator", itb);
  960. }
  961. };
  962. } // namespace iterator_detail
  963. //////////////////////////////////////////////////////////////////////////
  964. // table structure used by the containers
  965. template <typename Types>
  966. struct table : boost::unordered::detail::functions<typename Types::hasher,
  967. typename Types::key_equal>
  968. {
  969. private:
  970. table(table const&);
  971. table& operator=(table const&);
  972. public:
  973. typedef typename Types::hasher hasher;
  974. typedef typename Types::key_equal key_equal;
  975. typedef typename Types::const_key_type const_key_type;
  976. typedef typename Types::extractor extractor;
  977. typedef typename Types::value_type value_type;
  978. typedef typename Types::table table_impl;
  979. typedef boost::unordered::detail::functions<typename Types::hasher,
  980. typename Types::key_equal>
  981. functions;
  982. typedef typename Types::value_allocator value_allocator;
  983. typedef typename boost::allocator_void_pointer<value_allocator>::type
  984. void_pointer;
  985. typedef node<value_type, void_pointer> node_type;
  986. typedef boost::unordered::detail::grouped_bucket_array<
  987. bucket<node_type, void_pointer>, value_allocator, prime_fmod_size<> >
  988. bucket_array_type;
  989. typedef
  990. typename bucket_array_type::node_allocator_type node_allocator_type;
  991. typedef typename boost::allocator_pointer<node_allocator_type>::type
  992. node_pointer;
  993. typedef boost::unordered::detail::node_constructor<node_allocator_type>
  994. node_constructor;
  995. typedef boost::unordered::detail::node_tmp<node_allocator_type>
  996. node_tmp;
  997. typedef typename bucket_array_type::bucket_type bucket_type;
  998. typedef typename bucket_array_type::iterator bucket_iterator;
  999. typedef typename bucket_array_type::local_iterator l_iterator;
  1000. typedef typename bucket_array_type::const_local_iterator cl_iterator;
  1001. typedef std::size_t size_type;
  1002. typedef iterator_detail::iterator<node_type, bucket_type> iterator;
  1003. typedef iterator_detail::c_iterator<node_type, bucket_type> c_iterator;
  1004. typedef std::pair<iterator, bool> emplace_return;
  1005. ////////////////////////////////////////////////////////////////////////
  1006. // Members
  1007. std::size_t size_;
  1008. float mlf_;
  1009. std::size_t max_load_;
  1010. bucket_array_type buckets_;
  1011. public:
  1012. ////////////////////////////////////////////////////////////////////////
  1013. // Data access
  1014. size_type bucket_count() const { return buckets_.bucket_count(); }
  1015. template <class Key>
  1016. iterator next_group(Key const& k, c_iterator n) const
  1017. {
  1018. c_iterator last = this->end();
  1019. while (n != last && this->key_eq()(k, extractor::extract(*n))) {
  1020. ++n;
  1021. }
  1022. return iterator(n.p, n.itb);
  1023. }
  1024. template <class Key> std::size_t group_count(Key const& k) const
  1025. {
  1026. if (size_ == 0) {
  1027. return 0;
  1028. }
  1029. std::size_t c = 0;
  1030. std::size_t const key_hash = this->hash(k);
  1031. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1032. bool found = false;
  1033. for (node_pointer pos = itb->next; pos; pos = pos->next) {
  1034. if (this->key_eq()(k, this->get_key(pos))) {
  1035. ++c;
  1036. found = true;
  1037. } else if (found) {
  1038. break;
  1039. }
  1040. }
  1041. return c;
  1042. }
  1043. node_allocator_type const& node_alloc() const
  1044. {
  1045. return buckets_.get_node_allocator();
  1046. }
  1047. node_allocator_type& node_alloc()
  1048. {
  1049. return buckets_.get_node_allocator();
  1050. }
  1051. std::size_t max_bucket_count() const
  1052. {
  1053. typedef typename bucket_array_type::size_policy size_policy;
  1054. return size_policy::size(size_policy::size_index(
  1055. boost::allocator_max_size(this->node_alloc())));
  1056. }
  1057. iterator begin() const
  1058. {
  1059. if (size_ == 0) {
  1060. return end();
  1061. }
  1062. bucket_iterator itb = buckets_.begin();
  1063. return iterator(itb->next, itb);
  1064. }
  1065. iterator end() const { return iterator(); }
  1066. l_iterator begin(std::size_t bucket_index) const
  1067. {
  1068. return buckets_.begin(bucket_index);
  1069. }
  1070. std::size_t hash_to_bucket(std::size_t hash_value) const
  1071. {
  1072. return buckets_.position(hash_value);
  1073. }
  1074. std::size_t bucket_size(std::size_t index) const
  1075. {
  1076. std::size_t count = 0;
  1077. if (size_ > 0) {
  1078. bucket_iterator itb = buckets_.at(index);
  1079. node_pointer n = itb->next;
  1080. while (n) {
  1081. ++count;
  1082. n = n->next;
  1083. }
  1084. }
  1085. return count;
  1086. }
  1087. ////////////////////////////////////////////////////////////////////////
  1088. // Load methods
  1089. void recalculate_max_load()
  1090. {
  1091. // From 6.3.1/13:
  1092. // Only resize when size >= mlf_ * count
  1093. std::size_t const bc = buckets_.bucket_count();
  1094. // it's important we do the `bc == 0` check here because the `mlf_`
  1095. // can be specified to be infinity. The operation `n * INF` is `INF`
  1096. // for all `n > 0` but NaN for `n == 0`.
  1097. //
  1098. max_load_ =
  1099. bc == 0 ? 0
  1100. : boost::unordered::detail::double_to_size(
  1101. static_cast<double>(mlf_) * static_cast<double>(bc));
  1102. }
  1103. void max_load_factor(float z)
  1104. {
  1105. BOOST_ASSERT(z > 0);
  1106. mlf_ = (std::max)(z, minimum_max_load_factor);
  1107. recalculate_max_load();
  1108. }
  1109. ////////////////////////////////////////////////////////////////////////
  1110. // Constructors
  1111. table()
  1112. : functions(hasher(), key_equal()), size_(0), mlf_(1.0f),
  1113. max_load_(0)
  1114. {
  1115. }
  1116. table(std::size_t num_buckets, hasher const& hf, key_equal const& eq,
  1117. value_allocator const& a)
  1118. : functions(hf, eq), size_(0), mlf_(1.0f), max_load_(0),
  1119. buckets_(num_buckets, a)
  1120. {
  1121. recalculate_max_load();
  1122. }
  1123. table(table const& x, value_allocator const& a)
  1124. : functions(x), size_(0), mlf_(x.mlf_), max_load_(0),
  1125. buckets_(x.size_, a)
  1126. {
  1127. recalculate_max_load();
  1128. }
  1129. table(table& x, boost::unordered::detail::move_tag m)
  1130. : functions(x, m), size_(x.size_), mlf_(x.mlf_),
  1131. max_load_(x.max_load_), buckets_(std::move(x.buckets_))
  1132. {
  1133. x.size_ = 0;
  1134. x.max_load_ = 0;
  1135. }
  1136. table(table& x, value_allocator const& a,
  1137. boost::unordered::detail::move_tag m)
  1138. : functions(x, m), size_(0), mlf_(x.mlf_), max_load_(0),
  1139. buckets_(x.bucket_count(), a)
  1140. {
  1141. recalculate_max_load();
  1142. }
  1143. ////////////////////////////////////////////////////////////////////////
  1144. // Swap and Move
  1145. void swap_allocators(table& other, std::false_type)
  1146. {
  1147. boost::unordered::detail::func::ignore_unused_variable_warning(other);
  1148. // According to 23.2.1.8, if propagate_on_container_swap is
  1149. // false the behaviour is undefined unless the allocators
  1150. // are equal.
  1151. BOOST_ASSERT(node_alloc() == other.node_alloc());
  1152. }
  1153. // Not nothrow swappable
  1154. void swap(table& x, std::false_type)
  1155. {
  1156. if (this == &x) {
  1157. return;
  1158. }
  1159. this->construct_spare_functions(x.current_functions());
  1160. BOOST_TRY { x.construct_spare_functions(this->current_functions()); }
  1161. BOOST_CATCH(...)
  1162. {
  1163. this->cleanup_spare_functions();
  1164. BOOST_RETHROW
  1165. }
  1166. BOOST_CATCH_END
  1167. this->switch_functions();
  1168. x.switch_functions();
  1169. buckets_.swap(x.buckets_);
  1170. boost::core::invoke_swap(size_, x.size_);
  1171. std::swap(mlf_, x.mlf_);
  1172. std::swap(max_load_, x.max_load_);
  1173. }
  1174. // Nothrow swappable
  1175. void swap(table& x, std::true_type)
  1176. {
  1177. buckets_.swap(x.buckets_);
  1178. boost::core::invoke_swap(size_, x.size_);
  1179. std::swap(mlf_, x.mlf_);
  1180. std::swap(max_load_, x.max_load_);
  1181. this->current_functions().swap(x.current_functions());
  1182. }
  1183. // Only swaps the allocators if propagate_on_container_swap.
  1184. // If not propagate_on_container_swap and allocators aren't
  1185. // equal, behaviour is undefined.
  1186. void swap(table& x)
  1187. {
  1188. BOOST_ASSERT(boost::allocator_propagate_on_container_swap<
  1189. node_allocator_type>::type::value ||
  1190. node_alloc() == x.node_alloc());
  1191. swap(x, std::integral_constant<bool, functions::nothrow_swappable>());
  1192. }
  1193. // Only call with nodes allocated with the currect allocator, or
  1194. // one that is equal to it. (Can't assert because other's
  1195. // allocators might have already been moved).
  1196. void move_buckets_from(table& other)
  1197. {
  1198. buckets_ = std::move(other.buckets_);
  1199. size_ = other.size_;
  1200. max_load_ = other.max_load_;
  1201. other.size_ = 0;
  1202. other.max_load_ = 0;
  1203. }
  1204. // For use in the constructor when allocators might be different.
  1205. void move_construct_buckets(table& src)
  1206. {
  1207. if (this->node_alloc() == src.node_alloc()) {
  1208. move_buckets_from(src);
  1209. return;
  1210. }
  1211. if (src.size_ == 0) {
  1212. return;
  1213. }
  1214. BOOST_ASSERT(buckets_.bucket_count() == src.buckets_.bucket_count());
  1215. this->reserve(src.size_);
  1216. for (iterator pos = src.begin(); pos != src.end(); ++pos) {
  1217. node_tmp b(detail::func::construct_node(
  1218. this->node_alloc(), std::move(pos.p->value())),
  1219. this->node_alloc());
  1220. const_key_type& k = this->get_key(b.node_);
  1221. std::size_t key_hash = this->hash(k);
  1222. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1223. buckets_.insert_node(itb, b.release());
  1224. ++size_;
  1225. }
  1226. }
  1227. ////////////////////////////////////////////////////////////////////////
  1228. // Delete/destruct
  1229. ~table() { delete_buckets(); }
  1230. void delete_node(node_pointer p)
  1231. {
  1232. node_allocator_type alloc = this->node_alloc();
  1233. value_allocator val_alloc(alloc);
  1234. boost::allocator_destroy(val_alloc, p->value_ptr());
  1235. boost::unordered::detail::func::destroy(boost::to_address(p));
  1236. boost::allocator_deallocate(alloc, p, 1);
  1237. }
  1238. void delete_buckets()
  1239. {
  1240. iterator pos = begin(), last = this->end();
  1241. for (; pos != last;) {
  1242. node_pointer p = pos.p;
  1243. bucket_iterator itb = pos.itb;
  1244. ++pos;
  1245. buckets_.extract_node(itb, p);
  1246. delete_node(p);
  1247. --size_;
  1248. }
  1249. buckets_.clear();
  1250. }
  1251. ////////////////////////////////////////////////////////////////////////
  1252. // Clear
  1253. void clear_impl();
  1254. ////////////////////////////////////////////////////////////////////////
  1255. // Assignment
  1256. template <typename UniqueType>
  1257. void assign(table const& x, UniqueType is_unique)
  1258. {
  1259. typedef
  1260. typename boost::allocator_propagate_on_container_copy_assignment<
  1261. node_allocator_type>::type pocca;
  1262. if (this != &x) {
  1263. assign(x, is_unique, std::integral_constant<bool, pocca::value>());
  1264. }
  1265. }
  1266. template <typename UniqueType>
  1267. void assign(table const& x, UniqueType is_unique, std::false_type)
  1268. {
  1269. // Strong exception safety.
  1270. this->construct_spare_functions(x.current_functions());
  1271. BOOST_TRY
  1272. {
  1273. mlf_ = x.mlf_;
  1274. recalculate_max_load();
  1275. this->reserve_for_insert(x.size_);
  1276. this->clear_impl();
  1277. }
  1278. BOOST_CATCH(...)
  1279. {
  1280. this->cleanup_spare_functions();
  1281. BOOST_RETHROW
  1282. }
  1283. BOOST_CATCH_END
  1284. this->switch_functions();
  1285. copy_buckets(x, is_unique);
  1286. }
  1287. template <typename UniqueType>
  1288. void assign(table const& x, UniqueType is_unique, std::true_type)
  1289. {
  1290. if (node_alloc() == x.node_alloc()) {
  1291. buckets_.reset_allocator(x.node_alloc());
  1292. assign(x, is_unique, std::false_type());
  1293. } else {
  1294. bucket_array_type new_buckets(x.size_, x.node_alloc());
  1295. this->construct_spare_functions(x.current_functions());
  1296. this->switch_functions();
  1297. // Delete everything with current allocators before assigning
  1298. // the new ones.
  1299. delete_buckets();
  1300. buckets_.reset_allocator(x.node_alloc());
  1301. buckets_ = std::move(new_buckets);
  1302. // Copy over other data, all no throw.
  1303. mlf_ = x.mlf_;
  1304. reserve(x.size_);
  1305. // Finally copy the elements.
  1306. if (x.size_) {
  1307. copy_buckets(x, is_unique);
  1308. }
  1309. }
  1310. }
  1311. template <typename UniqueType>
  1312. void move_assign(table& x, UniqueType is_unique)
  1313. {
  1314. if (this != &x) {
  1315. move_assign(x, is_unique,
  1316. std::integral_constant<bool,
  1317. boost::allocator_propagate_on_container_move_assignment<
  1318. node_allocator_type>::type::value>());
  1319. }
  1320. }
  1321. // Propagate allocator
  1322. template <typename UniqueType>
  1323. void move_assign(table& x, UniqueType, std::true_type)
  1324. {
  1325. if (!functions::nothrow_move_assignable) {
  1326. this->construct_spare_functions(x.current_functions());
  1327. this->switch_functions();
  1328. } else {
  1329. this->current_functions().move_assign(x.current_functions());
  1330. }
  1331. delete_buckets();
  1332. buckets_.reset_allocator(x.buckets_.get_node_allocator());
  1333. mlf_ = x.mlf_;
  1334. move_buckets_from(x);
  1335. }
  1336. // Don't propagate allocator
  1337. template <typename UniqueType>
  1338. void move_assign(table& x, UniqueType is_unique, std::false_type)
  1339. {
  1340. if (node_alloc() == x.node_alloc()) {
  1341. move_assign_equal_alloc(x);
  1342. } else {
  1343. move_assign_realloc(x, is_unique);
  1344. }
  1345. }
  1346. void move_assign_equal_alloc(table& x)
  1347. {
  1348. if (!functions::nothrow_move_assignable) {
  1349. this->construct_spare_functions(x.current_functions());
  1350. this->switch_functions();
  1351. } else {
  1352. this->current_functions().move_assign(x.current_functions());
  1353. }
  1354. delete_buckets();
  1355. mlf_ = x.mlf_;
  1356. move_buckets_from(x);
  1357. }
  1358. template <typename UniqueType>
  1359. void move_assign_realloc(table& x, UniqueType is_unique)
  1360. {
  1361. this->construct_spare_functions(x.current_functions());
  1362. BOOST_TRY
  1363. {
  1364. mlf_ = x.mlf_;
  1365. recalculate_max_load();
  1366. if (x.size_ > 0) {
  1367. this->reserve_for_insert(x.size_);
  1368. }
  1369. this->clear_impl();
  1370. }
  1371. BOOST_CATCH(...)
  1372. {
  1373. this->cleanup_spare_functions();
  1374. BOOST_RETHROW
  1375. }
  1376. BOOST_CATCH_END
  1377. this->switch_functions();
  1378. move_assign_buckets(x, is_unique);
  1379. }
  1380. // Accessors
  1381. const_key_type& get_key(node_pointer n) const
  1382. {
  1383. return extractor::extract(n->value());
  1384. }
  1385. template <class Key> std::size_t hash(Key const& k) const
  1386. {
  1387. return this->hash_function()(k);
  1388. }
  1389. // Find Node
  1390. template <class Key>
  1391. node_pointer find_node_impl(Key const& x, bucket_iterator itb) const
  1392. {
  1393. node_pointer p = node_pointer();
  1394. if (itb != buckets_.end()) {
  1395. key_equal const& pred = this->key_eq();
  1396. p = itb->next;
  1397. for (; p; p = p->next) {
  1398. if (pred(x, extractor::extract(p->value()))) {
  1399. break;
  1400. }
  1401. }
  1402. }
  1403. return p;
  1404. }
  1405. template <class Key> node_pointer find_node(Key const& k) const
  1406. {
  1407. std::size_t const key_hash = this->hash(k);
  1408. return find_node_impl(k, buckets_.at(buckets_.position(key_hash)));
  1409. }
  1410. node_pointer find_node(const_key_type& k, bucket_iterator itb) const
  1411. {
  1412. return find_node_impl(k, itb);
  1413. }
  1414. template <class Key> iterator find(Key const& k) const
  1415. {
  1416. return this->transparent_find(
  1417. k, this->hash_function(), this->key_eq());
  1418. }
  1419. template <class Key, class Hash, class Pred>
  1420. inline iterator transparent_find(
  1421. Key const& k, Hash const& h, Pred const& pred) const
  1422. {
  1423. if (size_ > 0) {
  1424. std::size_t const key_hash = h(k);
  1425. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1426. for (node_pointer p = itb->next; p; p = p->next) {
  1427. if (BOOST_LIKELY(pred(k, extractor::extract(p->value())))) {
  1428. return iterator(p, itb);
  1429. }
  1430. }
  1431. }
  1432. return this->end();
  1433. }
  1434. template <class Key>
  1435. node_pointer* find_prev(Key const& key, bucket_iterator itb)
  1436. {
  1437. if (size_ > 0) {
  1438. key_equal pred = this->key_eq();
  1439. for (node_pointer* pp = std::addressof(itb->next); *pp;
  1440. pp = std::addressof((*pp)->next)) {
  1441. if (pred(key, extractor::extract((*pp)->value()))) {
  1442. return pp;
  1443. }
  1444. }
  1445. }
  1446. typedef node_pointer* node_pointer_pointer;
  1447. return node_pointer_pointer();
  1448. }
  1449. // Extract and erase
  1450. template <class Key> node_pointer extract_by_key_impl(Key const& k)
  1451. {
  1452. iterator it = this->find(k);
  1453. if (it == this->end()) {
  1454. return node_pointer();
  1455. }
  1456. buckets_.extract_node(it.itb, it.p);
  1457. --size_;
  1458. return it.p;
  1459. }
  1460. // Reserve and rehash
  1461. void transfer_node(
  1462. node_pointer p, bucket_type&, bucket_array_type& new_buckets)
  1463. {
  1464. const_key_type& key = extractor::extract(p->value());
  1465. std::size_t const h = this->hash(key);
  1466. bucket_iterator itnewb = new_buckets.at(new_buckets.position(h));
  1467. new_buckets.insert_node(itnewb, p);
  1468. }
  1469. static std::size_t min_buckets(std::size_t num_elements, float mlf)
  1470. {
  1471. std::size_t num_buckets = static_cast<std::size_t>(
  1472. std::ceil(static_cast<float>(num_elements) / mlf));
  1473. if (num_buckets == 0 && num_elements > 0) { // mlf == inf
  1474. num_buckets = 1;
  1475. }
  1476. return num_buckets;
  1477. }
  1478. void rehash(std::size_t);
  1479. void reserve(std::size_t);
  1480. void reserve_for_insert(std::size_t);
  1481. void rehash_impl(std::size_t);
  1482. ////////////////////////////////////////////////////////////////////////
  1483. // Unique keys
  1484. // equals
  1485. bool equals_unique(table const& other) const
  1486. {
  1487. if (this->size_ != other.size_)
  1488. return false;
  1489. c_iterator pos = this->begin();
  1490. c_iterator last = this->end();
  1491. while (pos != last) {
  1492. node_pointer p = pos.p;
  1493. node_pointer p2 = other.find_node(this->get_key(p));
  1494. if (!p2 || !(p->value() == p2->value())) {
  1495. return false;
  1496. }
  1497. ++pos;
  1498. }
  1499. return true;
  1500. }
  1501. // Emplace/Insert
  1502. template <typename... Args>
  1503. iterator emplace_hint_unique(
  1504. c_iterator hint, const_key_type& k, Args&&... args)
  1505. {
  1506. if (hint.p && this->key_eq()(k, this->get_key(hint.p))) {
  1507. return iterator(hint.p, hint.itb);
  1508. } else {
  1509. return emplace_unique(k, std::forward<Args>(args)...).first;
  1510. }
  1511. }
  1512. template <typename... Args>
  1513. emplace_return emplace_unique(const_key_type& k, Args&&... args)
  1514. {
  1515. std::size_t key_hash = this->hash(k);
  1516. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1517. node_pointer pos = this->find_node_impl(k, itb);
  1518. if (pos) {
  1519. return emplace_return(iterator(pos, itb), false);
  1520. } else {
  1521. node_tmp b(boost::unordered::detail::func::construct_node_from_args(
  1522. this->node_alloc(), std::forward<Args>(args)...),
  1523. this->node_alloc());
  1524. if (size_ + 1 > max_load_) {
  1525. reserve(size_ + 1);
  1526. itb = buckets_.at(buckets_.position(key_hash));
  1527. }
  1528. node_pointer p = b.release();
  1529. buckets_.insert_node(itb, p);
  1530. ++size_;
  1531. return emplace_return(iterator(p, itb), true);
  1532. }
  1533. }
  1534. template <typename... Args>
  1535. iterator emplace_hint_unique(c_iterator hint, no_key, Args&&... args)
  1536. {
  1537. node_tmp b(boost::unordered::detail::func::construct_node_from_args(
  1538. this->node_alloc(), std::forward<Args>(args)...),
  1539. this->node_alloc());
  1540. const_key_type& k = this->get_key(b.node_);
  1541. if (hint.p && this->key_eq()(k, this->get_key(hint.p))) {
  1542. return iterator(hint.p, hint.itb);
  1543. }
  1544. std::size_t const key_hash = this->hash(k);
  1545. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1546. node_pointer p = this->find_node_impl(k, itb);
  1547. if (p) {
  1548. return iterator(p, itb);
  1549. }
  1550. if (size_ + 1 > max_load_) {
  1551. this->reserve(size_ + 1);
  1552. itb = buckets_.at(buckets_.position(key_hash));
  1553. }
  1554. p = b.release();
  1555. buckets_.insert_node(itb, p);
  1556. ++size_;
  1557. return iterator(p, itb);
  1558. }
  1559. template <typename... Args>
  1560. emplace_return emplace_unique(no_key, Args&&... args)
  1561. {
  1562. node_tmp b(boost::unordered::detail::func::construct_node_from_args(
  1563. this->node_alloc(), std::forward<Args>(args)...),
  1564. this->node_alloc());
  1565. const_key_type& k = this->get_key(b.node_);
  1566. std::size_t key_hash = this->hash(k);
  1567. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1568. node_pointer pos = this->find_node_impl(k, itb);
  1569. if (pos) {
  1570. return emplace_return(iterator(pos, itb), false);
  1571. } else {
  1572. if (size_ + 1 > max_load_) {
  1573. reserve(size_ + 1);
  1574. itb = buckets_.at(buckets_.position(key_hash));
  1575. }
  1576. node_pointer p = b.release();
  1577. buckets_.insert_node(itb, p);
  1578. ++size_;
  1579. return emplace_return(iterator(p, itb), true);
  1580. }
  1581. }
  1582. template <typename K, typename V>
  1583. emplace_return emplace_unique(converting_key, K&& k, V&& v)
  1584. {
  1585. using alloc_cted = allocator_constructed<node_allocator_type,
  1586. typename Types::key_type>;
  1587. alloc_cted key(this->node_alloc(), std::forward<K>(k));
  1588. return emplace_unique(
  1589. key.value(), std::move(key.value()), std::forward<V>(v));
  1590. }
  1591. template <typename Key> emplace_return try_emplace_unique(Key&& k)
  1592. {
  1593. std::size_t key_hash = this->hash(k);
  1594. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1595. node_pointer pos = this->find_node_impl(k, itb);
  1596. if (pos) {
  1597. return emplace_return(iterator(pos, itb), false);
  1598. } else {
  1599. node_allocator_type alloc = node_alloc();
  1600. value_type* dispatch = BOOST_NULLPTR;
  1601. node_tmp tmp(detail::func::construct_node_from_key(
  1602. dispatch, alloc, std::forward<Key>(k)),
  1603. alloc);
  1604. if (size_ + 1 > max_load_) {
  1605. reserve(size_ + 1);
  1606. itb = buckets_.at(buckets_.position(key_hash));
  1607. }
  1608. node_pointer p = tmp.release();
  1609. buckets_.insert_node(itb, p);
  1610. ++size_;
  1611. return emplace_return(iterator(p, itb), true);
  1612. }
  1613. }
  1614. template <typename Key>
  1615. iterator try_emplace_hint_unique(c_iterator hint, Key&& k)
  1616. {
  1617. if (hint.p && this->key_eq()(extractor::extract(*hint), k)) {
  1618. return iterator(hint.p, hint.itb);
  1619. } else {
  1620. return try_emplace_unique(k).first;
  1621. }
  1622. }
  1623. template <typename Key, typename... Args>
  1624. emplace_return try_emplace_unique(Key&& k, Args&&... args)
  1625. {
  1626. std::size_t key_hash = this->hash(k);
  1627. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1628. node_pointer pos = this->find_node_impl(k, itb);
  1629. if (pos) {
  1630. return emplace_return(iterator(pos, itb), false);
  1631. }
  1632. node_tmp b(
  1633. boost::unordered::detail::func::construct_node_pair_from_args(
  1634. this->node_alloc(), k, std::forward<Args>(args)...),
  1635. this->node_alloc());
  1636. if (size_ + 1 > max_load_) {
  1637. reserve(size_ + 1);
  1638. itb = buckets_.at(buckets_.position(key_hash));
  1639. }
  1640. pos = b.release();
  1641. buckets_.insert_node(itb, pos);
  1642. ++size_;
  1643. return emplace_return(iterator(pos, itb), true);
  1644. }
  1645. template <typename Key, typename... Args>
  1646. iterator try_emplace_hint_unique(
  1647. c_iterator hint, Key&& k, Args&&... args)
  1648. {
  1649. if (hint.p && this->key_eq()(hint->first, k)) {
  1650. return iterator(hint.p, hint.itb);
  1651. } else {
  1652. return try_emplace_unique(k, std::forward<Args>(args)...).first;
  1653. }
  1654. }
  1655. template <typename Key, typename M>
  1656. emplace_return insert_or_assign_unique(Key&& k, M&& obj)
  1657. {
  1658. std::size_t key_hash = this->hash(k);
  1659. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1660. node_pointer p = this->find_node_impl(k, itb);
  1661. if (p) {
  1662. p->value().second = std::forward<M>(obj);
  1663. return emplace_return(iterator(p, itb), false);
  1664. }
  1665. node_tmp b(
  1666. boost::unordered::detail::func::construct_node_pair(
  1667. this->node_alloc(), std::forward<Key>(k), std::forward<M>(obj)),
  1668. node_alloc());
  1669. if (size_ + 1 > max_load_) {
  1670. reserve(size_ + 1);
  1671. itb = buckets_.at(buckets_.position(key_hash));
  1672. }
  1673. p = b.release();
  1674. buckets_.insert_node(itb, p);
  1675. ++size_;
  1676. return emplace_return(iterator(p, itb), true);
  1677. }
  1678. template <typename NodeType, typename InsertReturnType>
  1679. void move_insert_node_type_unique(
  1680. NodeType& np, InsertReturnType& result)
  1681. {
  1682. if (!np) {
  1683. result.position = this->end();
  1684. result.inserted = false;
  1685. return;
  1686. }
  1687. const_key_type& k = this->get_key(np.ptr_);
  1688. std::size_t const key_hash = this->hash(k);
  1689. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1690. node_pointer p = this->find_node_impl(k, itb);
  1691. if (p) {
  1692. iterator pos(p, itb);
  1693. result.node = std::move(np);
  1694. result.position = pos;
  1695. result.inserted = false;
  1696. return;
  1697. }
  1698. this->reserve_for_insert(size_ + 1);
  1699. p = np.ptr_;
  1700. itb = buckets_.at(buckets_.position(key_hash));
  1701. buckets_.insert_node(itb, p);
  1702. np.ptr_ = node_pointer();
  1703. ++size_;
  1704. result.position = iterator(p, itb);
  1705. result.inserted = true;
  1706. }
  1707. template <typename NodeType>
  1708. iterator move_insert_node_type_with_hint_unique(
  1709. c_iterator hint, NodeType& np)
  1710. {
  1711. if (!np) {
  1712. return this->end();
  1713. }
  1714. const_key_type& k = this->get_key(np.ptr_);
  1715. if (hint.p && this->key_eq()(k, this->get_key(hint.p))) {
  1716. return iterator(hint.p, hint.itb);
  1717. }
  1718. std::size_t const key_hash = this->hash(k);
  1719. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1720. node_pointer p = this->find_node_impl(k, itb);
  1721. if (p) {
  1722. return iterator(p, itb);
  1723. }
  1724. p = np.ptr_;
  1725. if (size_ + 1 > max_load_) {
  1726. this->reserve(size_ + 1);
  1727. itb = buckets_.at(buckets_.position(key_hash));
  1728. }
  1729. buckets_.insert_node(itb, p);
  1730. ++size_;
  1731. np.ptr_ = node_pointer();
  1732. return iterator(p, itb);
  1733. }
  1734. template <typename Types2>
  1735. void merge_unique(boost::unordered::detail::table<Types2>& other)
  1736. {
  1737. typedef boost::unordered::detail::table<Types2> other_table;
  1738. BOOST_UNORDERED_STATIC_ASSERT(
  1739. (std::is_same<node_type, typename other_table::node_type>::value));
  1740. BOOST_ASSERT(this->node_alloc() == other.node_alloc());
  1741. if (other.size_ == 0) {
  1742. return;
  1743. }
  1744. this->reserve_for_insert(size_ + other.size_);
  1745. iterator last = other.end();
  1746. for (iterator pos = other.begin(); pos != last;) {
  1747. const_key_type& key = other.get_key(pos.p);
  1748. std::size_t const key_hash = this->hash(key);
  1749. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1750. if (this->find_node_impl(key, itb)) {
  1751. ++pos;
  1752. continue;
  1753. }
  1754. iterator old = pos;
  1755. ++pos;
  1756. node_pointer p = other.extract_by_iterator_unique(old);
  1757. buckets_.insert_node(itb, p);
  1758. ++size_;
  1759. }
  1760. }
  1761. ////////////////////////////////////////////////////////////////////////
  1762. // Insert range methods
  1763. //
  1764. // if hash function throws, or inserting > 1 element, basic exception
  1765. // safety strong otherwise
  1766. template <class InputIt>
  1767. void insert_range_unique(no_key, InputIt i, InputIt j)
  1768. {
  1769. hasher const& hf = this->hash_function();
  1770. node_allocator_type alloc = this->node_alloc();
  1771. for (; i != j; ++i) {
  1772. node_tmp tmp(detail::func::construct_node(alloc, *i), alloc);
  1773. value_type const& value = tmp.node_->value();
  1774. const_key_type& key = extractor::extract(value);
  1775. std::size_t const h = hf(key);
  1776. bucket_iterator itb = buckets_.at(buckets_.position(h));
  1777. node_pointer it = find_node_impl(key, itb);
  1778. if (it) {
  1779. continue;
  1780. }
  1781. if (size_ + 1 > max_load_) {
  1782. reserve(size_ + 1);
  1783. itb = buckets_.at(buckets_.position(h));
  1784. }
  1785. node_pointer nptr = tmp.release();
  1786. buckets_.insert_node(itb, nptr);
  1787. ++size_;
  1788. }
  1789. }
  1790. ////////////////////////////////////////////////////////////////////////
  1791. // Extract
  1792. inline node_pointer extract_by_iterator_unique(c_iterator i)
  1793. {
  1794. node_pointer p = i.p;
  1795. bucket_iterator itb = i.itb;
  1796. buckets_.extract_node(itb, p);
  1797. --size_;
  1798. return p;
  1799. }
  1800. ////////////////////////////////////////////////////////////////////////
  1801. // Erase
  1802. //
  1803. template <class Key> std::size_t erase_key_unique_impl(Key const& k)
  1804. {
  1805. bucket_iterator itb = buckets_.at(buckets_.position(this->hash(k)));
  1806. node_pointer* pp = this->find_prev(k, itb);
  1807. if (!pp) {
  1808. return 0;
  1809. }
  1810. node_pointer p = *pp;
  1811. buckets_.extract_node_after(itb, pp);
  1812. this->delete_node(p);
  1813. --size_;
  1814. return 1;
  1815. }
  1816. iterator erase_node(c_iterator pos)
  1817. {
  1818. c_iterator next = pos;
  1819. ++next;
  1820. bucket_iterator itb = pos.itb;
  1821. node_pointer* pp = std::addressof(itb->next);
  1822. while (*pp != pos.p) {
  1823. pp = std::addressof((*pp)->next);
  1824. }
  1825. buckets_.extract_node_after(itb, pp);
  1826. this->delete_node(pos.p);
  1827. --size_;
  1828. return iterator(next.p, next.itb);
  1829. }
  1830. iterator erase_nodes_range(c_iterator first, c_iterator last)
  1831. {
  1832. if (first == last) {
  1833. return iterator(last.p, last.itb);
  1834. }
  1835. // though `first` stores of a copy of a pointer to a node, we wish to
  1836. // mutate the pointers stored internally by the singly-linked list in
  1837. // each bucket group so we have to retrieve it manually by iterating
  1838. //
  1839. bucket_iterator itb = first.itb;
  1840. node_pointer* pp = std::addressof(itb->next);
  1841. while (*pp != first.p) {
  1842. pp = std::addressof((*pp)->next);
  1843. }
  1844. while (*pp != last.p) {
  1845. node_pointer p = *pp;
  1846. *pp = (*pp)->next;
  1847. this->delete_node(p);
  1848. --size_;
  1849. bool const at_end = !(*pp);
  1850. bool const is_empty_bucket = !itb->next;
  1851. if (at_end) {
  1852. if (is_empty_bucket) {
  1853. buckets_.unlink_bucket(itb++);
  1854. } else {
  1855. ++itb;
  1856. }
  1857. pp = std::addressof(itb->next);
  1858. }
  1859. }
  1860. return iterator(last.p, last.itb);
  1861. }
  1862. ////////////////////////////////////////////////////////////////////////
  1863. // fill_buckets_unique
  1864. void copy_buckets(table const& src, std::true_type)
  1865. {
  1866. BOOST_ASSERT(size_ == 0);
  1867. this->reserve_for_insert(src.size_);
  1868. for (iterator pos = src.begin(); pos != src.end(); ++pos) {
  1869. value_type const& value = *pos;
  1870. const_key_type& key = extractor::extract(value);
  1871. std::size_t const key_hash = this->hash(key);
  1872. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1873. node_allocator_type alloc = this->node_alloc();
  1874. node_tmp tmp(detail::func::construct_node(alloc, value), alloc);
  1875. buckets_.insert_node(itb, tmp.release());
  1876. ++size_;
  1877. }
  1878. }
  1879. void move_assign_buckets(table& src, std::true_type)
  1880. {
  1881. BOOST_ASSERT(size_ == 0);
  1882. BOOST_ASSERT(max_load_ >= src.size_);
  1883. iterator last = src.end();
  1884. node_allocator_type alloc = this->node_alloc();
  1885. for (iterator pos = src.begin(); pos != last; ++pos) {
  1886. value_type value = std::move(*pos);
  1887. const_key_type& key = extractor::extract(value);
  1888. std::size_t const key_hash = this->hash(key);
  1889. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1890. node_tmp tmp(
  1891. detail::func::construct_node(alloc, std::move(value)), alloc);
  1892. buckets_.insert_node(itb, tmp.release());
  1893. ++size_;
  1894. }
  1895. }
  1896. ////////////////////////////////////////////////////////////////////////
  1897. // Equivalent keys
  1898. // Equality
  1899. bool equals_equiv(table const& other) const
  1900. {
  1901. if (this->size_ != other.size_)
  1902. return false;
  1903. iterator last = this->end();
  1904. for (iterator n1 = this->begin(); n1 != last;) {
  1905. const_key_type& k = extractor::extract(*n1);
  1906. iterator n2 = other.find(k);
  1907. if (n2 == other.end()) {
  1908. return false;
  1909. }
  1910. iterator end1 = this->next_group(k, n1);
  1911. iterator end2 = other.next_group(k, n2);
  1912. if (!group_equals_equiv(n1, end1, n2, end2)) {
  1913. return false;
  1914. }
  1915. n1 = end1;
  1916. }
  1917. return true;
  1918. }
  1919. static bool group_equals_equiv(
  1920. iterator n1, iterator end1, iterator n2, iterator end2)
  1921. {
  1922. for (;;) {
  1923. if (*n1 != *n2)
  1924. break;
  1925. ++n1;
  1926. ++n2;
  1927. if (n1 == end1)
  1928. return n2 == end2;
  1929. if (n2 == end2)
  1930. return false;
  1931. }
  1932. for (iterator n1a = n1, n2a = n2;;) {
  1933. ++n1a;
  1934. ++n2a;
  1935. if (n1a == end1) {
  1936. if (n2a == end2)
  1937. break;
  1938. else
  1939. return false;
  1940. }
  1941. if (n2a == end2)
  1942. return false;
  1943. }
  1944. iterator start = n1;
  1945. for (; n1 != end1; ++n1) {
  1946. value_type const& v = *n1;
  1947. if (!find_equiv(start, n1, v)) {
  1948. std::size_t matches = count_equal_equiv(n2, end2, v);
  1949. if (!matches)
  1950. return false;
  1951. iterator t = n1;
  1952. if (matches != 1 + count_equal_equiv(++t, end1, v))
  1953. return false;
  1954. }
  1955. }
  1956. return true;
  1957. }
  1958. static bool find_equiv(iterator n, iterator last, value_type const& v)
  1959. {
  1960. for (; n != last; ++n)
  1961. if (*n == v)
  1962. return true;
  1963. return false;
  1964. }
  1965. static std::size_t count_equal_equiv(
  1966. iterator n, iterator last, value_type const& v)
  1967. {
  1968. std::size_t count = 0;
  1969. for (; n != last; ++n)
  1970. if (*n == v)
  1971. ++count;
  1972. return count;
  1973. }
  1974. // Emplace/Insert
  1975. iterator emplace_equiv(node_pointer n)
  1976. {
  1977. node_tmp a(n, this->node_alloc());
  1978. const_key_type& k = this->get_key(a.node_);
  1979. std::size_t key_hash = this->hash(k);
  1980. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  1981. node_pointer hint = this->find_node_impl(k, itb);
  1982. if (size_ + 1 > max_load_) {
  1983. this->reserve(size_ + 1);
  1984. itb = buckets_.at(buckets_.position(key_hash));
  1985. }
  1986. node_pointer p = a.release();
  1987. buckets_.insert_node_hint(itb, p, hint);
  1988. ++size_;
  1989. return iterator(p, itb);
  1990. }
  1991. iterator emplace_hint_equiv(c_iterator hint, node_pointer n)
  1992. {
  1993. node_tmp a(n, this->node_alloc());
  1994. const_key_type& k = this->get_key(a.node_);
  1995. bucket_iterator itb = hint.itb;
  1996. node_pointer p = hint.p;
  1997. std::size_t key_hash = 0u;
  1998. bool const needs_rehash = (size_ + 1 > max_load_);
  1999. bool const usable_hint = (p && this->key_eq()(k, this->get_key(p)));
  2000. if (!usable_hint) {
  2001. key_hash = this->hash(k);
  2002. itb = buckets_.at(buckets_.position(key_hash));
  2003. p = this->find_node_impl(k, itb);
  2004. } else if (usable_hint && needs_rehash) {
  2005. key_hash = this->hash(k);
  2006. }
  2007. if (needs_rehash) {
  2008. this->reserve(size_ + 1);
  2009. itb = buckets_.at(buckets_.position(key_hash));
  2010. }
  2011. a.release();
  2012. buckets_.insert_node_hint(itb, n, p);
  2013. ++size_;
  2014. return iterator(n, itb);
  2015. }
  2016. void emplace_no_rehash_equiv(node_pointer n)
  2017. {
  2018. BOOST_ASSERT(size_ + 1 <= max_load_);
  2019. node_tmp a(n, this->node_alloc());
  2020. const_key_type& k = this->get_key(a.node_);
  2021. std::size_t key_hash = this->hash(k);
  2022. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  2023. node_pointer hint = this->find_node_impl(k, itb);
  2024. node_pointer p = a.release();
  2025. buckets_.insert_node_hint(itb, p, hint);
  2026. ++size_;
  2027. }
  2028. template <typename NodeType>
  2029. iterator move_insert_node_type_equiv(NodeType& np)
  2030. {
  2031. iterator result;
  2032. if (np) {
  2033. this->reserve_for_insert(size_ + 1);
  2034. const_key_type& k = this->get_key(np.ptr_);
  2035. std::size_t key_hash = this->hash(k);
  2036. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  2037. node_pointer hint = this->find_node_impl(k, itb);
  2038. buckets_.insert_node_hint(itb, np.ptr_, hint);
  2039. ++size_;
  2040. result = iterator(np.ptr_, itb);
  2041. np.ptr_ = node_pointer();
  2042. }
  2043. return result;
  2044. }
  2045. template <typename NodeType>
  2046. iterator move_insert_node_type_with_hint_equiv(
  2047. c_iterator hint, NodeType& np)
  2048. {
  2049. iterator result;
  2050. if (np) {
  2051. bucket_iterator itb = hint.itb;
  2052. node_pointer pos = hint.p;
  2053. const_key_type& k = this->get_key(np.ptr_);
  2054. std::size_t key_hash = this->hash(k);
  2055. if (size_ + 1 > max_load_) {
  2056. this->reserve(size_ + 1);
  2057. itb = buckets_.at(buckets_.position(key_hash));
  2058. }
  2059. if (hint.p && this->key_eq()(k, this->get_key(hint.p))) {
  2060. } else {
  2061. itb = buckets_.at(buckets_.position(key_hash));
  2062. pos = this->find_node_impl(k, itb);
  2063. }
  2064. buckets_.insert_node_hint(itb, np.ptr_, pos);
  2065. ++size_;
  2066. result = iterator(np.ptr_, itb);
  2067. np.ptr_ = node_pointer();
  2068. }
  2069. return result;
  2070. }
  2071. ////////////////////////////////////////////////////////////////////////
  2072. // Insert range methods
  2073. // if hash function throws, or inserting > 1 element, basic exception
  2074. // safety. Strong otherwise
  2075. template <class I>
  2076. typename boost::unordered::detail::enable_if_forward<I, void>::type
  2077. insert_range_equiv(I i, I j)
  2078. {
  2079. if (i == j)
  2080. return;
  2081. std::size_t distance = static_cast<std::size_t>(std::distance(i, j));
  2082. if (distance == 1) {
  2083. emplace_equiv(boost::unordered::detail::func::construct_node(
  2084. this->node_alloc(), *i));
  2085. } else {
  2086. // Only require basic exception safety here
  2087. this->reserve_for_insert(size_ + distance);
  2088. for (; i != j; ++i) {
  2089. emplace_no_rehash_equiv(
  2090. boost::unordered::detail::func::construct_node(
  2091. this->node_alloc(), *i));
  2092. }
  2093. }
  2094. }
  2095. template <class I>
  2096. typename boost::unordered::detail::disable_if_forward<I, void>::type
  2097. insert_range_equiv(I i, I j)
  2098. {
  2099. for (; i != j; ++i) {
  2100. emplace_equiv(boost::unordered::detail::func::construct_node(
  2101. this->node_alloc(), *i));
  2102. }
  2103. }
  2104. ////////////////////////////////////////////////////////////////////////
  2105. // Extract
  2106. inline node_pointer extract_by_iterator_equiv(c_iterator n)
  2107. {
  2108. node_pointer p = n.p;
  2109. bucket_iterator itb = n.itb;
  2110. buckets_.extract_node(itb, p);
  2111. --size_;
  2112. return p;
  2113. }
  2114. ////////////////////////////////////////////////////////////////////////
  2115. // Erase
  2116. //
  2117. // no throw
  2118. template <class Key> std::size_t erase_key_equiv_impl(Key const& k)
  2119. {
  2120. std::size_t deleted_count = 0;
  2121. bucket_iterator itb = buckets_.at(buckets_.position(this->hash(k)));
  2122. node_pointer* pp = this->find_prev(k, itb);
  2123. if (pp) {
  2124. while (*pp && this->key_eq()(this->get_key(*pp), k)) {
  2125. node_pointer p = *pp;
  2126. *pp = (*pp)->next;
  2127. this->delete_node(p);
  2128. --size_;
  2129. ++deleted_count;
  2130. }
  2131. if (!itb->next) {
  2132. buckets_.unlink_bucket(itb);
  2133. }
  2134. }
  2135. return deleted_count;
  2136. }
  2137. std::size_t erase_key_equiv(const_key_type& k)
  2138. {
  2139. return this->erase_key_equiv_impl(k);
  2140. }
  2141. ////////////////////////////////////////////////////////////////////////
  2142. // fill_buckets
  2143. void copy_buckets(table const& src, std::false_type)
  2144. {
  2145. BOOST_ASSERT(size_ == 0);
  2146. this->reserve_for_insert(src.size_);
  2147. iterator last = src.end();
  2148. for (iterator pos = src.begin(); pos != last; ++pos) {
  2149. value_type const& value = *pos;
  2150. const_key_type& key = extractor::extract(value);
  2151. std::size_t const key_hash = this->hash(key);
  2152. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  2153. node_allocator_type alloc = this->node_alloc();
  2154. node_tmp tmp(detail::func::construct_node(alloc, value), alloc);
  2155. node_pointer hint = this->find_node_impl(key, itb);
  2156. buckets_.insert_node_hint(itb, tmp.release(), hint);
  2157. ++size_;
  2158. }
  2159. }
  2160. void move_assign_buckets(table& src, std::false_type)
  2161. {
  2162. BOOST_ASSERT(size_ == 0);
  2163. BOOST_ASSERT(max_load_ >= src.size_);
  2164. iterator last = src.end();
  2165. node_allocator_type alloc = this->node_alloc();
  2166. for (iterator pos = src.begin(); pos != last; ++pos) {
  2167. value_type value = std::move(*pos);
  2168. const_key_type& key = extractor::extract(value);
  2169. std::size_t const key_hash = this->hash(key);
  2170. bucket_iterator itb = buckets_.at(buckets_.position(key_hash));
  2171. node_pointer hint = this->find_node_impl(key, itb);
  2172. node_tmp tmp(
  2173. detail::func::construct_node(alloc, std::move(value)), alloc);
  2174. buckets_.insert_node_hint(itb, tmp.release(), hint);
  2175. ++size_;
  2176. }
  2177. }
  2178. };
  2179. //////////////////////////////////////////////////////////////////////////
  2180. // Clear
  2181. template <typename Types> inline void table<Types>::clear_impl()
  2182. {
  2183. bucket_iterator itb = buckets_.begin(), last = buckets_.end();
  2184. for (; itb != last;) {
  2185. bucket_iterator next_itb = itb;
  2186. ++next_itb;
  2187. node_pointer* pp = std::addressof(itb->next);
  2188. while (*pp) {
  2189. node_pointer p = *pp;
  2190. buckets_.extract_node_after(itb, pp);
  2191. this->delete_node(p);
  2192. --size_;
  2193. }
  2194. itb = next_itb;
  2195. }
  2196. }
  2197. //////////////////////////////////////////////////////////////////////////
  2198. // Reserve & Rehash
  2199. // if hash function throws, basic exception safety
  2200. // strong otherwise.
  2201. template <typename Types>
  2202. inline void table<Types>::rehash(std::size_t num_buckets)
  2203. {
  2204. num_buckets = buckets_.bucket_count_for(
  2205. (std::max)(min_buckets(size_, mlf_), num_buckets));
  2206. if (num_buckets != this->bucket_count()) {
  2207. this->rehash_impl(num_buckets);
  2208. }
  2209. }
  2210. template <class Types>
  2211. inline void table<Types>::reserve(std::size_t num_elements)
  2212. {
  2213. std::size_t num_buckets = min_buckets(num_elements, mlf_);
  2214. this->rehash(num_buckets);
  2215. }
  2216. template <class Types>
  2217. inline void table<Types>::reserve_for_insert(std::size_t num_elements)
  2218. {
  2219. if (num_elements > max_load_) {
  2220. std::size_t const num_buckets = static_cast<std::size_t>(
  2221. 1.0f + std::ceil(static_cast<float>(num_elements) / mlf_));
  2222. this->rehash_impl(num_buckets);
  2223. }
  2224. }
  2225. template <class Types>
  2226. inline void table<Types>::rehash_impl(std::size_t num_buckets)
  2227. {
  2228. bucket_array_type new_buckets(
  2229. num_buckets, buckets_.get_allocator());
  2230. BOOST_TRY
  2231. {
  2232. boost::unordered::detail::span<bucket_type> bspan = buckets_.raw();
  2233. bucket_type* pos = bspan.data;
  2234. std::size_t size = bspan.size;
  2235. bucket_type* last = pos + size;
  2236. for (; pos != last; ++pos) {
  2237. bucket_type& b = *pos;
  2238. for (node_pointer p = b.next; p;) {
  2239. node_pointer next_p = p->next;
  2240. transfer_node(p, b, new_buckets);
  2241. p = next_p;
  2242. b.next = p;
  2243. }
  2244. }
  2245. }
  2246. BOOST_CATCH(...)
  2247. {
  2248. for (bucket_iterator pos = new_buckets.begin();
  2249. pos != new_buckets.end(); ++pos) {
  2250. bucket_type& b = *pos;
  2251. for (node_pointer p = b.next; p;) {
  2252. node_pointer next_p = p->next;
  2253. delete_node(p);
  2254. --size_;
  2255. p = next_p;
  2256. }
  2257. }
  2258. buckets_.unlink_empty_buckets();
  2259. BOOST_RETHROW
  2260. }
  2261. BOOST_CATCH_END
  2262. buckets_ = std::move(new_buckets);
  2263. recalculate_max_load();
  2264. }
  2265. #if defined(BOOST_MSVC)
  2266. #pragma warning(pop)
  2267. #endif
  2268. ////////////////////////////////////////////////////////////////////////
  2269. // key extractors
  2270. //
  2271. // no throw
  2272. //
  2273. // 'extract_key' is called with the emplace parameters to return a
  2274. // key if available or 'no_key' is one isn't and will need to be
  2275. // constructed. This could be done by overloading the emplace
  2276. // implementation
  2277. // for the different cases, but that's a bit tricky on compilers without
  2278. // variadic templates.
  2279. template <typename Key, typename T> struct is_key
  2280. {
  2281. template <typename T2> static choice1::type test(T2 const&);
  2282. static choice2::type test(Key const&);
  2283. enum
  2284. {
  2285. value = sizeof(test(boost::unordered::detail::make<T>())) ==
  2286. sizeof(choice2::type)
  2287. };
  2288. typedef typename std::conditional<value, Key const&, no_key>::type type;
  2289. };
  2290. template <class ValueType> struct set_extractor
  2291. {
  2292. typedef ValueType value_type;
  2293. typedef ValueType key_type;
  2294. static key_type const& extract(value_type const& v) { return v; }
  2295. static key_type const& extract(value_type&& v) { return v; }
  2296. static no_key extract() { return no_key(); }
  2297. template <class Arg> static no_key extract(Arg const&)
  2298. {
  2299. return no_key();
  2300. }
  2301. template <class Arg1, class Arg2, class... Args>
  2302. static no_key extract(Arg1 const&, Arg2 const&, Args const&...)
  2303. {
  2304. return no_key();
  2305. }
  2306. };
  2307. template <class ValueType> struct map_extractor
  2308. {
  2309. typedef ValueType value_type;
  2310. typedef typename std::remove_const<typename boost::unordered::detail::
  2311. pair_traits<ValueType>::first_type>::type key_type;
  2312. static key_type const& extract(value_type const& v) { return v.first; }
  2313. template <class Second>
  2314. static key_type const& extract(std::pair<key_type, Second> const& v)
  2315. {
  2316. return v.first;
  2317. }
  2318. template <class Second>
  2319. static key_type const& extract(
  2320. std::pair<key_type const, Second> const& v)
  2321. {
  2322. return v.first;
  2323. }
  2324. template <class Arg1>
  2325. static key_type const& extract(key_type const& k, Arg1 const&)
  2326. {
  2327. return k;
  2328. }
  2329. static no_key extract() { return no_key(); }
  2330. template <class Arg> static no_key extract(Arg const&)
  2331. {
  2332. return no_key();
  2333. }
  2334. template <class Arg1, class Arg2>
  2335. static typename std::conditional<
  2336. (is_similar<Arg1, key_type>::value ||
  2337. is_complete_and_move_constructible<key_type>::value),
  2338. converting_key, no_key>::type
  2339. extract(Arg1 const&, Arg2 const&)
  2340. {
  2341. return {};
  2342. }
  2343. template <class Arg1, class Arg2, class Arg3, class... Args>
  2344. static no_key extract(
  2345. Arg1 const&, Arg2 const&, Arg3 const&, Args const&...)
  2346. {
  2347. return no_key();
  2348. }
  2349. template <template <class...> class Tuple, typename T2>
  2350. static no_key extract(
  2351. std::piecewise_construct_t, Tuple<> const&, T2 const&)
  2352. {
  2353. return no_key();
  2354. }
  2355. template <template <typename...> class Tuple, typename T, typename T2,
  2356. typename... Args>
  2357. static auto extract(
  2358. std::piecewise_construct_t, Tuple<T, Args...> const& k, T2 const&) ->
  2359. typename std::enable_if<
  2360. !std::is_same<T, boost::tuples::null_type>::value,
  2361. typename is_key<key_type, T>::type>::type
  2362. {
  2363. using std::get;
  2364. return typename is_key<key_type, T>::type(get<0>(k));
  2365. }
  2366. };
  2367. template <class Container, class Predicate>
  2368. typename Container::size_type erase_if(Container& c, Predicate& pred)
  2369. {
  2370. typedef typename Container::size_type size_type;
  2371. typedef typename Container::iterator iterator;
  2372. size_type const size = c.size();
  2373. for (iterator pos = c.begin(), last = c.end(); pos != last;) {
  2374. if (pred(*pos)) {
  2375. pos = c.erase(pos);
  2376. } else {
  2377. ++pos;
  2378. }
  2379. }
  2380. return (size - c.size());
  2381. }
  2382. } // namespace detail
  2383. } // namespace unordered
  2384. } // namespace boost
  2385. #endif