map.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. // Debugging map implementation -*- C++ -*-
  2. // Copyright (C) 2003-2022 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 <bits/stl_pair.h>
  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 __type_identity_t<allocator_type>& __a)
  96. : _Base(__m, __a) { }
  97. map(map&& __m, const __type_identity_t<allocator_type>& __a)
  98. noexcept( noexcept(_Base(std::move(__m), __a)) )
  99. : _Safe(std::move(__m), __a),
  100. _Base(std::move(__m), __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&) = default;
  128. map&
  129. operator=(map&&) = default;
  130. map&
  131. operator=(initializer_list<value_type> __l)
  132. {
  133. _Base::operator=(__l);
  134. this->_M_invalidate_all();
  135. return *this;
  136. }
  137. #endif
  138. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  139. // 133. map missing get_allocator()
  140. using _Base::get_allocator;
  141. // iterators:
  142. iterator
  143. begin() _GLIBCXX_NOEXCEPT
  144. { return iterator(_Base::begin(), this); }
  145. const_iterator
  146. begin() const _GLIBCXX_NOEXCEPT
  147. { return const_iterator(_Base::begin(), this); }
  148. iterator
  149. end() _GLIBCXX_NOEXCEPT
  150. { return iterator(_Base::end(), this); }
  151. const_iterator
  152. end() const _GLIBCXX_NOEXCEPT
  153. { return const_iterator(_Base::end(), this); }
  154. reverse_iterator
  155. rbegin() _GLIBCXX_NOEXCEPT
  156. { return reverse_iterator(end()); }
  157. const_reverse_iterator
  158. rbegin() const _GLIBCXX_NOEXCEPT
  159. { return const_reverse_iterator(end()); }
  160. reverse_iterator
  161. rend() _GLIBCXX_NOEXCEPT
  162. { return reverse_iterator(begin()); }
  163. const_reverse_iterator
  164. rend() const _GLIBCXX_NOEXCEPT
  165. { return const_reverse_iterator(begin()); }
  166. #if __cplusplus >= 201103L
  167. const_iterator
  168. cbegin() const noexcept
  169. { return const_iterator(_Base::begin(), this); }
  170. const_iterator
  171. cend() const noexcept
  172. { return const_iterator(_Base::end(), this); }
  173. const_reverse_iterator
  174. crbegin() const noexcept
  175. { return const_reverse_iterator(end()); }
  176. const_reverse_iterator
  177. crend() const noexcept
  178. { return const_reverse_iterator(begin()); }
  179. #endif
  180. // capacity:
  181. using _Base::empty;
  182. using _Base::size;
  183. using _Base::max_size;
  184. // 23.3.1.2 element access:
  185. using _Base::operator[];
  186. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  187. // DR 464. Suggestion for new member functions in standard containers.
  188. using _Base::at;
  189. // modifiers:
  190. #if __cplusplus >= 201103L
  191. template<typename... _Args>
  192. std::pair<iterator, bool>
  193. emplace(_Args&&... __args)
  194. {
  195. auto __res = _Base::emplace(std::forward<_Args>(__args)...);
  196. return { { __res.first, this }, __res.second };
  197. }
  198. template<typename... _Args>
  199. iterator
  200. emplace_hint(const_iterator __pos, _Args&&... __args)
  201. {
  202. __glibcxx_check_insert(__pos);
  203. return
  204. {
  205. _Base::emplace_hint(__pos.base(), std::forward<_Args>(__args)...),
  206. this
  207. };
  208. }
  209. #endif
  210. std::pair<iterator, bool>
  211. insert(const value_type& __x)
  212. {
  213. std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
  214. return std::pair<iterator, bool>(iterator(__res.first, this),
  215. __res.second);
  216. }
  217. #if __cplusplus >= 201103L
  218. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  219. // 2354. Unnecessary copying when inserting into maps with braced-init
  220. std::pair<iterator, bool>
  221. insert(value_type&& __x)
  222. {
  223. auto __res = _Base::insert(std::move(__x));
  224. return { { __res.first, this }, __res.second };
  225. }
  226. template<typename _Pair, typename = typename
  227. std::enable_if<std::is_constructible<value_type,
  228. _Pair&&>::value>::type>
  229. std::pair<iterator, bool>
  230. insert(_Pair&& __x)
  231. {
  232. auto __res = _Base::insert(std::forward<_Pair>(__x));
  233. return { { __res.first, this }, __res.second };
  234. }
  235. #endif
  236. #if __cplusplus >= 201103L
  237. void
  238. insert(std::initializer_list<value_type> __list)
  239. { _Base::insert(__list); }
  240. #endif
  241. iterator
  242. #if __cplusplus >= 201103L
  243. insert(const_iterator __position, const value_type& __x)
  244. #else
  245. insert(iterator __position, const value_type& __x)
  246. #endif
  247. {
  248. __glibcxx_check_insert(__position);
  249. return iterator(_Base::insert(__position.base(), __x), this);
  250. }
  251. #if __cplusplus >= 201103L
  252. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  253. // 2354. Unnecessary copying when inserting into maps with braced-init
  254. iterator
  255. insert(const_iterator __position, value_type&& __x)
  256. {
  257. __glibcxx_check_insert(__position);
  258. return { _Base::insert(__position.base(), std::move(__x)), this };
  259. }
  260. template<typename _Pair, typename = typename
  261. std::enable_if<std::is_constructible<value_type,
  262. _Pair&&>::value>::type>
  263. iterator
  264. insert(const_iterator __position, _Pair&& __x)
  265. {
  266. __glibcxx_check_insert(__position);
  267. return
  268. {
  269. _Base::insert(__position.base(), std::forward<_Pair>(__x)),
  270. this
  271. };
  272. }
  273. #endif
  274. template<typename _InputIterator>
  275. void
  276. insert(_InputIterator __first, _InputIterator __last)
  277. {
  278. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  279. __glibcxx_check_valid_range2(__first, __last, __dist);
  280. if (__dist.second >= __gnu_debug::__dp_sign)
  281. _Base::insert(__gnu_debug::__unsafe(__first),
  282. __gnu_debug::__unsafe(__last));
  283. else
  284. _Base::insert(__first, __last);
  285. }
  286. #if __cplusplus > 201402L
  287. template <typename... _Args>
  288. pair<iterator, bool>
  289. try_emplace(const key_type& __k, _Args&&... __args)
  290. {
  291. auto __res = _Base::try_emplace(__k,
  292. std::forward<_Args>(__args)...);
  293. return { { __res.first, this }, __res.second };
  294. }
  295. template <typename... _Args>
  296. pair<iterator, bool>
  297. try_emplace(key_type&& __k, _Args&&... __args)
  298. {
  299. auto __res = _Base::try_emplace(std::move(__k),
  300. std::forward<_Args>(__args)...);
  301. return { { __res.first, this }, __res.second };
  302. }
  303. template <typename... _Args>
  304. iterator
  305. try_emplace(const_iterator __hint, const key_type& __k,
  306. _Args&&... __args)
  307. {
  308. __glibcxx_check_insert(__hint);
  309. return
  310. {
  311. _Base::try_emplace(__hint.base(), __k,
  312. std::forward<_Args>(__args)...),
  313. this
  314. };
  315. }
  316. template <typename... _Args>
  317. iterator
  318. try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
  319. {
  320. __glibcxx_check_insert(__hint);
  321. return
  322. {
  323. _Base::try_emplace(__hint.base(), std::move(__k),
  324. std::forward<_Args>(__args)...),
  325. this
  326. };
  327. }
  328. template <typename _Obj>
  329. std::pair<iterator, bool>
  330. insert_or_assign(const key_type& __k, _Obj&& __obj)
  331. {
  332. auto __res = _Base::insert_or_assign(__k,
  333. std::forward<_Obj>(__obj));
  334. return { { __res.first, this }, __res.second };
  335. }
  336. template <typename _Obj>
  337. std::pair<iterator, bool>
  338. insert_or_assign(key_type&& __k, _Obj&& __obj)
  339. {
  340. auto __res = _Base::insert_or_assign(std::move(__k),
  341. std::forward<_Obj>(__obj));
  342. return { { __res.first, this }, __res.second };
  343. }
  344. template <typename _Obj>
  345. iterator
  346. insert_or_assign(const_iterator __hint,
  347. const key_type& __k, _Obj&& __obj)
  348. {
  349. __glibcxx_check_insert(__hint);
  350. return
  351. {
  352. _Base::insert_or_assign(__hint.base(), __k,
  353. std::forward<_Obj>(__obj)),
  354. this
  355. };
  356. }
  357. template <typename _Obj>
  358. iterator
  359. insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
  360. {
  361. __glibcxx_check_insert(__hint);
  362. return
  363. {
  364. _Base::insert_or_assign(__hint.base(), std::move(__k),
  365. std::forward<_Obj>(__obj)),
  366. this
  367. };
  368. }
  369. #endif // C++17
  370. #if __cplusplus > 201402L
  371. using node_type = typename _Base::node_type;
  372. using insert_return_type = _Node_insert_return<iterator, node_type>;
  373. node_type
  374. extract(const_iterator __position)
  375. {
  376. __glibcxx_check_erase(__position);
  377. this->_M_invalidate_if(_Equal(__position.base()));
  378. return _Base::extract(__position.base());
  379. }
  380. node_type
  381. extract(const key_type& __key)
  382. {
  383. const auto __position = find(__key);
  384. if (__position != end())
  385. return extract(__position);
  386. return {};
  387. }
  388. insert_return_type
  389. insert(node_type&& __nh)
  390. {
  391. auto __ret = _Base::insert(std::move(__nh));
  392. return
  393. { { __ret.position, this }, __ret.inserted, std::move(__ret.node) };
  394. }
  395. iterator
  396. insert(const_iterator __hint, node_type&& __nh)
  397. {
  398. __glibcxx_check_insert(__hint);
  399. return { _Base::insert(__hint.base(), std::move(__nh)), this };
  400. }
  401. using _Base::merge;
  402. #endif // C++17
  403. #if __cplusplus >= 201103L
  404. iterator
  405. erase(const_iterator __position)
  406. {
  407. __glibcxx_check_erase(__position);
  408. return { erase(__position.base()), this };
  409. }
  410. _Base_iterator
  411. erase(_Base_const_iterator __position)
  412. {
  413. __glibcxx_check_erase2(__position);
  414. this->_M_invalidate_if(_Equal(__position));
  415. return _Base::erase(__position);
  416. }
  417. _GLIBCXX_ABI_TAG_CXX11
  418. iterator
  419. erase(iterator __position)
  420. { return erase(const_iterator(__position)); }
  421. #else
  422. void
  423. erase(iterator __position)
  424. {
  425. __glibcxx_check_erase(__position);
  426. this->_M_invalidate_if(_Equal(__position.base()));
  427. _Base::erase(__position.base());
  428. }
  429. #endif
  430. size_type
  431. erase(const key_type& __x)
  432. {
  433. _Base_iterator __victim = _Base::find(__x);
  434. if (__victim == _Base::end())
  435. return 0;
  436. else
  437. {
  438. this->_M_invalidate_if(_Equal(__victim));
  439. _Base::erase(__victim);
  440. return 1;
  441. }
  442. }
  443. #if __cplusplus >= 201103L
  444. iterator
  445. erase(const_iterator __first, const_iterator __last)
  446. {
  447. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  448. // 151. can't currently clear() empty container
  449. __glibcxx_check_erase_range(__first, __last);
  450. for (_Base_const_iterator __victim = __first.base();
  451. __victim != __last.base(); ++__victim)
  452. {
  453. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::cend(),
  454. _M_message(__gnu_debug::__msg_valid_range)
  455. ._M_iterator(__first, "first")
  456. ._M_iterator(__last, "last"));
  457. this->_M_invalidate_if(_Equal(__victim));
  458. }
  459. return { _Base::erase(__first.base(), __last.base()), this };
  460. }
  461. #else
  462. void
  463. erase(iterator __first, iterator __last)
  464. {
  465. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  466. // 151. can't currently clear() empty container
  467. __glibcxx_check_erase_range(__first, __last);
  468. for (_Base_iterator __victim = __first.base();
  469. __victim != __last.base(); ++__victim)
  470. {
  471. _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
  472. _M_message(__gnu_debug::__msg_valid_range)
  473. ._M_iterator(__first, "first")
  474. ._M_iterator(__last, "last"));
  475. this->_M_invalidate_if(_Equal(__victim));
  476. }
  477. _Base::erase(__first.base(), __last.base());
  478. }
  479. #endif
  480. void
  481. swap(map& __x)
  482. _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
  483. {
  484. _Safe::_M_swap(__x);
  485. _Base::swap(__x);
  486. }
  487. void
  488. clear() _GLIBCXX_NOEXCEPT
  489. {
  490. this->_M_invalidate_all();
  491. _Base::clear();
  492. }
  493. // observers:
  494. using _Base::key_comp;
  495. using _Base::value_comp;
  496. // 23.3.1.3 map operations:
  497. iterator
  498. find(const key_type& __x)
  499. { return iterator(_Base::find(__x), this); }
  500. #if __cplusplus > 201103L
  501. template<typename _Kt,
  502. typename _Req =
  503. typename __has_is_transparent<_Compare, _Kt>::type>
  504. iterator
  505. find(const _Kt& __x)
  506. { return { _Base::find(__x), this }; }
  507. #endif
  508. const_iterator
  509. find(const key_type& __x) const
  510. { return const_iterator(_Base::find(__x), this); }
  511. #if __cplusplus > 201103L
  512. template<typename _Kt,
  513. typename _Req =
  514. typename __has_is_transparent<_Compare, _Kt>::type>
  515. const_iterator
  516. find(const _Kt& __x) const
  517. { return { _Base::find(__x), this }; }
  518. #endif
  519. using _Base::count;
  520. iterator
  521. lower_bound(const key_type& __x)
  522. { return iterator(_Base::lower_bound(__x), this); }
  523. #if __cplusplus > 201103L
  524. template<typename _Kt,
  525. typename _Req =
  526. typename __has_is_transparent<_Compare, _Kt>::type>
  527. iterator
  528. lower_bound(const _Kt& __x)
  529. { return { _Base::lower_bound(__x), this }; }
  530. #endif
  531. const_iterator
  532. lower_bound(const key_type& __x) const
  533. { return const_iterator(_Base::lower_bound(__x), this); }
  534. #if __cplusplus > 201103L
  535. template<typename _Kt,
  536. typename _Req =
  537. typename __has_is_transparent<_Compare, _Kt>::type>
  538. const_iterator
  539. lower_bound(const _Kt& __x) const
  540. { return { _Base::lower_bound(__x), this }; }
  541. #endif
  542. iterator
  543. upper_bound(const key_type& __x)
  544. { return iterator(_Base::upper_bound(__x), this); }
  545. #if __cplusplus > 201103L
  546. template<typename _Kt,
  547. typename _Req =
  548. typename __has_is_transparent<_Compare, _Kt>::type>
  549. iterator
  550. upper_bound(const _Kt& __x)
  551. { return { _Base::upper_bound(__x), this }; }
  552. #endif
  553. const_iterator
  554. upper_bound(const key_type& __x) const
  555. { return const_iterator(_Base::upper_bound(__x), this); }
  556. #if __cplusplus > 201103L
  557. template<typename _Kt,
  558. typename _Req =
  559. typename __has_is_transparent<_Compare, _Kt>::type>
  560. const_iterator
  561. upper_bound(const _Kt& __x) const
  562. { return { _Base::upper_bound(__x), this }; }
  563. #endif
  564. std::pair<iterator,iterator>
  565. equal_range(const key_type& __x)
  566. {
  567. std::pair<_Base_iterator, _Base_iterator> __res =
  568. _Base::equal_range(__x);
  569. return std::make_pair(iterator(__res.first, this),
  570. iterator(__res.second, this));
  571. }
  572. #if __cplusplus > 201103L
  573. template<typename _Kt,
  574. typename _Req =
  575. typename __has_is_transparent<_Compare, _Kt>::type>
  576. std::pair<iterator, iterator>
  577. equal_range(const _Kt& __x)
  578. {
  579. auto __res = _Base::equal_range(__x);
  580. return { { __res.first, this }, { __res.second, this } };
  581. }
  582. #endif
  583. std::pair<const_iterator,const_iterator>
  584. equal_range(const key_type& __x) const
  585. {
  586. std::pair<_Base_const_iterator, _Base_const_iterator> __res =
  587. _Base::equal_range(__x);
  588. return std::make_pair(const_iterator(__res.first, this),
  589. const_iterator(__res.second, this));
  590. }
  591. #if __cplusplus > 201103L
  592. template<typename _Kt,
  593. typename _Req =
  594. typename __has_is_transparent<_Compare, _Kt>::type>
  595. std::pair<const_iterator, const_iterator>
  596. equal_range(const _Kt& __x) const
  597. {
  598. auto __res = _Base::equal_range(__x);
  599. return { { __res.first, this }, { __res.second, this } };
  600. }
  601. #endif
  602. _Base&
  603. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  604. const _Base&
  605. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  606. };
  607. #if __cpp_deduction_guides >= 201606
  608. template<typename _InputIterator,
  609. typename _Compare = less<__iter_key_t<_InputIterator>>,
  610. typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
  611. typename = _RequireInputIter<_InputIterator>,
  612. typename = _RequireNotAllocator<_Compare>,
  613. typename = _RequireAllocator<_Allocator>>
  614. map(_InputIterator, _InputIterator,
  615. _Compare = _Compare(), _Allocator = _Allocator())
  616. -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
  617. _Compare, _Allocator>;
  618. template<typename _Key, typename _Tp, typename _Compare = less<_Key>,
  619. typename _Allocator = allocator<pair<const _Key, _Tp>>,
  620. typename = _RequireNotAllocator<_Compare>,
  621. typename = _RequireAllocator<_Allocator>>
  622. map(initializer_list<pair<_Key, _Tp>>,
  623. _Compare = _Compare(), _Allocator = _Allocator())
  624. -> map<_Key, _Tp, _Compare, _Allocator>;
  625. template <typename _InputIterator, typename _Allocator,
  626. typename = _RequireInputIter<_InputIterator>,
  627. typename = _RequireAllocator<_Allocator>>
  628. map(_InputIterator, _InputIterator, _Allocator)
  629. -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
  630. less<__iter_key_t<_InputIterator>>, _Allocator>;
  631. template<typename _Key, typename _Tp, typename _Allocator,
  632. typename = _RequireAllocator<_Allocator>>
  633. map(initializer_list<pair<_Key, _Tp>>, _Allocator)
  634. -> map<_Key, _Tp, less<_Key>, _Allocator>;
  635. #endif // deduction guides
  636. template<typename _Key, typename _Tp,
  637. typename _Compare, typename _Allocator>
  638. inline bool
  639. operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  640. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  641. { return __lhs._M_base() == __rhs._M_base(); }
  642. #if __cpp_lib_three_way_comparison
  643. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  644. inline __detail::__synth3way_t<pair<const _Key, _Tp>>
  645. operator<=>(const map<_Key, _Tp, _Compare, _Alloc>& __lhs,
  646. const map<_Key, _Tp, _Compare, _Alloc>& __rhs)
  647. { return __lhs._M_base() <=> __rhs._M_base(); }
  648. #else
  649. template<typename _Key, typename _Tp,
  650. typename _Compare, typename _Allocator>
  651. inline bool
  652. operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  653. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  654. { return __lhs._M_base() != __rhs._M_base(); }
  655. template<typename _Key, typename _Tp,
  656. typename _Compare, typename _Allocator>
  657. inline bool
  658. operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  659. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  660. { return __lhs._M_base() < __rhs._M_base(); }
  661. template<typename _Key, typename _Tp,
  662. typename _Compare, typename _Allocator>
  663. inline bool
  664. operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  665. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  666. { return __lhs._M_base() <= __rhs._M_base(); }
  667. template<typename _Key, typename _Tp,
  668. typename _Compare, typename _Allocator>
  669. inline bool
  670. operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  671. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  672. { return __lhs._M_base() >= __rhs._M_base(); }
  673. template<typename _Key, typename _Tp,
  674. typename _Compare, typename _Allocator>
  675. inline bool
  676. operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  677. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  678. { return __lhs._M_base() > __rhs._M_base(); }
  679. #endif // three-way comparison
  680. template<typename _Key, typename _Tp,
  681. typename _Compare, typename _Allocator>
  682. inline void
  683. swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  684. map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  685. _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
  686. { __lhs.swap(__rhs); }
  687. } // namespace __debug
  688. } // namespace std
  689. #endif