stl_map.h 55 KB

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