You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1569 lines
53KB

  1. // Map implementation -*- C++ -*-
  2. // Copyright (C) 2001-2020 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. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996,1997
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/stl_map.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{map}
  48. */
  49. #ifndef _STL_MAP_H
  50. #define _STL_MAP_H 1
  51. #include <bits/functexcept.h>
  52. #include <bits/concept_check.h>
  53. #if __cplusplus >= 201103L
  54. #include <initializer_list>
  55. #include <tuple>
  56. #endif
  57. namespace std _GLIBCXX_VISIBILITY(default)
  58. {
  59. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  60. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  61. template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  62. class multimap;
  63. /**
  64. * @brief A standard container made up of (key,value) pairs, which can be
  65. * retrieved based on a key, in logarithmic time.
  66. *
  67. * @ingroup associative_containers
  68. *
  69. * @tparam _Key Type of key objects.
  70. * @tparam _Tp Type of mapped objects.
  71. * @tparam _Compare Comparison function object type, defaults to less<_Key>.
  72. * @tparam _Alloc Allocator type, defaults to
  73. * allocator<pair<const _Key, _Tp>.
  74. *
  75. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  76. * <a href="tables.html#66">reversible container</a>, and an
  77. * <a href="tables.html#69">associative container</a> (using unique keys).
  78. * For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the
  79. * value_type is std::pair<const Key,T>.
  80. *
  81. * Maps support bidirectional iterators.
  82. *
  83. * The private tree data is declared exactly the same way for map and
  84. * multimap; the distinction is made entirely in how the tree functions are
  85. * called (*_unique versus *_equal, same as the standard).
  86. */
  87. template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
  88. typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  89. class map
  90. {
  91. public:
  92. typedef _Key key_type;
  93. typedef _Tp mapped_type;
  94. typedef std::pair<const _Key, _Tp> value_type;
  95. typedef _Compare key_compare;
  96. typedef _Alloc allocator_type;
  97. private:
  98. #ifdef _GLIBCXX_CONCEPT_CHECKS
  99. // concept requirements
  100. typedef typename _Alloc::value_type _Alloc_value_type;
  101. # if __cplusplus < 201103L
  102. __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
  103. # endif
  104. __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
  105. _BinaryFunctionConcept)
  106. __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
  107. #endif
  108. #if __cplusplus >= 201103L
  109. #if __cplusplus > 201703L || defined __STRICT_ANSI__
  110. static_assert(is_same<typename _Alloc::value_type, value_type>::value,
  111. "std::map must have the same value_type as its allocator");
  112. #endif
  113. #endif
  114. public:
  115. class value_compare
  116. : public std::binary_function<value_type, value_type, bool>
  117. {
  118. friend class map<_Key, _Tp, _Compare, _Alloc>;
  119. protected:
  120. _Compare comp;
  121. value_compare(_Compare __c)
  122. : comp(__c) { }
  123. public:
  124. bool operator()(const value_type& __x, const value_type& __y) const
  125. { return comp(__x.first, __y.first); }
  126. };
  127. private:
  128. /// This turns a red-black tree into a [multi]map.
  129. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  130. rebind<value_type>::other _Pair_alloc_type;
  131. typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
  132. key_compare, _Pair_alloc_type> _Rep_type;
  133. /// The actual tree structure.
  134. _Rep_type _M_t;
  135. typedef __gnu_cxx::__alloc_traits<_Pair_alloc_type> _Alloc_traits;
  136. public:
  137. // many of these are specified differently in ISO, but the following are
  138. // "functionally equivalent"
  139. typedef typename _Alloc_traits::pointer pointer;
  140. typedef typename _Alloc_traits::const_pointer const_pointer;
  141. typedef typename _Alloc_traits::reference reference;
  142. typedef typename _Alloc_traits::const_reference const_reference;
  143. typedef typename _Rep_type::iterator iterator;
  144. typedef typename _Rep_type::const_iterator const_iterator;
  145. typedef typename _Rep_type::size_type size_type;
  146. typedef typename _Rep_type::difference_type difference_type;
  147. typedef typename _Rep_type::reverse_iterator reverse_iterator;
  148. typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
  149. #if __cplusplus > 201402L
  150. using node_type = typename _Rep_type::node_type;
  151. using insert_return_type = typename _Rep_type::insert_return_type;
  152. #endif
  153. // [23.3.1.1] construct/copy/destroy
  154. // (get_allocator() is also listed in this section)
  155. /**
  156. * @brief Default constructor creates no elements.
  157. */
  158. #if __cplusplus < 201103L
  159. map() : _M_t() { }
  160. #else
  161. map() = default;
  162. #endif
  163. /**
  164. * @brief Creates a %map with no elements.
  165. * @param __comp A comparison object.
  166. * @param __a An allocator object.
  167. */
  168. explicit
  169. map(const _Compare& __comp,
  170. const allocator_type& __a = allocator_type())
  171. : _M_t(__comp, _Pair_alloc_type(__a)) { }
  172. /**
  173. * @brief %Map copy constructor.
  174. *
  175. * Whether the allocator is copied depends on the allocator traits.
  176. */
  177. #if __cplusplus < 201103L
  178. map(const map& __x)
  179. : _M_t(__x._M_t) { }
  180. #else
  181. map(const map&) = default;
  182. /**
  183. * @brief %Map move constructor.
  184. *
  185. * The newly-created %map contains the exact contents of the moved
  186. * instance. The moved instance is a valid, but unspecified, %map.
  187. */
  188. map(map&&) = default;
  189. /**
  190. * @brief Builds a %map from an initializer_list.
  191. * @param __l An initializer_list.
  192. * @param __comp A comparison object.
  193. * @param __a An allocator object.
  194. *
  195. * Create a %map consisting of copies of the elements in the
  196. * initializer_list @a __l.
  197. * This is linear in N if the range is already sorted, and NlogN
  198. * otherwise (where N is @a __l.size()).
  199. */
  200. map(initializer_list<value_type> __l,
  201. const _Compare& __comp = _Compare(),
  202. const allocator_type& __a = allocator_type())
  203. : _M_t(__comp, _Pair_alloc_type(__a))
  204. { _M_t._M_insert_range_unique(__l.begin(), __l.end()); }
  205. /// Allocator-extended default constructor.
  206. explicit
  207. map(const allocator_type& __a)
  208. : _M_t(_Pair_alloc_type(__a)) { }
  209. /// Allocator-extended copy constructor.
  210. map(const map& __m, const allocator_type& __a)
  211. : _M_t(__m._M_t, _Pair_alloc_type(__a)) { }
  212. /// Allocator-extended move constructor.
  213. map(map&& __m, const allocator_type& __a)
  214. noexcept(is_nothrow_copy_constructible<_Compare>::value
  215. && _Alloc_traits::_S_always_equal())
  216. : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { }
  217. /// Allocator-extended initialier-list constructor.
  218. map(initializer_list<value_type> __l, const allocator_type& __a)
  219. : _M_t(_Pair_alloc_type(__a))
  220. { _M_t._M_insert_range_unique(__l.begin(), __l.end()); }
  221. /// Allocator-extended range constructor.
  222. template<typename _InputIterator>
  223. map(_InputIterator __first, _InputIterator __last,
  224. const allocator_type& __a)
  225. : _M_t(_Pair_alloc_type(__a))
  226. { _M_t._M_insert_range_unique(__first, __last); }
  227. #endif
  228. /**
  229. * @brief Builds a %map from a range.
  230. * @param __first An input iterator.
  231. * @param __last An input iterator.
  232. *
  233. * Create a %map consisting of copies of the elements from
  234. * [__first,__last). This is linear in N if the range is
  235. * already sorted, and NlogN otherwise (where N is
  236. * distance(__first,__last)).
  237. */
  238. template<typename _InputIterator>
  239. map(_InputIterator __first, _InputIterator __last)
  240. : _M_t()
  241. { _M_t._M_insert_range_unique(__first, __last); }
  242. /**
  243. * @brief Builds a %map from a range.
  244. * @param __first An input iterator.
  245. * @param __last An input iterator.
  246. * @param __comp A comparison functor.
  247. * @param __a An allocator object.
  248. *
  249. * Create a %map consisting of copies of the elements from
  250. * [__first,__last). This is linear in N if the range is
  251. * already sorted, and NlogN otherwise (where N is
  252. * distance(__first,__last)).
  253. */
  254. template<typename _InputIterator>
  255. map(_InputIterator __first, _InputIterator __last,
  256. const _Compare& __comp,
  257. const allocator_type& __a = allocator_type())
  258. : _M_t(__comp, _Pair_alloc_type(__a))
  259. { _M_t._M_insert_range_unique(__first, __last); }
  260. #if __cplusplus >= 201103L
  261. /**
  262. * The dtor only erases the elements, and note that if the elements
  263. * themselves are pointers, the pointed-to memory is not touched in any
  264. * way. Managing the pointer is the user's responsibility.
  265. */
  266. ~map() = default;
  267. #endif
  268. /**
  269. * @brief %Map assignment operator.
  270. *
  271. * Whether the allocator is copied depends on the allocator traits.
  272. */
  273. #if __cplusplus < 201103L
  274. map&
  275. operator=(const map& __x)
  276. {
  277. _M_t = __x._M_t;
  278. return *this;
  279. }
  280. #else
  281. map&
  282. operator=(const map&) = default;
  283. /// Move assignment operator.
  284. map&
  285. operator=(map&&) = default;
  286. /**
  287. * @brief %Map list assignment operator.
  288. * @param __l An initializer_list.
  289. *
  290. * This function fills a %map with copies of the elements in the
  291. * initializer list @a __l.
  292. *
  293. * Note that the assignment completely changes the %map and
  294. * that the resulting %map's size is the same as the number
  295. * of elements assigned.
  296. */
  297. map&
  298. operator=(initializer_list<value_type> __l)
  299. {
  300. _M_t._M_assign_unique(__l.begin(), __l.end());
  301. return *this;
  302. }
  303. #endif
  304. /// Get a copy of the memory allocation object.
  305. allocator_type
  306. get_allocator() const _GLIBCXX_NOEXCEPT
  307. { return allocator_type(_M_t.get_allocator()); }
  308. // iterators
  309. /**
  310. * Returns a read/write iterator that points to the first pair in the
  311. * %map.
  312. * Iteration is done in ascending order according to the keys.
  313. */
  314. iterator
  315. begin() _GLIBCXX_NOEXCEPT
  316. { return _M_t.begin(); }
  317. /**
  318. * Returns a read-only (constant) iterator that points to the first pair
  319. * in the %map. Iteration is done in ascending order according to the
  320. * keys.
  321. */
  322. const_iterator
  323. begin() const _GLIBCXX_NOEXCEPT
  324. { return _M_t.begin(); }
  325. /**
  326. * Returns a read/write iterator that points one past the last
  327. * pair in the %map. Iteration is done in ascending order
  328. * according to the keys.
  329. */
  330. iterator
  331. end() _GLIBCXX_NOEXCEPT
  332. { return _M_t.end(); }
  333. /**
  334. * Returns a read-only (constant) iterator that points one past the last
  335. * pair in the %map. Iteration is done in ascending order according to
  336. * the keys.
  337. */
  338. const_iterator
  339. end() const _GLIBCXX_NOEXCEPT
  340. { return _M_t.end(); }
  341. /**
  342. * Returns a read/write reverse iterator that points to the last pair in
  343. * the %map. Iteration is done in descending order according to the
  344. * keys.
  345. */
  346. reverse_iterator
  347. rbegin() _GLIBCXX_NOEXCEPT
  348. { return _M_t.rbegin(); }
  349. /**
  350. * Returns a read-only (constant) reverse iterator that points to the
  351. * last pair in the %map. Iteration is done in descending order
  352. * according to the keys.
  353. */
  354. const_reverse_iterator
  355. rbegin() const _GLIBCXX_NOEXCEPT
  356. { return _M_t.rbegin(); }
  357. /**
  358. * Returns a read/write reverse iterator that points to one before the
  359. * first pair in the %map. Iteration is done in descending order
  360. * according to the keys.
  361. */
  362. reverse_iterator
  363. rend() _GLIBCXX_NOEXCEPT
  364. { return _M_t.rend(); }
  365. /**
  366. * Returns a read-only (constant) reverse iterator that points to one
  367. * before the first pair in the %map. Iteration is done in descending
  368. * order according to the keys.
  369. */
  370. const_reverse_iterator
  371. rend() const _GLIBCXX_NOEXCEPT
  372. { return _M_t.rend(); }
  373. #if __cplusplus >= 201103L
  374. /**
  375. * Returns a read-only (constant) iterator that points to the first pair
  376. * in the %map. Iteration is done in ascending order according to the
  377. * keys.
  378. */
  379. const_iterator
  380. cbegin() const noexcept
  381. { return _M_t.begin(); }
  382. /**
  383. * Returns a read-only (constant) iterator that points one past the last
  384. * pair in the %map. Iteration is done in ascending order according to
  385. * the keys.
  386. */
  387. const_iterator
  388. cend() const noexcept
  389. { return _M_t.end(); }
  390. /**
  391. * Returns a read-only (constant) reverse iterator that points to the
  392. * last pair in the %map. Iteration is done in descending order
  393. * according to the keys.
  394. */
  395. const_reverse_iterator
  396. crbegin() const noexcept
  397. { return _M_t.rbegin(); }
  398. /**
  399. * Returns a read-only (constant) reverse iterator that points to one
  400. * before the first pair in the %map. Iteration is done in descending
  401. * order according to the keys.
  402. */
  403. const_reverse_iterator
  404. crend() const noexcept
  405. { return _M_t.rend(); }
  406. #endif
  407. // capacity
  408. /** Returns true if the %map is empty. (Thus begin() would equal
  409. * end().)
  410. */
  411. _GLIBCXX_NODISCARD bool
  412. empty() const _GLIBCXX_NOEXCEPT
  413. { return _M_t.empty(); }
  414. /** Returns the size of the %map. */
  415. size_type
  416. size() const _GLIBCXX_NOEXCEPT
  417. { return _M_t.size(); }
  418. /** Returns the maximum size of the %map. */
  419. size_type
  420. max_size() const _GLIBCXX_NOEXCEPT
  421. { return _M_t.max_size(); }
  422. // [23.3.1.2] element access
  423. /**
  424. * @brief Subscript ( @c [] ) access to %map data.
  425. * @param __k The key for which data should be retrieved.
  426. * @return A reference to the data of the (key,data) %pair.
  427. *
  428. * Allows for easy lookup with the subscript ( @c [] )
  429. * operator. Returns data associated with the key specified in
  430. * subscript. If the key does not exist, a pair with that key
  431. * is created using default values, which is then returned.
  432. *
  433. * Lookup requires logarithmic time.
  434. */
  435. mapped_type&
  436. operator[](const key_type& __k)
  437. {
  438. // concept requirements
  439. __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
  440. iterator __i = lower_bound(__k);
  441. // __i->first is greater than or equivalent to __k.
  442. if (__i == end() || key_comp()(__k, (*__i).first))
  443. #if __cplusplus >= 201103L
  444. __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
  445. std::tuple<const key_type&>(__k),
  446. std::tuple<>());
  447. #else
  448. __i = insert(__i, value_type(__k, mapped_type()));
  449. #endif
  450. return (*__i).second;
  451. }
  452. #if __cplusplus >= 201103L
  453. mapped_type&
  454. operator[](key_type&& __k)
  455. {
  456. // concept requirements
  457. __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
  458. iterator __i = lower_bound(__k);
  459. // __i->first is greater than or equivalent to __k.
  460. if (__i == end() || key_comp()(__k, (*__i).first))
  461. __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
  462. std::forward_as_tuple(std::move(__k)),
  463. std::tuple<>());
  464. return (*__i).second;
  465. }
  466. #endif
  467. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  468. // DR 464. Suggestion for new member functions in standard containers.
  469. /**
  470. * @brief Access to %map data.
  471. * @param __k The key for which data should be retrieved.
  472. * @return A reference to the data whose key is equivalent to @a __k, if
  473. * such a data is present in the %map.
  474. * @throw std::out_of_range If no such data is present.
  475. */
  476. mapped_type&
  477. at(const key_type& __k)
  478. {
  479. iterator __i = lower_bound(__k);
  480. if (__i == end() || key_comp()(__k, (*__i).first))
  481. __throw_out_of_range(__N("map::at"));
  482. return (*__i).second;
  483. }
  484. const mapped_type&
  485. at(const key_type& __k) const
  486. {
  487. const_iterator __i = lower_bound(__k);
  488. if (__i == end() || key_comp()(__k, (*__i).first))
  489. __throw_out_of_range(__N("map::at"));
  490. return (*__i).second;
  491. }
  492. // modifiers
  493. #if __cplusplus >= 201103L
  494. /**
  495. * @brief Attempts to build and insert a std::pair into the %map.
  496. *
  497. * @param __args Arguments used to generate a new pair instance (see
  498. * std::piecewise_contruct for passing arguments to each
  499. * part of the pair constructor).
  500. *
  501. * @return A pair, of which the first element is an iterator that points
  502. * to the possibly inserted pair, and the second is a bool that
  503. * is true if the pair was actually inserted.
  504. *
  505. * This function attempts to build and insert a (key, value) %pair into
  506. * the %map.
  507. * A %map relies on unique keys and thus a %pair is only inserted if its
  508. * first element (the key) is not already present in the %map.
  509. *
  510. * Insertion requires logarithmic time.
  511. */
  512. template<typename... _Args>
  513. std::pair<iterator, bool>
  514. emplace(_Args&&... __args)
  515. { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
  516. /**
  517. * @brief Attempts to build and insert a std::pair into the %map.
  518. *
  519. * @param __pos An iterator that serves as a hint as to where the pair
  520. * should be inserted.
  521. * @param __args Arguments used to generate a new pair instance (see
  522. * std::piecewise_contruct for passing arguments to each
  523. * part of the pair constructor).
  524. * @return An iterator that points to the element with key of the
  525. * std::pair built from @a __args (may or may not be that
  526. * std::pair).
  527. *
  528. * This function is not concerned about whether the insertion took place,
  529. * and thus does not return a boolean like the single-argument emplace()
  530. * does.
  531. * Note that the first parameter is only a hint and can potentially
  532. * improve the performance of the insertion process. A bad hint would
  533. * cause no gains in efficiency.
  534. *
  535. * See
  536. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  537. * for more on @a hinting.
  538. *
  539. * Insertion requires logarithmic time (if the hint is not taken).
  540. */
  541. template<typename... _Args>
  542. iterator
  543. emplace_hint(const_iterator __pos, _Args&&... __args)
  544. {
  545. return _M_t._M_emplace_hint_unique(__pos,
  546. std::forward<_Args>(__args)...);
  547. }
  548. #endif
  549. #if __cplusplus > 201402L
  550. /// Extract a node.
  551. node_type
  552. extract(const_iterator __pos)
  553. {
  554. __glibcxx_assert(__pos != end());
  555. return _M_t.extract(__pos);
  556. }
  557. /// Extract a node.
  558. node_type
  559. extract(const key_type& __x)
  560. { return _M_t.extract(__x); }
  561. /// Re-insert an extracted node.
  562. insert_return_type
  563. insert(node_type&& __nh)
  564. { return _M_t._M_reinsert_node_unique(std::move(__nh)); }
  565. /// Re-insert an extracted node.
  566. iterator
  567. insert(const_iterator __hint, node_type&& __nh)
  568. { return _M_t._M_reinsert_node_hint_unique(__hint, std::move(__nh)); }
  569. template<typename, typename>
  570. friend class std::_Rb_tree_merge_helper;
  571. template<typename _Cmp2>
  572. void
  573. merge(map<_Key, _Tp, _Cmp2, _Alloc>& __source)
  574. {
  575. using _Merge_helper = _Rb_tree_merge_helper<map, _Cmp2>;
  576. _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
  577. }
  578. template<typename _Cmp2>
  579. void
  580. merge(map<_Key, _Tp, _Cmp2, _Alloc>&& __source)
  581. { merge(__source); }
  582. template<typename _Cmp2>
  583. void
  584. merge(multimap<_Key, _Tp, _Cmp2, _Alloc>& __source)
  585. {
  586. using _Merge_helper = _Rb_tree_merge_helper<map, _Cmp2>;
  587. _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
  588. }
  589. template<typename _Cmp2>
  590. void
  591. merge(multimap<_Key, _Tp, _Cmp2, _Alloc>&& __source)
  592. { merge(__source); }
  593. #endif // C++17
  594. #if __cplusplus > 201402L
  595. #define __cpp_lib_map_try_emplace 201411
  596. /**
  597. * @brief Attempts to build and insert a std::pair into the %map.
  598. *
  599. * @param __k Key to use for finding a possibly existing pair in
  600. * the map.
  601. * @param __args Arguments used to generate the .second for a new pair
  602. * instance.
  603. *
  604. * @return A pair, of which the first element is an iterator that points
  605. * to the possibly inserted pair, and the second is a bool that
  606. * is true if the pair was actually inserted.
  607. *
  608. * This function attempts to build and insert a (key, value) %pair into
  609. * the %map.
  610. * A %map relies on unique keys and thus a %pair is only inserted if its
  611. * first element (the key) is not already present in the %map.
  612. * If a %pair is not inserted, this function has no effect.
  613. *
  614. * Insertion requires logarithmic time.
  615. */
  616. template <typename... _Args>
  617. pair<iterator, bool>
  618. try_emplace(const key_type& __k, _Args&&... __args)
  619. {
  620. iterator __i = lower_bound(__k);
  621. if (__i == end() || key_comp()(__k, (*__i).first))
  622. {
  623. __i = emplace_hint(__i, std::piecewise_construct,
  624. std::forward_as_tuple(__k),
  625. std::forward_as_tuple(
  626. std::forward<_Args>(__args)...));
  627. return {__i, true};
  628. }
  629. return {__i, false};
  630. }
  631. // move-capable overload
  632. template <typename... _Args>
  633. pair<iterator, bool>
  634. try_emplace(key_type&& __k, _Args&&... __args)
  635. {
  636. iterator __i = lower_bound(__k);
  637. if (__i == end() || key_comp()(__k, (*__i).first))
  638. {
  639. __i = emplace_hint(__i, std::piecewise_construct,
  640. std::forward_as_tuple(std::move(__k)),
  641. std::forward_as_tuple(
  642. std::forward<_Args>(__args)...));
  643. return {__i, true};
  644. }
  645. return {__i, false};
  646. }
  647. /**
  648. * @brief Attempts to build and insert a std::pair into the %map.
  649. *
  650. * @param __hint An iterator that serves as a hint as to where the
  651. * pair should be inserted.
  652. * @param __k Key to use for finding a possibly existing pair in
  653. * the map.
  654. * @param __args Arguments used to generate the .second for a new pair
  655. * instance.
  656. * @return An iterator that points to the element with key of the
  657. * std::pair built from @a __args (may or may not be that
  658. * std::pair).
  659. *
  660. * This function is not concerned about whether the insertion took place,
  661. * and thus does not return a boolean like the single-argument
  662. * try_emplace() does. However, if insertion did not take place,
  663. * this function has no effect.
  664. * Note that the first parameter is only a hint and can potentially
  665. * improve the performance of the insertion process. A bad hint would
  666. * cause no gains in efficiency.
  667. *
  668. * See
  669. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  670. * for more on @a hinting.
  671. *
  672. * Insertion requires logarithmic time (if the hint is not taken).
  673. */
  674. template <typename... _Args>
  675. iterator
  676. try_emplace(const_iterator __hint, const key_type& __k,
  677. _Args&&... __args)
  678. {
  679. iterator __i;
  680. auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
  681. if (__true_hint.second)
  682. __i = emplace_hint(iterator(__true_hint.second),
  683. std::piecewise_construct,
  684. std::forward_as_tuple(__k),
  685. std::forward_as_tuple(
  686. std::forward<_Args>(__args)...));
  687. else
  688. __i = iterator(__true_hint.first);
  689. return __i;
  690. }
  691. // move-capable overload
  692. template <typename... _Args>
  693. iterator
  694. try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
  695. {
  696. iterator __i;
  697. auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
  698. if (__true_hint.second)
  699. __i = emplace_hint(iterator(__true_hint.second),
  700. std::piecewise_construct,
  701. std::forward_as_tuple(std::move(__k)),
  702. std::forward_as_tuple(
  703. std::forward<_Args>(__args)...));
  704. else
  705. __i = iterator(__true_hint.first);
  706. return __i;
  707. }
  708. #endif
  709. /**
  710. * @brief Attempts to insert a std::pair into the %map.
  711. * @param __x Pair to be inserted (see std::make_pair for easy
  712. * creation of pairs).
  713. *
  714. * @return A pair, of which the first element is an iterator that
  715. * points to the possibly inserted pair, and the second is
  716. * a bool that is true if the pair was actually inserted.
  717. *
  718. * This function attempts to insert a (key, value) %pair into the %map.
  719. * A %map relies on unique keys and thus a %pair is only inserted if its
  720. * first element (the key) is not already present in the %map.
  721. *
  722. * Insertion requires logarithmic time.
  723. * @{
  724. */
  725. std::pair<iterator, bool>
  726. insert(const value_type& __x)
  727. { return _M_t._M_insert_unique(__x); }
  728. #if __cplusplus >= 201103L
  729. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  730. // 2354. Unnecessary copying when inserting into maps with braced-init
  731. std::pair<iterator, bool>
  732. insert(value_type&& __x)
  733. { return _M_t._M_insert_unique(std::move(__x)); }
  734. template<typename _Pair>
  735. __enable_if_t<is_constructible<value_type, _Pair>::value,
  736. pair<iterator, bool>>
  737. insert(_Pair&& __x)
  738. { return _M_t._M_emplace_unique(std::forward<_Pair>(__x)); }
  739. #endif
  740. // @}
  741. #if __cplusplus >= 201103L
  742. /**
  743. * @brief Attempts to insert a list of std::pairs into the %map.
  744. * @param __list A std::initializer_list<value_type> of pairs to be
  745. * inserted.
  746. *
  747. * Complexity similar to that of the range constructor.
  748. */
  749. void
  750. insert(std::initializer_list<value_type> __list)
  751. { insert(__list.begin(), __list.end()); }
  752. #endif
  753. /**
  754. * @brief Attempts to insert a std::pair into the %map.
  755. * @param __position An iterator that serves as a hint as to where the
  756. * pair should be inserted.
  757. * @param __x Pair to be inserted (see std::make_pair for easy creation
  758. * of pairs).
  759. * @return An iterator that points to the element with key of
  760. * @a __x (may or may not be the %pair passed in).
  761. *
  762. * This function is not concerned about whether the insertion
  763. * took place, and thus does not return a boolean like the
  764. * single-argument insert() does. Note that the first
  765. * parameter is only a hint and can potentially improve the
  766. * performance of the insertion process. A bad hint would
  767. * cause no gains in efficiency.
  768. *
  769. * See
  770. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  771. * for more on @a hinting.
  772. *
  773. * Insertion requires logarithmic time (if the hint is not taken).
  774. * @{
  775. */
  776. iterator
  777. #if __cplusplus >= 201103L
  778. insert(const_iterator __position, const value_type& __x)
  779. #else
  780. insert(iterator __position, const value_type& __x)
  781. #endif
  782. { return _M_t._M_insert_unique_(__position, __x); }
  783. #if __cplusplus >= 201103L
  784. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  785. // 2354. Unnecessary copying when inserting into maps with braced-init
  786. iterator
  787. insert(const_iterator __position, value_type&& __x)
  788. { return _M_t._M_insert_unique_(__position, std::move(__x)); }
  789. template<typename _Pair>
  790. __enable_if_t<is_constructible<value_type, _Pair>::value, iterator>
  791. insert(const_iterator __position, _Pair&& __x)
  792. {
  793. return _M_t._M_emplace_hint_unique(__position,
  794. std::forward<_Pair>(__x));
  795. }
  796. #endif
  797. // @}
  798. /**
  799. * @brief Template function that attempts to insert a range of elements.
  800. * @param __first Iterator pointing to the start of the range to be
  801. * inserted.
  802. * @param __last Iterator pointing to the end of the range.
  803. *
  804. * Complexity similar to that of the range constructor.
  805. */
  806. template<typename _InputIterator>
  807. void
  808. insert(_InputIterator __first, _InputIterator __last)
  809. { _M_t._M_insert_range_unique(__first, __last); }
  810. #if __cplusplus > 201402L
  811. /**
  812. * @brief Attempts to insert or assign a std::pair into the %map.
  813. * @param __k Key to use for finding a possibly existing pair in
  814. * the map.
  815. * @param __obj Argument used to generate the .second for a pair
  816. * instance.
  817. *
  818. * @return A pair, of which the first element is an iterator that
  819. * points to the possibly inserted pair, and the second is
  820. * a bool that is true if the pair was actually inserted.
  821. *
  822. * This function attempts to insert a (key, value) %pair into the %map.
  823. * A %map relies on unique keys and thus a %pair is only inserted if its
  824. * first element (the key) is not already present in the %map.
  825. * If the %pair was already in the %map, the .second of the %pair
  826. * is assigned from __obj.
  827. *
  828. * Insertion requires logarithmic time.
  829. */
  830. template <typename _Obj>
  831. pair<iterator, bool>
  832. insert_or_assign(const key_type& __k, _Obj&& __obj)
  833. {
  834. iterator __i = lower_bound(__k);
  835. if (__i == end() || key_comp()(__k, (*__i).first))
  836. {
  837. __i = emplace_hint(__i, std::piecewise_construct,
  838. std::forward_as_tuple(__k),
  839. std::forward_as_tuple(
  840. std::forward<_Obj>(__obj)));
  841. return {__i, true};
  842. }
  843. (*__i).second = std::forward<_Obj>(__obj);
  844. return {__i, false};
  845. }
  846. // move-capable overload
  847. template <typename _Obj>
  848. pair<iterator, bool>
  849. insert_or_assign(key_type&& __k, _Obj&& __obj)
  850. {
  851. iterator __i = lower_bound(__k);
  852. if (__i == end() || key_comp()(__k, (*__i).first))
  853. {
  854. __i = emplace_hint(__i, std::piecewise_construct,
  855. std::forward_as_tuple(std::move(__k)),
  856. std::forward_as_tuple(
  857. std::forward<_Obj>(__obj)));
  858. return {__i, true};
  859. }
  860. (*__i).second = std::forward<_Obj>(__obj);
  861. return {__i, false};
  862. }
  863. /**
  864. * @brief Attempts to insert or assign a std::pair into the %map.
  865. * @param __hint An iterator that serves as a hint as to where the
  866. * pair should be inserted.
  867. * @param __k Key to use for finding a possibly existing pair in
  868. * the map.
  869. * @param __obj Argument used to generate the .second for a pair
  870. * instance.
  871. *
  872. * @return An iterator that points to the element with key of
  873. * @a __x (may or may not be the %pair passed in).
  874. *
  875. * This function attempts to insert a (key, value) %pair into the %map.
  876. * A %map relies on unique keys and thus a %pair is only inserted if its
  877. * first element (the key) is not already present in the %map.
  878. * If the %pair was already in the %map, the .second of the %pair
  879. * is assigned from __obj.
  880. *
  881. * Insertion requires logarithmic time.
  882. */
  883. template <typename _Obj>
  884. iterator
  885. insert_or_assign(const_iterator __hint,
  886. const key_type& __k, _Obj&& __obj)
  887. {
  888. iterator __i;
  889. auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
  890. if (__true_hint.second)
  891. {
  892. return emplace_hint(iterator(__true_hint.second),
  893. std::piecewise_construct,
  894. std::forward_as_tuple(__k),
  895. std::forward_as_tuple(
  896. std::forward<_Obj>(__obj)));
  897. }
  898. __i = iterator(__true_hint.first);
  899. (*__i).second = std::forward<_Obj>(__obj);
  900. return __i;
  901. }
  902. // move-capable overload
  903. template <typename _Obj>
  904. iterator
  905. insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
  906. {
  907. iterator __i;
  908. auto __true_hint = _M_t._M_get_insert_hint_unique_pos(__hint, __k);
  909. if (__true_hint.second)
  910. {
  911. return emplace_hint(iterator(__true_hint.second),
  912. std::piecewise_construct,
  913. std::forward_as_tuple(std::move(__k)),
  914. std::forward_as_tuple(
  915. std::forward<_Obj>(__obj)));
  916. }
  917. __i = iterator(__true_hint.first);
  918. (*__i).second = std::forward<_Obj>(__obj);
  919. return __i;
  920. }
  921. #endif
  922. #if __cplusplus >= 201103L
  923. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  924. // DR 130. Associative erase should return an iterator.
  925. /**
  926. * @brief Erases an element from a %map.
  927. * @param __position An iterator pointing to the element to be erased.
  928. * @return An iterator pointing to the element immediately following
  929. * @a position prior to the element being erased. If no such
  930. * element exists, end() is returned.
  931. *
  932. * This function erases an element, pointed to by the given
  933. * iterator, from a %map. Note that this function only erases
  934. * the element, and that if the element is itself a pointer,
  935. * the pointed-to memory is not touched in any way. Managing
  936. * the pointer is the user's responsibility.
  937. *
  938. * @{
  939. */
  940. iterator
  941. erase(const_iterator __position)
  942. { return _M_t.erase(__position); }
  943. // LWG 2059
  944. _GLIBCXX_ABI_TAG_CXX11
  945. iterator
  946. erase(iterator __position)
  947. { return _M_t.erase(__position); }
  948. // @}
  949. #else
  950. /**
  951. * @brief Erases an element from a %map.
  952. * @param __position An iterator pointing to the element to be erased.
  953. *
  954. * This function erases an element, pointed to by the given
  955. * iterator, from a %map. Note that this function only erases
  956. * the element, and that if the element is itself a pointer,
  957. * the pointed-to memory is not touched in any way. Managing
  958. * the pointer is the user's responsibility.
  959. */
  960. void
  961. erase(iterator __position)
  962. { _M_t.erase(__position); }
  963. #endif
  964. /**
  965. * @brief Erases elements according to the provided key.
  966. * @param __x Key of element to be erased.
  967. * @return The number of elements erased.
  968. *
  969. * This function erases all the elements located by the given key from
  970. * a %map.
  971. * Note that this function only erases the element, and that if
  972. * the element is itself a pointer, the pointed-to memory is not touched
  973. * in any way. Managing the pointer is the user's responsibility.
  974. */
  975. size_type
  976. erase(const key_type& __x)
  977. { return _M_t.erase(__x); }
  978. #if __cplusplus >= 201103L
  979. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  980. // DR 130. Associative erase should return an iterator.
  981. /**
  982. * @brief Erases a [first,last) range of elements from a %map.
  983. * @param __first Iterator pointing to the start of the range to be
  984. * erased.
  985. * @param __last Iterator pointing to the end of the range to
  986. * be erased.
  987. * @return The iterator @a __last.
  988. *
  989. * This function erases a sequence of elements from a %map.
  990. * Note that this function only erases the element, and that if
  991. * the element is itself a pointer, the pointed-to memory is not touched
  992. * in any way. Managing the pointer is the user's responsibility.
  993. */
  994. iterator
  995. erase(const_iterator __first, const_iterator __last)
  996. { return _M_t.erase(__first, __last); }
  997. #else
  998. /**
  999. * @brief Erases a [__first,__last) range of elements from a %map.
  1000. * @param __first Iterator pointing to the start of the range to be
  1001. * erased.
  1002. * @param __last Iterator pointing to the end of the range to
  1003. * be erased.
  1004. *
  1005. * This function erases a sequence of elements from a %map.
  1006. * Note that this function only erases the element, and that if
  1007. * the element is itself a pointer, the pointed-to memory is not touched
  1008. * in any way. Managing the pointer is the user's responsibility.
  1009. */
  1010. void
  1011. erase(iterator __first, iterator __last)
  1012. { _M_t.erase(__first, __last); }
  1013. #endif
  1014. /**
  1015. * @brief Swaps data with another %map.
  1016. * @param __x A %map of the same element and allocator types.
  1017. *
  1018. * This exchanges the elements between two maps in constant
  1019. * time. (It is only swapping a pointer, an integer, and an
  1020. * instance of the @c Compare type (which itself is often
  1021. * stateless and empty), so it should be quite fast.) Note
  1022. * that the global std::swap() function is specialized such
  1023. * that std::swap(m1,m2) will feed to this function.
  1024. *
  1025. * Whether the allocators are swapped depends on the allocator traits.
  1026. */
  1027. void
  1028. swap(map& __x)
  1029. _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
  1030. { _M_t.swap(__x._M_t); }
  1031. /**
  1032. * Erases all elements in a %map. Note that this function only
  1033. * erases the elements, and that if the elements themselves are
  1034. * pointers, the pointed-to memory is not touched in any way.
  1035. * Managing the pointer is the user's responsibility.
  1036. */
  1037. void
  1038. clear() _GLIBCXX_NOEXCEPT
  1039. { _M_t.clear(); }
  1040. // observers
  1041. /**
  1042. * Returns the key comparison object out of which the %map was
  1043. * constructed.
  1044. */
  1045. key_compare
  1046. key_comp() const
  1047. { return _M_t.key_comp(); }
  1048. /**
  1049. * Returns a value comparison object, built from the key comparison
  1050. * object out of which the %map was constructed.
  1051. */
  1052. value_compare
  1053. value_comp() const
  1054. { return value_compare(_M_t.key_comp()); }
  1055. // [23.3.1.3] map operations
  1056. //@{
  1057. /**
  1058. * @brief Tries to locate an element in a %map.
  1059. * @param __x Key of (key, value) %pair to be located.
  1060. * @return Iterator pointing to sought-after element, or end() if not
  1061. * found.
  1062. *
  1063. * This function takes a key and tries to locate the element with which
  1064. * the key matches. If successful the function returns an iterator
  1065. * pointing to the sought after %pair. If unsuccessful it returns the
  1066. * past-the-end ( @c end() ) iterator.
  1067. */
  1068. iterator
  1069. find(const key_type& __x)
  1070. { return _M_t.find(__x); }
  1071. #if __cplusplus > 201103L
  1072. template<typename _Kt>
  1073. auto
  1074. find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x))
  1075. { return _M_t._M_find_tr(__x); }
  1076. #endif
  1077. //@}
  1078. //@{
  1079. /**
  1080. * @brief Tries to locate an element in a %map.
  1081. * @param __x Key of (key, value) %pair to be located.
  1082. * @return Read-only (constant) iterator pointing to sought-after
  1083. * element, or end() if not found.
  1084. *
  1085. * This function takes a key and tries to locate the element with which
  1086. * the key matches. If successful the function returns a constant
  1087. * iterator pointing to the sought after %pair. If unsuccessful it
  1088. * returns the past-the-end ( @c end() ) iterator.
  1089. */
  1090. const_iterator
  1091. find(const key_type& __x) const
  1092. { return _M_t.find(__x); }
  1093. #if __cplusplus > 201103L
  1094. template<typename _Kt>
  1095. auto
  1096. find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x))
  1097. { return _M_t._M_find_tr(__x); }
  1098. #endif
  1099. //@}
  1100. //@{
  1101. /**
  1102. * @brief Finds the number of elements with given key.
  1103. * @param __x Key of (key, value) pairs to be located.
  1104. * @return Number of elements with specified key.
  1105. *
  1106. * This function only makes sense for multimaps; for map the result will
  1107. * either be 0 (not present) or 1 (present).
  1108. */
  1109. size_type
  1110. count(const key_type& __x) const
  1111. { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
  1112. #if __cplusplus > 201103L
  1113. template<typename _Kt>
  1114. auto
  1115. count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x))
  1116. { return _M_t._M_count_tr(__x); }
  1117. #endif
  1118. //@}
  1119. #if __cplusplus > 201703L
  1120. //@{
  1121. /**
  1122. * @brief Finds whether an element with the given key exists.
  1123. * @param __x Key of (key, value) pairs to be located.
  1124. * @return True if there is an element with the specified key.
  1125. */
  1126. bool
  1127. contains(const key_type& __x) const
  1128. { return _M_t.find(__x) != _M_t.end(); }
  1129. template<typename _Kt>
  1130. auto
  1131. contains(const _Kt& __x) const
  1132. -> decltype(_M_t._M_find_tr(__x), void(), true)
  1133. { return _M_t._M_find_tr(__x) != _M_t.end(); }
  1134. //@}
  1135. #endif
  1136. //@{
  1137. /**
  1138. * @brief Finds the beginning of a subsequence matching given key.
  1139. * @param __x Key of (key, value) pair to be located.
  1140. * @return Iterator pointing to first element equal to or greater
  1141. * than key, or end().
  1142. *
  1143. * This function returns the first element of a subsequence of elements
  1144. * that matches the given key. If unsuccessful it returns an iterator
  1145. * pointing to the first element that has a greater value than given key
  1146. * or end() if no such element exists.
  1147. */
  1148. iterator
  1149. lower_bound(const key_type& __x)
  1150. { return _M_t.lower_bound(__x); }
  1151. #if __cplusplus > 201103L
  1152. template<typename _Kt>
  1153. auto
  1154. lower_bound(const _Kt& __x)
  1155. -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
  1156. { return iterator(_M_t._M_lower_bound_tr(__x)); }
  1157. #endif
  1158. //@}
  1159. //@{
  1160. /**
  1161. * @brief Finds the beginning of a subsequence matching given key.
  1162. * @param __x Key of (key, value) pair to be located.
  1163. * @return Read-only (constant) iterator pointing to first element
  1164. * equal to or greater than key, or end().
  1165. *
  1166. * This function returns the first element of a subsequence of elements
  1167. * that matches the given key. If unsuccessful it returns an iterator
  1168. * pointing to the first element that has a greater value than given key
  1169. * or end() if no such element exists.
  1170. */
  1171. const_iterator
  1172. lower_bound(const key_type& __x) const
  1173. { return _M_t.lower_bound(__x); }
  1174. #if __cplusplus > 201103L
  1175. template<typename _Kt>
  1176. auto
  1177. lower_bound(const _Kt& __x) const
  1178. -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x)))
  1179. { return const_iterator(_M_t._M_lower_bound_tr(__x)); }
  1180. #endif
  1181. //@}
  1182. //@{
  1183. /**
  1184. * @brief Finds the end of a subsequence matching given key.
  1185. * @param __x Key of (key, value) pair to be located.
  1186. * @return Iterator pointing to the first element
  1187. * greater than key, or end().
  1188. */
  1189. iterator
  1190. upper_bound(const key_type& __x)
  1191. { return _M_t.upper_bound(__x); }
  1192. #if __cplusplus > 201103L
  1193. template<typename _Kt>
  1194. auto
  1195. upper_bound(const _Kt& __x)
  1196. -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
  1197. { return iterator(_M_t._M_upper_bound_tr(__x)); }
  1198. #endif
  1199. //@}
  1200. //@{
  1201. /**
  1202. * @brief Finds the end of a subsequence matching given key.
  1203. * @param __x Key of (key, value) pair to be located.
  1204. * @return Read-only (constant) iterator pointing to first iterator
  1205. * greater than key, or end().
  1206. */
  1207. const_iterator
  1208. upper_bound(const key_type& __x) const
  1209. { return _M_t.upper_bound(__x); }
  1210. #if __cplusplus > 201103L
  1211. template<typename _Kt>
  1212. auto
  1213. upper_bound(const _Kt& __x) const
  1214. -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x)))
  1215. { return const_iterator(_M_t._M_upper_bound_tr(__x)); }
  1216. #endif
  1217. //@}
  1218. //@{
  1219. /**
  1220. * @brief Finds a subsequence matching given key.
  1221. * @param __x Key of (key, value) pairs to be located.
  1222. * @return Pair of iterators that possibly points to the subsequence
  1223. * matching given key.
  1224. *
  1225. * This function is equivalent to
  1226. * @code
  1227. * std::make_pair(c.lower_bound(val),
  1228. * c.upper_bound(val))
  1229. * @endcode
  1230. * (but is faster than making the calls separately).
  1231. *
  1232. * This function probably only makes sense for multimaps.
  1233. */
  1234. std::pair<iterator, iterator>
  1235. equal_range(const key_type& __x)
  1236. { return _M_t.equal_range(__x); }
  1237. #if __cplusplus > 201103L
  1238. template<typename _Kt>
  1239. auto
  1240. equal_range(const _Kt& __x)
  1241. -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
  1242. { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
  1243. #endif
  1244. //@}
  1245. //@{
  1246. /**
  1247. * @brief Finds a subsequence matching given key.
  1248. * @param __x Key of (key, value) pairs to be located.
  1249. * @return Pair of read-only (constant) iterators that possibly points
  1250. * to the subsequence matching given key.
  1251. *
  1252. * This function is equivalent to
  1253. * @code
  1254. * std::make_pair(c.lower_bound(val),
  1255. * c.upper_bound(val))
  1256. * @endcode
  1257. * (but is faster than making the calls separately).
  1258. *
  1259. * This function probably only makes sense for multimaps.
  1260. */
  1261. std::pair<const_iterator, const_iterator>
  1262. equal_range(const key_type& __x) const
  1263. { return _M_t.equal_range(__x); }
  1264. #if __cplusplus > 201103L
  1265. template<typename _Kt>
  1266. auto
  1267. equal_range(const _Kt& __x) const
  1268. -> decltype(pair<const_iterator, const_iterator>(
  1269. _M_t._M_equal_range_tr(__x)))
  1270. {
  1271. return pair<const_iterator, const_iterator>(
  1272. _M_t._M_equal_range_tr(__x));
  1273. }
  1274. #endif
  1275. //@}
  1276. template<typename _K1, typename _T1, typename _C1, typename _A1>
  1277. friend bool
  1278. operator==(const map<_K1, _T1, _C1, _A1>&,
  1279. const map<_K1, _T1, _C1, _A1>&);
  1280. #if __cpp_lib_three_way_comparison
  1281. template<typename _K1, typename _T1, typename _C1, typename _A1>
  1282. friend __detail::__synth3way_t<pair<const _K1, _T1>>
  1283. operator<=>(const map<_K1, _T1, _C1, _A1>&,
  1284. const map<_K1, _T1, _C1, _A1>&);
  1285. #else
  1286. template<typename _K1, typename _T1, typename _C1, typename _A1>
  1287. friend bool
  1288. operator<(const map<_K1, _T1, _C1, _A1>&,
  1289. const map<_K1, _T1, _C1, _A1>&);
  1290. #endif
  1291. };
  1292. #if __cpp_deduction_guides >= 201606
  1293. template<typename _InputIterator,
  1294. typename _Compare = less<__iter_key_t<_InputIterator>>,
  1295. typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
  1296. typename = _RequireInputIter<_InputIterator>,
  1297. typename = _RequireNotAllocator<_Compare>,
  1298. typename = _RequireAllocator<_Allocator>>
  1299. map(_InputIterator, _InputIterator,
  1300. _Compare = _Compare(), _Allocator = _Allocator())
  1301. -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
  1302. _Compare, _Allocator>;
  1303. template<typename _Key, typename _Tp, typename _Compare = less<_Key>,
  1304. typename _Allocator = allocator<pair<const _Key, _Tp>>,
  1305. typename = _RequireNotAllocator<_Compare>,
  1306. typename = _RequireAllocator<_Allocator>>
  1307. map(initializer_list<pair<_Key, _Tp>>,
  1308. _Compare = _Compare(), _Allocator = _Allocator())
  1309. -> map<_Key, _Tp, _Compare, _Allocator>;
  1310. template <typename _InputIterator, typename _Allocator,
  1311. typename = _RequireInputIter<_InputIterator>,
  1312. typename = _RequireAllocator<_Allocator>>
  1313. map(_InputIterator, _InputIterator, _Allocator)
  1314. -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
  1315. less<__iter_key_t<_InputIterator>>, _Allocator>;
  1316. template<typename _Key, typename _Tp, typename _Allocator,
  1317. typename = _RequireAllocator<_Allocator>>
  1318. map(initializer_list<pair<_Key, _Tp>>, _Allocator)
  1319. -> map<_Key, _Tp, less<_Key>, _Allocator>;
  1320. #endif // deduction guides
  1321. /**
  1322. * @brief Map equality comparison.
  1323. * @param __x A %map.
  1324. * @param __y A %map of the same type as @a x.
  1325. * @return True iff the size and elements of the maps are equal.
  1326. *
  1327. * This is an equivalence relation. It is linear in the size of the
  1328. * maps. Maps are considered equivalent if their sizes are equal,
  1329. * and if corresponding elements compare equal.
  1330. */
  1331. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1332. inline bool
  1333. operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x,
  1334. const map<_Key, _Tp, _Compare, _Alloc>& __y)
  1335. { return __x._M_t == __y._M_t; }
  1336. #if __cpp_lib_three_way_comparison
  1337. /**
  1338. * @brief Map ordering relation.
  1339. * @param __x A `map`.
  1340. * @param __y A `map` of the same type as `x`.
  1341. * @return A value indicating whether `__x` is less than, equal to,
  1342. * greater than, or incomparable with `__y`.
  1343. *
  1344. * This is a total ordering relation. It is linear in the size of the
  1345. * maps. The elements must be comparable with @c <.
  1346. *
  1347. * See `std::lexicographical_compare_three_way()` for how the determination
  1348. * is made. This operator is used to synthesize relational operators like
  1349. * `<` and `>=` etc.
  1350. */
  1351. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1352. inline __detail::__synth3way_t<pair<const _Key, _Tp>>
  1353. operator<=>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
  1354. const map<_Key, _Tp, _Compare, _Alloc>& __y)
  1355. { return __x._M_t <=> __y._M_t; }
  1356. #else
  1357. /**
  1358. * @brief Map ordering relation.
  1359. * @param __x A %map.
  1360. * @param __y A %map of the same type as @a x.
  1361. * @return True iff @a x is lexicographically less than @a y.
  1362. *
  1363. * This is a total ordering relation. It is linear in the size of the
  1364. * maps. The elements must be comparable with @c <.
  1365. *
  1366. * See std::lexicographical_compare() for how the determination is made.
  1367. */
  1368. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1369. inline bool
  1370. operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
  1371. const map<_Key, _Tp, _Compare, _Alloc>& __y)
  1372. { return __x._M_t < __y._M_t; }
  1373. /// Based on operator==
  1374. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1375. inline bool
  1376. operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
  1377. const map<_Key, _Tp, _Compare, _Alloc>& __y)
  1378. { return !(__x == __y); }
  1379. /// Based on operator<
  1380. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1381. inline bool
  1382. operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
  1383. const map<_Key, _Tp, _Compare, _Alloc>& __y)
  1384. { return __y < __x; }
  1385. /// Based on operator<
  1386. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1387. inline bool
  1388. operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
  1389. const map<_Key, _Tp, _Compare, _Alloc>& __y)
  1390. { return !(__y < __x); }
  1391. /// Based on operator<
  1392. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1393. inline bool
  1394. operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
  1395. const map<_Key, _Tp, _Compare, _Alloc>& __y)
  1396. { return !(__x < __y); }
  1397. #endif // three-way comparison
  1398. /// See std::map::swap().
  1399. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1400. inline void
  1401. swap(map<_Key, _Tp, _Compare, _Alloc>& __x,
  1402. map<_Key, _Tp, _Compare, _Alloc>& __y)
  1403. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  1404. { __x.swap(__y); }
  1405. _GLIBCXX_END_NAMESPACE_CONTAINER
  1406. #if __cplusplus > 201402L
  1407. // Allow std::map access to internals of compatible maps.
  1408. template<typename _Key, typename _Val, typename _Cmp1, typename _Alloc,
  1409. typename _Cmp2>
  1410. struct
  1411. _Rb_tree_merge_helper<_GLIBCXX_STD_C::map<_Key, _Val, _Cmp1, _Alloc>,
  1412. _Cmp2>
  1413. {
  1414. private:
  1415. friend class _GLIBCXX_STD_C::map<_Key, _Val, _Cmp1, _Alloc>;
  1416. static auto&
  1417. _S_get_tree(_GLIBCXX_STD_C::map<_Key, _Val, _Cmp2, _Alloc>& __map)
  1418. { return __map._M_t; }
  1419. static auto&
  1420. _S_get_tree(_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp2, _Alloc>& __map)
  1421. { return __map._M_t; }
  1422. };
  1423. #endif // C++17
  1424. _GLIBCXX_END_NAMESPACE_VERSION
  1425. } // namespace std
  1426. #endif /* _STL_MAP_H */