stl_multimap.h 41 KB

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