map 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_MAP
  10. #define _LIBCPP_MAP
  11. /*
  12. map synopsis
  13. namespace std
  14. {
  15. template <class Key, class T, class Compare = less<Key>,
  16. class Allocator = allocator<pair<const Key, T>>>
  17. class map
  18. {
  19. public:
  20. // types:
  21. typedef Key key_type;
  22. typedef T mapped_type;
  23. typedef pair<const key_type, mapped_type> value_type;
  24. typedef Compare key_compare;
  25. typedef Allocator allocator_type;
  26. typedef typename allocator_type::reference reference;
  27. typedef typename allocator_type::const_reference const_reference;
  28. typedef typename allocator_type::pointer pointer;
  29. typedef typename allocator_type::const_pointer const_pointer;
  30. typedef typename allocator_type::size_type size_type;
  31. typedef typename allocator_type::difference_type difference_type;
  32. typedef implementation-defined iterator;
  33. typedef implementation-defined const_iterator;
  34. typedef std::reverse_iterator<iterator> reverse_iterator;
  35. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  36. typedef unspecified node_type; // C++17
  37. typedef INSERT_RETURN_TYPE<iterator, node_type> insert_return_type; // C++17
  38. class value_compare
  39. {
  40. friend class map;
  41. protected:
  42. key_compare comp;
  43. value_compare(key_compare c);
  44. public:
  45. typedef bool result_type; // deprecated in C++17, removed in C++20
  46. typedef value_type first_argument_type; // deprecated in C++17, removed in C++20
  47. typedef value_type second_argument_type; // deprecated in C++17, removed in C++20
  48. bool operator()(const value_type& x, const value_type& y) const;
  49. };
  50. // construct/copy/destroy:
  51. map()
  52. noexcept(
  53. is_nothrow_default_constructible<allocator_type>::value &&
  54. is_nothrow_default_constructible<key_compare>::value &&
  55. is_nothrow_copy_constructible<key_compare>::value);
  56. explicit map(const key_compare& comp);
  57. map(const key_compare& comp, const allocator_type& a);
  58. template <class InputIterator>
  59. map(InputIterator first, InputIterator last,
  60. const key_compare& comp = key_compare());
  61. template <class InputIterator>
  62. map(InputIterator first, InputIterator last,
  63. const key_compare& comp, const allocator_type& a);
  64. template<container-compatible-range<value_type> R>
  65. map(from_range_t, R&& rg, const Compare& comp = Compare(), const Allocator& = Allocator()); // C++23
  66. map(const map& m);
  67. map(map&& m)
  68. noexcept(
  69. is_nothrow_move_constructible<allocator_type>::value &&
  70. is_nothrow_move_constructible<key_compare>::value);
  71. explicit map(const allocator_type& a);
  72. map(const map& m, const allocator_type& a);
  73. map(map&& m, const allocator_type& a);
  74. map(initializer_list<value_type> il, const key_compare& comp = key_compare());
  75. map(initializer_list<value_type> il, const key_compare& comp, const allocator_type& a);
  76. template <class InputIterator>
  77. map(InputIterator first, InputIterator last, const allocator_type& a)
  78. : map(first, last, Compare(), a) {} // C++14
  79. template<container-compatible-range<value_type> R>
  80. map(from_range_t, R&& rg, const Allocator& a))
  81. : map(from_range, std::forward<R>(rg), Compare(), a) { } // C++23
  82. map(initializer_list<value_type> il, const allocator_type& a)
  83. : map(il, Compare(), a) {} // C++14
  84. ~map();
  85. map& operator=(const map& m);
  86. map& operator=(map&& m)
  87. noexcept(
  88. allocator_type::propagate_on_container_move_assignment::value &&
  89. is_nothrow_move_assignable<allocator_type>::value &&
  90. is_nothrow_move_assignable<key_compare>::value);
  91. map& operator=(initializer_list<value_type> il);
  92. // iterators:
  93. iterator begin() noexcept;
  94. const_iterator begin() const noexcept;
  95. iterator end() noexcept;
  96. const_iterator end() const noexcept;
  97. reverse_iterator rbegin() noexcept;
  98. const_reverse_iterator rbegin() const noexcept;
  99. reverse_iterator rend() noexcept;
  100. const_reverse_iterator rend() const noexcept;
  101. const_iterator cbegin() const noexcept;
  102. const_iterator cend() const noexcept;
  103. const_reverse_iterator crbegin() const noexcept;
  104. const_reverse_iterator crend() const noexcept;
  105. // capacity:
  106. bool empty() const noexcept;
  107. size_type size() const noexcept;
  108. size_type max_size() const noexcept;
  109. // element access:
  110. mapped_type& operator[](const key_type& k);
  111. mapped_type& operator[](key_type&& k);
  112. mapped_type& at(const key_type& k);
  113. const mapped_type& at(const key_type& k) const;
  114. // modifiers:
  115. template <class... Args>
  116. pair<iterator, bool> emplace(Args&&... args);
  117. template <class... Args>
  118. iterator emplace_hint(const_iterator position, Args&&... args);
  119. pair<iterator, bool> insert(const value_type& v);
  120. pair<iterator, bool> insert( value_type&& v); // C++17
  121. template <class P>
  122. pair<iterator, bool> insert(P&& p);
  123. iterator insert(const_iterator position, const value_type& v);
  124. iterator insert(const_iterator position, value_type&& v); // C++17
  125. template <class P>
  126. iterator insert(const_iterator position, P&& p);
  127. template <class InputIterator>
  128. void insert(InputIterator first, InputIterator last);
  129. template<container-compatible-range<value_type> R>
  130. void insert_range(R&& rg); // C++23
  131. void insert(initializer_list<value_type> il);
  132. node_type extract(const_iterator position); // C++17
  133. node_type extract(const key_type& x); // C++17
  134. insert_return_type insert(node_type&& nh); // C++17
  135. iterator insert(const_iterator hint, node_type&& nh); // C++17
  136. template <class... Args>
  137. pair<iterator, bool> try_emplace(const key_type& k, Args&&... args); // C++17
  138. template <class... Args>
  139. pair<iterator, bool> try_emplace(key_type&& k, Args&&... args); // C++17
  140. template <class... Args>
  141. iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args); // C++17
  142. template <class... Args>
  143. iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args); // C++17
  144. template <class M>
  145. pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj); // C++17
  146. template <class M>
  147. pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj); // C++17
  148. template <class M>
  149. iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj); // C++17
  150. template <class M>
  151. iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj); // C++17
  152. iterator erase(const_iterator position);
  153. iterator erase(iterator position); // C++14
  154. size_type erase(const key_type& k);
  155. iterator erase(const_iterator first, const_iterator last);
  156. void clear() noexcept;
  157. template<class C2>
  158. void merge(map<Key, T, C2, Allocator>& source); // C++17
  159. template<class C2>
  160. void merge(map<Key, T, C2, Allocator>&& source); // C++17
  161. template<class C2>
  162. void merge(multimap<Key, T, C2, Allocator>& source); // C++17
  163. template<class C2>
  164. void merge(multimap<Key, T, C2, Allocator>&& source); // C++17
  165. void swap(map& m)
  166. noexcept(allocator_traits<allocator_type>::is_always_equal::value &&
  167. is_nothrow_swappable<key_compare>::value); // C++17
  168. // observers:
  169. allocator_type get_allocator() const noexcept;
  170. key_compare key_comp() const;
  171. value_compare value_comp() const;
  172. // map operations:
  173. iterator find(const key_type& k);
  174. const_iterator find(const key_type& k) const;
  175. template<typename K>
  176. iterator find(const K& x); // C++14
  177. template<typename K>
  178. const_iterator find(const K& x) const; // C++14
  179. template<typename K>
  180. size_type count(const K& x) const; // C++14
  181. size_type count(const key_type& k) const;
  182. bool contains(const key_type& x) const; // C++20
  183. template<class K> bool contains(const K& x) const; // C++20
  184. iterator lower_bound(const key_type& k);
  185. const_iterator lower_bound(const key_type& k) const;
  186. template<typename K>
  187. iterator lower_bound(const K& x); // C++14
  188. template<typename K>
  189. const_iterator lower_bound(const K& x) const; // C++14
  190. iterator upper_bound(const key_type& k);
  191. const_iterator upper_bound(const key_type& k) const;
  192. template<typename K>
  193. iterator upper_bound(const K& x); // C++14
  194. template<typename K>
  195. const_iterator upper_bound(const K& x) const; // C++14
  196. pair<iterator,iterator> equal_range(const key_type& k);
  197. pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
  198. template<typename K>
  199. pair<iterator,iterator> equal_range(const K& x); // C++14
  200. template<typename K>
  201. pair<const_iterator,const_iterator> equal_range(const K& x) const; // C++14
  202. };
  203. template <class InputIterator,
  204. class Compare = less<iter_key_t<InputIterator>>,
  205. class Allocator = allocator<iter_to_alloc_t<InputIterator>>>
  206. map(InputIterator, InputIterator, Compare = Compare(), Allocator = Allocator())
  207. -> map<iter_key_t<InputIterator>, iter_val_t<InputIterator>, Compare, Allocator>; // C++17
  208. template<ranges::input_range R, class Compare = less<range-key-type<R>,
  209. class Allocator = allocator<range-to-alloc-type<R>>>
  210. map(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())
  211. -> map<range-key-type<R>, range-mapped-type<R>, Compare, Allocator>; // C++23
  212. template<class Key, class T, class Compare = less<Key>,
  213. class Allocator = allocator<pair<const Key, T>>>
  214. map(initializer_list<pair<const Key, T>>, Compare = Compare(), Allocator = Allocator())
  215. -> map<Key, T, Compare, Allocator>; // C++17
  216. template <class InputIterator, class Allocator>
  217. map(InputIterator, InputIterator, Allocator)
  218. -> map<iter_key_t<InputIterator>, iter_val_t<InputIterator>, less<iter_key_t<InputIterator>>,
  219. Allocator>; // C++17
  220. template<ranges::input_range R, class Allocator>
  221. map(from_range_t, R&&, Allocator)
  222. -> map<range-key-type<R>, range-mapped-type<R>, less<range-key-type<R>>, Allocator>; // C++23
  223. template<class Key, class T, class Allocator>
  224. map(initializer_list<pair<const Key, T>>, Allocator) -> map<Key, T, less<Key>, Allocator>; // C++17
  225. template <class Key, class T, class Compare, class Allocator>
  226. bool
  227. operator==(const map<Key, T, Compare, Allocator>& x,
  228. const map<Key, T, Compare, Allocator>& y);
  229. template <class Key, class T, class Compare, class Allocator>
  230. bool
  231. operator< (const map<Key, T, Compare, Allocator>& x,
  232. const map<Key, T, Compare, Allocator>& y); // removed in C++20
  233. template <class Key, class T, class Compare, class Allocator>
  234. bool
  235. operator!=(const map<Key, T, Compare, Allocator>& x,
  236. const map<Key, T, Compare, Allocator>& y); // removed in C++20
  237. template <class Key, class T, class Compare, class Allocator>
  238. bool
  239. operator> (const map<Key, T, Compare, Allocator>& x,
  240. const map<Key, T, Compare, Allocator>& y); // removed in C++20
  241. template <class Key, class T, class Compare, class Allocator>
  242. bool
  243. operator>=(const map<Key, T, Compare, Allocator>& x,
  244. const map<Key, T, Compare, Allocator>& y); // removed in C++20
  245. template <class Key, class T, class Compare, class Allocator>
  246. bool
  247. operator<=(const map<Key, T, Compare, Allocator>& x,
  248. const map<Key, T, Compare, Allocator>& y); // removed in C++20
  249. template<class Key, class T, class Compare, class Allocator>
  250. synth-three-way-result<pair<const Key, T>>
  251. operator<=>(const map<Key, T, Compare, Allocator>& x,
  252. const map<Key, T, Compare, Allocator>& y); // since C++20
  253. // specialized algorithms:
  254. template <class Key, class T, class Compare, class Allocator>
  255. void
  256. swap(map<Key, T, Compare, Allocator>& x, map<Key, T, Compare, Allocator>& y)
  257. noexcept(noexcept(x.swap(y)));
  258. template <class Key, class T, class Compare, class Allocator, class Predicate>
  259. typename map<Key, T, Compare, Allocator>::size_type
  260. erase_if(map<Key, T, Compare, Allocator>& c, Predicate pred); // C++20
  261. template <class Key, class T, class Compare = less<Key>,
  262. class Allocator = allocator<pair<const Key, T>>>
  263. class multimap
  264. {
  265. public:
  266. // types:
  267. typedef Key key_type;
  268. typedef T mapped_type;
  269. typedef pair<const key_type,mapped_type> value_type;
  270. typedef Compare key_compare;
  271. typedef Allocator allocator_type;
  272. typedef typename allocator_type::reference reference;
  273. typedef typename allocator_type::const_reference const_reference;
  274. typedef typename allocator_type::size_type size_type;
  275. typedef typename allocator_type::difference_type difference_type;
  276. typedef typename allocator_type::pointer pointer;
  277. typedef typename allocator_type::const_pointer const_pointer;
  278. typedef implementation-defined iterator;
  279. typedef implementation-defined const_iterator;
  280. typedef std::reverse_iterator<iterator> reverse_iterator;
  281. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  282. typedef unspecified node_type; // C++17
  283. class value_compare
  284. {
  285. friend class multimap;
  286. protected:
  287. key_compare comp;
  288. value_compare(key_compare c);
  289. public:
  290. typedef bool result_type; // deprecated in C++17, removed in C++20
  291. typedef value_type first_argument_type; // deprecated in C++17, removed in C++20
  292. typedef value_type second_argument_type; // deprecated in C++17, removed in C++20
  293. bool operator()(const value_type& x, const value_type& y) const;
  294. };
  295. // construct/copy/destroy:
  296. multimap()
  297. noexcept(
  298. is_nothrow_default_constructible<allocator_type>::value &&
  299. is_nothrow_default_constructible<key_compare>::value &&
  300. is_nothrow_copy_constructible<key_compare>::value);
  301. explicit multimap(const key_compare& comp);
  302. multimap(const key_compare& comp, const allocator_type& a);
  303. template <class InputIterator>
  304. multimap(InputIterator first, InputIterator last, const key_compare& comp);
  305. template <class InputIterator>
  306. multimap(InputIterator first, InputIterator last, const key_compare& comp,
  307. const allocator_type& a);
  308. template<container-compatible-range<value_type> R>
  309. multimap(from_range_t, R&& rg,
  310. const Compare& comp = Compare(), const Allocator& = Allocator()); // C++23
  311. multimap(const multimap& m);
  312. multimap(multimap&& m)
  313. noexcept(
  314. is_nothrow_move_constructible<allocator_type>::value &&
  315. is_nothrow_move_constructible<key_compare>::value);
  316. explicit multimap(const allocator_type& a);
  317. multimap(const multimap& m, const allocator_type& a);
  318. multimap(multimap&& m, const allocator_type& a);
  319. multimap(initializer_list<value_type> il, const key_compare& comp = key_compare());
  320. multimap(initializer_list<value_type> il, const key_compare& comp,
  321. const allocator_type& a);
  322. template <class InputIterator>
  323. multimap(InputIterator first, InputIterator last, const allocator_type& a)
  324. : multimap(first, last, Compare(), a) {} // C++14
  325. template<container-compatible-range<value_type> R>
  326. multimap(from_range_t, R&& rg, const Allocator& a))
  327. : multimap(from_range, std::forward<R>(rg), Compare(), a) { } // C++23
  328. multimap(initializer_list<value_type> il, const allocator_type& a)
  329. : multimap(il, Compare(), a) {} // C++14
  330. ~multimap();
  331. multimap& operator=(const multimap& m);
  332. multimap& operator=(multimap&& m)
  333. noexcept(
  334. allocator_type::propagate_on_container_move_assignment::value &&
  335. is_nothrow_move_assignable<allocator_type>::value &&
  336. is_nothrow_move_assignable<key_compare>::value);
  337. multimap& operator=(initializer_list<value_type> il);
  338. // iterators:
  339. iterator begin() noexcept;
  340. const_iterator begin() const noexcept;
  341. iterator end() noexcept;
  342. const_iterator end() const noexcept;
  343. reverse_iterator rbegin() noexcept;
  344. const_reverse_iterator rbegin() const noexcept;
  345. reverse_iterator rend() noexcept;
  346. const_reverse_iterator rend() const noexcept;
  347. const_iterator cbegin() const noexcept;
  348. const_iterator cend() const noexcept;
  349. const_reverse_iterator crbegin() const noexcept;
  350. const_reverse_iterator crend() const noexcept;
  351. // capacity:
  352. bool empty() const noexcept;
  353. size_type size() const noexcept;
  354. size_type max_size() const noexcept;
  355. // modifiers:
  356. template <class... Args>
  357. iterator emplace(Args&&... args);
  358. template <class... Args>
  359. iterator emplace_hint(const_iterator position, Args&&... args);
  360. iterator insert(const value_type& v);
  361. iterator insert( value_type&& v); // C++17
  362. template <class P>
  363. iterator insert(P&& p);
  364. iterator insert(const_iterator position, const value_type& v);
  365. iterator insert(const_iterator position, value_type&& v); // C++17
  366. template <class P>
  367. iterator insert(const_iterator position, P&& p);
  368. template <class InputIterator>
  369. void insert(InputIterator first, InputIterator last);
  370. template<container-compatible-range<value_type> R>
  371. void insert_range(R&& rg); // C++23
  372. void insert(initializer_list<value_type> il);
  373. node_type extract(const_iterator position); // C++17
  374. node_type extract(const key_type& x); // C++17
  375. iterator insert(node_type&& nh); // C++17
  376. iterator insert(const_iterator hint, node_type&& nh); // C++17
  377. iterator erase(const_iterator position);
  378. iterator erase(iterator position); // C++14
  379. size_type erase(const key_type& k);
  380. iterator erase(const_iterator first, const_iterator last);
  381. void clear() noexcept;
  382. template<class C2>
  383. void merge(multimap<Key, T, C2, Allocator>& source); // C++17
  384. template<class C2>
  385. void merge(multimap<Key, T, C2, Allocator>&& source); // C++17
  386. template<class C2>
  387. void merge(map<Key, T, C2, Allocator>& source); // C++17
  388. template<class C2>
  389. void merge(map<Key, T, C2, Allocator>&& source); // C++17
  390. void swap(multimap& m)
  391. noexcept(allocator_traits<allocator_type>::is_always_equal::value &&
  392. is_nothrow_swappable<key_compare>::value); // C++17
  393. // observers:
  394. allocator_type get_allocator() const noexcept;
  395. key_compare key_comp() const;
  396. value_compare value_comp() const;
  397. // map operations:
  398. iterator find(const key_type& k);
  399. const_iterator find(const key_type& k) const;
  400. template<typename K>
  401. iterator find(const K& x); // C++14
  402. template<typename K>
  403. const_iterator find(const K& x) const; // C++14
  404. template<typename K>
  405. size_type count(const K& x) const; // C++14
  406. size_type count(const key_type& k) const;
  407. bool contains(const key_type& x) const; // C++20
  408. template<class K> bool contains(const K& x) const; // C++20
  409. iterator lower_bound(const key_type& k);
  410. const_iterator lower_bound(const key_type& k) const;
  411. template<typename K>
  412. iterator lower_bound(const K& x); // C++14
  413. template<typename K>
  414. const_iterator lower_bound(const K& x) const; // C++14
  415. iterator upper_bound(const key_type& k);
  416. const_iterator upper_bound(const key_type& k) const;
  417. template<typename K>
  418. iterator upper_bound(const K& x); // C++14
  419. template<typename K>
  420. const_iterator upper_bound(const K& x) const; // C++14
  421. pair<iterator,iterator> equal_range(const key_type& k);
  422. pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
  423. template<typename K>
  424. pair<iterator,iterator> equal_range(const K& x); // C++14
  425. template<typename K>
  426. pair<const_iterator,const_iterator> equal_range(const K& x) const; // C++14
  427. };
  428. template <class InputIterator,
  429. class Compare = less<iter_key_t<InputIterator>>,
  430. class Allocator = allocator<iter_to_alloc_t<InputIterator>>>
  431. multimap(InputIterator, InputIterator, Compare = Compare(), Allocator = Allocator())
  432. -> multimap<iter_key_t<InputIterator>, iter_val_t<InputIterator>, Compare, Allocator>; // C++17
  433. template<ranges::input_range R, class Compare = less<range-key-type<R>>,
  434. class Allocator = allocator<range-to-alloc-type<R>>>
  435. multimap(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())
  436. -> multimap<range-key-type<R>, range-mapped-type<R>, Compare, Allocator>; // C++23
  437. template<class Key, class T, class Compare = less<Key>,
  438. class Allocator = allocator<pair<const Key, T>>>
  439. multimap(initializer_list<pair<const Key, T>>, Compare = Compare(), Allocator = Allocator())
  440. -> multimap<Key, T, Compare, Allocator>; // C++17
  441. template <class InputIterator, class Allocator>
  442. multimap(InputIterator, InputIterator, Allocator)
  443. -> multimap<iter_key_t<InputIterator>, iter_val_t<InputIterator>,
  444. less<iter_key_t<InputIterator>>, Allocator>; // C++17
  445. template<ranges::input_range R, class Allocator>
  446. multimap(from_range_t, R&&, Allocator)
  447. -> multimap<range-key-type<R>, range-mapped-type<R>, less<range-key-type<R>>, Allocator>; // C++23
  448. template<class Key, class T, class Allocator>
  449. multimap(initializer_list<pair<const Key, T>>, Allocator)
  450. -> multimap<Key, T, less<Key>, Allocator>; // C++17
  451. template <class Key, class T, class Compare, class Allocator>
  452. bool
  453. operator==(const multimap<Key, T, Compare, Allocator>& x,
  454. const multimap<Key, T, Compare, Allocator>& y);
  455. template <class Key, class T, class Compare, class Allocator>
  456. bool
  457. operator< (const multimap<Key, T, Compare, Allocator>& x,
  458. const multimap<Key, T, Compare, Allocator>& y); // removed in C++20
  459. template <class Key, class T, class Compare, class Allocator>
  460. bool
  461. operator!=(const multimap<Key, T, Compare, Allocator>& x,
  462. const multimap<Key, T, Compare, Allocator>& y); // removed in C++20
  463. template <class Key, class T, class Compare, class Allocator>
  464. bool
  465. operator> (const multimap<Key, T, Compare, Allocator>& x,
  466. const multimap<Key, T, Compare, Allocator>& y); // removed in C++20
  467. template <class Key, class T, class Compare, class Allocator>
  468. bool
  469. operator>=(const multimap<Key, T, Compare, Allocator>& x,
  470. const multimap<Key, T, Compare, Allocator>& y); // removed in C++20
  471. template <class Key, class T, class Compare, class Allocator>
  472. bool
  473. operator<=(const multimap<Key, T, Compare, Allocator>& x,
  474. const multimap<Key, T, Compare, Allocator>& y); // removed in C++20
  475. template<class Key, class T, class Compare, class Allocator>
  476. synth-three-way-result<pair<const Key, T>>
  477. operator<=>(const multimap<Key, T, Compare, Allocator>& x,
  478. const multimap<Key, T, Compare, Allocator>& y); // since c++20
  479. // specialized algorithms:
  480. template <class Key, class T, class Compare, class Allocator>
  481. void
  482. swap(multimap<Key, T, Compare, Allocator>& x,
  483. multimap<Key, T, Compare, Allocator>& y)
  484. noexcept(noexcept(x.swap(y)));
  485. template <class Key, class T, class Compare, class Allocator, class Predicate>
  486. typename multimap<Key, T, Compare, Allocator>::size_type
  487. erase_if(multimap<Key, T, Compare, Allocator>& c, Predicate pred); // C++20
  488. } // std
  489. */
  490. #include <__algorithm/equal.h>
  491. #include <__algorithm/lexicographical_compare.h>
  492. #include <__algorithm/lexicographical_compare_three_way.h>
  493. #include <__assert>
  494. #include <__availability>
  495. #include <__config>
  496. #include <__functional/binary_function.h>
  497. #include <__functional/is_transparent.h>
  498. #include <__functional/operations.h>
  499. #include <__iterator/erase_if_container.h>
  500. #include <__iterator/iterator_traits.h>
  501. #include <__iterator/ranges_iterator_traits.h>
  502. #include <__iterator/reverse_iterator.h>
  503. #include <__memory/addressof.h>
  504. #include <__memory/allocator.h>
  505. #include <__memory_resource/polymorphic_allocator.h>
  506. #include <__node_handle>
  507. #include <__ranges/concepts.h>
  508. #include <__ranges/container_compatible_range.h>
  509. #include <__ranges/from_range.h>
  510. #include <__tree>
  511. #include <__type_traits/is_allocator.h>
  512. #include <__utility/forward.h>
  513. #include <__utility/piecewise_construct.h>
  514. #include <__utility/swap.h>
  515. #include <stdexcept>
  516. #include <tuple>
  517. #include <version>
  518. // standard-mandated includes
  519. // [iterator.range]
  520. #include <__iterator/access.h>
  521. #include <__iterator/data.h>
  522. #include <__iterator/empty.h>
  523. #include <__iterator/reverse_access.h>
  524. #include <__iterator/size.h>
  525. // [associative.map.syn]
  526. #include <compare>
  527. #include <initializer_list>
  528. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  529. # pragma GCC system_header
  530. #endif
  531. _LIBCPP_PUSH_MACROS
  532. #include <__undef_macros>
  533. _LIBCPP_BEGIN_NAMESPACE_STD
  534. template <class _Key,
  535. class _CP,
  536. class _Compare,
  537. bool = is_empty<_Compare>::value && !__libcpp_is_final<_Compare>::value>
  538. class __map_value_compare : private _Compare {
  539. public:
  540. _LIBCPP_HIDE_FROM_ABI __map_value_compare() _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
  541. : _Compare() {}
  542. _LIBCPP_HIDE_FROM_ABI __map_value_compare(_Compare __c) _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
  543. : _Compare(__c) {}
  544. _LIBCPP_HIDE_FROM_ABI const _Compare& key_comp() const _NOEXCEPT { return *this; }
  545. _LIBCPP_HIDE_FROM_ABI bool operator()(const _CP& __x, const _CP& __y) const {
  546. return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y.__get_value().first);
  547. }
  548. _LIBCPP_HIDE_FROM_ABI bool operator()(const _CP& __x, const _Key& __y) const {
  549. return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y);
  550. }
  551. _LIBCPP_HIDE_FROM_ABI bool operator()(const _Key& __x, const _CP& __y) const {
  552. return static_cast<const _Compare&>(*this)(__x, __y.__get_value().first);
  553. }
  554. _LIBCPP_HIDE_FROM_ABI void swap(__map_value_compare& __y) _NOEXCEPT_(__is_nothrow_swappable<_Compare>::value) {
  555. using std::swap;
  556. swap(static_cast<_Compare&>(*this), static_cast<_Compare&>(__y));
  557. }
  558. #if _LIBCPP_STD_VER >= 14
  559. template <typename _K2>
  560. _LIBCPP_HIDE_FROM_ABI bool operator()(const _K2& __x, const _CP& __y) const {
  561. return static_cast<const _Compare&>(*this)(__x, __y.__get_value().first);
  562. }
  563. template <typename _K2>
  564. _LIBCPP_HIDE_FROM_ABI bool operator()(const _CP& __x, const _K2& __y) const {
  565. return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y);
  566. }
  567. #endif
  568. };
  569. template <class _Key, class _CP, class _Compare>
  570. class __map_value_compare<_Key, _CP, _Compare, false> {
  571. _Compare __comp_;
  572. public:
  573. _LIBCPP_HIDE_FROM_ABI __map_value_compare() _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
  574. : __comp_() {}
  575. _LIBCPP_HIDE_FROM_ABI __map_value_compare(_Compare __c) _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
  576. : __comp_(__c) {}
  577. _LIBCPP_HIDE_FROM_ABI const _Compare& key_comp() const _NOEXCEPT { return __comp_; }
  578. _LIBCPP_HIDE_FROM_ABI bool operator()(const _CP& __x, const _CP& __y) const {
  579. return __comp_(__x.__get_value().first, __y.__get_value().first);
  580. }
  581. _LIBCPP_HIDE_FROM_ABI bool operator()(const _CP& __x, const _Key& __y) const {
  582. return __comp_(__x.__get_value().first, __y);
  583. }
  584. _LIBCPP_HIDE_FROM_ABI bool operator()(const _Key& __x, const _CP& __y) const {
  585. return __comp_(__x, __y.__get_value().first);
  586. }
  587. void swap(__map_value_compare& __y) _NOEXCEPT_(__is_nothrow_swappable<_Compare>::value) {
  588. using std::swap;
  589. swap(__comp_, __y.__comp_);
  590. }
  591. #if _LIBCPP_STD_VER >= 14
  592. template <typename _K2>
  593. _LIBCPP_HIDE_FROM_ABI bool operator()(const _K2& __x, const _CP& __y) const {
  594. return __comp_(__x, __y.__get_value().first);
  595. }
  596. template <typename _K2>
  597. _LIBCPP_HIDE_FROM_ABI bool operator()(const _CP& __x, const _K2& __y) const {
  598. return __comp_(__x.__get_value().first, __y);
  599. }
  600. #endif
  601. };
  602. template <class _Key, class _CP, class _Compare, bool __b>
  603. inline _LIBCPP_HIDE_FROM_ABI void
  604. swap(__map_value_compare<_Key, _CP, _Compare, __b>& __x, __map_value_compare<_Key, _CP, _Compare, __b>& __y)
  605. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
  606. __x.swap(__y);
  607. }
  608. template <class _Allocator>
  609. class __map_node_destructor {
  610. typedef _Allocator allocator_type;
  611. typedef allocator_traits<allocator_type> __alloc_traits;
  612. public:
  613. typedef typename __alloc_traits::pointer pointer;
  614. private:
  615. allocator_type& __na_;
  616. __map_node_destructor& operator=(const __map_node_destructor&);
  617. public:
  618. bool __first_constructed;
  619. bool __second_constructed;
  620. _LIBCPP_HIDE_FROM_ABI explicit __map_node_destructor(allocator_type& __na) _NOEXCEPT
  621. : __na_(__na),
  622. __first_constructed(false),
  623. __second_constructed(false) {}
  624. #ifndef _LIBCPP_CXX03_LANG
  625. _LIBCPP_HIDE_FROM_ABI __map_node_destructor(__tree_node_destructor<allocator_type>&& __x) _NOEXCEPT
  626. : __na_(__x.__na_),
  627. __first_constructed(__x.__value_constructed),
  628. __second_constructed(__x.__value_constructed) {
  629. __x.__value_constructed = false;
  630. }
  631. #endif // _LIBCPP_CXX03_LANG
  632. _LIBCPP_HIDE_FROM_ABI void operator()(pointer __p) _NOEXCEPT {
  633. if (__second_constructed)
  634. __alloc_traits::destroy(__na_, std::addressof(__p->__value_.__get_value().second));
  635. if (__first_constructed)
  636. __alloc_traits::destroy(__na_, std::addressof(__p->__value_.__get_value().first));
  637. if (__p)
  638. __alloc_traits::deallocate(__na_, __p, 1);
  639. }
  640. };
  641. template <class _Key, class _Tp, class _Compare, class _Allocator>
  642. class map;
  643. template <class _Key, class _Tp, class _Compare, class _Allocator>
  644. class multimap;
  645. template <class _TreeIterator>
  646. class __map_const_iterator;
  647. #ifndef _LIBCPP_CXX03_LANG
  648. template <class _Key, class _Tp>
  649. struct _LIBCPP_STANDALONE_DEBUG __value_type {
  650. typedef _Key key_type;
  651. typedef _Tp mapped_type;
  652. typedef pair<const key_type, mapped_type> value_type;
  653. typedef pair<key_type&, mapped_type&> __nc_ref_pair_type;
  654. typedef pair<key_type&&, mapped_type&&> __nc_rref_pair_type;
  655. private:
  656. value_type __cc_;
  657. public:
  658. _LIBCPP_HIDE_FROM_ABI value_type& __get_value() {
  659. # if _LIBCPP_STD_VER >= 17
  660. return *std::launder(std::addressof(__cc_));
  661. # else
  662. return __cc_;
  663. # endif
  664. }
  665. _LIBCPP_HIDE_FROM_ABI const value_type& __get_value() const {
  666. # if _LIBCPP_STD_VER >= 17
  667. return *std::launder(std::addressof(__cc_));
  668. # else
  669. return __cc_;
  670. # endif
  671. }
  672. _LIBCPP_HIDE_FROM_ABI __nc_ref_pair_type __ref() {
  673. value_type& __v = __get_value();
  674. return __nc_ref_pair_type(const_cast<key_type&>(__v.first), __v.second);
  675. }
  676. _LIBCPP_HIDE_FROM_ABI __nc_rref_pair_type __move() {
  677. value_type& __v = __get_value();
  678. return __nc_rref_pair_type(std::move(const_cast<key_type&>(__v.first)), std::move(__v.second));
  679. }
  680. _LIBCPP_HIDE_FROM_ABI __value_type& operator=(const __value_type& __v) {
  681. __ref() = __v.__get_value();
  682. return *this;
  683. }
  684. _LIBCPP_HIDE_FROM_ABI __value_type& operator=(__value_type&& __v) {
  685. __ref() = __v.__move();
  686. return *this;
  687. }
  688. template <class _ValueTp, __enable_if_t<__is_same_uncvref<_ValueTp, value_type>::value, int> = 0>
  689. _LIBCPP_HIDE_FROM_ABI __value_type& operator=(_ValueTp&& __v) {
  690. __ref() = std::forward<_ValueTp>(__v);
  691. return *this;
  692. }
  693. private:
  694. __value_type() = delete;
  695. ~__value_type() = delete;
  696. __value_type(const __value_type&) = delete;
  697. __value_type(__value_type&&) = delete;
  698. };
  699. #else
  700. template <class _Key, class _Tp>
  701. struct __value_type {
  702. typedef _Key key_type;
  703. typedef _Tp mapped_type;
  704. typedef pair<const key_type, mapped_type> value_type;
  705. private:
  706. value_type __cc_;
  707. public:
  708. _LIBCPP_HIDE_FROM_ABI value_type& __get_value() { return __cc_; }
  709. _LIBCPP_HIDE_FROM_ABI const value_type& __get_value() const { return __cc_; }
  710. private:
  711. __value_type();
  712. __value_type(__value_type const&);
  713. __value_type& operator=(__value_type const&);
  714. ~__value_type();
  715. };
  716. #endif // _LIBCPP_CXX03_LANG
  717. template <class _Tp>
  718. struct __extract_key_value_types;
  719. template <class _Key, class _Tp>
  720. struct __extract_key_value_types<__value_type<_Key, _Tp> > {
  721. typedef _Key const __key_type;
  722. typedef _Tp __mapped_type;
  723. };
  724. template <class _TreeIterator>
  725. class _LIBCPP_TEMPLATE_VIS __map_iterator {
  726. typedef typename _TreeIterator::_NodeTypes _NodeTypes;
  727. typedef typename _TreeIterator::__pointer_traits __pointer_traits;
  728. _TreeIterator __i_;
  729. public:
  730. typedef bidirectional_iterator_tag iterator_category;
  731. typedef typename _NodeTypes::__map_value_type value_type;
  732. typedef typename _TreeIterator::difference_type difference_type;
  733. typedef value_type& reference;
  734. typedef typename _NodeTypes::__map_value_type_pointer pointer;
  735. _LIBCPP_HIDE_FROM_ABI __map_iterator() _NOEXCEPT {}
  736. _LIBCPP_HIDE_FROM_ABI __map_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
  737. _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __i_->__get_value(); }
  738. _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return pointer_traits<pointer>::pointer_to(__i_->__get_value()); }
  739. _LIBCPP_HIDE_FROM_ABI __map_iterator& operator++() {
  740. ++__i_;
  741. return *this;
  742. }
  743. _LIBCPP_HIDE_FROM_ABI __map_iterator operator++(int) {
  744. __map_iterator __t(*this);
  745. ++(*this);
  746. return __t;
  747. }
  748. _LIBCPP_HIDE_FROM_ABI __map_iterator& operator--() {
  749. --__i_;
  750. return *this;
  751. }
  752. _LIBCPP_HIDE_FROM_ABI __map_iterator operator--(int) {
  753. __map_iterator __t(*this);
  754. --(*this);
  755. return __t;
  756. }
  757. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __map_iterator& __x, const __map_iterator& __y) {
  758. return __x.__i_ == __y.__i_;
  759. }
  760. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __map_iterator& __x, const __map_iterator& __y) {
  761. return __x.__i_ != __y.__i_;
  762. }
  763. template <class, class, class, class>
  764. friend class _LIBCPP_TEMPLATE_VIS map;
  765. template <class, class, class, class>
  766. friend class _LIBCPP_TEMPLATE_VIS multimap;
  767. template <class>
  768. friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
  769. };
  770. template <class _TreeIterator>
  771. class _LIBCPP_TEMPLATE_VIS __map_const_iterator {
  772. typedef typename _TreeIterator::_NodeTypes _NodeTypes;
  773. typedef typename _TreeIterator::__pointer_traits __pointer_traits;
  774. _TreeIterator __i_;
  775. public:
  776. typedef bidirectional_iterator_tag iterator_category;
  777. typedef typename _NodeTypes::__map_value_type value_type;
  778. typedef typename _TreeIterator::difference_type difference_type;
  779. typedef const value_type& reference;
  780. typedef typename _NodeTypes::__const_map_value_type_pointer pointer;
  781. _LIBCPP_HIDE_FROM_ABI __map_const_iterator() _NOEXCEPT {}
  782. _LIBCPP_HIDE_FROM_ABI __map_const_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
  783. _LIBCPP_HIDE_FROM_ABI
  784. __map_const_iterator(__map_iterator< typename _TreeIterator::__non_const_iterator> __i) _NOEXCEPT : __i_(__i.__i_) {}
  785. _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __i_->__get_value(); }
  786. _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return pointer_traits<pointer>::pointer_to(__i_->__get_value()); }
  787. _LIBCPP_HIDE_FROM_ABI __map_const_iterator& operator++() {
  788. ++__i_;
  789. return *this;
  790. }
  791. _LIBCPP_HIDE_FROM_ABI __map_const_iterator operator++(int) {
  792. __map_const_iterator __t(*this);
  793. ++(*this);
  794. return __t;
  795. }
  796. _LIBCPP_HIDE_FROM_ABI __map_const_iterator& operator--() {
  797. --__i_;
  798. return *this;
  799. }
  800. _LIBCPP_HIDE_FROM_ABI __map_const_iterator operator--(int) {
  801. __map_const_iterator __t(*this);
  802. --(*this);
  803. return __t;
  804. }
  805. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __map_const_iterator& __x, const __map_const_iterator& __y) {
  806. return __x.__i_ == __y.__i_;
  807. }
  808. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __map_const_iterator& __x, const __map_const_iterator& __y) {
  809. return __x.__i_ != __y.__i_;
  810. }
  811. template <class, class, class, class>
  812. friend class _LIBCPP_TEMPLATE_VIS map;
  813. template <class, class, class, class>
  814. friend class _LIBCPP_TEMPLATE_VIS multimap;
  815. template <class, class, class>
  816. friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
  817. };
  818. template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
  819. class _LIBCPP_TEMPLATE_VIS map {
  820. public:
  821. // types:
  822. typedef _Key key_type;
  823. typedef _Tp mapped_type;
  824. typedef pair<const key_type, mapped_type> value_type;
  825. typedef __type_identity_t<_Compare> key_compare;
  826. typedef __type_identity_t<_Allocator> allocator_type;
  827. typedef value_type& reference;
  828. typedef const value_type& const_reference;
  829. static_assert((is_same<typename allocator_type::value_type, value_type>::value),
  830. "Allocator::value_type must be same type as value_type");
  831. class _LIBCPP_TEMPLATE_VIS value_compare : public __binary_function<value_type, value_type, bool> {
  832. friend class map;
  833. protected:
  834. key_compare comp;
  835. _LIBCPP_HIDE_FROM_ABI value_compare(key_compare __c) : comp(__c) {}
  836. public:
  837. _LIBCPP_HIDE_FROM_ABI bool operator()(const value_type& __x, const value_type& __y) const {
  838. return comp(__x.first, __y.first);
  839. }
  840. };
  841. private:
  842. typedef std::__value_type<key_type, mapped_type> __value_type;
  843. typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
  844. typedef __rebind_alloc<allocator_traits<allocator_type>, __value_type> __allocator_type;
  845. typedef __tree<__value_type, __vc, __allocator_type> __base;
  846. typedef typename __base::__node_traits __node_traits;
  847. typedef allocator_traits<allocator_type> __alloc_traits;
  848. static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
  849. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  850. "original allocator");
  851. __base __tree_;
  852. public:
  853. typedef typename __alloc_traits::pointer pointer;
  854. typedef typename __alloc_traits::const_pointer const_pointer;
  855. typedef typename __alloc_traits::size_type size_type;
  856. typedef typename __alloc_traits::difference_type difference_type;
  857. typedef __map_iterator<typename __base::iterator> iterator;
  858. typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
  859. typedef std::reverse_iterator<iterator> reverse_iterator;
  860. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  861. #if _LIBCPP_STD_VER >= 17
  862. typedef __map_node_handle<typename __base::__node, allocator_type> node_type;
  863. typedef __insert_return_type<iterator, node_type> insert_return_type;
  864. #endif
  865. template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
  866. friend class _LIBCPP_TEMPLATE_VIS map;
  867. template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
  868. friend class _LIBCPP_TEMPLATE_VIS multimap;
  869. _LIBCPP_HIDE_FROM_ABI map() _NOEXCEPT_(
  870. is_nothrow_default_constructible<allocator_type>::value&& is_nothrow_default_constructible<key_compare>::value&&
  871. is_nothrow_copy_constructible<key_compare>::value)
  872. : __tree_(__vc(key_compare())) {}
  873. _LIBCPP_HIDE_FROM_ABI explicit map(const key_compare& __comp) _NOEXCEPT_(
  874. is_nothrow_default_constructible<allocator_type>::value&& is_nothrow_copy_constructible<key_compare>::value)
  875. : __tree_(__vc(__comp)) {}
  876. _LIBCPP_HIDE_FROM_ABI explicit map(const key_compare& __comp, const allocator_type& __a)
  877. : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {}
  878. template <class _InputIterator>
  879. _LIBCPP_HIDE_FROM_ABI map(_InputIterator __f, _InputIterator __l, const key_compare& __comp = key_compare())
  880. : __tree_(__vc(__comp)) {
  881. insert(__f, __l);
  882. }
  883. template <class _InputIterator>
  884. _LIBCPP_HIDE_FROM_ABI
  885. map(_InputIterator __f, _InputIterator __l, const key_compare& __comp, const allocator_type& __a)
  886. : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {
  887. insert(__f, __l);
  888. }
  889. #if _LIBCPP_STD_VER >= 23
  890. template <_ContainerCompatibleRange<value_type> _Range>
  891. _LIBCPP_HIDE_FROM_ABI
  892. map(from_range_t,
  893. _Range&& __range,
  894. const key_compare& __comp = key_compare(),
  895. const allocator_type& __a = allocator_type())
  896. : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {
  897. insert_range(std::forward<_Range>(__range));
  898. }
  899. #endif
  900. #if _LIBCPP_STD_VER >= 14
  901. template <class _InputIterator>
  902. _LIBCPP_HIDE_FROM_ABI map(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
  903. : map(__f, __l, key_compare(), __a) {}
  904. #endif
  905. #if _LIBCPP_STD_VER >= 23
  906. template <_ContainerCompatibleRange<value_type> _Range>
  907. _LIBCPP_HIDE_FROM_ABI map(from_range_t, _Range&& __range, const allocator_type& __a)
  908. : map(from_range, std::forward<_Range>(__range), key_compare(), __a) {}
  909. #endif
  910. _LIBCPP_HIDE_FROM_ABI map(const map& __m) : __tree_(__m.__tree_) { insert(__m.begin(), __m.end()); }
  911. _LIBCPP_HIDE_FROM_ABI map& operator=(const map& __m) {
  912. #ifndef _LIBCPP_CXX03_LANG
  913. __tree_ = __m.__tree_;
  914. #else
  915. if (this != std::addressof(__m)) {
  916. __tree_.clear();
  917. __tree_.value_comp() = __m.__tree_.value_comp();
  918. __tree_.__copy_assign_alloc(__m.__tree_);
  919. insert(__m.begin(), __m.end());
  920. }
  921. #endif
  922. return *this;
  923. }
  924. #ifndef _LIBCPP_CXX03_LANG
  925. _LIBCPP_HIDE_FROM_ABI map(map&& __m) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
  926. : __tree_(std::move(__m.__tree_)) {}
  927. _LIBCPP_HIDE_FROM_ABI map(map&& __m, const allocator_type& __a);
  928. _LIBCPP_HIDE_FROM_ABI map& operator=(map&& __m) _NOEXCEPT_(is_nothrow_move_assignable<__base>::value) {
  929. __tree_ = std::move(__m.__tree_);
  930. return *this;
  931. }
  932. _LIBCPP_HIDE_FROM_ABI map(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
  933. : __tree_(__vc(__comp)) {
  934. insert(__il.begin(), __il.end());
  935. }
  936. _LIBCPP_HIDE_FROM_ABI map(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
  937. : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {
  938. insert(__il.begin(), __il.end());
  939. }
  940. # if _LIBCPP_STD_VER >= 14
  941. _LIBCPP_HIDE_FROM_ABI map(initializer_list<value_type> __il, const allocator_type& __a)
  942. : map(__il, key_compare(), __a) {}
  943. # endif
  944. _LIBCPP_HIDE_FROM_ABI map& operator=(initializer_list<value_type> __il) {
  945. __tree_.__assign_unique(__il.begin(), __il.end());
  946. return *this;
  947. }
  948. #endif // _LIBCPP_CXX03_LANG
  949. _LIBCPP_HIDE_FROM_ABI explicit map(const allocator_type& __a) : __tree_(typename __base::allocator_type(__a)) {}
  950. _LIBCPP_HIDE_FROM_ABI map(const map& __m, const allocator_type& __a)
  951. : __tree_(__m.__tree_.value_comp(), typename __base::allocator_type(__a)) {
  952. insert(__m.begin(), __m.end());
  953. }
  954. _LIBCPP_HIDE_FROM_ABI ~map() { static_assert(sizeof(__diagnose_non_const_comparator<_Key, _Compare>()), ""); }
  955. _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __tree_.begin(); }
  956. _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __tree_.begin(); }
  957. _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __tree_.end(); }
  958. _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __tree_.end(); }
  959. _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
  960. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
  961. _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
  962. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
  963. _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
  964. _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
  965. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return rbegin(); }
  966. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
  967. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __tree_.size() == 0; }
  968. _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __tree_.size(); }
  969. _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return __tree_.max_size(); }
  970. _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
  971. #ifndef _LIBCPP_CXX03_LANG
  972. _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
  973. #endif
  974. _LIBCPP_HIDE_FROM_ABI mapped_type& at(const key_type& __k);
  975. _LIBCPP_HIDE_FROM_ABI const mapped_type& at(const key_type& __k) const;
  976. _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT { return allocator_type(__tree_.__alloc()); }
  977. _LIBCPP_HIDE_FROM_ABI key_compare key_comp() const { return __tree_.value_comp().key_comp(); }
  978. _LIBCPP_HIDE_FROM_ABI value_compare value_comp() const { return value_compare(__tree_.value_comp().key_comp()); }
  979. #ifndef _LIBCPP_CXX03_LANG
  980. template <class... _Args>
  981. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> emplace(_Args&&... __args) {
  982. return __tree_.__emplace_unique(std::forward<_Args>(__args)...);
  983. }
  984. template <class... _Args>
  985. _LIBCPP_HIDE_FROM_ABI iterator emplace_hint(const_iterator __p, _Args&&... __args) {
  986. return __tree_.__emplace_hint_unique(__p.__i_, std::forward<_Args>(__args)...);
  987. }
  988. template <class _Pp, __enable_if_t<is_constructible<value_type, _Pp>::value, int> = 0>
  989. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> insert(_Pp&& __p) {
  990. return __tree_.__insert_unique(std::forward<_Pp>(__p));
  991. }
  992. template <class _Pp, __enable_if_t<is_constructible<value_type, _Pp>::value, int> = 0>
  993. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __pos, _Pp&& __p) {
  994. return __tree_.__insert_unique(__pos.__i_, std::forward<_Pp>(__p));
  995. }
  996. #endif // _LIBCPP_CXX03_LANG
  997. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> insert(const value_type& __v) { return __tree_.__insert_unique(__v); }
  998. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __v) {
  999. return __tree_.__insert_unique(__p.__i_, __v);
  1000. }
  1001. #ifndef _LIBCPP_CXX03_LANG
  1002. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> insert(value_type&& __v) {
  1003. return __tree_.__insert_unique(std::move(__v));
  1004. }
  1005. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __v) {
  1006. return __tree_.__insert_unique(__p.__i_, std::move(__v));
  1007. }
  1008. _LIBCPP_HIDE_FROM_ABI void insert(initializer_list<value_type> __il) { insert(__il.begin(), __il.end()); }
  1009. #endif
  1010. template <class _InputIterator>
  1011. _LIBCPP_HIDE_FROM_ABI void insert(_InputIterator __f, _InputIterator __l) {
  1012. for (const_iterator __e = cend(); __f != __l; ++__f)
  1013. insert(__e.__i_, *__f);
  1014. }
  1015. #if _LIBCPP_STD_VER >= 23
  1016. template <_ContainerCompatibleRange<value_type> _Range>
  1017. _LIBCPP_HIDE_FROM_ABI void insert_range(_Range&& __range) {
  1018. const_iterator __end = cend();
  1019. for (auto&& __element : __range) {
  1020. insert(__end.__i_, std::forward<decltype(__element)>(__element));
  1021. }
  1022. }
  1023. #endif
  1024. #if _LIBCPP_STD_VER >= 17
  1025. template <class... _Args>
  1026. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> try_emplace(const key_type& __k, _Args&&... __args) {
  1027. return __tree_.__emplace_unique_key_args(
  1028. __k,
  1029. std::piecewise_construct,
  1030. std::forward_as_tuple(__k),
  1031. std::forward_as_tuple(std::forward<_Args>(__args)...));
  1032. }
  1033. template <class... _Args>
  1034. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> try_emplace(key_type&& __k, _Args&&... __args) {
  1035. return __tree_.__emplace_unique_key_args(
  1036. __k,
  1037. std::piecewise_construct,
  1038. std::forward_as_tuple(std::move(__k)),
  1039. std::forward_as_tuple(std::forward<_Args>(__args)...));
  1040. }
  1041. template <class... _Args>
  1042. _LIBCPP_HIDE_FROM_ABI iterator try_emplace(const_iterator __h, const key_type& __k, _Args&&... __args) {
  1043. return __tree_
  1044. .__emplace_hint_unique_key_args(
  1045. __h.__i_,
  1046. __k,
  1047. std::piecewise_construct,
  1048. std::forward_as_tuple(__k),
  1049. std::forward_as_tuple(std::forward<_Args>(__args)...))
  1050. .first;
  1051. }
  1052. template <class... _Args>
  1053. _LIBCPP_HIDE_FROM_ABI iterator try_emplace(const_iterator __h, key_type&& __k, _Args&&... __args) {
  1054. return __tree_
  1055. .__emplace_hint_unique_key_args(
  1056. __h.__i_,
  1057. __k,
  1058. std::piecewise_construct,
  1059. std::forward_as_tuple(std::move(__k)),
  1060. std::forward_as_tuple(std::forward<_Args>(__args)...))
  1061. .first;
  1062. }
  1063. template <class _Vp>
  1064. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> insert_or_assign(const key_type& __k, _Vp&& __v) {
  1065. iterator __p = lower_bound(__k);
  1066. if (__p != end() && !key_comp()(__k, __p->first)) {
  1067. __p->second = std::forward<_Vp>(__v);
  1068. return std::make_pair(__p, false);
  1069. }
  1070. return std::make_pair(emplace_hint(__p, __k, std::forward<_Vp>(__v)), true);
  1071. }
  1072. template <class _Vp>
  1073. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> insert_or_assign(key_type&& __k, _Vp&& __v) {
  1074. iterator __p = lower_bound(__k);
  1075. if (__p != end() && !key_comp()(__k, __p->first)) {
  1076. __p->second = std::forward<_Vp>(__v);
  1077. return std::make_pair(__p, false);
  1078. }
  1079. return std::make_pair(emplace_hint(__p, std::move(__k), std::forward<_Vp>(__v)), true);
  1080. }
  1081. template <class _Vp>
  1082. _LIBCPP_HIDE_FROM_ABI iterator insert_or_assign(const_iterator __h, const key_type& __k, _Vp&& __v) {
  1083. auto [__r, __inserted] = __tree_.__emplace_hint_unique_key_args(__h.__i_, __k, __k, std::forward<_Vp>(__v));
  1084. if (!__inserted)
  1085. __r->__get_value().second = std::forward<_Vp>(__v);
  1086. return __r;
  1087. }
  1088. template <class _Vp>
  1089. _LIBCPP_HIDE_FROM_ABI iterator insert_or_assign(const_iterator __h, key_type&& __k, _Vp&& __v) {
  1090. auto [__r, __inserted] =
  1091. __tree_.__emplace_hint_unique_key_args(__h.__i_, __k, std::move(__k), std::forward<_Vp>(__v));
  1092. if (!__inserted)
  1093. __r->__get_value().second = std::forward<_Vp>(__v);
  1094. return __r;
  1095. }
  1096. #endif // _LIBCPP_STD_VER >= 17
  1097. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p) { return __tree_.erase(__p.__i_); }
  1098. _LIBCPP_HIDE_FROM_ABI iterator erase(iterator __p) { return __tree_.erase(__p.__i_); }
  1099. _LIBCPP_HIDE_FROM_ABI size_type erase(const key_type& __k) { return __tree_.__erase_unique(__k); }
  1100. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l) {
  1101. return __tree_.erase(__f.__i_, __l.__i_);
  1102. }
  1103. _LIBCPP_REINITIALIZES_OBJECT
  1104. _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { __tree_.clear(); }
  1105. #if _LIBCPP_STD_VER >= 17
  1106. _LIBCPP_HIDE_FROM_ABI insert_return_type insert(node_type&& __nh) {
  1107. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(__nh.empty() || __nh.get_allocator() == get_allocator(),
  1108. "node_type with incompatible allocator passed to map::insert()");
  1109. return __tree_.template __node_handle_insert_unique< node_type, insert_return_type>(std::move(__nh));
  1110. }
  1111. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __hint, node_type&& __nh) {
  1112. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(__nh.empty() || __nh.get_allocator() == get_allocator(),
  1113. "node_type with incompatible allocator passed to map::insert()");
  1114. return __tree_.template __node_handle_insert_unique<node_type>(__hint.__i_, std::move(__nh));
  1115. }
  1116. _LIBCPP_HIDE_FROM_ABI node_type extract(key_type const& __key) {
  1117. return __tree_.template __node_handle_extract<node_type>(__key);
  1118. }
  1119. _LIBCPP_HIDE_FROM_ABI node_type extract(const_iterator __it) {
  1120. return __tree_.template __node_handle_extract<node_type>(__it.__i_);
  1121. }
  1122. template <class _Compare2>
  1123. _LIBCPP_HIDE_FROM_ABI void merge(map<key_type, mapped_type, _Compare2, allocator_type>& __source) {
  1124. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  1125. __source.get_allocator() == get_allocator(), "merging container with incompatible allocator");
  1126. __tree_.__node_handle_merge_unique(__source.__tree_);
  1127. }
  1128. template <class _Compare2>
  1129. _LIBCPP_HIDE_FROM_ABI void merge(map<key_type, mapped_type, _Compare2, allocator_type>&& __source) {
  1130. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  1131. __source.get_allocator() == get_allocator(), "merging container with incompatible allocator");
  1132. __tree_.__node_handle_merge_unique(__source.__tree_);
  1133. }
  1134. template <class _Compare2>
  1135. _LIBCPP_HIDE_FROM_ABI void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>& __source) {
  1136. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  1137. __source.get_allocator() == get_allocator(), "merging container with incompatible allocator");
  1138. __tree_.__node_handle_merge_unique(__source.__tree_);
  1139. }
  1140. template <class _Compare2>
  1141. _LIBCPP_HIDE_FROM_ABI void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>&& __source) {
  1142. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  1143. __source.get_allocator() == get_allocator(), "merging container with incompatible allocator");
  1144. __tree_.__node_handle_merge_unique(__source.__tree_);
  1145. }
  1146. #endif
  1147. _LIBCPP_HIDE_FROM_ABI void swap(map& __m) _NOEXCEPT_(__is_nothrow_swappable<__base>::value) {
  1148. __tree_.swap(__m.__tree_);
  1149. }
  1150. _LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__k); }
  1151. _LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__k); }
  1152. #if _LIBCPP_STD_VER >= 14
  1153. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1154. _LIBCPP_HIDE_FROM_ABI iterator find(const _K2& __k) {
  1155. return __tree_.find(__k);
  1156. }
  1157. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1158. _LIBCPP_HIDE_FROM_ABI const_iterator find(const _K2& __k) const {
  1159. return __tree_.find(__k);
  1160. }
  1161. #endif
  1162. _LIBCPP_HIDE_FROM_ABI size_type count(const key_type& __k) const { return __tree_.__count_unique(__k); }
  1163. #if _LIBCPP_STD_VER >= 14
  1164. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1165. _LIBCPP_HIDE_FROM_ABI size_type count(const _K2& __k) const {
  1166. return __tree_.__count_multi(__k);
  1167. }
  1168. #endif
  1169. #if _LIBCPP_STD_VER >= 20
  1170. _LIBCPP_HIDE_FROM_ABI bool contains(const key_type& __k) const { return find(__k) != end(); }
  1171. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1172. _LIBCPP_HIDE_FROM_ABI bool contains(const _K2& __k) const {
  1173. return find(__k) != end();
  1174. }
  1175. #endif // _LIBCPP_STD_VER >= 20
  1176. _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const key_type& __k) { return __tree_.lower_bound(__k); }
  1177. _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const key_type& __k) const { return __tree_.lower_bound(__k); }
  1178. #if _LIBCPP_STD_VER >= 14
  1179. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1180. _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const _K2& __k) {
  1181. return __tree_.lower_bound(__k);
  1182. }
  1183. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1184. _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const _K2& __k) const {
  1185. return __tree_.lower_bound(__k);
  1186. }
  1187. #endif
  1188. _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const key_type& __k) { return __tree_.upper_bound(__k); }
  1189. _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const key_type& __k) const { return __tree_.upper_bound(__k); }
  1190. #if _LIBCPP_STD_VER >= 14
  1191. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1192. _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const _K2& __k) {
  1193. return __tree_.upper_bound(__k);
  1194. }
  1195. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1196. _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const _K2& __k) const {
  1197. return __tree_.upper_bound(__k);
  1198. }
  1199. #endif
  1200. _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const key_type& __k) {
  1201. return __tree_.__equal_range_unique(__k);
  1202. }
  1203. _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const key_type& __k) const {
  1204. return __tree_.__equal_range_unique(__k);
  1205. }
  1206. #if _LIBCPP_STD_VER >= 14
  1207. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1208. _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const _K2& __k) {
  1209. return __tree_.__equal_range_multi(__k);
  1210. }
  1211. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1212. _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const _K2& __k) const {
  1213. return __tree_.__equal_range_multi(__k);
  1214. }
  1215. #endif
  1216. private:
  1217. typedef typename __base::__node __node;
  1218. typedef typename __base::__node_allocator __node_allocator;
  1219. typedef typename __base::__node_pointer __node_pointer;
  1220. typedef typename __base::__node_base_pointer __node_base_pointer;
  1221. typedef typename __base::__parent_pointer __parent_pointer;
  1222. typedef __map_node_destructor<__node_allocator> _Dp;
  1223. typedef unique_ptr<__node, _Dp> __node_holder;
  1224. #ifdef _LIBCPP_CXX03_LANG
  1225. _LIBCPP_HIDE_FROM_ABI __node_holder __construct_node_with_key(const key_type& __k);
  1226. #endif
  1227. };
  1228. #if _LIBCPP_STD_VER >= 17
  1229. template <class _InputIterator,
  1230. class _Compare = less<__iter_key_type<_InputIterator>>,
  1231. class _Allocator = allocator<__iter_to_alloc_type<_InputIterator>>,
  1232. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value, void>,
  1233. class = enable_if_t<!__is_allocator<_Compare>::value, void>,
  1234. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1235. map(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
  1236. -> map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare, _Allocator>;
  1237. # if _LIBCPP_STD_VER >= 23
  1238. template <ranges::input_range _Range,
  1239. class _Compare = less<__range_key_type<_Range>>,
  1240. class _Allocator = allocator<__range_to_alloc_type<_Range>>,
  1241. class = enable_if_t<!__is_allocator<_Compare>::value, void>,
  1242. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1243. map(from_range_t, _Range&&, _Compare = _Compare(), _Allocator = _Allocator())
  1244. -> map<__range_key_type<_Range>, __range_mapped_type<_Range>, _Compare, _Allocator>;
  1245. # endif
  1246. template <class _Key,
  1247. class _Tp,
  1248. class _Compare = less<remove_const_t<_Key>>,
  1249. class _Allocator = allocator<pair<const _Key, _Tp>>,
  1250. class = enable_if_t<!__is_allocator<_Compare>::value, void>,
  1251. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1252. map(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare(), _Allocator = _Allocator())
  1253. -> map<remove_const_t<_Key>, _Tp, _Compare, _Allocator>;
  1254. template <class _InputIterator,
  1255. class _Allocator,
  1256. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value, void>,
  1257. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1258. map(_InputIterator, _InputIterator, _Allocator)
  1259. -> map<__iter_key_type<_InputIterator>,
  1260. __iter_mapped_type<_InputIterator>,
  1261. less<__iter_key_type<_InputIterator>>,
  1262. _Allocator>;
  1263. # if _LIBCPP_STD_VER >= 23
  1264. template <ranges::input_range _Range, class _Allocator, class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1265. map(from_range_t, _Range&&, _Allocator)
  1266. -> map<__range_key_type<_Range>, __range_mapped_type<_Range>, less<__range_key_type<_Range>>, _Allocator>;
  1267. # endif
  1268. template <class _Key, class _Tp, class _Allocator, class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1269. map(initializer_list<pair<_Key, _Tp>>, _Allocator)
  1270. -> map<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
  1271. #endif
  1272. #ifndef _LIBCPP_CXX03_LANG
  1273. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1274. map<_Key, _Tp, _Compare, _Allocator>::map(map&& __m, const allocator_type& __a)
  1275. : __tree_(std::move(__m.__tree_), typename __base::allocator_type(__a)) {
  1276. if (__a != __m.get_allocator()) {
  1277. const_iterator __e = cend();
  1278. while (!__m.empty())
  1279. __tree_.__insert_unique(__e.__i_, __m.__tree_.remove(__m.begin().__i_)->__value_.__move());
  1280. }
  1281. }
  1282. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1283. _Tp& map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k) {
  1284. return __tree_
  1285. .__emplace_unique_key_args(__k, std::piecewise_construct, std::forward_as_tuple(__k), std::forward_as_tuple())
  1286. .first->__get_value()
  1287. .second;
  1288. }
  1289. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1290. _Tp& map<_Key, _Tp, _Compare, _Allocator>::operator[](key_type&& __k) {
  1291. // TODO investigate this clang-tidy warning.
  1292. // NOLINTBEGIN(bugprone-use-after-move)
  1293. return __tree_
  1294. .__emplace_unique_key_args(
  1295. __k, std::piecewise_construct, std::forward_as_tuple(std::move(__k)), std::forward_as_tuple())
  1296. .first->__get_value()
  1297. .second;
  1298. // NOLINTEND(bugprone-use-after-move)
  1299. }
  1300. #else // _LIBCPP_CXX03_LANG
  1301. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1302. typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
  1303. map<_Key, _Tp, _Compare, _Allocator>::__construct_node_with_key(const key_type& __k) {
  1304. __node_allocator& __na = __tree_.__node_alloc();
  1305. __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
  1306. __node_traits::construct(__na, std::addressof(__h->__value_.__get_value().first), __k);
  1307. __h.get_deleter().__first_constructed = true;
  1308. __node_traits::construct(__na, std::addressof(__h->__value_.__get_value().second));
  1309. __h.get_deleter().__second_constructed = true;
  1310. return __h;
  1311. }
  1312. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1313. _Tp& map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k) {
  1314. __parent_pointer __parent;
  1315. __node_base_pointer& __child = __tree_.__find_equal(__parent, __k);
  1316. __node_pointer __r = static_cast<__node_pointer>(__child);
  1317. if (__child == nullptr) {
  1318. __node_holder __h = __construct_node_with_key(__k);
  1319. __tree_.__insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
  1320. __r = __h.release();
  1321. }
  1322. return __r->__value_.__get_value().second;
  1323. }
  1324. #endif // _LIBCPP_CXX03_LANG
  1325. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1326. _Tp& map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k) {
  1327. __parent_pointer __parent;
  1328. __node_base_pointer& __child = __tree_.__find_equal(__parent, __k);
  1329. if (__child == nullptr)
  1330. __throw_out_of_range("map::at: key not found");
  1331. return static_cast<__node_pointer>(__child)->__value_.__get_value().second;
  1332. }
  1333. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1334. const _Tp& map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k) const {
  1335. __parent_pointer __parent;
  1336. __node_base_pointer __child = __tree_.__find_equal(__parent, __k);
  1337. if (__child == nullptr)
  1338. __throw_out_of_range("map::at: key not found");
  1339. return static_cast<__node_pointer>(__child)->__value_.__get_value().second;
  1340. }
  1341. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1342. inline _LIBCPP_HIDE_FROM_ABI bool
  1343. operator==(const map<_Key, _Tp, _Compare, _Allocator>& __x, const map<_Key, _Tp, _Compare, _Allocator>& __y) {
  1344. return __x.size() == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
  1345. }
  1346. #if _LIBCPP_STD_VER <= 17
  1347. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1348. inline _LIBCPP_HIDE_FROM_ABI bool
  1349. operator<(const map<_Key, _Tp, _Compare, _Allocator>& __x, const map<_Key, _Tp, _Compare, _Allocator>& __y) {
  1350. return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  1351. }
  1352. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1353. inline _LIBCPP_HIDE_FROM_ABI bool
  1354. operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __x, const map<_Key, _Tp, _Compare, _Allocator>& __y) {
  1355. return !(__x == __y);
  1356. }
  1357. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1358. inline _LIBCPP_HIDE_FROM_ABI bool
  1359. operator>(const map<_Key, _Tp, _Compare, _Allocator>& __x, const map<_Key, _Tp, _Compare, _Allocator>& __y) {
  1360. return __y < __x;
  1361. }
  1362. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1363. inline _LIBCPP_HIDE_FROM_ABI bool
  1364. operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __x, const map<_Key, _Tp, _Compare, _Allocator>& __y) {
  1365. return !(__x < __y);
  1366. }
  1367. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1368. inline _LIBCPP_HIDE_FROM_ABI bool
  1369. operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __x, const map<_Key, _Tp, _Compare, _Allocator>& __y) {
  1370. return !(__y < __x);
  1371. }
  1372. #else // #if _LIBCPP_STD_VER <= 17
  1373. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1374. _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<pair<const _Key, _Tp>>
  1375. operator<=>(const map<_Key, _Tp, _Compare, _Allocator>& __x, const map<_Key, _Tp, _Compare, _Allocator>& __y) {
  1376. return std::lexicographical_compare_three_way(
  1377. __x.begin(),
  1378. __x.end(),
  1379. __y.begin(),
  1380. __y.end(),
  1381. std::__synth_three_way<pair<const _Key, _Tp>, pair<const _Key, _Tp>>);
  1382. }
  1383. #endif // #if _LIBCPP_STD_VER <= 17
  1384. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1385. inline _LIBCPP_HIDE_FROM_ABI void
  1386. swap(map<_Key, _Tp, _Compare, _Allocator>& __x, map<_Key, _Tp, _Compare, _Allocator>& __y)
  1387. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
  1388. __x.swap(__y);
  1389. }
  1390. #if _LIBCPP_STD_VER >= 20
  1391. template <class _Key, class _Tp, class _Compare, class _Allocator, class _Predicate>
  1392. inline _LIBCPP_HIDE_FROM_ABI typename map<_Key, _Tp, _Compare, _Allocator>::size_type
  1393. erase_if(map<_Key, _Tp, _Compare, _Allocator>& __c, _Predicate __pred) {
  1394. return std::__libcpp_erase_if_container(__c, __pred);
  1395. }
  1396. #endif
  1397. template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
  1398. class _LIBCPP_TEMPLATE_VIS multimap {
  1399. public:
  1400. // types:
  1401. typedef _Key key_type;
  1402. typedef _Tp mapped_type;
  1403. typedef pair<const key_type, mapped_type> value_type;
  1404. typedef __type_identity_t<_Compare> key_compare;
  1405. typedef __type_identity_t<_Allocator> allocator_type;
  1406. typedef value_type& reference;
  1407. typedef const value_type& const_reference;
  1408. static_assert((is_same<typename allocator_type::value_type, value_type>::value),
  1409. "Allocator::value_type must be same type as value_type");
  1410. class _LIBCPP_TEMPLATE_VIS value_compare : public __binary_function<value_type, value_type, bool> {
  1411. friend class multimap;
  1412. protected:
  1413. key_compare comp;
  1414. _LIBCPP_HIDE_FROM_ABI value_compare(key_compare __c) : comp(__c) {}
  1415. public:
  1416. _LIBCPP_HIDE_FROM_ABI bool operator()(const value_type& __x, const value_type& __y) const {
  1417. return comp(__x.first, __y.first);
  1418. }
  1419. };
  1420. private:
  1421. typedef std::__value_type<key_type, mapped_type> __value_type;
  1422. typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
  1423. typedef __rebind_alloc<allocator_traits<allocator_type>, __value_type> __allocator_type;
  1424. typedef __tree<__value_type, __vc, __allocator_type> __base;
  1425. typedef typename __base::__node_traits __node_traits;
  1426. typedef allocator_traits<allocator_type> __alloc_traits;
  1427. static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
  1428. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  1429. "original allocator");
  1430. __base __tree_;
  1431. public:
  1432. typedef typename __alloc_traits::pointer pointer;
  1433. typedef typename __alloc_traits::const_pointer const_pointer;
  1434. typedef typename __alloc_traits::size_type size_type;
  1435. typedef typename __alloc_traits::difference_type difference_type;
  1436. typedef __map_iterator<typename __base::iterator> iterator;
  1437. typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
  1438. typedef std::reverse_iterator<iterator> reverse_iterator;
  1439. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  1440. #if _LIBCPP_STD_VER >= 17
  1441. typedef __map_node_handle<typename __base::__node, allocator_type> node_type;
  1442. #endif
  1443. template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
  1444. friend class _LIBCPP_TEMPLATE_VIS map;
  1445. template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
  1446. friend class _LIBCPP_TEMPLATE_VIS multimap;
  1447. _LIBCPP_HIDE_FROM_ABI multimap() _NOEXCEPT_(
  1448. is_nothrow_default_constructible<allocator_type>::value&& is_nothrow_default_constructible<key_compare>::value&&
  1449. is_nothrow_copy_constructible<key_compare>::value)
  1450. : __tree_(__vc(key_compare())) {}
  1451. _LIBCPP_HIDE_FROM_ABI explicit multimap(const key_compare& __comp) _NOEXCEPT_(
  1452. is_nothrow_default_constructible<allocator_type>::value&& is_nothrow_copy_constructible<key_compare>::value)
  1453. : __tree_(__vc(__comp)) {}
  1454. _LIBCPP_HIDE_FROM_ABI explicit multimap(const key_compare& __comp, const allocator_type& __a)
  1455. : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {}
  1456. template <class _InputIterator>
  1457. _LIBCPP_HIDE_FROM_ABI multimap(_InputIterator __f, _InputIterator __l, const key_compare& __comp = key_compare())
  1458. : __tree_(__vc(__comp)) {
  1459. insert(__f, __l);
  1460. }
  1461. template <class _InputIterator>
  1462. _LIBCPP_HIDE_FROM_ABI
  1463. multimap(_InputIterator __f, _InputIterator __l, const key_compare& __comp, const allocator_type& __a)
  1464. : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {
  1465. insert(__f, __l);
  1466. }
  1467. #if _LIBCPP_STD_VER >= 23
  1468. template <_ContainerCompatibleRange<value_type> _Range>
  1469. _LIBCPP_HIDE_FROM_ABI
  1470. multimap(from_range_t,
  1471. _Range&& __range,
  1472. const key_compare& __comp = key_compare(),
  1473. const allocator_type& __a = allocator_type())
  1474. : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {
  1475. insert_range(std::forward<_Range>(__range));
  1476. }
  1477. #endif
  1478. #if _LIBCPP_STD_VER >= 14
  1479. template <class _InputIterator>
  1480. _LIBCPP_HIDE_FROM_ABI multimap(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
  1481. : multimap(__f, __l, key_compare(), __a) {}
  1482. #endif
  1483. #if _LIBCPP_STD_VER >= 23
  1484. template <_ContainerCompatibleRange<value_type> _Range>
  1485. _LIBCPP_HIDE_FROM_ABI multimap(from_range_t, _Range&& __range, const allocator_type& __a)
  1486. : multimap(from_range, std::forward<_Range>(__range), key_compare(), __a) {}
  1487. #endif
  1488. _LIBCPP_HIDE_FROM_ABI multimap(const multimap& __m)
  1489. : __tree_(__m.__tree_.value_comp(),
  1490. __alloc_traits::select_on_container_copy_construction(__m.__tree_.__alloc())) {
  1491. insert(__m.begin(), __m.end());
  1492. }
  1493. _LIBCPP_HIDE_FROM_ABI multimap& operator=(const multimap& __m) {
  1494. #ifndef _LIBCPP_CXX03_LANG
  1495. __tree_ = __m.__tree_;
  1496. #else
  1497. if (this != std::addressof(__m)) {
  1498. __tree_.clear();
  1499. __tree_.value_comp() = __m.__tree_.value_comp();
  1500. __tree_.__copy_assign_alloc(__m.__tree_);
  1501. insert(__m.begin(), __m.end());
  1502. }
  1503. #endif
  1504. return *this;
  1505. }
  1506. #ifndef _LIBCPP_CXX03_LANG
  1507. _LIBCPP_HIDE_FROM_ABI multimap(multimap&& __m) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
  1508. : __tree_(std::move(__m.__tree_)) {}
  1509. _LIBCPP_HIDE_FROM_ABI multimap(multimap&& __m, const allocator_type& __a);
  1510. _LIBCPP_HIDE_FROM_ABI multimap& operator=(multimap&& __m) _NOEXCEPT_(is_nothrow_move_assignable<__base>::value) {
  1511. __tree_ = std::move(__m.__tree_);
  1512. return *this;
  1513. }
  1514. _LIBCPP_HIDE_FROM_ABI multimap(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
  1515. : __tree_(__vc(__comp)) {
  1516. insert(__il.begin(), __il.end());
  1517. }
  1518. _LIBCPP_HIDE_FROM_ABI
  1519. multimap(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
  1520. : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {
  1521. insert(__il.begin(), __il.end());
  1522. }
  1523. # if _LIBCPP_STD_VER >= 14
  1524. _LIBCPP_HIDE_FROM_ABI multimap(initializer_list<value_type> __il, const allocator_type& __a)
  1525. : multimap(__il, key_compare(), __a) {}
  1526. # endif
  1527. _LIBCPP_HIDE_FROM_ABI multimap& operator=(initializer_list<value_type> __il) {
  1528. __tree_.__assign_multi(__il.begin(), __il.end());
  1529. return *this;
  1530. }
  1531. #endif // _LIBCPP_CXX03_LANG
  1532. _LIBCPP_HIDE_FROM_ABI explicit multimap(const allocator_type& __a) : __tree_(typename __base::allocator_type(__a)) {}
  1533. _LIBCPP_HIDE_FROM_ABI multimap(const multimap& __m, const allocator_type& __a)
  1534. : __tree_(__m.__tree_.value_comp(), typename __base::allocator_type(__a)) {
  1535. insert(__m.begin(), __m.end());
  1536. }
  1537. _LIBCPP_HIDE_FROM_ABI ~multimap() { static_assert(sizeof(__diagnose_non_const_comparator<_Key, _Compare>()), ""); }
  1538. _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __tree_.begin(); }
  1539. _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __tree_.begin(); }
  1540. _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __tree_.end(); }
  1541. _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __tree_.end(); }
  1542. _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
  1543. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
  1544. _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
  1545. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
  1546. _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
  1547. _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
  1548. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return rbegin(); }
  1549. _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
  1550. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __tree_.size() == 0; }
  1551. _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __tree_.size(); }
  1552. _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return __tree_.max_size(); }
  1553. _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT { return allocator_type(__tree_.__alloc()); }
  1554. _LIBCPP_HIDE_FROM_ABI key_compare key_comp() const { return __tree_.value_comp().key_comp(); }
  1555. _LIBCPP_HIDE_FROM_ABI value_compare value_comp() const { return value_compare(__tree_.value_comp().key_comp()); }
  1556. #ifndef _LIBCPP_CXX03_LANG
  1557. template <class... _Args>
  1558. _LIBCPP_HIDE_FROM_ABI iterator emplace(_Args&&... __args) {
  1559. return __tree_.__emplace_multi(std::forward<_Args>(__args)...);
  1560. }
  1561. template <class... _Args>
  1562. _LIBCPP_HIDE_FROM_ABI iterator emplace_hint(const_iterator __p, _Args&&... __args) {
  1563. return __tree_.__emplace_hint_multi(__p.__i_, std::forward<_Args>(__args)...);
  1564. }
  1565. template <class _Pp, __enable_if_t<is_constructible<value_type, _Pp>::value, int> = 0>
  1566. _LIBCPP_HIDE_FROM_ABI iterator insert(_Pp&& __p) {
  1567. return __tree_.__insert_multi(std::forward<_Pp>(__p));
  1568. }
  1569. template <class _Pp, __enable_if_t<is_constructible<value_type, _Pp>::value, int> = 0>
  1570. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __pos, _Pp&& __p) {
  1571. return __tree_.__insert_multi(__pos.__i_, std::forward<_Pp>(__p));
  1572. }
  1573. _LIBCPP_HIDE_FROM_ABI iterator insert(value_type&& __v) { return __tree_.__insert_multi(std::move(__v)); }
  1574. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __v) {
  1575. return __tree_.__insert_multi(__p.__i_, std::move(__v));
  1576. }
  1577. _LIBCPP_HIDE_FROM_ABI void insert(initializer_list<value_type> __il) { insert(__il.begin(), __il.end()); }
  1578. #endif // _LIBCPP_CXX03_LANG
  1579. _LIBCPP_HIDE_FROM_ABI iterator insert(const value_type& __v) { return __tree_.__insert_multi(__v); }
  1580. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __v) {
  1581. return __tree_.__insert_multi(__p.__i_, __v);
  1582. }
  1583. template <class _InputIterator>
  1584. _LIBCPP_HIDE_FROM_ABI void insert(_InputIterator __f, _InputIterator __l) {
  1585. for (const_iterator __e = cend(); __f != __l; ++__f)
  1586. __tree_.__insert_multi(__e.__i_, *__f);
  1587. }
  1588. #if _LIBCPP_STD_VER >= 23
  1589. template <_ContainerCompatibleRange<value_type> _Range>
  1590. _LIBCPP_HIDE_FROM_ABI void insert_range(_Range&& __range) {
  1591. const_iterator __end = cend();
  1592. for (auto&& __element : __range) {
  1593. __tree_.__insert_multi(__end.__i_, std::forward<decltype(__element)>(__element));
  1594. }
  1595. }
  1596. #endif
  1597. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p) { return __tree_.erase(__p.__i_); }
  1598. _LIBCPP_HIDE_FROM_ABI iterator erase(iterator __p) { return __tree_.erase(__p.__i_); }
  1599. _LIBCPP_HIDE_FROM_ABI size_type erase(const key_type& __k) { return __tree_.__erase_multi(__k); }
  1600. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l) {
  1601. return __tree_.erase(__f.__i_, __l.__i_);
  1602. }
  1603. #if _LIBCPP_STD_VER >= 17
  1604. _LIBCPP_HIDE_FROM_ABI iterator insert(node_type&& __nh) {
  1605. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(__nh.empty() || __nh.get_allocator() == get_allocator(),
  1606. "node_type with incompatible allocator passed to multimap::insert()");
  1607. return __tree_.template __node_handle_insert_multi<node_type>(std::move(__nh));
  1608. }
  1609. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __hint, node_type&& __nh) {
  1610. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(__nh.empty() || __nh.get_allocator() == get_allocator(),
  1611. "node_type with incompatible allocator passed to multimap::insert()");
  1612. return __tree_.template __node_handle_insert_multi<node_type>(__hint.__i_, std::move(__nh));
  1613. }
  1614. _LIBCPP_HIDE_FROM_ABI node_type extract(key_type const& __key) {
  1615. return __tree_.template __node_handle_extract<node_type>(__key);
  1616. }
  1617. _LIBCPP_HIDE_FROM_ABI node_type extract(const_iterator __it) {
  1618. return __tree_.template __node_handle_extract<node_type>(__it.__i_);
  1619. }
  1620. template <class _Compare2>
  1621. _LIBCPP_HIDE_FROM_ABI void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>& __source) {
  1622. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  1623. __source.get_allocator() == get_allocator(), "merging container with incompatible allocator");
  1624. return __tree_.__node_handle_merge_multi(__source.__tree_);
  1625. }
  1626. template <class _Compare2>
  1627. _LIBCPP_HIDE_FROM_ABI void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>&& __source) {
  1628. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  1629. __source.get_allocator() == get_allocator(), "merging container with incompatible allocator");
  1630. return __tree_.__node_handle_merge_multi(__source.__tree_);
  1631. }
  1632. template <class _Compare2>
  1633. _LIBCPP_HIDE_FROM_ABI void merge(map<key_type, mapped_type, _Compare2, allocator_type>& __source) {
  1634. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  1635. __source.get_allocator() == get_allocator(), "merging container with incompatible allocator");
  1636. return __tree_.__node_handle_merge_multi(__source.__tree_);
  1637. }
  1638. template <class _Compare2>
  1639. _LIBCPP_HIDE_FROM_ABI void merge(map<key_type, mapped_type, _Compare2, allocator_type>&& __source) {
  1640. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  1641. __source.get_allocator() == get_allocator(), "merging container with incompatible allocator");
  1642. return __tree_.__node_handle_merge_multi(__source.__tree_);
  1643. }
  1644. #endif
  1645. _LIBCPP_REINITIALIZES_OBJECT
  1646. _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { __tree_.clear(); }
  1647. _LIBCPP_HIDE_FROM_ABI void swap(multimap& __m) _NOEXCEPT_(__is_nothrow_swappable<__base>::value) {
  1648. __tree_.swap(__m.__tree_);
  1649. }
  1650. _LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__k); }
  1651. _LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__k); }
  1652. #if _LIBCPP_STD_VER >= 14
  1653. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1654. _LIBCPP_HIDE_FROM_ABI iterator find(const _K2& __k) {
  1655. return __tree_.find(__k);
  1656. }
  1657. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1658. _LIBCPP_HIDE_FROM_ABI const_iterator find(const _K2& __k) const {
  1659. return __tree_.find(__k);
  1660. }
  1661. #endif
  1662. _LIBCPP_HIDE_FROM_ABI size_type count(const key_type& __k) const { return __tree_.__count_multi(__k); }
  1663. #if _LIBCPP_STD_VER >= 14
  1664. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1665. _LIBCPP_HIDE_FROM_ABI size_type count(const _K2& __k) const {
  1666. return __tree_.__count_multi(__k);
  1667. }
  1668. #endif
  1669. #if _LIBCPP_STD_VER >= 20
  1670. _LIBCPP_HIDE_FROM_ABI bool contains(const key_type& __k) const { return find(__k) != end(); }
  1671. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1672. _LIBCPP_HIDE_FROM_ABI bool contains(const _K2& __k) const {
  1673. return find(__k) != end();
  1674. }
  1675. #endif // _LIBCPP_STD_VER >= 20
  1676. _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const key_type& __k) { return __tree_.lower_bound(__k); }
  1677. _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const key_type& __k) const { return __tree_.lower_bound(__k); }
  1678. #if _LIBCPP_STD_VER >= 14
  1679. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1680. _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const _K2& __k) {
  1681. return __tree_.lower_bound(__k);
  1682. }
  1683. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1684. _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const _K2& __k) const {
  1685. return __tree_.lower_bound(__k);
  1686. }
  1687. #endif
  1688. _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const key_type& __k) { return __tree_.upper_bound(__k); }
  1689. _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const key_type& __k) const { return __tree_.upper_bound(__k); }
  1690. #if _LIBCPP_STD_VER >= 14
  1691. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1692. _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const _K2& __k) {
  1693. return __tree_.upper_bound(__k);
  1694. }
  1695. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1696. _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const _K2& __k) const {
  1697. return __tree_.upper_bound(__k);
  1698. }
  1699. #endif
  1700. _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const key_type& __k) {
  1701. return __tree_.__equal_range_multi(__k);
  1702. }
  1703. _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const key_type& __k) const {
  1704. return __tree_.__equal_range_multi(__k);
  1705. }
  1706. #if _LIBCPP_STD_VER >= 14
  1707. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1708. _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const _K2& __k) {
  1709. return __tree_.__equal_range_multi(__k);
  1710. }
  1711. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1712. _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const _K2& __k) const {
  1713. return __tree_.__equal_range_multi(__k);
  1714. }
  1715. #endif
  1716. private:
  1717. typedef typename __base::__node __node;
  1718. typedef typename __base::__node_allocator __node_allocator;
  1719. typedef typename __base::__node_pointer __node_pointer;
  1720. typedef __map_node_destructor<__node_allocator> _Dp;
  1721. typedef unique_ptr<__node, _Dp> __node_holder;
  1722. };
  1723. #if _LIBCPP_STD_VER >= 17
  1724. template <class _InputIterator,
  1725. class _Compare = less<__iter_key_type<_InputIterator>>,
  1726. class _Allocator = allocator<__iter_to_alloc_type<_InputIterator>>,
  1727. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value, void>,
  1728. class = enable_if_t<!__is_allocator<_Compare>::value, void>,
  1729. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1730. multimap(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
  1731. -> multimap<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare, _Allocator>;
  1732. # if _LIBCPP_STD_VER >= 23
  1733. template <ranges::input_range _Range,
  1734. class _Compare = less<__range_key_type<_Range>>,
  1735. class _Allocator = allocator<__range_to_alloc_type<_Range>>,
  1736. class = enable_if_t<!__is_allocator<_Compare>::value, void>,
  1737. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1738. multimap(from_range_t, _Range&&, _Compare = _Compare(), _Allocator = _Allocator())
  1739. -> multimap<__range_key_type<_Range>, __range_mapped_type<_Range>, _Compare, _Allocator>;
  1740. # endif
  1741. template <class _Key,
  1742. class _Tp,
  1743. class _Compare = less<remove_const_t<_Key>>,
  1744. class _Allocator = allocator<pair<const _Key, _Tp>>,
  1745. class = enable_if_t<!__is_allocator<_Compare>::value, void>,
  1746. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1747. multimap(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare(), _Allocator = _Allocator())
  1748. -> multimap<remove_const_t<_Key>, _Tp, _Compare, _Allocator>;
  1749. template <class _InputIterator,
  1750. class _Allocator,
  1751. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value, void>,
  1752. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1753. multimap(_InputIterator, _InputIterator, _Allocator)
  1754. -> multimap<__iter_key_type<_InputIterator>,
  1755. __iter_mapped_type<_InputIterator>,
  1756. less<__iter_key_type<_InputIterator>>,
  1757. _Allocator>;
  1758. # if _LIBCPP_STD_VER >= 23
  1759. template <ranges::input_range _Range, class _Allocator, class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1760. multimap(from_range_t, _Range&&, _Allocator)
  1761. -> multimap<__range_key_type<_Range>, __range_mapped_type<_Range>, less<__range_key_type<_Range>>, _Allocator>;
  1762. # endif
  1763. template <class _Key, class _Tp, class _Allocator, class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1764. multimap(initializer_list<pair<_Key, _Tp>>, _Allocator)
  1765. -> multimap<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
  1766. #endif
  1767. #ifndef _LIBCPP_CXX03_LANG
  1768. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1769. multimap<_Key, _Tp, _Compare, _Allocator>::multimap(multimap&& __m, const allocator_type& __a)
  1770. : __tree_(std::move(__m.__tree_), typename __base::allocator_type(__a)) {
  1771. if (__a != __m.get_allocator()) {
  1772. const_iterator __e = cend();
  1773. while (!__m.empty())
  1774. __tree_.__insert_multi(__e.__i_, std::move(__m.__tree_.remove(__m.begin().__i_)->__value_.__move()));
  1775. }
  1776. }
  1777. #endif
  1778. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1779. inline _LIBCPP_HIDE_FROM_ABI bool
  1780. operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __x, const multimap<_Key, _Tp, _Compare, _Allocator>& __y) {
  1781. return __x.size() == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
  1782. }
  1783. #if _LIBCPP_STD_VER <= 17
  1784. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1785. inline _LIBCPP_HIDE_FROM_ABI bool
  1786. operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __x, const multimap<_Key, _Tp, _Compare, _Allocator>& __y) {
  1787. return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  1788. }
  1789. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1790. inline _LIBCPP_HIDE_FROM_ABI bool
  1791. operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x, const multimap<_Key, _Tp, _Compare, _Allocator>& __y) {
  1792. return !(__x == __y);
  1793. }
  1794. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1795. inline _LIBCPP_HIDE_FROM_ABI bool
  1796. operator>(const multimap<_Key, _Tp, _Compare, _Allocator>& __x, const multimap<_Key, _Tp, _Compare, _Allocator>& __y) {
  1797. return __y < __x;
  1798. }
  1799. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1800. inline _LIBCPP_HIDE_FROM_ABI bool
  1801. operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x, const multimap<_Key, _Tp, _Compare, _Allocator>& __y) {
  1802. return !(__x < __y);
  1803. }
  1804. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1805. inline _LIBCPP_HIDE_FROM_ABI bool
  1806. operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x, const multimap<_Key, _Tp, _Compare, _Allocator>& __y) {
  1807. return !(__y < __x);
  1808. }
  1809. #else // #if _LIBCPP_STD_VER <= 17
  1810. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1811. _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<pair<const _Key, _Tp>>
  1812. operator<=>(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
  1813. const multimap<_Key, _Tp, _Compare, _Allocator>& __y) {
  1814. return std::lexicographical_compare_three_way(
  1815. __x.begin(),
  1816. __x.end(),
  1817. __y.begin(),
  1818. __y.end(),
  1819. std::__synth_three_way<pair<const _Key, _Tp>, pair<const _Key, _Tp>>);
  1820. }
  1821. #endif // #if _LIBCPP_STD_VER <= 17
  1822. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1823. inline _LIBCPP_HIDE_FROM_ABI void
  1824. swap(multimap<_Key, _Tp, _Compare, _Allocator>& __x, multimap<_Key, _Tp, _Compare, _Allocator>& __y)
  1825. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
  1826. __x.swap(__y);
  1827. }
  1828. #if _LIBCPP_STD_VER >= 20
  1829. template <class _Key, class _Tp, class _Compare, class _Allocator, class _Predicate>
  1830. inline _LIBCPP_HIDE_FROM_ABI typename multimap<_Key, _Tp, _Compare, _Allocator>::size_type
  1831. erase_if(multimap<_Key, _Tp, _Compare, _Allocator>& __c, _Predicate __pred) {
  1832. return std::__libcpp_erase_if_container(__c, __pred);
  1833. }
  1834. #endif
  1835. _LIBCPP_END_NAMESPACE_STD
  1836. #if _LIBCPP_STD_VER >= 17
  1837. _LIBCPP_BEGIN_NAMESPACE_STD
  1838. namespace pmr {
  1839. template <class _KeyT, class _ValueT, class _CompareT = std::less<_KeyT>>
  1840. using map _LIBCPP_AVAILABILITY_PMR =
  1841. std::map<_KeyT, _ValueT, _CompareT, polymorphic_allocator<std::pair<const _KeyT, _ValueT>>>;
  1842. template <class _KeyT, class _ValueT, class _CompareT = std::less<_KeyT>>
  1843. using multimap _LIBCPP_AVAILABILITY_PMR =
  1844. std::multimap<_KeyT, _ValueT, _CompareT, polymorphic_allocator<std::pair<const _KeyT, _ValueT>>>;
  1845. } // namespace pmr
  1846. _LIBCPP_END_NAMESPACE_STD
  1847. #endif
  1848. _LIBCPP_POP_MACROS
  1849. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  1850. # include <concepts>
  1851. # include <cstdlib>
  1852. # include <functional>
  1853. # include <iterator>
  1854. # include <type_traits>
  1855. # include <utility>
  1856. #endif
  1857. #endif // _LIBCPP_MAP