implementation.hpp 90 KB

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