unordered_node_map.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. // Copyright (C) 2022-2023 Christian Mazakas
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_UNORDERED_UNORDERED_NODE_MAP_HPP_INCLUDED
  5. #define BOOST_UNORDERED_UNORDERED_NODE_MAP_HPP_INCLUDED
  6. #include <boost/config.hpp>
  7. #if defined(BOOST_HAS_PRAGMA_ONCE)
  8. #pragma once
  9. #endif
  10. #include <boost/unordered/detail/foa/element_type.hpp>
  11. #include <boost/unordered/detail/foa/node_handle.hpp>
  12. #include <boost/unordered/detail/foa/node_map_types.hpp>
  13. #include <boost/unordered/detail/foa/table.hpp>
  14. #include <boost/unordered/detail/serialize_container.hpp>
  15. #include <boost/unordered/detail/type_traits.hpp>
  16. #include <boost/unordered/unordered_node_map_fwd.hpp>
  17. #include <boost/core/allocator_access.hpp>
  18. #include <boost/container_hash/hash.hpp>
  19. #include <boost/throw_exception.hpp>
  20. #include <initializer_list>
  21. #include <iterator>
  22. #include <stdexcept>
  23. #include <type_traits>
  24. #include <utility>
  25. namespace boost {
  26. namespace unordered {
  27. #if defined(BOOST_MSVC)
  28. #pragma warning(push)
  29. #pragma warning(disable : 4714) /* marked as __forceinline not inlined */
  30. #endif
  31. namespace detail {
  32. template <class TypePolicy, class Allocator>
  33. struct node_map_handle
  34. : public detail::foa::node_handle_base<TypePolicy, Allocator>
  35. {
  36. private:
  37. using base_type = detail::foa::node_handle_base<TypePolicy, Allocator>;
  38. using typename base_type::type_policy;
  39. template <class Key, class T, class Hash, class Pred, class Alloc>
  40. friend class boost::unordered::unordered_node_map;
  41. public:
  42. using key_type = typename TypePolicy::key_type;
  43. using mapped_type = typename TypePolicy::mapped_type;
  44. constexpr node_map_handle() noexcept = default;
  45. node_map_handle(node_map_handle&& nh) noexcept = default;
  46. node_map_handle& operator=(node_map_handle&&) noexcept = default;
  47. key_type& key() const
  48. {
  49. BOOST_ASSERT(!this->empty());
  50. return const_cast<key_type&>(this->data().first);
  51. }
  52. mapped_type& mapped() const
  53. {
  54. BOOST_ASSERT(!this->empty());
  55. return const_cast<mapped_type&>(this->data().second);
  56. }
  57. };
  58. } // namespace detail
  59. template <class Key, class T, class Hash, class KeyEqual, class Allocator>
  60. class unordered_node_map
  61. {
  62. using map_types = detail::foa::node_map_types<Key, T,
  63. typename boost::allocator_void_pointer<Allocator>::type>;
  64. using table_type = detail::foa::table<map_types, Hash, KeyEqual,
  65. typename boost::allocator_rebind<Allocator,
  66. std::pair<Key const, T> >::type>;
  67. table_type table_;
  68. template <class K, class V, class H, class KE, class A>
  69. bool friend operator==(unordered_node_map<K, V, H, KE, A> const& lhs,
  70. unordered_node_map<K, V, H, KE, A> const& rhs);
  71. template <class K, class V, class H, class KE, class A, class Pred>
  72. typename unordered_node_map<K, V, H, KE, A>::size_type friend erase_if(
  73. unordered_node_map<K, V, H, KE, A>& set, Pred pred);
  74. public:
  75. using key_type = Key;
  76. using mapped_type = T;
  77. using value_type = typename map_types::value_type;
  78. using init_type = typename map_types::init_type;
  79. using size_type = std::size_t;
  80. using difference_type = std::ptrdiff_t;
  81. using hasher = typename boost::unordered::detail::type_identity<Hash>::type;
  82. using key_equal = typename boost::unordered::detail::type_identity<KeyEqual>::type;
  83. using allocator_type = typename boost::unordered::detail::type_identity<Allocator>::type;
  84. using reference = value_type&;
  85. using const_reference = value_type const&;
  86. using pointer = typename boost::allocator_pointer<allocator_type>::type;
  87. using const_pointer =
  88. typename boost::allocator_const_pointer<allocator_type>::type;
  89. using iterator = typename table_type::iterator;
  90. using const_iterator = typename table_type::const_iterator;
  91. using node_type = detail::node_map_handle<map_types,
  92. typename boost::allocator_rebind<Allocator,
  93. typename map_types::value_type>::type>;
  94. using insert_return_type =
  95. detail::foa::insert_return_type<iterator, node_type>;
  96. unordered_node_map() : unordered_node_map(0) {}
  97. explicit unordered_node_map(size_type n, hasher const& h = hasher(),
  98. key_equal const& pred = key_equal(),
  99. allocator_type const& a = allocator_type())
  100. : table_(n, h, pred, a)
  101. {
  102. }
  103. unordered_node_map(size_type n, allocator_type const& a)
  104. : unordered_node_map(n, hasher(), key_equal(), a)
  105. {
  106. }
  107. unordered_node_map(size_type n, hasher const& h, allocator_type const& a)
  108. : unordered_node_map(n, h, key_equal(), a)
  109. {
  110. }
  111. template <class InputIterator>
  112. unordered_node_map(
  113. InputIterator f, InputIterator l, allocator_type const& a)
  114. : unordered_node_map(f, l, size_type(0), hasher(), key_equal(), a)
  115. {
  116. }
  117. explicit unordered_node_map(allocator_type const& a)
  118. : unordered_node_map(0, a)
  119. {
  120. }
  121. template <class Iterator>
  122. unordered_node_map(Iterator first, Iterator last, size_type n = 0,
  123. hasher const& h = hasher(), key_equal const& pred = key_equal(),
  124. allocator_type const& a = allocator_type())
  125. : unordered_node_map(n, h, pred, a)
  126. {
  127. this->insert(first, last);
  128. }
  129. template <class Iterator>
  130. unordered_node_map(
  131. Iterator first, Iterator last, size_type n, allocator_type const& a)
  132. : unordered_node_map(first, last, n, hasher(), key_equal(), a)
  133. {
  134. }
  135. template <class Iterator>
  136. unordered_node_map(Iterator first, Iterator last, size_type n,
  137. hasher const& h, allocator_type const& a)
  138. : unordered_node_map(first, last, n, h, key_equal(), a)
  139. {
  140. }
  141. unordered_node_map(unordered_node_map const& other) : table_(other.table_)
  142. {
  143. }
  144. unordered_node_map(
  145. unordered_node_map const& other, allocator_type const& a)
  146. : table_(other.table_, a)
  147. {
  148. }
  149. unordered_node_map(unordered_node_map&& other)
  150. noexcept(std::is_nothrow_move_constructible<table_type>::value)
  151. : table_(std::move(other.table_))
  152. {
  153. }
  154. unordered_node_map(unordered_node_map&& other, allocator_type const& al)
  155. : table_(std::move(other.table_), al)
  156. {
  157. }
  158. unordered_node_map(std::initializer_list<value_type> ilist,
  159. size_type n = 0, hasher const& h = hasher(),
  160. key_equal const& pred = key_equal(),
  161. allocator_type const& a = allocator_type())
  162. : unordered_node_map(ilist.begin(), ilist.end(), n, h, pred, a)
  163. {
  164. }
  165. unordered_node_map(
  166. std::initializer_list<value_type> il, allocator_type const& a)
  167. : unordered_node_map(il, size_type(0), hasher(), key_equal(), a)
  168. {
  169. }
  170. unordered_node_map(std::initializer_list<value_type> init, size_type n,
  171. allocator_type const& a)
  172. : unordered_node_map(init, n, hasher(), key_equal(), a)
  173. {
  174. }
  175. unordered_node_map(std::initializer_list<value_type> init, size_type n,
  176. hasher const& h, allocator_type const& a)
  177. : unordered_node_map(init, n, h, key_equal(), a)
  178. {
  179. }
  180. ~unordered_node_map() = default;
  181. unordered_node_map& operator=(unordered_node_map const& other)
  182. {
  183. table_ = other.table_;
  184. return *this;
  185. }
  186. unordered_node_map& operator=(unordered_node_map&& other) noexcept(
  187. noexcept(std::declval<table_type&>() = std::declval<table_type&&>()))
  188. {
  189. table_ = std::move(other.table_);
  190. return *this;
  191. }
  192. allocator_type get_allocator() const noexcept
  193. {
  194. return table_.get_allocator();
  195. }
  196. /// Iterators
  197. ///
  198. iterator begin() noexcept { return table_.begin(); }
  199. const_iterator begin() const noexcept { return table_.begin(); }
  200. const_iterator cbegin() const noexcept { return table_.cbegin(); }
  201. iterator end() noexcept { return table_.end(); }
  202. const_iterator end() const noexcept { return table_.end(); }
  203. const_iterator cend() const noexcept { return table_.cend(); }
  204. /// Capacity
  205. ///
  206. BOOST_ATTRIBUTE_NODISCARD bool empty() const noexcept
  207. {
  208. return table_.empty();
  209. }
  210. size_type size() const noexcept { return table_.size(); }
  211. size_type max_size() const noexcept { return table_.max_size(); }
  212. /// Modifiers
  213. ///
  214. void clear() noexcept { table_.clear(); }
  215. template <class Ty>
  216. BOOST_FORCEINLINE auto insert(Ty&& value)
  217. -> decltype(table_.insert(std::forward<Ty>(value)))
  218. {
  219. return table_.insert(std::forward<Ty>(value));
  220. }
  221. BOOST_FORCEINLINE std::pair<iterator, bool> insert(init_type&& value)
  222. {
  223. return table_.insert(std::move(value));
  224. }
  225. template <class Ty>
  226. BOOST_FORCEINLINE auto insert(const_iterator, Ty&& value)
  227. -> decltype(table_.insert(std::forward<Ty>(value)).first)
  228. {
  229. return table_.insert(std::forward<Ty>(value)).first;
  230. }
  231. BOOST_FORCEINLINE iterator insert(const_iterator, init_type&& value)
  232. {
  233. return table_.insert(std::move(value)).first;
  234. }
  235. template <class InputIterator>
  236. BOOST_FORCEINLINE void insert(InputIterator first, InputIterator last)
  237. {
  238. for (auto pos = first; pos != last; ++pos) {
  239. table_.emplace(*pos);
  240. }
  241. }
  242. void insert(std::initializer_list<value_type> ilist)
  243. {
  244. this->insert(ilist.begin(), ilist.end());
  245. }
  246. insert_return_type insert(node_type&& nh)
  247. {
  248. if (nh.empty()) {
  249. return {end(), false, node_type{}};
  250. }
  251. BOOST_ASSERT(get_allocator() == nh.get_allocator());
  252. auto itp = table_.insert(std::move(nh.element()));
  253. if (itp.second) {
  254. nh.reset();
  255. return {itp.first, true, node_type{}};
  256. } else {
  257. return {itp.first, false, std::move(nh)};
  258. }
  259. }
  260. iterator insert(const_iterator, node_type&& nh)
  261. {
  262. if (nh.empty()) {
  263. return end();
  264. }
  265. BOOST_ASSERT(get_allocator() == nh.get_allocator());
  266. auto itp = table_.insert(std::move(nh.element()));
  267. if (itp.second) {
  268. nh.reset();
  269. return itp.first;
  270. } else {
  271. return itp.first;
  272. }
  273. }
  274. template <class M>
  275. std::pair<iterator, bool> insert_or_assign(key_type const& key, M&& obj)
  276. {
  277. auto ibp = table_.try_emplace(key, std::forward<M>(obj));
  278. if (ibp.second) {
  279. return ibp;
  280. }
  281. ibp.first->second = std::forward<M>(obj);
  282. return ibp;
  283. }
  284. template <class M>
  285. std::pair<iterator, bool> insert_or_assign(key_type&& key, M&& obj)
  286. {
  287. auto ibp = table_.try_emplace(std::move(key), std::forward<M>(obj));
  288. if (ibp.second) {
  289. return ibp;
  290. }
  291. ibp.first->second = std::forward<M>(obj);
  292. return ibp;
  293. }
  294. template <class K, class M>
  295. typename std::enable_if<
  296. boost::unordered::detail::are_transparent<K, hasher, key_equal>::value,
  297. std::pair<iterator, bool> >::type
  298. insert_or_assign(K&& k, M&& obj)
  299. {
  300. auto ibp = table_.try_emplace(std::forward<K>(k), std::forward<M>(obj));
  301. if (ibp.second) {
  302. return ibp;
  303. }
  304. ibp.first->second = std::forward<M>(obj);
  305. return ibp;
  306. }
  307. template <class M>
  308. iterator insert_or_assign(const_iterator, key_type const& key, M&& obj)
  309. {
  310. return this->insert_or_assign(key, std::forward<M>(obj)).first;
  311. }
  312. template <class M>
  313. iterator insert_or_assign(const_iterator, key_type&& key, M&& obj)
  314. {
  315. return this->insert_or_assign(std::move(key), std::forward<M>(obj))
  316. .first;
  317. }
  318. template <class K, class M>
  319. typename std::enable_if<
  320. boost::unordered::detail::are_transparent<K, hasher, key_equal>::value,
  321. iterator>::type
  322. insert_or_assign(const_iterator, K&& k, M&& obj)
  323. {
  324. return this->insert_or_assign(std::forward<K>(k), std::forward<M>(obj))
  325. .first;
  326. }
  327. template <class... Args>
  328. BOOST_FORCEINLINE std::pair<iterator, bool> emplace(Args&&... args)
  329. {
  330. return table_.emplace(std::forward<Args>(args)...);
  331. }
  332. template <class... Args>
  333. BOOST_FORCEINLINE iterator emplace_hint(const_iterator, Args&&... args)
  334. {
  335. return table_.emplace(std::forward<Args>(args)...).first;
  336. }
  337. template <class... Args>
  338. BOOST_FORCEINLINE std::pair<iterator, bool> try_emplace(
  339. key_type const& key, Args&&... args)
  340. {
  341. return table_.try_emplace(key, std::forward<Args>(args)...);
  342. }
  343. template <class... Args>
  344. BOOST_FORCEINLINE std::pair<iterator, bool> try_emplace(
  345. key_type&& key, Args&&... args)
  346. {
  347. return table_.try_emplace(std::move(key), std::forward<Args>(args)...);
  348. }
  349. template <class K, class... Args>
  350. BOOST_FORCEINLINE typename std::enable_if<
  351. boost::unordered::detail::transparent_non_iterable<K,
  352. unordered_node_map>::value,
  353. std::pair<iterator, bool> >::type
  354. try_emplace(K&& key, Args&&... args)
  355. {
  356. return table_.try_emplace(
  357. std::forward<K>(key), std::forward<Args>(args)...);
  358. }
  359. template <class... Args>
  360. BOOST_FORCEINLINE iterator try_emplace(
  361. const_iterator, key_type const& key, Args&&... args)
  362. {
  363. return table_.try_emplace(key, std::forward<Args>(args)...).first;
  364. }
  365. template <class... Args>
  366. BOOST_FORCEINLINE iterator try_emplace(
  367. const_iterator, key_type&& key, Args&&... args)
  368. {
  369. return table_.try_emplace(std::move(key), std::forward<Args>(args)...)
  370. .first;
  371. }
  372. template <class K, class... Args>
  373. BOOST_FORCEINLINE typename std::enable_if<
  374. boost::unordered::detail::transparent_non_iterable<K,
  375. unordered_node_map>::value,
  376. iterator>::type
  377. try_emplace(const_iterator, K&& key, Args&&... args)
  378. {
  379. return table_
  380. .try_emplace(std::forward<K>(key), std::forward<Args>(args)...)
  381. .first;
  382. }
  383. BOOST_FORCEINLINE typename table_type::erase_return_type erase(
  384. iterator pos)
  385. {
  386. return table_.erase(pos);
  387. }
  388. BOOST_FORCEINLINE typename table_type::erase_return_type erase(
  389. const_iterator pos)
  390. {
  391. return table_.erase(pos);
  392. }
  393. iterator erase(const_iterator first, const_iterator last)
  394. {
  395. while (first != last) {
  396. this->erase(first++);
  397. }
  398. return iterator{detail::foa::const_iterator_cast_tag{}, last};
  399. }
  400. BOOST_FORCEINLINE size_type erase(key_type const& key)
  401. {
  402. return table_.erase(key);
  403. }
  404. template <class K>
  405. BOOST_FORCEINLINE typename std::enable_if<
  406. detail::transparent_non_iterable<K, unordered_node_map>::value,
  407. size_type>::type
  408. erase(K const& key)
  409. {
  410. return table_.erase(key);
  411. }
  412. void swap(unordered_node_map& rhs) noexcept(
  413. noexcept(std::declval<table_type&>().swap(std::declval<table_type&>())))
  414. {
  415. table_.swap(rhs.table_);
  416. }
  417. node_type extract(const_iterator pos)
  418. {
  419. BOOST_ASSERT(pos != end());
  420. node_type nh;
  421. auto elem = table_.extract(pos);
  422. nh.emplace(std::move(elem), get_allocator());
  423. return nh;
  424. }
  425. node_type extract(key_type const& key)
  426. {
  427. auto pos = find(key);
  428. return pos != end() ? extract(pos) : node_type();
  429. }
  430. template <class K>
  431. typename std::enable_if<
  432. boost::unordered::detail::transparent_non_iterable<K,
  433. unordered_node_map>::value,
  434. node_type>::type
  435. extract(K const& key)
  436. {
  437. auto pos = find(key);
  438. return pos != end() ? extract(pos) : node_type();
  439. }
  440. template <class H2, class P2>
  441. void merge(
  442. unordered_node_map<key_type, mapped_type, H2, P2, allocator_type>&
  443. source)
  444. {
  445. BOOST_ASSERT(get_allocator() == source.get_allocator());
  446. table_.merge(source.table_);
  447. }
  448. template <class H2, class P2>
  449. void merge(
  450. unordered_node_map<key_type, mapped_type, H2, P2, allocator_type>&&
  451. source)
  452. {
  453. BOOST_ASSERT(get_allocator() == source.get_allocator());
  454. table_.merge(std::move(source.table_));
  455. }
  456. /// Lookup
  457. ///
  458. mapped_type& at(key_type const& key)
  459. {
  460. auto pos = table_.find(key);
  461. if (pos != table_.end()) {
  462. return pos->second;
  463. }
  464. // TODO: someday refactor this to conditionally serialize the key and
  465. // include it in the error message
  466. //
  467. boost::throw_exception(
  468. std::out_of_range("key was not found in unordered_node_map"));
  469. }
  470. mapped_type const& at(key_type const& key) const
  471. {
  472. auto pos = table_.find(key);
  473. if (pos != table_.end()) {
  474. return pos->second;
  475. }
  476. boost::throw_exception(
  477. std::out_of_range("key was not found in unordered_node_map"));
  478. }
  479. template <class K>
  480. typename std::enable_if<
  481. boost::unordered::detail::are_transparent<K, hasher, key_equal>::value,
  482. mapped_type&>::type
  483. at(K&& key)
  484. {
  485. auto pos = table_.find(std::forward<K>(key));
  486. if (pos != table_.end()) {
  487. return pos->second;
  488. }
  489. boost::throw_exception(
  490. std::out_of_range("key was not found in unordered_node_map"));
  491. }
  492. template <class K>
  493. typename std::enable_if<
  494. boost::unordered::detail::are_transparent<K, hasher, key_equal>::value,
  495. mapped_type const&>::type
  496. at(K&& key) const
  497. {
  498. auto pos = table_.find(std::forward<K>(key));
  499. if (pos != table_.end()) {
  500. return pos->second;
  501. }
  502. boost::throw_exception(
  503. std::out_of_range("key was not found in unordered_node_map"));
  504. }
  505. BOOST_FORCEINLINE mapped_type& operator[](key_type const& key)
  506. {
  507. return table_.try_emplace(key).first->second;
  508. }
  509. BOOST_FORCEINLINE mapped_type& operator[](key_type&& key)
  510. {
  511. return table_.try_emplace(std::move(key)).first->second;
  512. }
  513. template <class K>
  514. typename std::enable_if<
  515. boost::unordered::detail::are_transparent<K, hasher, key_equal>::value,
  516. mapped_type&>::type
  517. operator[](K&& key)
  518. {
  519. return table_.try_emplace(std::forward<K>(key)).first->second;
  520. }
  521. BOOST_FORCEINLINE size_type count(key_type const& key) const
  522. {
  523. auto pos = table_.find(key);
  524. return pos != table_.end() ? 1 : 0;
  525. }
  526. template <class K>
  527. BOOST_FORCEINLINE typename std::enable_if<
  528. detail::are_transparent<K, hasher, key_equal>::value, size_type>::type
  529. count(K const& key) const
  530. {
  531. auto pos = table_.find(key);
  532. return pos != table_.end() ? 1 : 0;
  533. }
  534. BOOST_FORCEINLINE iterator find(key_type const& key)
  535. {
  536. return table_.find(key);
  537. }
  538. BOOST_FORCEINLINE const_iterator find(key_type const& key) const
  539. {
  540. return table_.find(key);
  541. }
  542. template <class K>
  543. BOOST_FORCEINLINE typename std::enable_if<
  544. boost::unordered::detail::are_transparent<K, hasher, key_equal>::value,
  545. iterator>::type
  546. find(K const& key)
  547. {
  548. return table_.find(key);
  549. }
  550. template <class K>
  551. BOOST_FORCEINLINE typename std::enable_if<
  552. boost::unordered::detail::are_transparent<K, hasher, key_equal>::value,
  553. const_iterator>::type
  554. find(K const& key) const
  555. {
  556. return table_.find(key);
  557. }
  558. BOOST_FORCEINLINE bool contains(key_type const& key) const
  559. {
  560. return this->find(key) != this->end();
  561. }
  562. template <class K>
  563. BOOST_FORCEINLINE typename std::enable_if<
  564. boost::unordered::detail::are_transparent<K, hasher, key_equal>::value,
  565. bool>::type
  566. contains(K const& key) const
  567. {
  568. return this->find(key) != this->end();
  569. }
  570. std::pair<iterator, iterator> equal_range(key_type const& key)
  571. {
  572. auto pos = table_.find(key);
  573. if (pos == table_.end()) {
  574. return {pos, pos};
  575. }
  576. auto next = pos;
  577. ++next;
  578. return {pos, next};
  579. }
  580. std::pair<const_iterator, const_iterator> equal_range(
  581. key_type const& key) const
  582. {
  583. auto pos = table_.find(key);
  584. if (pos == table_.end()) {
  585. return {pos, pos};
  586. }
  587. auto next = pos;
  588. ++next;
  589. return {pos, next};
  590. }
  591. template <class K>
  592. typename std::enable_if<
  593. detail::are_transparent<K, hasher, key_equal>::value,
  594. std::pair<iterator, iterator> >::type
  595. equal_range(K const& key)
  596. {
  597. auto pos = table_.find(key);
  598. if (pos == table_.end()) {
  599. return {pos, pos};
  600. }
  601. auto next = pos;
  602. ++next;
  603. return {pos, next};
  604. }
  605. template <class K>
  606. typename std::enable_if<
  607. detail::are_transparent<K, hasher, key_equal>::value,
  608. std::pair<const_iterator, const_iterator> >::type
  609. equal_range(K const& key) const
  610. {
  611. auto pos = table_.find(key);
  612. if (pos == table_.end()) {
  613. return {pos, pos};
  614. }
  615. auto next = pos;
  616. ++next;
  617. return {pos, next};
  618. }
  619. /// Hash Policy
  620. ///
  621. size_type bucket_count() const noexcept { return table_.capacity(); }
  622. float load_factor() const noexcept { return table_.load_factor(); }
  623. float max_load_factor() const noexcept
  624. {
  625. return table_.max_load_factor();
  626. }
  627. void max_load_factor(float) {}
  628. size_type max_load() const noexcept { return table_.max_load(); }
  629. void rehash(size_type n) { table_.rehash(n); }
  630. void reserve(size_type n) { table_.reserve(n); }
  631. /// Observers
  632. ///
  633. hasher hash_function() const { return table_.hash_function(); }
  634. key_equal key_eq() const { return table_.key_eq(); }
  635. };
  636. template <class Key, class T, class Hash, class KeyEqual, class Allocator>
  637. bool operator==(
  638. unordered_node_map<Key, T, Hash, KeyEqual, Allocator> const& lhs,
  639. unordered_node_map<Key, T, Hash, KeyEqual, Allocator> const& rhs)
  640. {
  641. return lhs.table_ == rhs.table_;
  642. }
  643. template <class Key, class T, class Hash, class KeyEqual, class Allocator>
  644. bool operator!=(
  645. unordered_node_map<Key, T, Hash, KeyEqual, Allocator> const& lhs,
  646. unordered_node_map<Key, T, Hash, KeyEqual, Allocator> const& rhs)
  647. {
  648. return !(lhs == rhs);
  649. }
  650. template <class Key, class T, class Hash, class KeyEqual, class Allocator>
  651. void swap(unordered_node_map<Key, T, Hash, KeyEqual, Allocator>& lhs,
  652. unordered_node_map<Key, T, Hash, KeyEqual, Allocator>& rhs)
  653. noexcept(noexcept(lhs.swap(rhs)))
  654. {
  655. lhs.swap(rhs);
  656. }
  657. template <class Key, class T, class Hash, class KeyEqual, class Allocator,
  658. class Pred>
  659. typename unordered_node_map<Key, T, Hash, KeyEqual, Allocator>::size_type
  660. erase_if(
  661. unordered_node_map<Key, T, Hash, KeyEqual, Allocator>& map, Pred pred)
  662. {
  663. return erase_if(map.table_, pred);
  664. }
  665. template <class Archive, class Key, class T, class Hash, class KeyEqual,
  666. class Allocator>
  667. void serialize(Archive& ar,
  668. unordered_node_map<Key, T, Hash, KeyEqual, Allocator>& map,
  669. unsigned int version)
  670. {
  671. detail::serialize_container(ar, map, version);
  672. }
  673. #if defined(BOOST_MSVC)
  674. #pragma warning(pop) /* C4714 */
  675. #endif
  676. #if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
  677. template <class InputIterator,
  678. class Hash =
  679. boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
  680. class Pred =
  681. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  682. class Allocator = std::allocator<
  683. boost::unordered::detail::iter_to_alloc_t<InputIterator> >,
  684. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  685. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  686. class = std::enable_if_t<detail::is_pred_v<Pred> >,
  687. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  688. unordered_node_map(InputIterator, InputIterator,
  689. std::size_t = boost::unordered::detail::foa::default_bucket_count,
  690. Hash = Hash(), Pred = Pred(), Allocator = Allocator())
  691. -> unordered_node_map<boost::unordered::detail::iter_key_t<InputIterator>,
  692. boost::unordered::detail::iter_val_t<InputIterator>, Hash, Pred,
  693. Allocator>;
  694. template <class Key, class T,
  695. class Hash = boost::hash<std::remove_const_t<Key> >,
  696. class Pred = std::equal_to<std::remove_const_t<Key> >,
  697. class Allocator = std::allocator<std::pair<const Key, T> >,
  698. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  699. class = std::enable_if_t<detail::is_pred_v<Pred> >,
  700. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  701. unordered_node_map(std::initializer_list<std::pair<Key, T> >,
  702. std::size_t = boost::unordered::detail::foa::default_bucket_count,
  703. Hash = Hash(), Pred = Pred(), Allocator = Allocator())
  704. -> unordered_node_map<std::remove_const_t<Key>, T, Hash, Pred,
  705. Allocator>;
  706. template <class InputIterator, class Allocator,
  707. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  708. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  709. unordered_node_map(InputIterator, InputIterator, std::size_t, Allocator)
  710. -> unordered_node_map<boost::unordered::detail::iter_key_t<InputIterator>,
  711. boost::unordered::detail::iter_val_t<InputIterator>,
  712. boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
  713. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  714. Allocator>;
  715. template <class InputIterator, class Allocator,
  716. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  717. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  718. unordered_node_map(InputIterator, InputIterator, Allocator)
  719. -> unordered_node_map<boost::unordered::detail::iter_key_t<InputIterator>,
  720. boost::unordered::detail::iter_val_t<InputIterator>,
  721. boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
  722. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  723. Allocator>;
  724. template <class InputIterator, class Hash, class Allocator,
  725. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  726. class = std::enable_if_t<detail::is_input_iterator_v<InputIterator> >,
  727. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  728. unordered_node_map(
  729. InputIterator, InputIterator, std::size_t, Hash, Allocator)
  730. -> unordered_node_map<boost::unordered::detail::iter_key_t<InputIterator>,
  731. boost::unordered::detail::iter_val_t<InputIterator>, Hash,
  732. std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
  733. Allocator>;
  734. template <class Key, class T, class Allocator,
  735. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  736. unordered_node_map(std::initializer_list<std::pair<Key, T> >, std::size_t,
  737. Allocator) -> unordered_node_map<std::remove_const_t<Key>, T,
  738. boost::hash<std::remove_const_t<Key> >,
  739. std::equal_to<std::remove_const_t<Key> >, Allocator>;
  740. template <class Key, class T, class Allocator,
  741. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  742. unordered_node_map(std::initializer_list<std::pair<Key, T> >, Allocator)
  743. -> unordered_node_map<std::remove_const_t<Key>, T,
  744. boost::hash<std::remove_const_t<Key> >,
  745. std::equal_to<std::remove_const_t<Key> >, Allocator>;
  746. template <class Key, class T, class Hash, class Allocator,
  747. class = std::enable_if_t<detail::is_hash_v<Hash> >,
  748. class = std::enable_if_t<detail::is_allocator_v<Allocator> > >
  749. unordered_node_map(std::initializer_list<std::pair<Key, T> >, std::size_t,
  750. Hash, Allocator) -> unordered_node_map<std::remove_const_t<Key>, T,
  751. Hash, std::equal_to<std::remove_const_t<Key> >, Allocator>;
  752. #endif
  753. } // namespace unordered
  754. } // namespace boost
  755. #endif