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.

1234 lines
42KB

  1. // Multimap 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_multimap.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_MULTIMAP_H
  50. #define _STL_MULTIMAP_H 1
  51. #include <bits/concept_check.h>
  52. #if __cplusplus >= 201103L
  53. #include <initializer_list>
  54. #endif
  55. namespace std _GLIBCXX_VISIBILITY(default)
  56. {
  57. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  58. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  59. template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  60. class map;
  61. /**
  62. * @brief A standard container made up of (key,value) pairs, which can be
  63. * retrieved based on a key, in logarithmic time.
  64. *
  65. * @ingroup associative_containers
  66. *
  67. * @tparam _Key Type of key objects.
  68. * @tparam _Tp Type of mapped objects.
  69. * @tparam _Compare Comparison function object type, defaults to less<_Key>.
  70. * @tparam _Alloc Allocator type, defaults to
  71. * allocator<pair<const _Key, _Tp>.
  72. *
  73. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  74. * <a href="tables.html#66">reversible container</a>, and an
  75. * <a href="tables.html#69">associative container</a> (using equivalent
  76. * keys). For a @c multimap<Key,T> the key_type is Key, the mapped_type
  77. * is T, and the value_type is std::pair<const Key,T>.
  78. *
  79. * Multimaps support bidirectional iterators.
  80. *
  81. * The private tree data is declared exactly the same way for map and
  82. * multimap; the distinction is made entirely in how the tree functions are
  83. * called (*_unique versus *_equal, same as the standard).
  84. */
  85. template <typename _Key, typename _Tp,
  86. typename _Compare = std::less<_Key>,
  87. typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  88. class multimap
  89. {
  90. public:
  91. typedef _Key key_type;
  92. typedef _Tp mapped_type;
  93. typedef std::pair<const _Key, _Tp> value_type;
  94. typedef _Compare key_compare;
  95. typedef _Alloc allocator_type;
  96. private:
  97. #ifdef _GLIBCXX_CONCEPT_CHECKS
  98. // concept requirements
  99. typedef typename _Alloc::value_type _Alloc_value_type;
  100. # if __cplusplus < 201103L
  101. __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
  102. # endif
  103. __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
  104. _BinaryFunctionConcept)
  105. __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
  106. #endif
  107. #if __cplusplus >= 201103L
  108. #if __cplusplus > 201703L || defined __STRICT_ANSI__
  109. static_assert(is_same<typename _Alloc::value_type, value_type>::value,
  110. "std::multimap must have the same value_type as its allocator");
  111. #endif
  112. #endif
  113. public:
  114. class value_compare
  115. : public std::binary_function<value_type, value_type, bool>
  116. {
  117. friend class multimap<_Key, _Tp, _Compare, _Alloc>;
  118. protected:
  119. _Compare comp;
  120. value_compare(_Compare __c)
  121. : comp(__c) { }
  122. public:
  123. bool operator()(const value_type& __x, const value_type& __y) const
  124. { return comp(__x.first, __y.first); }
  125. };
  126. private:
  127. /// This turns a red-black tree into a [multi]map.
  128. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  129. rebind<value_type>::other _Pair_alloc_type;
  130. typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
  131. key_compare, _Pair_alloc_type> _Rep_type;
  132. /// The actual tree structure.
  133. _Rep_type _M_t;
  134. typedef __gnu_cxx::__alloc_traits<_Pair_alloc_type> _Alloc_traits;
  135. public:
  136. // many of these are specified differently in ISO, but the following are
  137. // "functionally equivalent"
  138. typedef typename _Alloc_traits::pointer pointer;
  139. typedef typename _Alloc_traits::const_pointer const_pointer;
  140. typedef typename _Alloc_traits::reference reference;
  141. typedef typename _Alloc_traits::const_reference const_reference;
  142. typedef typename _Rep_type::iterator iterator;
  143. typedef typename _Rep_type::const_iterator const_iterator;
  144. typedef typename _Rep_type::size_type size_type;
  145. typedef typename _Rep_type::difference_type difference_type;
  146. typedef typename _Rep_type::reverse_iterator reverse_iterator;
  147. typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
  148. #if __cplusplus > 201402L
  149. using node_type = typename _Rep_type::node_type;
  150. #endif
  151. // [23.3.2] construct/copy/destroy
  152. // (get_allocator() is also listed in this section)
  153. /**
  154. * @brief Default constructor creates no elements.
  155. */
  156. #if __cplusplus < 201103L
  157. multimap() : _M_t() { }
  158. #else
  159. multimap() = default;
  160. #endif
  161. /**
  162. * @brief Creates a %multimap with no elements.
  163. * @param __comp A comparison object.
  164. * @param __a An allocator object.
  165. */
  166. explicit
  167. multimap(const _Compare& __comp,
  168. const allocator_type& __a = allocator_type())
  169. : _M_t(__comp, _Pair_alloc_type(__a)) { }
  170. /**
  171. * @brief %Multimap copy constructor.
  172. *
  173. * Whether the allocator is copied depends on the allocator traits.
  174. */
  175. #if __cplusplus < 201103L
  176. multimap(const multimap& __x)
  177. : _M_t(__x._M_t) { }
  178. #else
  179. multimap(const multimap&) = default;
  180. /**
  181. * @brief %Multimap move constructor.
  182. *
  183. * The newly-created %multimap contains the exact contents of the
  184. * moved instance. The moved instance is a valid, but unspecified
  185. * %multimap.
  186. */
  187. multimap(multimap&&) = default;
  188. /**
  189. * @brief Builds a %multimap from an initializer_list.
  190. * @param __l An initializer_list.
  191. * @param __comp A comparison functor.
  192. * @param __a An allocator object.
  193. *
  194. * Create a %multimap consisting of copies of the elements from
  195. * the initializer_list. This is linear in N if the list is already
  196. * sorted, and NlogN otherwise (where N is @a __l.size()).
  197. */
  198. multimap(initializer_list<value_type> __l,
  199. const _Compare& __comp = _Compare(),
  200. const allocator_type& __a = allocator_type())
  201. : _M_t(__comp, _Pair_alloc_type(__a))
  202. { _M_t._M_insert_range_equal(__l.begin(), __l.end()); }
  203. /// Allocator-extended default constructor.
  204. explicit
  205. multimap(const allocator_type& __a)
  206. : _M_t(_Pair_alloc_type(__a)) { }
  207. /// Allocator-extended copy constructor.
  208. multimap(const multimap& __m, const allocator_type& __a)
  209. : _M_t(__m._M_t, _Pair_alloc_type(__a)) { }
  210. /// Allocator-extended move constructor.
  211. multimap(multimap&& __m, const allocator_type& __a)
  212. noexcept(is_nothrow_copy_constructible<_Compare>::value
  213. && _Alloc_traits::_S_always_equal())
  214. : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { }
  215. /// Allocator-extended initialier-list constructor.
  216. multimap(initializer_list<value_type> __l, const allocator_type& __a)
  217. : _M_t(_Pair_alloc_type(__a))
  218. { _M_t._M_insert_range_equal(__l.begin(), __l.end()); }
  219. /// Allocator-extended range constructor.
  220. template<typename _InputIterator>
  221. multimap(_InputIterator __first, _InputIterator __last,
  222. const allocator_type& __a)
  223. : _M_t(_Pair_alloc_type(__a))
  224. { _M_t._M_insert_range_equal(__first, __last); }
  225. #endif
  226. /**
  227. * @brief Builds a %multimap from a range.
  228. * @param __first An input iterator.
  229. * @param __last An input iterator.
  230. *
  231. * Create a %multimap consisting of copies of the elements from
  232. * [__first,__last). This is linear in N if the range is already sorted,
  233. * and NlogN otherwise (where N is distance(__first,__last)).
  234. */
  235. template<typename _InputIterator>
  236. multimap(_InputIterator __first, _InputIterator __last)
  237. : _M_t()
  238. { _M_t._M_insert_range_equal(__first, __last); }
  239. /**
  240. * @brief Builds a %multimap from a range.
  241. * @param __first An input iterator.
  242. * @param __last An input iterator.
  243. * @param __comp A comparison functor.
  244. * @param __a An allocator object.
  245. *
  246. * Create a %multimap consisting of copies of the elements from
  247. * [__first,__last). This is linear in N if the range is already sorted,
  248. * and NlogN otherwise (where N is distance(__first,__last)).
  249. */
  250. template<typename _InputIterator>
  251. multimap(_InputIterator __first, _InputIterator __last,
  252. const _Compare& __comp,
  253. const allocator_type& __a = allocator_type())
  254. : _M_t(__comp, _Pair_alloc_type(__a))
  255. { _M_t._M_insert_range_equal(__first, __last); }
  256. #if __cplusplus >= 201103L
  257. /**
  258. * The dtor only erases the elements, and note that if the elements
  259. * themselves are pointers, the pointed-to memory is not touched in any
  260. * way. Managing the pointer is the user's responsibility.
  261. */
  262. ~multimap() = default;
  263. #endif
  264. /**
  265. * @brief %Multimap assignment operator.
  266. *
  267. * Whether the allocator is copied depends on the allocator traits.
  268. */
  269. #if __cplusplus < 201103L
  270. multimap&
  271. operator=(const multimap& __x)
  272. {
  273. _M_t = __x._M_t;
  274. return *this;
  275. }
  276. #else
  277. multimap&
  278. operator=(const multimap&) = default;
  279. /// Move assignment operator.
  280. multimap&
  281. operator=(multimap&&) = default;
  282. /**
  283. * @brief %Multimap list assignment operator.
  284. * @param __l An initializer_list.
  285. *
  286. * This function fills a %multimap with copies of the elements
  287. * in the initializer list @a __l.
  288. *
  289. * Note that the assignment completely changes the %multimap and
  290. * that the resulting %multimap's size is the same as the number
  291. * of elements assigned.
  292. */
  293. multimap&
  294. operator=(initializer_list<value_type> __l)
  295. {
  296. _M_t._M_assign_equal(__l.begin(), __l.end());
  297. return *this;
  298. }
  299. #endif
  300. /// Get a copy of the memory allocation object.
  301. allocator_type
  302. get_allocator() const _GLIBCXX_NOEXCEPT
  303. { return allocator_type(_M_t.get_allocator()); }
  304. // iterators
  305. /**
  306. * Returns a read/write iterator that points to the first pair in the
  307. * %multimap. Iteration is done in ascending order according to the
  308. * keys.
  309. */
  310. iterator
  311. begin() _GLIBCXX_NOEXCEPT
  312. { return _M_t.begin(); }
  313. /**
  314. * Returns a read-only (constant) iterator that points to the first pair
  315. * in the %multimap. Iteration is done in ascending order according to
  316. * the keys.
  317. */
  318. const_iterator
  319. begin() const _GLIBCXX_NOEXCEPT
  320. { return _M_t.begin(); }
  321. /**
  322. * Returns a read/write iterator that points one past the last pair in
  323. * the %multimap. Iteration is done in ascending order according to the
  324. * keys.
  325. */
  326. iterator
  327. end() _GLIBCXX_NOEXCEPT
  328. { return _M_t.end(); }
  329. /**
  330. * Returns a read-only (constant) iterator that points one past the last
  331. * pair in the %multimap. Iteration is done in ascending order according
  332. * to the keys.
  333. */
  334. const_iterator
  335. end() const _GLIBCXX_NOEXCEPT
  336. { return _M_t.end(); }
  337. /**
  338. * Returns a read/write reverse iterator that points to the last pair in
  339. * the %multimap. Iteration is done in descending order according to the
  340. * keys.
  341. */
  342. reverse_iterator
  343. rbegin() _GLIBCXX_NOEXCEPT
  344. { return _M_t.rbegin(); }
  345. /**
  346. * Returns a read-only (constant) reverse iterator that points to the
  347. * last pair in the %multimap. Iteration is done in descending order
  348. * according to the keys.
  349. */
  350. const_reverse_iterator
  351. rbegin() const _GLIBCXX_NOEXCEPT
  352. { return _M_t.rbegin(); }
  353. /**
  354. * Returns a read/write reverse iterator that points to one before the
  355. * first pair in the %multimap. Iteration is done in descending order
  356. * according to the keys.
  357. */
  358. reverse_iterator
  359. rend() _GLIBCXX_NOEXCEPT
  360. { return _M_t.rend(); }
  361. /**
  362. * Returns a read-only (constant) reverse iterator that points to one
  363. * before the first pair in the %multimap. Iteration is done in
  364. * descending order according to the keys.
  365. */
  366. const_reverse_iterator
  367. rend() const _GLIBCXX_NOEXCEPT
  368. { return _M_t.rend(); }
  369. #if __cplusplus >= 201103L
  370. /**
  371. * Returns a read-only (constant) iterator that points to the first pair
  372. * in the %multimap. Iteration is done in ascending order according to
  373. * the keys.
  374. */
  375. const_iterator
  376. cbegin() const noexcept
  377. { return _M_t.begin(); }
  378. /**
  379. * Returns a read-only (constant) iterator that points one past the last
  380. * pair in the %multimap. Iteration is done in ascending order according
  381. * to the keys.
  382. */
  383. const_iterator
  384. cend() const noexcept
  385. { return _M_t.end(); }
  386. /**
  387. * Returns a read-only (constant) reverse iterator that points to the
  388. * last pair in the %multimap. Iteration is done in descending order
  389. * according to the keys.
  390. */
  391. const_reverse_iterator
  392. crbegin() const noexcept
  393. { return _M_t.rbegin(); }
  394. /**
  395. * Returns a read-only (constant) reverse iterator that points to one
  396. * before the first pair in the %multimap. Iteration is done in
  397. * descending order according to the keys.
  398. */
  399. const_reverse_iterator
  400. crend() const noexcept
  401. { return _M_t.rend(); }
  402. #endif
  403. // capacity
  404. /** Returns true if the %multimap is empty. */
  405. _GLIBCXX_NODISCARD bool
  406. empty() const _GLIBCXX_NOEXCEPT
  407. { return _M_t.empty(); }
  408. /** Returns the size of the %multimap. */
  409. size_type
  410. size() const _GLIBCXX_NOEXCEPT
  411. { return _M_t.size(); }
  412. /** Returns the maximum size of the %multimap. */
  413. size_type
  414. max_size() const _GLIBCXX_NOEXCEPT
  415. { return _M_t.max_size(); }
  416. // modifiers
  417. #if __cplusplus >= 201103L
  418. /**
  419. * @brief Build and insert a std::pair into the %multimap.
  420. *
  421. * @param __args Arguments used to generate a new pair instance (see
  422. * std::piecewise_contruct for passing arguments to each
  423. * part of the pair constructor).
  424. *
  425. * @return An iterator that points to the inserted (key,value) pair.
  426. *
  427. * This function builds and inserts a (key, value) %pair into the
  428. * %multimap.
  429. * Contrary to a std::map the %multimap does not rely on unique keys and
  430. * thus multiple pairs with the same key can be inserted.
  431. *
  432. * Insertion requires logarithmic time.
  433. */
  434. template<typename... _Args>
  435. iterator
  436. emplace(_Args&&... __args)
  437. { return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
  438. /**
  439. * @brief Builds and inserts a std::pair into the %multimap.
  440. *
  441. * @param __pos An iterator that serves as a hint as to where the pair
  442. * should be inserted.
  443. * @param __args Arguments used to generate a new pair instance (see
  444. * std::piecewise_contruct for passing arguments to each
  445. * part of the pair constructor).
  446. * @return An iterator that points to the inserted (key,value) pair.
  447. *
  448. * This function inserts a (key, value) pair into the %multimap.
  449. * Contrary to a std::map the %multimap does not rely on unique keys and
  450. * thus multiple pairs with the same key can be inserted.
  451. * Note that the first parameter is only a hint and can potentially
  452. * improve the performance of the insertion process. A bad hint would
  453. * cause no gains in efficiency.
  454. *
  455. * For more on @a hinting, see:
  456. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  457. *
  458. * Insertion requires logarithmic time (if the hint is not taken).
  459. */
  460. template<typename... _Args>
  461. iterator
  462. emplace_hint(const_iterator __pos, _Args&&... __args)
  463. {
  464. return _M_t._M_emplace_hint_equal(__pos,
  465. std::forward<_Args>(__args)...);
  466. }
  467. #endif
  468. /**
  469. * @brief Inserts a std::pair into the %multimap.
  470. * @param __x Pair to be inserted (see std::make_pair for easy creation
  471. * of pairs).
  472. * @return An iterator that points to the inserted (key,value) pair.
  473. *
  474. * This function inserts a (key, value) pair into the %multimap.
  475. * Contrary to a std::map the %multimap does not rely on unique keys and
  476. * thus multiple pairs with the same key can be inserted.
  477. *
  478. * Insertion requires logarithmic time.
  479. * @{
  480. */
  481. iterator
  482. insert(const value_type& __x)
  483. { return _M_t._M_insert_equal(__x); }
  484. #if __cplusplus >= 201103L
  485. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  486. // 2354. Unnecessary copying when inserting into maps with braced-init
  487. iterator
  488. insert(value_type&& __x)
  489. { return _M_t._M_insert_equal(std::move(__x)); }
  490. template<typename _Pair>
  491. __enable_if_t<is_constructible<value_type, _Pair>::value, iterator>
  492. insert(_Pair&& __x)
  493. { return _M_t._M_emplace_equal(std::forward<_Pair>(__x)); }
  494. #endif
  495. // @}
  496. /**
  497. * @brief Inserts a std::pair into the %multimap.
  498. * @param __position An iterator that serves as a hint as to where the
  499. * pair should be inserted.
  500. * @param __x Pair to be inserted (see std::make_pair for easy creation
  501. * of pairs).
  502. * @return An iterator that points to the inserted (key,value) pair.
  503. *
  504. * This function inserts a (key, value) pair into the %multimap.
  505. * Contrary to a std::map the %multimap does not rely on unique keys and
  506. * thus multiple pairs with the same key can be inserted.
  507. * Note that the first parameter is only a hint and can potentially
  508. * improve the performance of the insertion process. A bad hint would
  509. * cause no gains in efficiency.
  510. *
  511. * For more on @a hinting, see:
  512. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  513. *
  514. * Insertion requires logarithmic time (if the hint is not taken).
  515. * @{
  516. */
  517. iterator
  518. #if __cplusplus >= 201103L
  519. insert(const_iterator __position, const value_type& __x)
  520. #else
  521. insert(iterator __position, const value_type& __x)
  522. #endif
  523. { return _M_t._M_insert_equal_(__position, __x); }
  524. #if __cplusplus >= 201103L
  525. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  526. // 2354. Unnecessary copying when inserting into maps with braced-init
  527. iterator
  528. insert(const_iterator __position, value_type&& __x)
  529. { return _M_t._M_insert_equal_(__position, std::move(__x)); }
  530. template<typename _Pair>
  531. __enable_if_t<is_constructible<value_type, _Pair&&>::value, iterator>
  532. insert(const_iterator __position, _Pair&& __x)
  533. {
  534. return _M_t._M_emplace_hint_equal(__position,
  535. std::forward<_Pair>(__x));
  536. }
  537. #endif
  538. // @}
  539. /**
  540. * @brief A template function that attempts to insert a range
  541. * of elements.
  542. * @param __first Iterator pointing to the start of the range to be
  543. * inserted.
  544. * @param __last Iterator pointing to the end of the range.
  545. *
  546. * Complexity similar to that of the range constructor.
  547. */
  548. template<typename _InputIterator>
  549. void
  550. insert(_InputIterator __first, _InputIterator __last)
  551. { _M_t._M_insert_range_equal(__first, __last); }
  552. #if __cplusplus >= 201103L
  553. /**
  554. * @brief Attempts to insert a list of std::pairs into the %multimap.
  555. * @param __l A std::initializer_list<value_type> of pairs to be
  556. * inserted.
  557. *
  558. * Complexity similar to that of the range constructor.
  559. */
  560. void
  561. insert(initializer_list<value_type> __l)
  562. { this->insert(__l.begin(), __l.end()); }
  563. #endif
  564. #if __cplusplus > 201402L
  565. /// Extract a node.
  566. node_type
  567. extract(const_iterator __pos)
  568. {
  569. __glibcxx_assert(__pos != end());
  570. return _M_t.extract(__pos);
  571. }
  572. /// Extract a node.
  573. node_type
  574. extract(const key_type& __x)
  575. { return _M_t.extract(__x); }
  576. /// Re-insert an extracted node.
  577. iterator
  578. insert(node_type&& __nh)
  579. { return _M_t._M_reinsert_node_equal(std::move(__nh)); }
  580. /// Re-insert an extracted node.
  581. iterator
  582. insert(const_iterator __hint, node_type&& __nh)
  583. { return _M_t._M_reinsert_node_hint_equal(__hint, std::move(__nh)); }
  584. template<typename, typename>
  585. friend class std::_Rb_tree_merge_helper;
  586. template<typename _Cmp2>
  587. void
  588. merge(multimap<_Key, _Tp, _Cmp2, _Alloc>& __source)
  589. {
  590. using _Merge_helper = _Rb_tree_merge_helper<multimap, _Cmp2>;
  591. _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
  592. }
  593. template<typename _Cmp2>
  594. void
  595. merge(multimap<_Key, _Tp, _Cmp2, _Alloc>&& __source)
  596. { merge(__source); }
  597. template<typename _Cmp2>
  598. void
  599. merge(map<_Key, _Tp, _Cmp2, _Alloc>& __source)
  600. {
  601. using _Merge_helper = _Rb_tree_merge_helper<multimap, _Cmp2>;
  602. _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
  603. }
  604. template<typename _Cmp2>
  605. void
  606. merge(map<_Key, _Tp, _Cmp2, _Alloc>&& __source)
  607. { merge(__source); }
  608. #endif // C++17
  609. #if __cplusplus >= 201103L
  610. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  611. // DR 130. Associative erase should return an iterator.
  612. /**
  613. * @brief Erases an element from a %multimap.
  614. * @param __position An iterator pointing to the element to be erased.
  615. * @return An iterator pointing to the element immediately following
  616. * @a position prior to the element being erased. If no such
  617. * element exists, end() is returned.
  618. *
  619. * This function erases an element, pointed to by the given iterator,
  620. * from a %multimap. Note that this function only erases the element,
  621. * and that if the element is itself a pointer, the pointed-to memory is
  622. * not touched in any way. Managing the pointer is the user's
  623. * responsibility.
  624. *
  625. * @{
  626. */
  627. iterator
  628. erase(const_iterator __position)
  629. { return _M_t.erase(__position); }
  630. // LWG 2059.
  631. _GLIBCXX_ABI_TAG_CXX11
  632. iterator
  633. erase(iterator __position)
  634. { return _M_t.erase(__position); }
  635. // @}
  636. #else
  637. /**
  638. * @brief Erases an element from a %multimap.
  639. * @param __position An iterator pointing to the element to be erased.
  640. *
  641. * This function erases an element, pointed to by the given iterator,
  642. * from a %multimap. Note that this function only erases the element,
  643. * and that if the element is itself a pointer, the pointed-to memory is
  644. * not touched in any way. Managing the pointer is the user's
  645. * responsibility.
  646. */
  647. void
  648. erase(iterator __position)
  649. { _M_t.erase(__position); }
  650. #endif
  651. /**
  652. * @brief Erases elements according to the provided key.
  653. * @param __x Key of element to be erased.
  654. * @return The number of elements erased.
  655. *
  656. * This function erases all elements located by the given key from a
  657. * %multimap.
  658. * Note that this function only erases the element, and that if
  659. * the element is itself a pointer, the pointed-to memory is not touched
  660. * in any way. Managing the pointer is the user's responsibility.
  661. */
  662. size_type
  663. erase(const key_type& __x)
  664. { return _M_t.erase(__x); }
  665. #if __cplusplus >= 201103L
  666. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  667. // DR 130. Associative erase should return an iterator.
  668. /**
  669. * @brief Erases a [first,last) range of elements from a %multimap.
  670. * @param __first Iterator pointing to the start of the range to be
  671. * erased.
  672. * @param __last Iterator pointing to the end of the range to be
  673. * erased .
  674. * @return The iterator @a __last.
  675. *
  676. * This function erases a sequence of elements from a %multimap.
  677. * Note that this function only erases the elements, and that if
  678. * the elements themselves are pointers, the pointed-to memory is not
  679. * touched in any way. Managing the pointer is the user's
  680. * responsibility.
  681. */
  682. iterator
  683. erase(const_iterator __first, const_iterator __last)
  684. { return _M_t.erase(__first, __last); }
  685. #else
  686. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  687. // DR 130. Associative erase should return an iterator.
  688. /**
  689. * @brief Erases a [first,last) range of elements from a %multimap.
  690. * @param __first Iterator pointing to the start of the range to be
  691. * erased.
  692. * @param __last Iterator pointing to the end of the range to
  693. * be erased.
  694. *
  695. * This function erases a sequence of elements from a %multimap.
  696. * Note that this function only erases the elements, and that if
  697. * the elements themselves are pointers, the pointed-to memory is not
  698. * touched in any way. Managing the pointer is the user's
  699. * responsibility.
  700. */
  701. void
  702. erase(iterator __first, iterator __last)
  703. { _M_t.erase(__first, __last); }
  704. #endif
  705. /**
  706. * @brief Swaps data with another %multimap.
  707. * @param __x A %multimap of the same element and allocator types.
  708. *
  709. * This exchanges the elements between two multimaps in constant time.
  710. * (It is only swapping a pointer, an integer, and an instance of
  711. * the @c Compare type (which itself is often stateless and empty), so it
  712. * should be quite fast.)
  713. * Note that the global std::swap() function is specialized such that
  714. * std::swap(m1,m2) will feed to this function.
  715. *
  716. * Whether the allocators are swapped depends on the allocator traits.
  717. */
  718. void
  719. swap(multimap& __x)
  720. _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
  721. { _M_t.swap(__x._M_t); }
  722. /**
  723. * Erases all elements in a %multimap. Note that this function only
  724. * erases the elements, and that if the elements themselves are pointers,
  725. * the pointed-to memory is not touched in any way. Managing the pointer
  726. * is the user's responsibility.
  727. */
  728. void
  729. clear() _GLIBCXX_NOEXCEPT
  730. { _M_t.clear(); }
  731. // observers
  732. /**
  733. * Returns the key comparison object out of which the %multimap
  734. * was constructed.
  735. */
  736. key_compare
  737. key_comp() const
  738. { return _M_t.key_comp(); }
  739. /**
  740. * Returns a value comparison object, built from the key comparison
  741. * object out of which the %multimap was constructed.
  742. */
  743. value_compare
  744. value_comp() const
  745. { return value_compare(_M_t.key_comp()); }
  746. // multimap operations
  747. //@{
  748. /**
  749. * @brief Tries to locate an element in a %multimap.
  750. * @param __x Key of (key, value) pair to be located.
  751. * @return Iterator pointing to sought-after element,
  752. * or end() if not found.
  753. *
  754. * This function takes a key and tries to locate the element with which
  755. * the key matches. If successful the function returns an iterator
  756. * pointing to the sought after %pair. If unsuccessful it returns the
  757. * past-the-end ( @c end() ) iterator.
  758. */
  759. iterator
  760. find(const key_type& __x)
  761. { return _M_t.find(__x); }
  762. #if __cplusplus > 201103L
  763. template<typename _Kt>
  764. auto
  765. find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x))
  766. { return _M_t._M_find_tr(__x); }
  767. #endif
  768. //@}
  769. //@{
  770. /**
  771. * @brief Tries to locate an element in a %multimap.
  772. * @param __x Key of (key, value) pair to be located.
  773. * @return Read-only (constant) iterator pointing to sought-after
  774. * element, or end() if not found.
  775. *
  776. * This function takes a key and tries to locate the element with which
  777. * the key matches. If successful the function returns a constant
  778. * iterator pointing to the sought after %pair. If unsuccessful it
  779. * returns the past-the-end ( @c end() ) iterator.
  780. */
  781. const_iterator
  782. find(const key_type& __x) const
  783. { return _M_t.find(__x); }
  784. #if __cplusplus > 201103L
  785. template<typename _Kt>
  786. auto
  787. find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x))
  788. { return _M_t._M_find_tr(__x); }
  789. #endif
  790. //@}
  791. //@{
  792. /**
  793. * @brief Finds the number of elements with given key.
  794. * @param __x Key of (key, value) pairs to be located.
  795. * @return Number of elements with specified key.
  796. */
  797. size_type
  798. count(const key_type& __x) const
  799. { return _M_t.count(__x); }
  800. #if __cplusplus > 201103L
  801. template<typename _Kt>
  802. auto
  803. count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x))
  804. { return _M_t._M_count_tr(__x); }
  805. #endif
  806. //@}
  807. #if __cplusplus > 201703L
  808. //@{
  809. /**
  810. * @brief Finds whether an element with the given key exists.
  811. * @param __x Key of (key, value) pairs to be located.
  812. * @return True if there is any element with the specified key.
  813. */
  814. bool
  815. contains(const key_type& __x) const
  816. { return _M_t.find(__x) != _M_t.end(); }
  817. template<typename _Kt>
  818. auto
  819. contains(const _Kt& __x) const
  820. -> decltype(_M_t._M_find_tr(__x), void(), true)
  821. { return _M_t._M_find_tr(__x) != _M_t.end(); }
  822. //@}
  823. #endif
  824. //@{
  825. /**
  826. * @brief Finds the beginning of a subsequence matching given key.
  827. * @param __x Key of (key, value) pair to be located.
  828. * @return Iterator pointing to first element equal to or greater
  829. * than key, or end().
  830. *
  831. * This function returns the first element of a subsequence of elements
  832. * that matches the given key. If unsuccessful it returns an iterator
  833. * pointing to the first element that has a greater value than given key
  834. * or end() if no such element exists.
  835. */
  836. iterator
  837. lower_bound(const key_type& __x)
  838. { return _M_t.lower_bound(__x); }
  839. #if __cplusplus > 201103L
  840. template<typename _Kt>
  841. auto
  842. lower_bound(const _Kt& __x)
  843. -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
  844. { return iterator(_M_t._M_lower_bound_tr(__x)); }
  845. #endif
  846. //@}
  847. //@{
  848. /**
  849. * @brief Finds the beginning of a subsequence matching given key.
  850. * @param __x Key of (key, value) pair to be located.
  851. * @return Read-only (constant) iterator pointing to first element
  852. * equal to or greater than key, or end().
  853. *
  854. * This function returns the first element of a subsequence of
  855. * elements that matches the given key. If unsuccessful the
  856. * iterator will point to the next greatest element or, if no
  857. * such greater element exists, to end().
  858. */
  859. const_iterator
  860. lower_bound(const key_type& __x) const
  861. { return _M_t.lower_bound(__x); }
  862. #if __cplusplus > 201103L
  863. template<typename _Kt>
  864. auto
  865. lower_bound(const _Kt& __x) const
  866. -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x)))
  867. { return const_iterator(_M_t._M_lower_bound_tr(__x)); }
  868. #endif
  869. //@}
  870. //@{
  871. /**
  872. * @brief Finds the end of a subsequence matching given key.
  873. * @param __x Key of (key, value) pair to be located.
  874. * @return Iterator pointing to the first element
  875. * greater than key, or end().
  876. */
  877. iterator
  878. upper_bound(const key_type& __x)
  879. { return _M_t.upper_bound(__x); }
  880. #if __cplusplus > 201103L
  881. template<typename _Kt>
  882. auto
  883. upper_bound(const _Kt& __x)
  884. -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
  885. { return iterator(_M_t._M_upper_bound_tr(__x)); }
  886. #endif
  887. //@}
  888. //@{
  889. /**
  890. * @brief Finds the end of a subsequence matching given key.
  891. * @param __x Key of (key, value) pair to be located.
  892. * @return Read-only (constant) iterator pointing to first iterator
  893. * greater than key, or end().
  894. */
  895. const_iterator
  896. upper_bound(const key_type& __x) const
  897. { return _M_t.upper_bound(__x); }
  898. #if __cplusplus > 201103L
  899. template<typename _Kt>
  900. auto
  901. upper_bound(const _Kt& __x) const
  902. -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x)))
  903. { return const_iterator(_M_t._M_upper_bound_tr(__x)); }
  904. #endif
  905. //@}
  906. //@{
  907. /**
  908. * @brief Finds a subsequence matching given key.
  909. * @param __x Key of (key, value) pairs to be located.
  910. * @return Pair of iterators that possibly points to the subsequence
  911. * matching given key.
  912. *
  913. * This function is equivalent to
  914. * @code
  915. * std::make_pair(c.lower_bound(val),
  916. * c.upper_bound(val))
  917. * @endcode
  918. * (but is faster than making the calls separately).
  919. */
  920. std::pair<iterator, iterator>
  921. equal_range(const key_type& __x)
  922. { return _M_t.equal_range(__x); }
  923. #if __cplusplus > 201103L
  924. template<typename _Kt>
  925. auto
  926. equal_range(const _Kt& __x)
  927. -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
  928. { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
  929. #endif
  930. //@}
  931. //@{
  932. /**
  933. * @brief Finds a subsequence matching given key.
  934. * @param __x Key of (key, value) pairs to be located.
  935. * @return Pair of read-only (constant) iterators that possibly points
  936. * to the subsequence matching given key.
  937. *
  938. * This function is equivalent to
  939. * @code
  940. * std::make_pair(c.lower_bound(val),
  941. * c.upper_bound(val))
  942. * @endcode
  943. * (but is faster than making the calls separately).
  944. */
  945. std::pair<const_iterator, const_iterator>
  946. equal_range(const key_type& __x) const
  947. { return _M_t.equal_range(__x); }
  948. #if __cplusplus > 201103L
  949. template<typename _Kt>
  950. auto
  951. equal_range(const _Kt& __x) const
  952. -> decltype(pair<const_iterator, const_iterator>(
  953. _M_t._M_equal_range_tr(__x)))
  954. {
  955. return pair<const_iterator, const_iterator>(
  956. _M_t._M_equal_range_tr(__x));
  957. }
  958. #endif
  959. //@}
  960. template<typename _K1, typename _T1, typename _C1, typename _A1>
  961. friend bool
  962. operator==(const multimap<_K1, _T1, _C1, _A1>&,
  963. const multimap<_K1, _T1, _C1, _A1>&);
  964. #if __cpp_lib_three_way_comparison
  965. template<typename _K1, typename _T1, typename _C1, typename _A1>
  966. friend __detail::__synth3way_t<pair<const _K1, _T1>>
  967. operator<=>(const multimap<_K1, _T1, _C1, _A1>&,
  968. const multimap<_K1, _T1, _C1, _A1>&);
  969. #else
  970. template<typename _K1, typename _T1, typename _C1, typename _A1>
  971. friend bool
  972. operator<(const multimap<_K1, _T1, _C1, _A1>&,
  973. const multimap<_K1, _T1, _C1, _A1>&);
  974. #endif
  975. };
  976. #if __cpp_deduction_guides >= 201606
  977. template<typename _InputIterator,
  978. typename _Compare = less<__iter_key_t<_InputIterator>>,
  979. typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
  980. typename = _RequireInputIter<_InputIterator>,
  981. typename = _RequireNotAllocator<_Compare>,
  982. typename = _RequireAllocator<_Allocator>>
  983. multimap(_InputIterator, _InputIterator,
  984. _Compare = _Compare(), _Allocator = _Allocator())
  985. -> multimap<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
  986. _Compare, _Allocator>;
  987. template<typename _Key, typename _Tp, typename _Compare = less<_Key>,
  988. typename _Allocator = allocator<pair<const _Key, _Tp>>,
  989. typename = _RequireNotAllocator<_Compare>,
  990. typename = _RequireAllocator<_Allocator>>
  991. multimap(initializer_list<pair<_Key, _Tp>>,
  992. _Compare = _Compare(), _Allocator = _Allocator())
  993. -> multimap<_Key, _Tp, _Compare, _Allocator>;
  994. template<typename _InputIterator, typename _Allocator,
  995. typename = _RequireInputIter<_InputIterator>,
  996. typename = _RequireAllocator<_Allocator>>
  997. multimap(_InputIterator, _InputIterator, _Allocator)
  998. -> multimap<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
  999. less<__iter_key_t<_InputIterator>>, _Allocator>;
  1000. template<typename _Key, typename _Tp, typename _Allocator,
  1001. typename = _RequireAllocator<_Allocator>>
  1002. multimap(initializer_list<pair<_Key, _Tp>>, _Allocator)
  1003. -> multimap<_Key, _Tp, less<_Key>, _Allocator>;
  1004. #endif // deduction guides
  1005. /**
  1006. * @brief Multimap equality comparison.
  1007. * @param __x A %multimap.
  1008. * @param __y A %multimap of the same type as @a __x.
  1009. * @return True iff the size and elements of the maps are equal.
  1010. *
  1011. * This is an equivalence relation. It is linear in the size of the
  1012. * multimaps. Multimaps are considered equivalent if their sizes are equal,
  1013. * and if corresponding elements compare equal.
  1014. */
  1015. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1016. inline bool
  1017. operator==(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
  1018. const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
  1019. { return __x._M_t == __y._M_t; }
  1020. #if __cpp_lib_three_way_comparison
  1021. /**
  1022. * @brief Multimap ordering relation.
  1023. * @param __x A `multimap`.
  1024. * @param __y A `multimap` of the same type as `x`.
  1025. * @return A value indicating whether `__x` is less than, equal to,
  1026. * greater than, or incomparable with `__y`.
  1027. *
  1028. * This is a total ordering relation. It is linear in the size of the
  1029. * maps. The elements must be comparable with @c <.
  1030. *
  1031. * See `std::lexicographical_compare_three_way()` for how the determination
  1032. * is made. This operator is used to synthesize relational operators like
  1033. * `<` and `>=` etc.
  1034. */
  1035. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1036. inline __detail::__synth3way_t<pair<const _Key, _Tp>>
  1037. operator<=>(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
  1038. const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
  1039. { return __x._M_t <=> __y._M_t; }
  1040. #else
  1041. /**
  1042. * @brief Multimap ordering relation.
  1043. * @param __x A %multimap.
  1044. * @param __y A %multimap of the same type as @a __x.
  1045. * @return True iff @a x is lexicographically less than @a y.
  1046. *
  1047. * This is a total ordering relation. It is linear in the size of the
  1048. * multimaps. The elements must be comparable with @c <.
  1049. *
  1050. * See std::lexicographical_compare() for how the determination is made.
  1051. */
  1052. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1053. inline bool
  1054. operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
  1055. const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
  1056. { return __x._M_t < __y._M_t; }
  1057. /// Based on operator==
  1058. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1059. inline bool
  1060. operator!=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
  1061. const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
  1062. { return !(__x == __y); }
  1063. /// Based on operator<
  1064. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1065. inline bool
  1066. operator>(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
  1067. const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
  1068. { return __y < __x; }
  1069. /// Based on operator<
  1070. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1071. inline bool
  1072. operator<=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
  1073. const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
  1074. { return !(__y < __x); }
  1075. /// Based on operator<
  1076. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1077. inline bool
  1078. operator>=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
  1079. const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
  1080. { return !(__x < __y); }
  1081. #endif // three-way comparison
  1082. /// See std::multimap::swap().
  1083. template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
  1084. inline void
  1085. swap(multimap<_Key, _Tp, _Compare, _Alloc>& __x,
  1086. multimap<_Key, _Tp, _Compare, _Alloc>& __y)
  1087. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  1088. { __x.swap(__y); }
  1089. _GLIBCXX_END_NAMESPACE_CONTAINER
  1090. #if __cplusplus > 201402L
  1091. // Allow std::multimap access to internals of compatible maps.
  1092. template<typename _Key, typename _Val, typename _Cmp1, typename _Alloc,
  1093. typename _Cmp2>
  1094. struct
  1095. _Rb_tree_merge_helper<_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp1, _Alloc>,
  1096. _Cmp2>
  1097. {
  1098. private:
  1099. friend class _GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp1, _Alloc>;
  1100. static auto&
  1101. _S_get_tree(_GLIBCXX_STD_C::map<_Key, _Val, _Cmp2, _Alloc>& __map)
  1102. { return __map._M_t; }
  1103. static auto&
  1104. _S_get_tree(_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp2, _Alloc>& __map)
  1105. { return __map._M_t; }
  1106. };
  1107. #endif // C++17
  1108. _GLIBCXX_END_NAMESPACE_VERSION
  1109. } // namespace std
  1110. #endif /* _STL_MULTIMAP_H */