map 87 KB

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