stl_map.h 52 KB

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