map.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. // Debugging map implementation -*- C++ -*-
  2. // Copyright (C) 2003-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file debug/map.h
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_MAP_H
  24. #define _GLIBCXX_DEBUG_MAP_H 1
  25. #include <debug/safe_sequence.h>
  26. #include <debug/safe_container.h>
  27. #include <debug/safe_iterator.h>
  28. #include <utility>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. namespace __debug
  32. {
  33. /// Class std::map with safety/checking/debug instrumentation.
  34. template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
  35. typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
  36. class map
  37. : public __gnu_debug::_Safe_container<
  38. map<_Key, _Tp, _Compare, _Allocator>, _Allocator,
  39. __gnu_debug::_Safe_node_sequence>,
  40. public _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator>
  41. {
  42. typedef _GLIBCXX_STD_C::map<
  43. _Key, _Tp, _Compare, _Allocator> _Base;
  44. typedef __gnu_debug::_Safe_container<
  45. map, _Allocator, __gnu_debug::_Safe_node_sequence> _Safe;
  46. typedef typename _Base::const_iterator _Base_const_iterator;
  47. typedef typename _Base::iterator _Base_iterator;
  48. typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
  49. template<typename _ItT, typename _SeqT, typename _CatT>
  50. friend class ::__gnu_debug::_Safe_iterator;
  51. // Reference wrapper for base class. Disambiguates map(const _Base&)
  52. // from copy constructor by requiring a user-defined conversion.
  53. // See PR libstdc++/90102.
  54. struct _Base_ref
  55. {
  56. _Base_ref(const _Base& __r) : _M_ref(__r) { }
  57. const _Base& _M_ref;
  58. };
  59. public:
  60. // types:
  61. typedef _Key key_type;
  62. typedef _Tp mapped_type;
  63. typedef std::pair<const _Key, _Tp> value_type;
  64. typedef _Compare key_compare;
  65. typedef _Allocator allocator_type;
  66. typedef typename _Base::reference reference;
  67. typedef typename _Base::const_reference const_reference;
  68. typedef __gnu_debug::_Safe_iterator<_Base_iterator, map>
  69. iterator;
  70. typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, map>
  71. const_iterator;
  72. typedef typename _Base::size_type size_type;
  73. typedef typename _Base::difference_type difference_type;
  74. typedef typename _Base::pointer pointer;
  75. typedef typename _Base::const_pointer const_pointer;
  76. typedef std::reverse_iterator<iterator> reverse_iterator;
  77. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  78. // 23.3.1.1 construct/copy/destroy:
  79. #if __cplusplus < 201103L
  80. map() : _Base() { }
  81. map(const map& __x)
  82. : _Base(__x) { }
  83. ~map() { }
  84. #else
  85. map() = default;
  86. map(const map&) = default;
  87. map(map&&) = default;
  88. map(initializer_list<value_type> __l,
  89. const _Compare& __c = _Compare(),
  90. const allocator_type& __a = allocator_type())
  91. : _Base(__l, __c, __a) { }
  92. explicit
  93. map(const allocator_type& __a)
  94. : _Base(__a) { }
  95. map(const map& __m, const allocator_type& __a)
  96. : _Base(__m, __a) { }
  97. map(map&& __m, const allocator_type& __a)
  98. noexcept( noexcept(_Base(std::move(__m._M_base()), __a)) )
  99. : _Safe(std::move(__m._M_safe()), __a),
  100. _Base(std::move(__m._M_base()), __a) { }
  101. map(initializer_list<value_type> __l, const allocator_type& __a)
  102. : _Base(__l, __a) { }
  103. template<typename _InputIterator>
  104. map(_InputIterator __first, _InputIterator __last,
  105. const allocator_type& __a)
  106. : _Base(__gnu_debug::__base(
  107. __glibcxx_check_valid_constructor_range(__first, __last)),
  108. __gnu_debug::__base(__last), __a)
  109. { }
  110. ~map() = default;
  111. #endif
  112. map(_Base_ref __x)
  113. : _Base(__x._M_ref) { }
  114. explicit map(const _Compare& __comp,
  115. const _Allocator& __a = _Allocator())
  116. : _Base(__comp, __a) { }
  117. template<typename _InputIterator>
  118. map(_InputIterator __first, _InputIterator __last,
  119. const _Compare& __comp = _Compare(),
  120. const _Allocator& __a = _Allocator())
  121. : _Base(__gnu_debug::__base(
  122. __glibcxx_check_valid_constructor_range(__first, __last)),
  123. __gnu_debug::__base(__last),
  124. __comp, __a) { }
  125. #if __cplusplus < 201103L
  126. map&
  127. operator=(const map& __x)
  128. {
  129. this->_M_safe() = __x;
  130. _M_base() = __x;
  131. return *this;
  132. }
  133. #else
  134. map&
  135. operator=(const map&) = default;
  136. map&
  137. operator=(map&&) = default;
  138. map&
  139. operator=(initializer_list<value_type> __l)
  140. {
  141. _M_base() = __l;
  142. this->_M_invalidate_all();
  143. return *this;
  144. }
  145. #endif
  146. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  147. // 133. map missing get_allocator()
  148. using _Base::get_allocator;
  149. // iterators:
  150. iterator
  151. begin() _GLIBCXX_NOEXCEPT
  152. { return iterator(_Base::begin(), this); }
  153. const_iterator
  154. begin() const _GLIBCXX_NOEXCEPT
  155. { return const_iterator(_Base::begin(), this); }
  156. iterator
  157. end() _GLIBCXX_NOEXCEPT
  158. { return iterator(_Base::end(), this); }
  159. const_iterator
  160. end() const _GLIBCXX_NOEXCEPT
  161. { return const_iterator(_Base::end(), this); }
  162. reverse_iterator
  163. rbegin() _GLIBCXX_NOEXCEPT
  164. { return reverse_iterator(end()); }
  165. const_reverse_iterator
  166. rbegin() const _GLIBCXX_NOEXCEPT
  167. { return const_reverse_iterator(end()); }
  168. reverse_iterator
  169. rend() _GLIBCXX_NOEXCEPT
  170. { return reverse_iterator(begin()); }
  171. const_reverse_iterator
  172. rend() const _GLIBCXX_NOEXCEPT
  173. { return const_reverse_iterator(begin()); }
  174. #if __cplusplus >= 201103L
  175. const_iterator
  176. cbegin() const noexcept
  177. { return const_iterator(_Base::begin(), this); }
  178. const_iterator
  179. cend() const noexcept
  180. { return const_iterator(_Base::end(), this); }
  181. const_reverse_iterator
  182. crbegin() const noexcept
  183. { return const_reverse_iterator(end()); }
  184. const_reverse_iterator
  185. crend() const noexcept
  186. { return const_reverse_iterator(begin()); }
  187. #endif
  188. // capacity:
  189. using _Base::empty;
  190. using _Base::size;
  191. using _Base::max_size;
  192. // 23.3.1.2 element access:
  193. using _Base::operator[];
  194. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  195. // DR 464. Suggestion for new member functions in standard containers.
  196. using _Base::at;
  197. // modifiers:
  198. #if __cplusplus >= 201103L
  199. template<typename... _Args>
  200. std::pair<iterator, bool>
  201. emplace(_Args&&... __args)
  202. {
  203. auto __res = _Base::emplace(std::forward<_Args>(__args)...);
  204. return { { __res.first, this }, __res.second };
  205. }
  206. template<typename... _Args>
  207. iterator
  208. emplace_hint(const_iterator __pos, _Args&&... __args)
  209. {
  210. __glibcxx_check_insert(__pos);
  211. return
  212. {
  213. _Base::emplace_hint(__pos.base(), std::forward<_Args>(__args)...),
  214. this
  215. };
  216. }
  217. #endif
  218. std::pair<iterator, bool>
  219. insert(const value_type& __x)
  220. {
  221. std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
  222. return std::pair<iterator, bool>(iterator(__res.first, this),
  223. __res.second);
  224. }
  225. #if __cplusplus >= 201103L
  226. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  227. // 2354. Unnecessary copying when inserting into maps with braced-init
  228. std::pair<iterator, bool>
  229. insert(value_type&& __x)
  230. {
  231. auto __res = _Base::insert(std::move(__x));
  232. return { { __res.first, this }, __res.second };
  233. }
  234. template<typename _Pair, typename = typename
  235. std::enable_if<std::is_constructible<value_type,
  236. _Pair&&>::value>::type>
  237. std::pair<iterator, bool>
  238. insert(_Pair&& __x)
  239. {
  240. auto __res = _Base::insert(std::forward<_Pair>(__x));
  241. return { { __res.first, this }, __res.second };
  242. }
  243. #endif
  244. #if __cplusplus >= 201103L
  245. void
  246. insert(std::initializer_list<value_type> __list)
  247. { _Base::insert(__list); }
  248. #endif
  249. iterator
  250. #if __cplusplus >= 201103L
  251. insert(const_iterator __position, const value_type& __x)
  252. #else
  253. insert(iterator __position, const value_type& __x)
  254. #endif
  255. {
  256. __glibcxx_check_insert(__position);
  257. return iterator(_Base::insert(__position.base(), __x), this);
  258. }
  259. #if __cplusplus >= 201103L
  260. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  261. // 2354. Unnecessary copying when inserting into maps with braced-init
  262. iterator
  263. insert(const_iterator __position, value_type&& __x)
  264. {
  265. __glibcxx_check_insert(__position);
  266. return { _Base::insert(__position.base(), std::move(__x)), this };
  267. }
  268. template<typename _Pair, typename = typename
  269. std::enable_if<std::is_constructible<value_type,
  270. _Pair&&>::value>::type>
  271. iterator
  272. insert(const_iterator __position, _Pair&& __x)
  273. {
  274. __glibcxx_check_insert(__position);
  275. return
  276. {
  277. _Base::insert(__position.base(), std::forward<_Pair>(__x)),
  278. this
  279. };
  280. }
  281. #endif
  282. template<typename _InputIterator>
  283. void
  284. insert(_InputIterator __first, _InputIterator __last)
  285. {
  286. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  287. __glibcxx_check_valid_range2(__first, __last, __dist);
  288. if (__dist.second >= __gnu_debug::__dp_sign)
  289. _Base::insert(__gnu_debug::__unsafe(__first),
  290. __gnu_debug::__unsafe(__last));
  291. else
  292. _Base::insert(__first, __last);
  293. }
  294. #if __cplusplus > 201402L
  295. template <typename... _Args>
  296. pair<iterator, bool>
  297. try_emplace(const key_type& __k, _Args&&... __args)
  298. {
  299. auto __res = _Base::try_emplace(__k,
  300. std::forward<_Args>(__args)...);
  301. return { { __res.first, this }, __res.second };
  302. }
  303. template <typename... _Args>
  304. pair<iterator, bool>
  305. try_emplace(key_type&& __k, _Args&&... __args)
  306. {
  307. auto __res = _Base::try_emplace(std::move(__k),
  308. std::forward<_Args>(__args)...);
  309. return { { __res.first, this }, __res.second };
  310. }
  311. template <typename... _Args>
  312. iterator
  313. try_emplace(const_iterator __hint, const key_type& __k,
  314. _Args&&... __args)
  315. {
  316. __glibcxx_check_insert(__hint);
  317. return
  318. {
  319. _Base::try_emplace(__hint.base(), __k,
  320. std::forward<_Args>(__args)...),
  321. this
  322. };
  323. }
  324. template <typename... _Args>
  325. iterator
  326. try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
  327. {
  328. __glibcxx_check_insert(__hint);
  329. return
  330. {
  331. _Base::try_emplace(__hint.base(), std::move(__k),
  332. std::forward<_Args>(__args)...),
  333. this
  334. };
  335. }
  336. template <typename _Obj>
  337. std::pair<iterator, bool>
  338. insert_or_assign(const key_type& __k, _Obj&& __obj)
  339. {
  340. auto __res = _Base::insert_or_assign(__k,
  341. std::forward<_Obj>(__obj));
  342. return { { __res.first, this }, __res.second };
  343. }
  344. template <typename _Obj>
  345. std::pair<iterator, bool>
  346. insert_or_assign(key_type&& __k, _Obj&& __obj)
  347. {
  348. auto __res = _Base::insert_or_assign(std::move(__k),
  349. std::forward<_Obj>(__obj));
  350. return { { __res.first, this }, __res.second };
  351. }
  352. template <typename _Obj>
  353. iterator
  354. insert_or_assign(const_iterator __hint,
  355. const key_type& __k, _Obj&& __obj)
  356. {
  357. __glibcxx_check_insert(__hint);
  358. return
  359. {
  360. _Base::insert_or_assign(__hint.base(), __k,
  361. std::forward<_Obj>(__obj)),
  362. this
  363. };
  364. }
  365. template <typename _Obj>
  366. iterator
  367. insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
  368. {
  369. __glibcxx_check_insert(__hint);
  370. return
  371. {
  372. _Base::insert_or_assign(__hint.base(), std::move(__k),
  373. std::forward<_Obj>(__obj)),
  374. this
  375. };
  376. }
  377. #endif // C++17
  378. #if __cplusplus > 201402L
  379. using node_type = typename _Base::node_type;
  380. using insert_return_type = _Node_insert_return<iterator, node_type>;
  381. node_type
  382. extract(const_iterator __position)
  383. {
  384. __glibcxx_check_erase(__position);
  385. this->_M_invalidate_if(_Equal(__position.base()));
  386. return _Base::extract(__position.base());
  387. }
  388. node_type
  389. extract(const key_type& __key)
  390. {
  391. const auto __position = find(__key);
  392. if (__position != end())
  393. return extract(__position);
  394. return {};
  395. }
  396. insert_return_type
  397. insert(node_type&& __nh)
  398. {
  399. auto __ret = _Base::insert(std::move(__nh));
  400. return
  401. { { __ret.position, this }, __ret.inserted, std::move(__ret.node) };
  402. }
  403. iterator
  404. insert(const_iterator __hint, node_type&& __nh)
  405. {
  406. __glibcxx_check_insert(__hint);
  407. return { _Base::insert(__hint.base(), std::move(__nh)), this };
  408. }
  409. using _Base::merge;
  410. #endif // C++17
  411. #if __cplusplus >= 201103L
  412. iterator
  413. erase(const_iterator __position)
  414. {
  415. __glibcxx_check_erase(__position);
  416. this->_M_invalidate_if(_Equal(__position.base()));
  417. return { _Base::erase(__position.base()), this };
  418. }
  419. _GLIBCXX_ABI_TAG_CXX11
  420. iterator
  421. erase(iterator __position)
  422. { return erase(const_iterator(__position)); }
  423. #else
  424. void
  425. erase(iterator __position)
  426. {
  427. __glibcxx_check_erase(__position);
  428. this->_M_invalidate_if(_Equal(__position.base()));
  429. _Base::erase(__position.base());
  430. }
  431. #endif
  432. size_type
  433. erase(const key_type& __x)
  434. {
  435. _Base_iterator __victim = _Base::find(__x);
  436. if (__victim == _Base::end())
  437. return 0;
  438. else
  439. {
  440. this->_M_invalidate_if(_Equal(__victim));
  441. _Base::erase(__victim);
  442. return 1;
  443. }
  444. }
  445. #if __cplusplus >= 201103L
  446. iterator
  447. erase(const_iterator __first, const_iterator __last)
  448. {
  449. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  450. // 151. can't currently clear() empty container
  451. __glibcxx_check_erase_range(__first, __last);
  452. for (_Base_const_iterator __victim = __first.base();
  453. __victim != __last.base(); ++__victim)
  454. {
  455. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::cend(),
  456. _M_message(__gnu_debug::__msg_valid_range)
  457. ._M_iterator(__first, "first")
  458. ._M_iterator(__last, "last"));
  459. this->_M_invalidate_if(_Equal(__victim));
  460. }
  461. return { _Base::erase(__first.base(), __last.base()), this };
  462. }
  463. #else
  464. void
  465. erase(iterator __first, iterator __last)
  466. {
  467. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  468. // 151. can't currently clear() empty container
  469. __glibcxx_check_erase_range(__first, __last);
  470. for (_Base_iterator __victim = __first.base();
  471. __victim != __last.base(); ++__victim)
  472. {
  473. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  474. _M_message(__gnu_debug::__msg_valid_range)
  475. ._M_iterator(__first, "first")
  476. ._M_iterator(__last, "last"));
  477. this->_M_invalidate_if(_Equal(__victim));
  478. }
  479. _Base::erase(__first.base(), __last.base());
  480. }
  481. #endif
  482. void
  483. swap(map& __x)
  484. _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
  485. {
  486. _Safe::_M_swap(__x);
  487. _Base::swap(__x);
  488. }
  489. void
  490. clear() _GLIBCXX_NOEXCEPT
  491. {
  492. this->_M_invalidate_all();
  493. _Base::clear();
  494. }
  495. // observers:
  496. using _Base::key_comp;
  497. using _Base::value_comp;
  498. // 23.3.1.3 map operations:
  499. iterator
  500. find(const key_type& __x)
  501. { return iterator(_Base::find(__x), this); }
  502. #if __cplusplus > 201103L
  503. template<typename _Kt,
  504. typename _Req =
  505. typename __has_is_transparent<_Compare, _Kt>::type>
  506. iterator
  507. find(const _Kt& __x)
  508. { return { _Base::find(__x), this }; }
  509. #endif
  510. const_iterator
  511. find(const key_type& __x) const
  512. { return const_iterator(_Base::find(__x), this); }
  513. #if __cplusplus > 201103L
  514. template<typename _Kt,
  515. typename _Req =
  516. typename __has_is_transparent<_Compare, _Kt>::type>
  517. const_iterator
  518. find(const _Kt& __x) const
  519. { return { _Base::find(__x), this }; }
  520. #endif
  521. using _Base::count;
  522. iterator
  523. lower_bound(const key_type& __x)
  524. { return iterator(_Base::lower_bound(__x), this); }
  525. #if __cplusplus > 201103L
  526. template<typename _Kt,
  527. typename _Req =
  528. typename __has_is_transparent<_Compare, _Kt>::type>
  529. iterator
  530. lower_bound(const _Kt& __x)
  531. { return { _Base::lower_bound(__x), this }; }
  532. #endif
  533. const_iterator
  534. lower_bound(const key_type& __x) const
  535. { return const_iterator(_Base::lower_bound(__x), this); }
  536. #if __cplusplus > 201103L
  537. template<typename _Kt,
  538. typename _Req =
  539. typename __has_is_transparent<_Compare, _Kt>::type>
  540. const_iterator
  541. lower_bound(const _Kt& __x) const
  542. { return { _Base::lower_bound(__x), this }; }
  543. #endif
  544. iterator
  545. upper_bound(const key_type& __x)
  546. { return iterator(_Base::upper_bound(__x), this); }
  547. #if __cplusplus > 201103L
  548. template<typename _Kt,
  549. typename _Req =
  550. typename __has_is_transparent<_Compare, _Kt>::type>
  551. iterator
  552. upper_bound(const _Kt& __x)
  553. { return { _Base::upper_bound(__x), this }; }
  554. #endif
  555. const_iterator
  556. upper_bound(const key_type& __x) const
  557. { return const_iterator(_Base::upper_bound(__x), this); }
  558. #if __cplusplus > 201103L
  559. template<typename _Kt,
  560. typename _Req =
  561. typename __has_is_transparent<_Compare, _Kt>::type>
  562. const_iterator
  563. upper_bound(const _Kt& __x) const
  564. { return { _Base::upper_bound(__x), this }; }
  565. #endif
  566. std::pair<iterator,iterator>
  567. equal_range(const key_type& __x)
  568. {
  569. std::pair<_Base_iterator, _Base_iterator> __res =
  570. _Base::equal_range(__x);
  571. return std::make_pair(iterator(__res.first, this),
  572. iterator(__res.second, this));
  573. }
  574. #if __cplusplus > 201103L
  575. template<typename _Kt,
  576. typename _Req =
  577. typename __has_is_transparent<_Compare, _Kt>::type>
  578. std::pair<iterator, iterator>
  579. equal_range(const _Kt& __x)
  580. {
  581. auto __res = _Base::equal_range(__x);
  582. return { { __res.first, this }, { __res.second, this } };
  583. }
  584. #endif
  585. std::pair<const_iterator,const_iterator>
  586. equal_range(const key_type& __x) const
  587. {
  588. std::pair<_Base_const_iterator, _Base_const_iterator> __res =
  589. _Base::equal_range(__x);
  590. return std::make_pair(const_iterator(__res.first, this),
  591. const_iterator(__res.second, this));
  592. }
  593. #if __cplusplus > 201103L
  594. template<typename _Kt,
  595. typename _Req =
  596. typename __has_is_transparent<_Compare, _Kt>::type>
  597. std::pair<const_iterator, const_iterator>
  598. equal_range(const _Kt& __x) const
  599. {
  600. auto __res = _Base::equal_range(__x);
  601. return { { __res.first, this }, { __res.second, this } };
  602. }
  603. #endif
  604. _Base&
  605. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  606. const _Base&
  607. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  608. };
  609. #if __cpp_deduction_guides >= 201606
  610. template<typename _InputIterator,
  611. typename _Compare = less<__iter_key_t<_InputIterator>>,
  612. typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
  613. typename = _RequireInputIter<_InputIterator>,
  614. typename = _RequireNotAllocator<_Compare>,
  615. typename = _RequireAllocator<_Allocator>>
  616. map(_InputIterator, _InputIterator,
  617. _Compare = _Compare(), _Allocator = _Allocator())
  618. -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
  619. _Compare, _Allocator>;
  620. template<typename _Key, typename _Tp, typename _Compare = less<_Key>,
  621. typename _Allocator = allocator<pair<const _Key, _Tp>>,
  622. typename = _RequireNotAllocator<_Compare>,
  623. typename = _RequireAllocator<_Allocator>>
  624. map(initializer_list<pair<_Key, _Tp>>,
  625. _Compare = _Compare(), _Allocator = _Allocator())
  626. -> map<_Key, _Tp, _Compare, _Allocator>;
  627. template <typename _InputIterator, typename _Allocator,
  628. typename = _RequireInputIter<_InputIterator>,
  629. typename = _RequireAllocator<_Allocator>>
  630. map(_InputIterator, _InputIterator, _Allocator)
  631. -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
  632. less<__iter_key_t<_InputIterator>>, _Allocator>;
  633. template<typename _Key, typename _Tp, typename _Allocator,
  634. typename = _RequireAllocator<_Allocator>>
  635. map(initializer_list<pair<_Key, _Tp>>, _Allocator)
  636. -> map<_Key, _Tp, less<_Key>, _Allocator>;
  637. #endif // deduction guides
  638. template<typename _Key, typename _Tp,
  639. typename _Compare, typename _Allocator>
  640. inline bool
  641. operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  642. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  643. { return __lhs._M_base() == __rhs._M_base(); }
  644. #if __cpp_lib_three_way_comparison
  645. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  646. inline __detail::__synth3way_t<pair<const _Key, _Tp>>
  647. operator<=>(const map<_Key, _Tp, _Compare, _Alloc>& __lhs,
  648. const map<_Key, _Tp, _Compare, _Alloc>& __rhs)
  649. { return __lhs._M_base() <=> __rhs._M_base(); }
  650. #else
  651. template<typename _Key, typename _Tp,
  652. typename _Compare, typename _Allocator>
  653. inline bool
  654. operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  655. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  656. { return __lhs._M_base() != __rhs._M_base(); }
  657. template<typename _Key, typename _Tp,
  658. typename _Compare, typename _Allocator>
  659. inline bool
  660. operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  661. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  662. { return __lhs._M_base() < __rhs._M_base(); }
  663. template<typename _Key, typename _Tp,
  664. typename _Compare, typename _Allocator>
  665. inline bool
  666. operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  667. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  668. { return __lhs._M_base() <= __rhs._M_base(); }
  669. template<typename _Key, typename _Tp,
  670. typename _Compare, typename _Allocator>
  671. inline bool
  672. operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  673. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  674. { return __lhs._M_base() >= __rhs._M_base(); }
  675. template<typename _Key, typename _Tp,
  676. typename _Compare, typename _Allocator>
  677. inline bool
  678. operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  679. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  680. { return __lhs._M_base() > __rhs._M_base(); }
  681. #endif // three-way comparison
  682. template<typename _Key, typename _Tp,
  683. typename _Compare, typename _Allocator>
  684. inline void
  685. swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  686. map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  687. _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
  688. { __lhs.swap(__rhs); }
  689. } // namespace __debug
  690. } // namespace std
  691. #endif