map 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361
  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> // all public C++ headers provide the assertion handler
  456. #include <__config>
  457. #include <__functional/binary_function.h>
  458. #include <__functional/is_transparent.h>
  459. #include <__functional/operations.h>
  460. #include <__iterator/erase_if_container.h>
  461. #include <__iterator/iterator_traits.h>
  462. #include <__iterator/reverse_iterator.h>
  463. #include <__memory/allocator.h>
  464. #include <__memory_resource/polymorphic_allocator.h>
  465. #include <__node_handle>
  466. #include <__tree>
  467. #include <__type_traits/is_allocator.h>
  468. #include <__utility/forward.h>
  469. #include <__utility/piecewise_construct.h>
  470. #include <__utility/swap.h>
  471. #include <tuple>
  472. #include <type_traits>
  473. #include <version>
  474. // standard-mandated includes
  475. // [iterator.range]
  476. #include <__iterator/access.h>
  477. #include <__iterator/data.h>
  478. #include <__iterator/empty.h>
  479. #include <__iterator/reverse_access.h>
  480. #include <__iterator/size.h>
  481. // [associative.map.syn]
  482. #include <compare>
  483. #include <initializer_list>
  484. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  485. # pragma GCC system_header
  486. #endif
  487. _LIBCPP_BEGIN_NAMESPACE_STD
  488. template <class _Key, class _CP, class _Compare,
  489. bool = is_empty<_Compare>::value && !__libcpp_is_final<_Compare>::value>
  490. class __map_value_compare
  491. : private _Compare
  492. {
  493. public:
  494. _LIBCPP_INLINE_VISIBILITY
  495. __map_value_compare()
  496. _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
  497. : _Compare() {}
  498. _LIBCPP_INLINE_VISIBILITY
  499. __map_value_compare(_Compare __c)
  500. _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
  501. : _Compare(__c) {}
  502. _LIBCPP_INLINE_VISIBILITY
  503. const _Compare& key_comp() const _NOEXCEPT {return *this;}
  504. _LIBCPP_INLINE_VISIBILITY
  505. bool operator()(const _CP& __x, const _CP& __y) const
  506. {return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y.__get_value().first);}
  507. _LIBCPP_INLINE_VISIBILITY
  508. bool operator()(const _CP& __x, const _Key& __y) const
  509. {return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y);}
  510. _LIBCPP_INLINE_VISIBILITY
  511. bool operator()(const _Key& __x, const _CP& __y) const
  512. {return static_cast<const _Compare&>(*this)(__x, __y.__get_value().first);}
  513. void swap(__map_value_compare& __y)
  514. _NOEXCEPT_(__is_nothrow_swappable<_Compare>::value)
  515. {
  516. using _VSTD::swap;
  517. swap(static_cast<_Compare&>(*this), static_cast<_Compare&>(__y));
  518. }
  519. #if _LIBCPP_STD_VER > 11
  520. template <typename _K2>
  521. _LIBCPP_INLINE_VISIBILITY
  522. bool operator()(const _K2& __x, const _CP& __y) const
  523. {return static_cast<const _Compare&>(*this)(__x, __y.__get_value().first);}
  524. template <typename _K2>
  525. _LIBCPP_INLINE_VISIBILITY
  526. bool operator()(const _CP& __x, const _K2& __y) const
  527. {return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y);}
  528. #endif
  529. };
  530. template <class _Key, class _CP, class _Compare>
  531. class __map_value_compare<_Key, _CP, _Compare, false>
  532. {
  533. _Compare __comp_;
  534. public:
  535. _LIBCPP_INLINE_VISIBILITY
  536. __map_value_compare()
  537. _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
  538. : __comp_() {}
  539. _LIBCPP_INLINE_VISIBILITY
  540. __map_value_compare(_Compare __c)
  541. _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
  542. : __comp_(__c) {}
  543. _LIBCPP_INLINE_VISIBILITY
  544. const _Compare& key_comp() const _NOEXCEPT {return __comp_;}
  545. _LIBCPP_INLINE_VISIBILITY
  546. bool operator()(const _CP& __x, const _CP& __y) const
  547. {return __comp_(__x.__get_value().first, __y.__get_value().first);}
  548. _LIBCPP_INLINE_VISIBILITY
  549. bool operator()(const _CP& __x, const _Key& __y) const
  550. {return __comp_(__x.__get_value().first, __y);}
  551. _LIBCPP_INLINE_VISIBILITY
  552. bool operator()(const _Key& __x, const _CP& __y) const
  553. {return __comp_(__x, __y.__get_value().first);}
  554. void swap(__map_value_compare& __y)
  555. _NOEXCEPT_(__is_nothrow_swappable<_Compare>::value)
  556. {
  557. using _VSTD::swap;
  558. swap(__comp_, __y.__comp_);
  559. }
  560. #if _LIBCPP_STD_VER > 11
  561. template <typename _K2>
  562. _LIBCPP_INLINE_VISIBILITY
  563. bool operator()(const _K2& __x, const _CP& __y) const
  564. {return __comp_(__x, __y.__get_value().first);}
  565. template <typename _K2>
  566. _LIBCPP_INLINE_VISIBILITY
  567. bool operator()(const _CP& __x, const _K2& __y) const
  568. {return __comp_(__x.__get_value().first, __y);}
  569. #endif
  570. };
  571. template <class _Key, class _CP, class _Compare, bool __b>
  572. inline _LIBCPP_INLINE_VISIBILITY
  573. void
  574. swap(__map_value_compare<_Key, _CP, _Compare, __b>& __x,
  575. __map_value_compare<_Key, _CP, _Compare, __b>& __y)
  576. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  577. {
  578. __x.swap(__y);
  579. }
  580. template <class _Allocator>
  581. class __map_node_destructor
  582. {
  583. typedef _Allocator allocator_type;
  584. typedef allocator_traits<allocator_type> __alloc_traits;
  585. public:
  586. typedef typename __alloc_traits::pointer pointer;
  587. private:
  588. allocator_type& __na_;
  589. __map_node_destructor& operator=(const __map_node_destructor&);
  590. public:
  591. bool __first_constructed;
  592. bool __second_constructed;
  593. _LIBCPP_INLINE_VISIBILITY
  594. explicit __map_node_destructor(allocator_type& __na) _NOEXCEPT
  595. : __na_(__na),
  596. __first_constructed(false),
  597. __second_constructed(false)
  598. {}
  599. #ifndef _LIBCPP_CXX03_LANG
  600. _LIBCPP_INLINE_VISIBILITY
  601. __map_node_destructor(__tree_node_destructor<allocator_type>&& __x) _NOEXCEPT
  602. : __na_(__x.__na_),
  603. __first_constructed(__x.__value_constructed),
  604. __second_constructed(__x.__value_constructed)
  605. {
  606. __x.__value_constructed = false;
  607. }
  608. #endif // _LIBCPP_CXX03_LANG
  609. _LIBCPP_INLINE_VISIBILITY
  610. void operator()(pointer __p) _NOEXCEPT
  611. {
  612. if (__second_constructed)
  613. __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.__get_value().second));
  614. if (__first_constructed)
  615. __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.__get_value().first));
  616. if (__p)
  617. __alloc_traits::deallocate(__na_, __p, 1);
  618. }
  619. };
  620. template <class _Key, class _Tp, class _Compare, class _Allocator>
  621. class map;
  622. template <class _Key, class _Tp, class _Compare, class _Allocator>
  623. class multimap;
  624. template <class _TreeIterator> class __map_const_iterator;
  625. #ifndef _LIBCPP_CXX03_LANG
  626. template <class _Key, class _Tp>
  627. struct _LIBCPP_STANDALONE_DEBUG __value_type
  628. {
  629. typedef _Key key_type;
  630. typedef _Tp mapped_type;
  631. typedef pair<const key_type, mapped_type> value_type;
  632. typedef pair<key_type&, mapped_type&> __nc_ref_pair_type;
  633. typedef pair<key_type&&, mapped_type&&> __nc_rref_pair_type;
  634. private:
  635. value_type __cc_;
  636. public:
  637. _LIBCPP_INLINE_VISIBILITY
  638. value_type& __get_value()
  639. {
  640. #if _LIBCPP_STD_VER > 14
  641. return *_VSTD::launder(_VSTD::addressof(__cc_));
  642. #else
  643. return __cc_;
  644. #endif
  645. }
  646. _LIBCPP_INLINE_VISIBILITY
  647. const value_type& __get_value() const
  648. {
  649. #if _LIBCPP_STD_VER > 14
  650. return *_VSTD::launder(_VSTD::addressof(__cc_));
  651. #else
  652. return __cc_;
  653. #endif
  654. }
  655. _LIBCPP_INLINE_VISIBILITY
  656. __nc_ref_pair_type __ref()
  657. {
  658. value_type& __v = __get_value();
  659. return __nc_ref_pair_type(const_cast<key_type&>(__v.first), __v.second);
  660. }
  661. _LIBCPP_INLINE_VISIBILITY
  662. __nc_rref_pair_type __move()
  663. {
  664. value_type& __v = __get_value();
  665. return __nc_rref_pair_type(
  666. _VSTD::move(const_cast<key_type&>(__v.first)),
  667. _VSTD::move(__v.second));
  668. }
  669. _LIBCPP_INLINE_VISIBILITY
  670. __value_type& operator=(const __value_type& __v)
  671. {
  672. __ref() = __v.__get_value();
  673. return *this;
  674. }
  675. _LIBCPP_INLINE_VISIBILITY
  676. __value_type& operator=(__value_type&& __v)
  677. {
  678. __ref() = __v.__move();
  679. return *this;
  680. }
  681. template <class _ValueTp,
  682. class = __enable_if_t<__is_same_uncvref<_ValueTp, value_type>::value>
  683. >
  684. _LIBCPP_INLINE_VISIBILITY
  685. __value_type& operator=(_ValueTp&& __v)
  686. {
  687. __ref() = _VSTD::forward<_ValueTp>(__v);
  688. return *this;
  689. }
  690. private:
  691. __value_type() = delete;
  692. ~__value_type() = delete;
  693. __value_type(const __value_type&) = delete;
  694. __value_type(__value_type&&) = delete;
  695. };
  696. #else
  697. template <class _Key, class _Tp>
  698. struct __value_type
  699. {
  700. typedef _Key key_type;
  701. typedef _Tp mapped_type;
  702. typedef pair<const key_type, mapped_type> value_type;
  703. private:
  704. value_type __cc_;
  705. public:
  706. _LIBCPP_INLINE_VISIBILITY
  707. value_type& __get_value() { return __cc_; }
  708. _LIBCPP_INLINE_VISIBILITY
  709. const value_type& __get_value() const { return __cc_; }
  710. private:
  711. __value_type();
  712. __value_type(__value_type const&);
  713. __value_type& operator=(__value_type const&);
  714. ~__value_type();
  715. };
  716. #endif // _LIBCPP_CXX03_LANG
  717. template <class _Tp>
  718. struct __extract_key_value_types;
  719. template <class _Key, class _Tp>
  720. struct __extract_key_value_types<__value_type<_Key, _Tp> >
  721. {
  722. typedef _Key const __key_type;
  723. typedef _Tp __mapped_type;
  724. };
  725. template <class _TreeIterator>
  726. class _LIBCPP_TEMPLATE_VIS __map_iterator
  727. {
  728. typedef typename _TreeIterator::_NodeTypes _NodeTypes;
  729. typedef typename _TreeIterator::__pointer_traits __pointer_traits;
  730. _TreeIterator __i_;
  731. public:
  732. typedef bidirectional_iterator_tag iterator_category;
  733. typedef typename _NodeTypes::__map_value_type value_type;
  734. typedef typename _TreeIterator::difference_type difference_type;
  735. typedef value_type& reference;
  736. typedef typename _NodeTypes::__map_value_type_pointer pointer;
  737. _LIBCPP_INLINE_VISIBILITY
  738. __map_iterator() _NOEXCEPT {}
  739. _LIBCPP_INLINE_VISIBILITY
  740. __map_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
  741. _LIBCPP_INLINE_VISIBILITY
  742. reference operator*() const {return __i_->__get_value();}
  743. _LIBCPP_INLINE_VISIBILITY
  744. pointer operator->() const {return pointer_traits<pointer>::pointer_to(__i_->__get_value());}
  745. _LIBCPP_INLINE_VISIBILITY
  746. __map_iterator& operator++() {++__i_; return *this;}
  747. _LIBCPP_INLINE_VISIBILITY
  748. __map_iterator operator++(int)
  749. {
  750. __map_iterator __t(*this);
  751. ++(*this);
  752. return __t;
  753. }
  754. _LIBCPP_INLINE_VISIBILITY
  755. __map_iterator& operator--() {--__i_; return *this;}
  756. _LIBCPP_INLINE_VISIBILITY
  757. __map_iterator operator--(int)
  758. {
  759. __map_iterator __t(*this);
  760. --(*this);
  761. return __t;
  762. }
  763. friend _LIBCPP_INLINE_VISIBILITY
  764. bool operator==(const __map_iterator& __x, const __map_iterator& __y)
  765. {return __x.__i_ == __y.__i_;}
  766. friend
  767. _LIBCPP_INLINE_VISIBILITY
  768. bool operator!=(const __map_iterator& __x, const __map_iterator& __y)
  769. {return __x.__i_ != __y.__i_;}
  770. template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
  771. template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
  772. template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
  773. };
  774. template <class _TreeIterator>
  775. class _LIBCPP_TEMPLATE_VIS __map_const_iterator
  776. {
  777. typedef typename _TreeIterator::_NodeTypes _NodeTypes;
  778. typedef typename _TreeIterator::__pointer_traits __pointer_traits;
  779. _TreeIterator __i_;
  780. public:
  781. typedef bidirectional_iterator_tag iterator_category;
  782. typedef typename _NodeTypes::__map_value_type value_type;
  783. typedef typename _TreeIterator::difference_type difference_type;
  784. typedef const value_type& reference;
  785. typedef typename _NodeTypes::__const_map_value_type_pointer pointer;
  786. _LIBCPP_INLINE_VISIBILITY
  787. __map_const_iterator() _NOEXCEPT {}
  788. _LIBCPP_INLINE_VISIBILITY
  789. __map_const_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
  790. _LIBCPP_INLINE_VISIBILITY
  791. __map_const_iterator(__map_iterator<
  792. typename _TreeIterator::__non_const_iterator> __i) _NOEXCEPT
  793. : __i_(__i.__i_) {}
  794. _LIBCPP_INLINE_VISIBILITY
  795. reference operator*() const {return __i_->__get_value();}
  796. _LIBCPP_INLINE_VISIBILITY
  797. pointer operator->() const {return pointer_traits<pointer>::pointer_to(__i_->__get_value());}
  798. _LIBCPP_INLINE_VISIBILITY
  799. __map_const_iterator& operator++() {++__i_; return *this;}
  800. _LIBCPP_INLINE_VISIBILITY
  801. __map_const_iterator operator++(int)
  802. {
  803. __map_const_iterator __t(*this);
  804. ++(*this);
  805. return __t;
  806. }
  807. _LIBCPP_INLINE_VISIBILITY
  808. __map_const_iterator& operator--() {--__i_; return *this;}
  809. _LIBCPP_INLINE_VISIBILITY
  810. __map_const_iterator operator--(int)
  811. {
  812. __map_const_iterator __t(*this);
  813. --(*this);
  814. return __t;
  815. }
  816. friend _LIBCPP_INLINE_VISIBILITY
  817. bool operator==(const __map_const_iterator& __x, const __map_const_iterator& __y)
  818. {return __x.__i_ == __y.__i_;}
  819. friend _LIBCPP_INLINE_VISIBILITY
  820. bool operator!=(const __map_const_iterator& __x, const __map_const_iterator& __y)
  821. {return __x.__i_ != __y.__i_;}
  822. template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
  823. template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
  824. template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
  825. };
  826. template <class _Key, class _Tp, class _Compare = less<_Key>,
  827. class _Allocator = allocator<pair<const _Key, _Tp> > >
  828. class _LIBCPP_TEMPLATE_VIS map
  829. {
  830. public:
  831. // types:
  832. typedef _Key key_type;
  833. typedef _Tp mapped_type;
  834. typedef pair<const key_type, mapped_type> value_type;
  835. typedef __type_identity_t<_Compare> key_compare;
  836. typedef __type_identity_t<_Allocator> allocator_type;
  837. typedef value_type& reference;
  838. typedef const value_type& const_reference;
  839. static_assert((is_same<typename allocator_type::value_type, value_type>::value),
  840. "Allocator::value_type must be same type as value_type");
  841. class _LIBCPP_TEMPLATE_VIS value_compare
  842. : public __binary_function<value_type, value_type, bool>
  843. {
  844. friend class map;
  845. protected:
  846. key_compare comp;
  847. _LIBCPP_INLINE_VISIBILITY value_compare(key_compare __c) : comp(__c) {}
  848. public:
  849. _LIBCPP_INLINE_VISIBILITY
  850. bool operator()(const value_type& __x, const value_type& __y) const
  851. {return comp(__x.first, __y.first);}
  852. };
  853. private:
  854. typedef _VSTD::__value_type<key_type, mapped_type> __value_type;
  855. typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
  856. typedef __rebind_alloc<allocator_traits<allocator_type>, __value_type> __allocator_type;
  857. typedef __tree<__value_type, __vc, __allocator_type> __base;
  858. typedef typename __base::__node_traits __node_traits;
  859. typedef allocator_traits<allocator_type> __alloc_traits;
  860. static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
  861. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  862. "original allocator");
  863. __base __tree_;
  864. public:
  865. typedef typename __alloc_traits::pointer pointer;
  866. typedef typename __alloc_traits::const_pointer const_pointer;
  867. typedef typename __alloc_traits::size_type size_type;
  868. typedef typename __alloc_traits::difference_type difference_type;
  869. typedef __map_iterator<typename __base::iterator> iterator;
  870. typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
  871. typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
  872. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  873. #if _LIBCPP_STD_VER > 14
  874. typedef __map_node_handle<typename __base::__node, allocator_type> node_type;
  875. typedef __insert_return_type<iterator, node_type> insert_return_type;
  876. #endif
  877. template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
  878. friend class _LIBCPP_TEMPLATE_VIS map;
  879. template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
  880. friend class _LIBCPP_TEMPLATE_VIS multimap;
  881. _LIBCPP_INLINE_VISIBILITY
  882. map()
  883. _NOEXCEPT_(
  884. is_nothrow_default_constructible<allocator_type>::value &&
  885. is_nothrow_default_constructible<key_compare>::value &&
  886. is_nothrow_copy_constructible<key_compare>::value)
  887. : __tree_(__vc(key_compare())) {}
  888. _LIBCPP_INLINE_VISIBILITY
  889. explicit map(const key_compare& __comp)
  890. _NOEXCEPT_(
  891. is_nothrow_default_constructible<allocator_type>::value &&
  892. is_nothrow_copy_constructible<key_compare>::value)
  893. : __tree_(__vc(__comp)) {}
  894. _LIBCPP_INLINE_VISIBILITY
  895. explicit map(const key_compare& __comp, const allocator_type& __a)
  896. : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {}
  897. template <class _InputIterator>
  898. _LIBCPP_INLINE_VISIBILITY
  899. map(_InputIterator __f, _InputIterator __l,
  900. const key_compare& __comp = key_compare())
  901. : __tree_(__vc(__comp))
  902. {
  903. insert(__f, __l);
  904. }
  905. template <class _InputIterator>
  906. _LIBCPP_INLINE_VISIBILITY
  907. map(_InputIterator __f, _InputIterator __l,
  908. const key_compare& __comp, const allocator_type& __a)
  909. : __tree_(__vc(__comp), typename __base::allocator_type(__a))
  910. {
  911. insert(__f, __l);
  912. }
  913. #if _LIBCPP_STD_VER > 11
  914. template <class _InputIterator>
  915. _LIBCPP_INLINE_VISIBILITY
  916. map(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
  917. : map(__f, __l, key_compare(), __a) {}
  918. #endif
  919. _LIBCPP_INLINE_VISIBILITY
  920. map(const map& __m)
  921. : __tree_(__m.__tree_)
  922. {
  923. insert(__m.begin(), __m.end());
  924. }
  925. _LIBCPP_INLINE_VISIBILITY
  926. map& operator=(const map& __m)
  927. {
  928. #ifndef _LIBCPP_CXX03_LANG
  929. __tree_ = __m.__tree_;
  930. #else
  931. if (this != _VSTD::addressof(__m)) {
  932. __tree_.clear();
  933. __tree_.value_comp() = __m.__tree_.value_comp();
  934. __tree_.__copy_assign_alloc(__m.__tree_);
  935. insert(__m.begin(), __m.end());
  936. }
  937. #endif
  938. return *this;
  939. }
  940. #ifndef _LIBCPP_CXX03_LANG
  941. _LIBCPP_INLINE_VISIBILITY
  942. map(map&& __m)
  943. _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
  944. : __tree_(_VSTD::move(__m.__tree_))
  945. {
  946. }
  947. map(map&& __m, const allocator_type& __a);
  948. _LIBCPP_INLINE_VISIBILITY
  949. map& operator=(map&& __m)
  950. _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
  951. {
  952. __tree_ = _VSTD::move(__m.__tree_);
  953. return *this;
  954. }
  955. _LIBCPP_INLINE_VISIBILITY
  956. map(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
  957. : __tree_(__vc(__comp))
  958. {
  959. insert(__il.begin(), __il.end());
  960. }
  961. _LIBCPP_INLINE_VISIBILITY
  962. map(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
  963. : __tree_(__vc(__comp), typename __base::allocator_type(__a))
  964. {
  965. insert(__il.begin(), __il.end());
  966. }
  967. #if _LIBCPP_STD_VER > 11
  968. _LIBCPP_INLINE_VISIBILITY
  969. map(initializer_list<value_type> __il, const allocator_type& __a)
  970. : map(__il, key_compare(), __a) {}
  971. #endif
  972. _LIBCPP_INLINE_VISIBILITY
  973. map& operator=(initializer_list<value_type> __il)
  974. {
  975. __tree_.__assign_unique(__il.begin(), __il.end());
  976. return *this;
  977. }
  978. #endif // _LIBCPP_CXX03_LANG
  979. _LIBCPP_INLINE_VISIBILITY
  980. explicit map(const allocator_type& __a)
  981. : __tree_(typename __base::allocator_type(__a))
  982. {
  983. }
  984. _LIBCPP_INLINE_VISIBILITY
  985. map(const map& __m, const allocator_type& __a)
  986. : __tree_(__m.__tree_.value_comp(), typename __base::allocator_type(__a))
  987. {
  988. insert(__m.begin(), __m.end());
  989. }
  990. _LIBCPP_INLINE_VISIBILITY
  991. ~map() {
  992. static_assert(sizeof(__diagnose_non_const_comparator<_Key, _Compare>()), "");
  993. }
  994. _LIBCPP_INLINE_VISIBILITY
  995. iterator begin() _NOEXCEPT {return __tree_.begin();}
  996. _LIBCPP_INLINE_VISIBILITY
  997. const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
  998. _LIBCPP_INLINE_VISIBILITY
  999. iterator end() _NOEXCEPT {return __tree_.end();}
  1000. _LIBCPP_INLINE_VISIBILITY
  1001. const_iterator end() const _NOEXCEPT {return __tree_.end();}
  1002. _LIBCPP_INLINE_VISIBILITY
  1003. reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
  1004. _LIBCPP_INLINE_VISIBILITY
  1005. const_reverse_iterator rbegin() const _NOEXCEPT
  1006. {return const_reverse_iterator(end());}
  1007. _LIBCPP_INLINE_VISIBILITY
  1008. reverse_iterator rend() _NOEXCEPT
  1009. {return reverse_iterator(begin());}
  1010. _LIBCPP_INLINE_VISIBILITY
  1011. const_reverse_iterator rend() const _NOEXCEPT
  1012. {return const_reverse_iterator(begin());}
  1013. _LIBCPP_INLINE_VISIBILITY
  1014. const_iterator cbegin() const _NOEXCEPT {return begin();}
  1015. _LIBCPP_INLINE_VISIBILITY
  1016. const_iterator cend() const _NOEXCEPT {return end();}
  1017. _LIBCPP_INLINE_VISIBILITY
  1018. const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
  1019. _LIBCPP_INLINE_VISIBILITY
  1020. const_reverse_iterator crend() const _NOEXCEPT {return rend();}
  1021. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  1022. bool empty() const _NOEXCEPT {return __tree_.size() == 0;}
  1023. _LIBCPP_INLINE_VISIBILITY
  1024. size_type size() const _NOEXCEPT {return __tree_.size();}
  1025. _LIBCPP_INLINE_VISIBILITY
  1026. size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
  1027. mapped_type& operator[](const key_type& __k);
  1028. #ifndef _LIBCPP_CXX03_LANG
  1029. mapped_type& operator[](key_type&& __k);
  1030. #endif
  1031. mapped_type& at(const key_type& __k);
  1032. const mapped_type& at(const key_type& __k) const;
  1033. _LIBCPP_INLINE_VISIBILITY
  1034. allocator_type get_allocator() const _NOEXCEPT {return allocator_type(__tree_.__alloc());}
  1035. _LIBCPP_INLINE_VISIBILITY
  1036. key_compare key_comp() const {return __tree_.value_comp().key_comp();}
  1037. _LIBCPP_INLINE_VISIBILITY
  1038. value_compare value_comp() const {return value_compare(__tree_.value_comp().key_comp());}
  1039. #ifndef _LIBCPP_CXX03_LANG
  1040. template <class ..._Args>
  1041. _LIBCPP_INLINE_VISIBILITY
  1042. pair<iterator, bool> emplace(_Args&& ...__args) {
  1043. return __tree_.__emplace_unique(_VSTD::forward<_Args>(__args)...);
  1044. }
  1045. template <class ..._Args>
  1046. _LIBCPP_INLINE_VISIBILITY
  1047. iterator emplace_hint(const_iterator __p, _Args&& ...__args) {
  1048. return __tree_.__emplace_hint_unique(__p.__i_, _VSTD::forward<_Args>(__args)...);
  1049. }
  1050. template <class _Pp,
  1051. class = __enable_if_t<is_constructible<value_type, _Pp>::value> >
  1052. _LIBCPP_INLINE_VISIBILITY
  1053. pair<iterator, bool> insert(_Pp&& __p)
  1054. {return __tree_.__insert_unique(_VSTD::forward<_Pp>(__p));}
  1055. template <class _Pp,
  1056. class = __enable_if_t<is_constructible<value_type, _Pp>::value> >
  1057. _LIBCPP_INLINE_VISIBILITY
  1058. iterator insert(const_iterator __pos, _Pp&& __p)
  1059. {return __tree_.__insert_unique(__pos.__i_, _VSTD::forward<_Pp>(__p));}
  1060. #endif // _LIBCPP_CXX03_LANG
  1061. _LIBCPP_INLINE_VISIBILITY
  1062. pair<iterator, bool>
  1063. insert(const value_type& __v) {return __tree_.__insert_unique(__v);}
  1064. _LIBCPP_INLINE_VISIBILITY
  1065. iterator
  1066. insert(const_iterator __p, const value_type& __v)
  1067. {return __tree_.__insert_unique(__p.__i_, __v);}
  1068. #ifndef _LIBCPP_CXX03_LANG
  1069. _LIBCPP_INLINE_VISIBILITY
  1070. pair<iterator, bool>
  1071. insert(value_type&& __v) {return __tree_.__insert_unique(_VSTD::move(__v));}
  1072. _LIBCPP_INLINE_VISIBILITY
  1073. iterator insert(const_iterator __p, value_type&& __v)
  1074. {return __tree_.__insert_unique(__p.__i_, _VSTD::move(__v));}
  1075. _LIBCPP_INLINE_VISIBILITY
  1076. void insert(initializer_list<value_type> __il)
  1077. {insert(__il.begin(), __il.end());}
  1078. #endif
  1079. template <class _InputIterator>
  1080. _LIBCPP_INLINE_VISIBILITY
  1081. void insert(_InputIterator __f, _InputIterator __l)
  1082. {
  1083. for (const_iterator __e = cend(); __f != __l; ++__f)
  1084. insert(__e.__i_, *__f);
  1085. }
  1086. #if _LIBCPP_STD_VER > 14
  1087. template <class... _Args>
  1088. _LIBCPP_INLINE_VISIBILITY
  1089. pair<iterator, bool> try_emplace(const key_type& __k, _Args&&... __args)
  1090. {
  1091. return __tree_.__emplace_unique_key_args(__k,
  1092. _VSTD::piecewise_construct,
  1093. _VSTD::forward_as_tuple(__k),
  1094. _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...));
  1095. }
  1096. template <class... _Args>
  1097. _LIBCPP_INLINE_VISIBILITY
  1098. pair<iterator, bool> try_emplace(key_type&& __k, _Args&&... __args)
  1099. {
  1100. return __tree_.__emplace_unique_key_args(__k,
  1101. _VSTD::piecewise_construct,
  1102. _VSTD::forward_as_tuple(_VSTD::move(__k)),
  1103. _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...));
  1104. }
  1105. template <class... _Args>
  1106. _LIBCPP_INLINE_VISIBILITY
  1107. iterator try_emplace(const_iterator __h, const key_type& __k, _Args&&... __args)
  1108. {
  1109. return __tree_.__emplace_hint_unique_key_args(__h.__i_, __k,
  1110. _VSTD::piecewise_construct,
  1111. _VSTD::forward_as_tuple(__k),
  1112. _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)).first;
  1113. }
  1114. template <class... _Args>
  1115. _LIBCPP_INLINE_VISIBILITY
  1116. iterator try_emplace(const_iterator __h, key_type&& __k, _Args&&... __args)
  1117. {
  1118. return __tree_.__emplace_hint_unique_key_args(__h.__i_, __k,
  1119. _VSTD::piecewise_construct,
  1120. _VSTD::forward_as_tuple(_VSTD::move(__k)),
  1121. _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)).first;
  1122. }
  1123. template <class _Vp>
  1124. _LIBCPP_INLINE_VISIBILITY
  1125. pair<iterator, bool> insert_or_assign(const key_type& __k, _Vp&& __v)
  1126. {
  1127. iterator __p = lower_bound(__k);
  1128. if ( __p != end() && !key_comp()(__k, __p->first))
  1129. {
  1130. __p->second = _VSTD::forward<_Vp>(__v);
  1131. return _VSTD::make_pair(__p, false);
  1132. }
  1133. return _VSTD::make_pair(emplace_hint(__p, __k, _VSTD::forward<_Vp>(__v)), true);
  1134. }
  1135. template <class _Vp>
  1136. _LIBCPP_INLINE_VISIBILITY
  1137. pair<iterator, bool> insert_or_assign(key_type&& __k, _Vp&& __v)
  1138. {
  1139. iterator __p = lower_bound(__k);
  1140. if ( __p != end() && !key_comp()(__k, __p->first))
  1141. {
  1142. __p->second = _VSTD::forward<_Vp>(__v);
  1143. return _VSTD::make_pair(__p, false);
  1144. }
  1145. return _VSTD::make_pair(emplace_hint(__p, _VSTD::move(__k), _VSTD::forward<_Vp>(__v)), true);
  1146. }
  1147. template <class _Vp>
  1148. _LIBCPP_INLINE_VISIBILITY iterator insert_or_assign(const_iterator __h,
  1149. const key_type& __k,
  1150. _Vp&& __v) {
  1151. auto [__r, __inserted] = __tree_.__emplace_hint_unique_key_args(
  1152. __h.__i_, __k, __k, _VSTD::forward<_Vp>(__v));
  1153. if (!__inserted)
  1154. __r->__get_value().second = _VSTD::forward<_Vp>(__v);
  1155. return __r;
  1156. }
  1157. template <class _Vp>
  1158. _LIBCPP_INLINE_VISIBILITY iterator insert_or_assign(const_iterator __h,
  1159. key_type&& __k,
  1160. _Vp&& __v) {
  1161. auto [__r, __inserted] = __tree_.__emplace_hint_unique_key_args(
  1162. __h.__i_, __k, _VSTD::move(__k), _VSTD::forward<_Vp>(__v));
  1163. if (!__inserted)
  1164. __r->__get_value().second = _VSTD::forward<_Vp>(__v);
  1165. return __r;
  1166. }
  1167. #endif // _LIBCPP_STD_VER > 14
  1168. _LIBCPP_INLINE_VISIBILITY
  1169. iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
  1170. _LIBCPP_INLINE_VISIBILITY
  1171. iterator erase(iterator __p) {return __tree_.erase(__p.__i_);}
  1172. _LIBCPP_INLINE_VISIBILITY
  1173. size_type erase(const key_type& __k)
  1174. {return __tree_.__erase_unique(__k);}
  1175. _LIBCPP_INLINE_VISIBILITY
  1176. iterator erase(const_iterator __f, const_iterator __l)
  1177. {return __tree_.erase(__f.__i_, __l.__i_);}
  1178. _LIBCPP_REINITIALIZES_OBJECT _LIBCPP_INLINE_VISIBILITY
  1179. void clear() _NOEXCEPT {__tree_.clear();}
  1180. #if _LIBCPP_STD_VER > 14
  1181. _LIBCPP_INLINE_VISIBILITY
  1182. insert_return_type insert(node_type&& __nh)
  1183. {
  1184. _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
  1185. "node_type with incompatible allocator passed to map::insert()");
  1186. return __tree_.template __node_handle_insert_unique<
  1187. node_type, insert_return_type>(_VSTD::move(__nh));
  1188. }
  1189. _LIBCPP_INLINE_VISIBILITY
  1190. iterator insert(const_iterator __hint, node_type&& __nh)
  1191. {
  1192. _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
  1193. "node_type with incompatible allocator passed to map::insert()");
  1194. return __tree_.template __node_handle_insert_unique<node_type>(
  1195. __hint.__i_, _VSTD::move(__nh));
  1196. }
  1197. _LIBCPP_INLINE_VISIBILITY
  1198. node_type extract(key_type const& __key)
  1199. {
  1200. return __tree_.template __node_handle_extract<node_type>(__key);
  1201. }
  1202. _LIBCPP_INLINE_VISIBILITY
  1203. node_type extract(const_iterator __it)
  1204. {
  1205. return __tree_.template __node_handle_extract<node_type>(__it.__i_);
  1206. }
  1207. template <class _Compare2>
  1208. _LIBCPP_INLINE_VISIBILITY
  1209. void merge(map<key_type, mapped_type, _Compare2, allocator_type>& __source)
  1210. {
  1211. _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
  1212. "merging container with incompatible allocator");
  1213. __tree_.__node_handle_merge_unique(__source.__tree_);
  1214. }
  1215. template <class _Compare2>
  1216. _LIBCPP_INLINE_VISIBILITY
  1217. void merge(map<key_type, mapped_type, _Compare2, allocator_type>&& __source)
  1218. {
  1219. _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
  1220. "merging container with incompatible allocator");
  1221. __tree_.__node_handle_merge_unique(__source.__tree_);
  1222. }
  1223. template <class _Compare2>
  1224. _LIBCPP_INLINE_VISIBILITY
  1225. void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>& __source)
  1226. {
  1227. _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
  1228. "merging container with incompatible allocator");
  1229. __tree_.__node_handle_merge_unique(__source.__tree_);
  1230. }
  1231. template <class _Compare2>
  1232. _LIBCPP_INLINE_VISIBILITY
  1233. void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>&& __source)
  1234. {
  1235. _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
  1236. "merging container with incompatible allocator");
  1237. __tree_.__node_handle_merge_unique(__source.__tree_);
  1238. }
  1239. #endif
  1240. _LIBCPP_INLINE_VISIBILITY
  1241. void swap(map& __m)
  1242. _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
  1243. {__tree_.swap(__m.__tree_);}
  1244. _LIBCPP_INLINE_VISIBILITY
  1245. iterator find(const key_type& __k) {return __tree_.find(__k);}
  1246. _LIBCPP_INLINE_VISIBILITY
  1247. const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
  1248. #if _LIBCPP_STD_VER > 11
  1249. template <typename _K2>
  1250. _LIBCPP_INLINE_VISIBILITY
  1251. __enable_if_t<__is_transparent<_Compare, _K2>::value, iterator>
  1252. find(const _K2& __k) {return __tree_.find(__k);}
  1253. template <typename _K2>
  1254. _LIBCPP_INLINE_VISIBILITY
  1255. __enable_if_t<__is_transparent<_Compare, _K2>::value, const_iterator>
  1256. find(const _K2& __k) const {return __tree_.find(__k);}
  1257. #endif
  1258. _LIBCPP_INLINE_VISIBILITY
  1259. size_type count(const key_type& __k) const
  1260. {return __tree_.__count_unique(__k);}
  1261. #if _LIBCPP_STD_VER > 11
  1262. template <typename _K2>
  1263. _LIBCPP_INLINE_VISIBILITY
  1264. __enable_if_t<__is_transparent<_Compare, _K2>::value, size_type>
  1265. count(const _K2& __k) const {return __tree_.__count_multi(__k);}
  1266. #endif
  1267. #if _LIBCPP_STD_VER > 17
  1268. _LIBCPP_INLINE_VISIBILITY
  1269. bool contains(const key_type& __k) const {return find(__k) != end();}
  1270. template <typename _K2>
  1271. _LIBCPP_INLINE_VISIBILITY
  1272. __enable_if_t<__is_transparent<_Compare, _K2>::value, bool>
  1273. contains(const _K2& __k) const { return find(__k) != end(); }
  1274. #endif // _LIBCPP_STD_VER > 17
  1275. _LIBCPP_INLINE_VISIBILITY
  1276. iterator lower_bound(const key_type& __k)
  1277. {return __tree_.lower_bound(__k);}
  1278. _LIBCPP_INLINE_VISIBILITY
  1279. const_iterator lower_bound(const key_type& __k) const
  1280. {return __tree_.lower_bound(__k);}
  1281. #if _LIBCPP_STD_VER > 11
  1282. template <typename _K2>
  1283. _LIBCPP_INLINE_VISIBILITY
  1284. __enable_if_t<__is_transparent<_Compare, _K2>::value, iterator>
  1285. lower_bound(const _K2& __k) {return __tree_.lower_bound(__k);}
  1286. template <typename _K2>
  1287. _LIBCPP_INLINE_VISIBILITY
  1288. __enable_if_t<__is_transparent<_Compare, _K2>::value, const_iterator>
  1289. lower_bound(const _K2& __k) const {return __tree_.lower_bound(__k);}
  1290. #endif
  1291. _LIBCPP_INLINE_VISIBILITY
  1292. iterator upper_bound(const key_type& __k)
  1293. {return __tree_.upper_bound(__k);}
  1294. _LIBCPP_INLINE_VISIBILITY
  1295. const_iterator upper_bound(const key_type& __k) const
  1296. {return __tree_.upper_bound(__k);}
  1297. #if _LIBCPP_STD_VER > 11
  1298. template <typename _K2>
  1299. _LIBCPP_INLINE_VISIBILITY
  1300. __enable_if_t<__is_transparent<_Compare, _K2>::value, iterator>
  1301. upper_bound(const _K2& __k) {return __tree_.upper_bound(__k);}
  1302. template <typename _K2>
  1303. _LIBCPP_INLINE_VISIBILITY
  1304. __enable_if_t<__is_transparent<_Compare, _K2>::value, const_iterator>
  1305. upper_bound(const _K2& __k) const {return __tree_.upper_bound(__k);}
  1306. #endif
  1307. _LIBCPP_INLINE_VISIBILITY
  1308. pair<iterator,iterator> equal_range(const key_type& __k)
  1309. {return __tree_.__equal_range_unique(__k);}
  1310. _LIBCPP_INLINE_VISIBILITY
  1311. pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
  1312. {return __tree_.__equal_range_unique(__k);}
  1313. #if _LIBCPP_STD_VER > 11
  1314. template <typename _K2>
  1315. _LIBCPP_INLINE_VISIBILITY
  1316. __enable_if_t<__is_transparent<_Compare, _K2>::value, pair<iterator,iterator>>
  1317. equal_range(const _K2& __k) {return __tree_.__equal_range_multi(__k);}
  1318. template <typename _K2>
  1319. _LIBCPP_INLINE_VISIBILITY
  1320. __enable_if_t<__is_transparent<_Compare, _K2>::value, pair<const_iterator,const_iterator>>
  1321. equal_range(const _K2& __k) const {return __tree_.__equal_range_multi(__k);}
  1322. #endif
  1323. private:
  1324. typedef typename __base::__node __node;
  1325. typedef typename __base::__node_allocator __node_allocator;
  1326. typedef typename __base::__node_pointer __node_pointer;
  1327. typedef typename __base::__node_base_pointer __node_base_pointer;
  1328. typedef typename __base::__parent_pointer __parent_pointer;
  1329. typedef __map_node_destructor<__node_allocator> _Dp;
  1330. typedef unique_ptr<__node, _Dp> __node_holder;
  1331. #ifdef _LIBCPP_CXX03_LANG
  1332. __node_holder __construct_node_with_key(const key_type& __k);
  1333. #endif
  1334. };
  1335. #if _LIBCPP_STD_VER >= 17
  1336. template<class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>,
  1337. class _Allocator = allocator<__iter_to_alloc_type<_InputIterator>>,
  1338. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
  1339. class = enable_if_t<!__is_allocator<_Compare>::value, void>,
  1340. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1341. map(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
  1342. -> map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare, _Allocator>;
  1343. template<class _Key, class _Tp, class _Compare = less<remove_const_t<_Key>>,
  1344. class _Allocator = allocator<pair<const _Key, _Tp>>,
  1345. class = enable_if_t<!__is_allocator<_Compare>::value, void>,
  1346. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1347. map(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare(), _Allocator = _Allocator())
  1348. -> map<remove_const_t<_Key>, _Tp, _Compare, _Allocator>;
  1349. template<class _InputIterator, class _Allocator,
  1350. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
  1351. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1352. map(_InputIterator, _InputIterator, _Allocator)
  1353. -> map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>,
  1354. less<__iter_key_type<_InputIterator>>, _Allocator>;
  1355. template<class _Key, class _Tp, class _Allocator,
  1356. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1357. map(initializer_list<pair<_Key, _Tp>>, _Allocator)
  1358. -> map<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
  1359. #endif
  1360. #ifndef _LIBCPP_CXX03_LANG
  1361. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1362. map<_Key, _Tp, _Compare, _Allocator>::map(map&& __m, const allocator_type& __a)
  1363. : __tree_(_VSTD::move(__m.__tree_), typename __base::allocator_type(__a))
  1364. {
  1365. if (__a != __m.get_allocator())
  1366. {
  1367. const_iterator __e = cend();
  1368. while (!__m.empty())
  1369. __tree_.__insert_unique(__e.__i_,
  1370. __m.__tree_.remove(__m.begin().__i_)->__value_.__move());
  1371. }
  1372. }
  1373. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1374. _Tp&
  1375. map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k)
  1376. {
  1377. return __tree_.__emplace_unique_key_args(__k,
  1378. _VSTD::piecewise_construct,
  1379. _VSTD::forward_as_tuple(__k),
  1380. _VSTD::forward_as_tuple()).first->__get_value().second;
  1381. }
  1382. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1383. _Tp&
  1384. map<_Key, _Tp, _Compare, _Allocator>::operator[](key_type&& __k)
  1385. {
  1386. return __tree_.__emplace_unique_key_args(__k,
  1387. _VSTD::piecewise_construct,
  1388. _VSTD::forward_as_tuple(_VSTD::move(__k)),
  1389. _VSTD::forward_as_tuple()).first->__get_value().second;
  1390. }
  1391. #else // _LIBCPP_CXX03_LANG
  1392. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1393. typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
  1394. map<_Key, _Tp, _Compare, _Allocator>::__construct_node_with_key(const key_type& __k)
  1395. {
  1396. __node_allocator& __na = __tree_.__node_alloc();
  1397. __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
  1398. __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__get_value().first), __k);
  1399. __h.get_deleter().__first_constructed = true;
  1400. __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__get_value().second));
  1401. __h.get_deleter().__second_constructed = true;
  1402. return __h;
  1403. }
  1404. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1405. _Tp&
  1406. map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k)
  1407. {
  1408. __parent_pointer __parent;
  1409. __node_base_pointer& __child = __tree_.__find_equal(__parent, __k);
  1410. __node_pointer __r = static_cast<__node_pointer>(__child);
  1411. if (__child == nullptr)
  1412. {
  1413. __node_holder __h = __construct_node_with_key(__k);
  1414. __tree_.__insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
  1415. __r = __h.release();
  1416. }
  1417. return __r->__value_.__get_value().second;
  1418. }
  1419. #endif // _LIBCPP_CXX03_LANG
  1420. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1421. _Tp&
  1422. map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k)
  1423. {
  1424. __parent_pointer __parent;
  1425. __node_base_pointer& __child = __tree_.__find_equal(__parent, __k);
  1426. if (__child == nullptr)
  1427. __throw_out_of_range("map::at: key not found");
  1428. return static_cast<__node_pointer>(__child)->__value_.__get_value().second;
  1429. }
  1430. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1431. const _Tp&
  1432. map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k) const
  1433. {
  1434. __parent_pointer __parent;
  1435. __node_base_pointer __child = __tree_.__find_equal(__parent, __k);
  1436. if (__child == nullptr)
  1437. __throw_out_of_range("map::at: key not found");
  1438. return static_cast<__node_pointer>(__child)->__value_.__get_value().second;
  1439. }
  1440. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1441. inline _LIBCPP_INLINE_VISIBILITY
  1442. bool
  1443. operator==(const map<_Key, _Tp, _Compare, _Allocator>& __x,
  1444. const map<_Key, _Tp, _Compare, _Allocator>& __y)
  1445. {
  1446. return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
  1447. }
  1448. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1449. inline _LIBCPP_INLINE_VISIBILITY
  1450. bool
  1451. operator< (const map<_Key, _Tp, _Compare, _Allocator>& __x,
  1452. const map<_Key, _Tp, _Compare, _Allocator>& __y)
  1453. {
  1454. return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  1455. }
  1456. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1457. inline _LIBCPP_INLINE_VISIBILITY
  1458. bool
  1459. operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
  1460. const map<_Key, _Tp, _Compare, _Allocator>& __y)
  1461. {
  1462. return !(__x == __y);
  1463. }
  1464. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1465. inline _LIBCPP_INLINE_VISIBILITY
  1466. bool
  1467. operator> (const map<_Key, _Tp, _Compare, _Allocator>& __x,
  1468. const map<_Key, _Tp, _Compare, _Allocator>& __y)
  1469. {
  1470. return __y < __x;
  1471. }
  1472. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1473. inline _LIBCPP_INLINE_VISIBILITY
  1474. bool
  1475. operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
  1476. const map<_Key, _Tp, _Compare, _Allocator>& __y)
  1477. {
  1478. return !(__x < __y);
  1479. }
  1480. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1481. inline _LIBCPP_INLINE_VISIBILITY
  1482. bool
  1483. operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
  1484. const map<_Key, _Tp, _Compare, _Allocator>& __y)
  1485. {
  1486. return !(__y < __x);
  1487. }
  1488. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1489. inline _LIBCPP_INLINE_VISIBILITY
  1490. void
  1491. swap(map<_Key, _Tp, _Compare, _Allocator>& __x,
  1492. map<_Key, _Tp, _Compare, _Allocator>& __y)
  1493. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  1494. {
  1495. __x.swap(__y);
  1496. }
  1497. #if _LIBCPP_STD_VER > 17
  1498. template <class _Key, class _Tp, class _Compare, class _Allocator,
  1499. class _Predicate>
  1500. inline _LIBCPP_INLINE_VISIBILITY
  1501. typename map<_Key, _Tp, _Compare, _Allocator>::size_type
  1502. erase_if(map<_Key, _Tp, _Compare, _Allocator>& __c, _Predicate __pred) {
  1503. return _VSTD::__libcpp_erase_if_container(__c, __pred);
  1504. }
  1505. #endif
  1506. template <class _Key, class _Tp, class _Compare = less<_Key>,
  1507. class _Allocator = allocator<pair<const _Key, _Tp> > >
  1508. class _LIBCPP_TEMPLATE_VIS multimap
  1509. {
  1510. public:
  1511. // types:
  1512. typedef _Key key_type;
  1513. typedef _Tp mapped_type;
  1514. typedef pair<const key_type, mapped_type> value_type;
  1515. typedef __type_identity_t<_Compare> key_compare;
  1516. typedef __type_identity_t<_Allocator> allocator_type;
  1517. typedef value_type& reference;
  1518. typedef const value_type& const_reference;
  1519. static_assert((is_same<typename allocator_type::value_type, value_type>::value),
  1520. "Allocator::value_type must be same type as value_type");
  1521. class _LIBCPP_TEMPLATE_VIS value_compare
  1522. : public __binary_function<value_type, value_type, bool>
  1523. {
  1524. friend class multimap;
  1525. protected:
  1526. key_compare comp;
  1527. _LIBCPP_INLINE_VISIBILITY
  1528. value_compare(key_compare __c) : comp(__c) {}
  1529. public:
  1530. _LIBCPP_INLINE_VISIBILITY
  1531. bool operator()(const value_type& __x, const value_type& __y) const
  1532. {return comp(__x.first, __y.first);}
  1533. };
  1534. private:
  1535. typedef _VSTD::__value_type<key_type, mapped_type> __value_type;
  1536. typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
  1537. typedef __rebind_alloc<allocator_traits<allocator_type>, __value_type> __allocator_type;
  1538. typedef __tree<__value_type, __vc, __allocator_type> __base;
  1539. typedef typename __base::__node_traits __node_traits;
  1540. typedef allocator_traits<allocator_type> __alloc_traits;
  1541. static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
  1542. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  1543. "original allocator");
  1544. __base __tree_;
  1545. public:
  1546. typedef typename __alloc_traits::pointer pointer;
  1547. typedef typename __alloc_traits::const_pointer const_pointer;
  1548. typedef typename __alloc_traits::size_type size_type;
  1549. typedef typename __alloc_traits::difference_type difference_type;
  1550. typedef __map_iterator<typename __base::iterator> iterator;
  1551. typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
  1552. typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
  1553. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  1554. #if _LIBCPP_STD_VER > 14
  1555. typedef __map_node_handle<typename __base::__node, allocator_type> node_type;
  1556. #endif
  1557. template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
  1558. friend class _LIBCPP_TEMPLATE_VIS map;
  1559. template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
  1560. friend class _LIBCPP_TEMPLATE_VIS multimap;
  1561. _LIBCPP_INLINE_VISIBILITY
  1562. multimap()
  1563. _NOEXCEPT_(
  1564. is_nothrow_default_constructible<allocator_type>::value &&
  1565. is_nothrow_default_constructible<key_compare>::value &&
  1566. is_nothrow_copy_constructible<key_compare>::value)
  1567. : __tree_(__vc(key_compare())) {}
  1568. _LIBCPP_INLINE_VISIBILITY
  1569. explicit multimap(const key_compare& __comp)
  1570. _NOEXCEPT_(
  1571. is_nothrow_default_constructible<allocator_type>::value &&
  1572. is_nothrow_copy_constructible<key_compare>::value)
  1573. : __tree_(__vc(__comp)) {}
  1574. _LIBCPP_INLINE_VISIBILITY
  1575. explicit multimap(const key_compare& __comp, const allocator_type& __a)
  1576. : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {}
  1577. template <class _InputIterator>
  1578. _LIBCPP_INLINE_VISIBILITY
  1579. multimap(_InputIterator __f, _InputIterator __l,
  1580. const key_compare& __comp = key_compare())
  1581. : __tree_(__vc(__comp))
  1582. {
  1583. insert(__f, __l);
  1584. }
  1585. template <class _InputIterator>
  1586. _LIBCPP_INLINE_VISIBILITY
  1587. multimap(_InputIterator __f, _InputIterator __l,
  1588. const key_compare& __comp, const allocator_type& __a)
  1589. : __tree_(__vc(__comp), typename __base::allocator_type(__a))
  1590. {
  1591. insert(__f, __l);
  1592. }
  1593. #if _LIBCPP_STD_VER > 11
  1594. template <class _InputIterator>
  1595. _LIBCPP_INLINE_VISIBILITY
  1596. multimap(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
  1597. : multimap(__f, __l, key_compare(), __a) {}
  1598. #endif
  1599. _LIBCPP_INLINE_VISIBILITY
  1600. multimap(const multimap& __m)
  1601. : __tree_(__m.__tree_.value_comp(),
  1602. __alloc_traits::select_on_container_copy_construction(__m.__tree_.__alloc()))
  1603. {
  1604. insert(__m.begin(), __m.end());
  1605. }
  1606. _LIBCPP_INLINE_VISIBILITY
  1607. multimap& operator=(const multimap& __m)
  1608. {
  1609. #ifndef _LIBCPP_CXX03_LANG
  1610. __tree_ = __m.__tree_;
  1611. #else
  1612. if (this != _VSTD::addressof(__m)) {
  1613. __tree_.clear();
  1614. __tree_.value_comp() = __m.__tree_.value_comp();
  1615. __tree_.__copy_assign_alloc(__m.__tree_);
  1616. insert(__m.begin(), __m.end());
  1617. }
  1618. #endif
  1619. return *this;
  1620. }
  1621. #ifndef _LIBCPP_CXX03_LANG
  1622. _LIBCPP_INLINE_VISIBILITY
  1623. multimap(multimap&& __m)
  1624. _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
  1625. : __tree_(_VSTD::move(__m.__tree_))
  1626. {
  1627. }
  1628. multimap(multimap&& __m, const allocator_type& __a);
  1629. _LIBCPP_INLINE_VISIBILITY
  1630. multimap& operator=(multimap&& __m)
  1631. _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
  1632. {
  1633. __tree_ = _VSTD::move(__m.__tree_);
  1634. return *this;
  1635. }
  1636. _LIBCPP_INLINE_VISIBILITY
  1637. multimap(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
  1638. : __tree_(__vc(__comp))
  1639. {
  1640. insert(__il.begin(), __il.end());
  1641. }
  1642. _LIBCPP_INLINE_VISIBILITY
  1643. multimap(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
  1644. : __tree_(__vc(__comp), typename __base::allocator_type(__a))
  1645. {
  1646. insert(__il.begin(), __il.end());
  1647. }
  1648. #if _LIBCPP_STD_VER > 11
  1649. _LIBCPP_INLINE_VISIBILITY
  1650. multimap(initializer_list<value_type> __il, const allocator_type& __a)
  1651. : multimap(__il, key_compare(), __a) {}
  1652. #endif
  1653. _LIBCPP_INLINE_VISIBILITY
  1654. multimap& operator=(initializer_list<value_type> __il)
  1655. {
  1656. __tree_.__assign_multi(__il.begin(), __il.end());
  1657. return *this;
  1658. }
  1659. #endif // _LIBCPP_CXX03_LANG
  1660. _LIBCPP_INLINE_VISIBILITY
  1661. explicit multimap(const allocator_type& __a)
  1662. : __tree_(typename __base::allocator_type(__a))
  1663. {
  1664. }
  1665. _LIBCPP_INLINE_VISIBILITY
  1666. multimap(const multimap& __m, const allocator_type& __a)
  1667. : __tree_(__m.__tree_.value_comp(), typename __base::allocator_type(__a))
  1668. {
  1669. insert(__m.begin(), __m.end());
  1670. }
  1671. _LIBCPP_INLINE_VISIBILITY
  1672. ~multimap() {
  1673. static_assert(sizeof(__diagnose_non_const_comparator<_Key, _Compare>()), "");
  1674. }
  1675. _LIBCPP_INLINE_VISIBILITY
  1676. iterator begin() _NOEXCEPT {return __tree_.begin();}
  1677. _LIBCPP_INLINE_VISIBILITY
  1678. const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
  1679. _LIBCPP_INLINE_VISIBILITY
  1680. iterator end() _NOEXCEPT {return __tree_.end();}
  1681. _LIBCPP_INLINE_VISIBILITY
  1682. const_iterator end() const _NOEXCEPT {return __tree_.end();}
  1683. _LIBCPP_INLINE_VISIBILITY
  1684. reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
  1685. _LIBCPP_INLINE_VISIBILITY
  1686. const_reverse_iterator rbegin() const _NOEXCEPT
  1687. {return const_reverse_iterator(end());}
  1688. _LIBCPP_INLINE_VISIBILITY
  1689. reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
  1690. _LIBCPP_INLINE_VISIBILITY
  1691. const_reverse_iterator rend() const _NOEXCEPT
  1692. {return const_reverse_iterator(begin());}
  1693. _LIBCPP_INLINE_VISIBILITY
  1694. const_iterator cbegin() const _NOEXCEPT {return begin();}
  1695. _LIBCPP_INLINE_VISIBILITY
  1696. const_iterator cend() const _NOEXCEPT {return end();}
  1697. _LIBCPP_INLINE_VISIBILITY
  1698. const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
  1699. _LIBCPP_INLINE_VISIBILITY
  1700. const_reverse_iterator crend() const _NOEXCEPT {return rend();}
  1701. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  1702. bool empty() const _NOEXCEPT {return __tree_.size() == 0;}
  1703. _LIBCPP_INLINE_VISIBILITY
  1704. size_type size() const _NOEXCEPT {return __tree_.size();}
  1705. _LIBCPP_INLINE_VISIBILITY
  1706. size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
  1707. _LIBCPP_INLINE_VISIBILITY
  1708. allocator_type get_allocator() const _NOEXCEPT {return allocator_type(__tree_.__alloc());}
  1709. _LIBCPP_INLINE_VISIBILITY
  1710. key_compare key_comp() const {return __tree_.value_comp().key_comp();}
  1711. _LIBCPP_INLINE_VISIBILITY
  1712. value_compare value_comp() const
  1713. {return value_compare(__tree_.value_comp().key_comp());}
  1714. #ifndef _LIBCPP_CXX03_LANG
  1715. template <class ..._Args>
  1716. _LIBCPP_INLINE_VISIBILITY
  1717. iterator emplace(_Args&& ...__args) {
  1718. return __tree_.__emplace_multi(_VSTD::forward<_Args>(__args)...);
  1719. }
  1720. template <class ..._Args>
  1721. _LIBCPP_INLINE_VISIBILITY
  1722. iterator emplace_hint(const_iterator __p, _Args&& ...__args) {
  1723. return __tree_.__emplace_hint_multi(__p.__i_, _VSTD::forward<_Args>(__args)...);
  1724. }
  1725. template <class _Pp,
  1726. class = __enable_if_t<is_constructible<value_type, _Pp>::value>>
  1727. _LIBCPP_INLINE_VISIBILITY
  1728. iterator insert(_Pp&& __p)
  1729. {return __tree_.__insert_multi(_VSTD::forward<_Pp>(__p));}
  1730. template <class _Pp,
  1731. class = __enable_if_t<is_constructible<value_type, _Pp>::value>>
  1732. _LIBCPP_INLINE_VISIBILITY
  1733. iterator insert(const_iterator __pos, _Pp&& __p)
  1734. {return __tree_.__insert_multi(__pos.__i_, _VSTD::forward<_Pp>(__p));}
  1735. _LIBCPP_INLINE_VISIBILITY
  1736. iterator insert(value_type&& __v)
  1737. {return __tree_.__insert_multi(_VSTD::move(__v));}
  1738. _LIBCPP_INLINE_VISIBILITY
  1739. iterator insert(const_iterator __p, value_type&& __v)
  1740. {return __tree_.__insert_multi(__p.__i_, _VSTD::move(__v));}
  1741. _LIBCPP_INLINE_VISIBILITY
  1742. void insert(initializer_list<value_type> __il)
  1743. {insert(__il.begin(), __il.end());}
  1744. #endif // _LIBCPP_CXX03_LANG
  1745. _LIBCPP_INLINE_VISIBILITY
  1746. iterator insert(const value_type& __v) {return __tree_.__insert_multi(__v);}
  1747. _LIBCPP_INLINE_VISIBILITY
  1748. iterator insert(const_iterator __p, const value_type& __v)
  1749. {return __tree_.__insert_multi(__p.__i_, __v);}
  1750. template <class _InputIterator>
  1751. _LIBCPP_INLINE_VISIBILITY
  1752. void insert(_InputIterator __f, _InputIterator __l)
  1753. {
  1754. for (const_iterator __e = cend(); __f != __l; ++__f)
  1755. __tree_.__insert_multi(__e.__i_, *__f);
  1756. }
  1757. _LIBCPP_INLINE_VISIBILITY
  1758. iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
  1759. _LIBCPP_INLINE_VISIBILITY
  1760. iterator erase(iterator __p) {return __tree_.erase(__p.__i_);}
  1761. _LIBCPP_INLINE_VISIBILITY
  1762. size_type erase(const key_type& __k) {return __tree_.__erase_multi(__k);}
  1763. _LIBCPP_INLINE_VISIBILITY
  1764. iterator erase(const_iterator __f, const_iterator __l)
  1765. {return __tree_.erase(__f.__i_, __l.__i_);}
  1766. #if _LIBCPP_STD_VER > 14
  1767. _LIBCPP_INLINE_VISIBILITY
  1768. iterator insert(node_type&& __nh)
  1769. {
  1770. _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
  1771. "node_type with incompatible allocator passed to multimap::insert()");
  1772. return __tree_.template __node_handle_insert_multi<node_type>(
  1773. _VSTD::move(__nh));
  1774. }
  1775. _LIBCPP_INLINE_VISIBILITY
  1776. iterator insert(const_iterator __hint, node_type&& __nh)
  1777. {
  1778. _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
  1779. "node_type with incompatible allocator passed to multimap::insert()");
  1780. return __tree_.template __node_handle_insert_multi<node_type>(
  1781. __hint.__i_, _VSTD::move(__nh));
  1782. }
  1783. _LIBCPP_INLINE_VISIBILITY
  1784. node_type extract(key_type const& __key)
  1785. {
  1786. return __tree_.template __node_handle_extract<node_type>(__key);
  1787. }
  1788. _LIBCPP_INLINE_VISIBILITY
  1789. node_type extract(const_iterator __it)
  1790. {
  1791. return __tree_.template __node_handle_extract<node_type>(
  1792. __it.__i_);
  1793. }
  1794. template <class _Compare2>
  1795. _LIBCPP_INLINE_VISIBILITY
  1796. void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>& __source)
  1797. {
  1798. _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
  1799. "merging container with incompatible allocator");
  1800. return __tree_.__node_handle_merge_multi(__source.__tree_);
  1801. }
  1802. template <class _Compare2>
  1803. _LIBCPP_INLINE_VISIBILITY
  1804. void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>&& __source)
  1805. {
  1806. _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
  1807. "merging container with incompatible allocator");
  1808. return __tree_.__node_handle_merge_multi(__source.__tree_);
  1809. }
  1810. template <class _Compare2>
  1811. _LIBCPP_INLINE_VISIBILITY
  1812. void merge(map<key_type, mapped_type, _Compare2, allocator_type>& __source)
  1813. {
  1814. _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
  1815. "merging container with incompatible allocator");
  1816. return __tree_.__node_handle_merge_multi(__source.__tree_);
  1817. }
  1818. template <class _Compare2>
  1819. _LIBCPP_INLINE_VISIBILITY
  1820. void merge(map<key_type, mapped_type, _Compare2, allocator_type>&& __source)
  1821. {
  1822. _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
  1823. "merging container with incompatible allocator");
  1824. return __tree_.__node_handle_merge_multi(__source.__tree_);
  1825. }
  1826. #endif
  1827. _LIBCPP_REINITIALIZES_OBJECT _LIBCPP_INLINE_VISIBILITY
  1828. void clear() _NOEXCEPT {__tree_.clear();}
  1829. _LIBCPP_INLINE_VISIBILITY
  1830. void swap(multimap& __m)
  1831. _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
  1832. {__tree_.swap(__m.__tree_);}
  1833. _LIBCPP_INLINE_VISIBILITY
  1834. iterator find(const key_type& __k) {return __tree_.find(__k);}
  1835. _LIBCPP_INLINE_VISIBILITY
  1836. const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
  1837. #if _LIBCPP_STD_VER > 11
  1838. template <typename _K2>
  1839. _LIBCPP_INLINE_VISIBILITY
  1840. __enable_if_t<__is_transparent<_Compare, _K2>::value, iterator>
  1841. find(const _K2& __k) {return __tree_.find(__k);}
  1842. template <typename _K2>
  1843. _LIBCPP_INLINE_VISIBILITY
  1844. __enable_if_t<__is_transparent<_Compare, _K2>::value, const_iterator>
  1845. find(const _K2& __k) const {return __tree_.find(__k);}
  1846. #endif
  1847. _LIBCPP_INLINE_VISIBILITY
  1848. size_type count(const key_type& __k) const
  1849. {return __tree_.__count_multi(__k);}
  1850. #if _LIBCPP_STD_VER > 11
  1851. template <typename _K2>
  1852. _LIBCPP_INLINE_VISIBILITY
  1853. __enable_if_t<__is_transparent<_Compare, _K2>::value, size_type>
  1854. count(const _K2& __k) const {return __tree_.__count_multi(__k);}
  1855. #endif
  1856. #if _LIBCPP_STD_VER > 17
  1857. _LIBCPP_INLINE_VISIBILITY
  1858. bool contains(const key_type& __k) const {return find(__k) != end();}
  1859. template <typename _K2>
  1860. _LIBCPP_INLINE_VISIBILITY
  1861. __enable_if_t<__is_transparent<_Compare, _K2>::value, bool>
  1862. contains(const _K2& __k) const { return find(__k) != end(); }
  1863. #endif // _LIBCPP_STD_VER > 17
  1864. _LIBCPP_INLINE_VISIBILITY
  1865. iterator lower_bound(const key_type& __k)
  1866. {return __tree_.lower_bound(__k);}
  1867. _LIBCPP_INLINE_VISIBILITY
  1868. const_iterator lower_bound(const key_type& __k) const
  1869. {return __tree_.lower_bound(__k);}
  1870. #if _LIBCPP_STD_VER > 11
  1871. template <typename _K2>
  1872. _LIBCPP_INLINE_VISIBILITY
  1873. __enable_if_t<__is_transparent<_Compare, _K2>::value, iterator>
  1874. lower_bound(const _K2& __k) {return __tree_.lower_bound(__k);}
  1875. template <typename _K2>
  1876. _LIBCPP_INLINE_VISIBILITY
  1877. __enable_if_t<__is_transparent<_Compare, _K2>::value, const_iterator>
  1878. lower_bound(const _K2& __k) const {return __tree_.lower_bound(__k);}
  1879. #endif
  1880. _LIBCPP_INLINE_VISIBILITY
  1881. iterator upper_bound(const key_type& __k)
  1882. {return __tree_.upper_bound(__k);}
  1883. _LIBCPP_INLINE_VISIBILITY
  1884. const_iterator upper_bound(const key_type& __k) const
  1885. {return __tree_.upper_bound(__k);}
  1886. #if _LIBCPP_STD_VER > 11
  1887. template <typename _K2>
  1888. _LIBCPP_INLINE_VISIBILITY
  1889. __enable_if_t<__is_transparent<_Compare, _K2>::value, iterator>
  1890. upper_bound(const _K2& __k) {return __tree_.upper_bound(__k);}
  1891. template <typename _K2>
  1892. _LIBCPP_INLINE_VISIBILITY
  1893. __enable_if_t<__is_transparent<_Compare, _K2>::value, const_iterator>
  1894. upper_bound(const _K2& __k) const {return __tree_.upper_bound(__k);}
  1895. #endif
  1896. _LIBCPP_INLINE_VISIBILITY
  1897. pair<iterator,iterator> equal_range(const key_type& __k)
  1898. {return __tree_.__equal_range_multi(__k);}
  1899. _LIBCPP_INLINE_VISIBILITY
  1900. pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
  1901. {return __tree_.__equal_range_multi(__k);}
  1902. #if _LIBCPP_STD_VER > 11
  1903. template <typename _K2>
  1904. _LIBCPP_INLINE_VISIBILITY
  1905. __enable_if_t<__is_transparent<_Compare, _K2>::value, pair<iterator,iterator>>
  1906. equal_range(const _K2& __k) {return __tree_.__equal_range_multi(__k);}
  1907. template <typename _K2>
  1908. _LIBCPP_INLINE_VISIBILITY
  1909. __enable_if_t<__is_transparent<_Compare, _K2>::value, pair<const_iterator,const_iterator>>
  1910. equal_range(const _K2& __k) const {return __tree_.__equal_range_multi(__k);}
  1911. #endif
  1912. private:
  1913. typedef typename __base::__node __node;
  1914. typedef typename __base::__node_allocator __node_allocator;
  1915. typedef typename __base::__node_pointer __node_pointer;
  1916. typedef __map_node_destructor<__node_allocator> _Dp;
  1917. typedef unique_ptr<__node, _Dp> __node_holder;
  1918. };
  1919. #if _LIBCPP_STD_VER >= 17
  1920. template<class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>,
  1921. class _Allocator = allocator<__iter_to_alloc_type<_InputIterator>>,
  1922. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
  1923. class = enable_if_t<!__is_allocator<_Compare>::value, void>,
  1924. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1925. multimap(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
  1926. -> multimap<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare, _Allocator>;
  1927. template<class _Key, class _Tp, class _Compare = less<remove_const_t<_Key>>,
  1928. class _Allocator = allocator<pair<const _Key, _Tp>>,
  1929. class = enable_if_t<!__is_allocator<_Compare>::value, void>,
  1930. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1931. multimap(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare(), _Allocator = _Allocator())
  1932. -> multimap<remove_const_t<_Key>, _Tp, _Compare, _Allocator>;
  1933. template<class _InputIterator, class _Allocator,
  1934. class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
  1935. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1936. multimap(_InputIterator, _InputIterator, _Allocator)
  1937. -> multimap<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>,
  1938. less<__iter_key_type<_InputIterator>>, _Allocator>;
  1939. template<class _Key, class _Tp, class _Allocator,
  1940. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1941. multimap(initializer_list<pair<_Key, _Tp>>, _Allocator)
  1942. -> multimap<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
  1943. #endif
  1944. #ifndef _LIBCPP_CXX03_LANG
  1945. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1946. multimap<_Key, _Tp, _Compare, _Allocator>::multimap(multimap&& __m, const allocator_type& __a)
  1947. : __tree_(_VSTD::move(__m.__tree_), typename __base::allocator_type(__a))
  1948. {
  1949. if (__a != __m.get_allocator())
  1950. {
  1951. const_iterator __e = cend();
  1952. while (!__m.empty())
  1953. __tree_.__insert_multi(__e.__i_,
  1954. _VSTD::move(__m.__tree_.remove(__m.begin().__i_)->__value_.__move()));
  1955. }
  1956. }
  1957. #endif
  1958. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1959. inline _LIBCPP_INLINE_VISIBILITY
  1960. bool
  1961. operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
  1962. const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
  1963. {
  1964. return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
  1965. }
  1966. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1967. inline _LIBCPP_INLINE_VISIBILITY
  1968. bool
  1969. operator< (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
  1970. const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
  1971. {
  1972. return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  1973. }
  1974. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1975. inline _LIBCPP_INLINE_VISIBILITY
  1976. bool
  1977. operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
  1978. const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
  1979. {
  1980. return !(__x == __y);
  1981. }
  1982. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1983. inline _LIBCPP_INLINE_VISIBILITY
  1984. bool
  1985. operator> (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
  1986. const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
  1987. {
  1988. return __y < __x;
  1989. }
  1990. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1991. inline _LIBCPP_INLINE_VISIBILITY
  1992. bool
  1993. operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
  1994. const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
  1995. {
  1996. return !(__x < __y);
  1997. }
  1998. template <class _Key, class _Tp, class _Compare, class _Allocator>
  1999. inline _LIBCPP_INLINE_VISIBILITY
  2000. bool
  2001. operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
  2002. const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
  2003. {
  2004. return !(__y < __x);
  2005. }
  2006. template <class _Key, class _Tp, class _Compare, class _Allocator>
  2007. inline _LIBCPP_INLINE_VISIBILITY
  2008. void
  2009. swap(multimap<_Key, _Tp, _Compare, _Allocator>& __x,
  2010. multimap<_Key, _Tp, _Compare, _Allocator>& __y)
  2011. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  2012. {
  2013. __x.swap(__y);
  2014. }
  2015. #if _LIBCPP_STD_VER > 17
  2016. template <class _Key, class _Tp, class _Compare, class _Allocator,
  2017. class _Predicate>
  2018. inline _LIBCPP_INLINE_VISIBILITY
  2019. typename multimap<_Key, _Tp, _Compare, _Allocator>::size_type
  2020. erase_if(multimap<_Key, _Tp, _Compare, _Allocator>& __c,
  2021. _Predicate __pred) {
  2022. return _VSTD::__libcpp_erase_if_container(__c, __pred);
  2023. }
  2024. #endif
  2025. _LIBCPP_END_NAMESPACE_STD
  2026. #if _LIBCPP_STD_VER > 14
  2027. _LIBCPP_BEGIN_NAMESPACE_STD
  2028. namespace pmr {
  2029. template <class _KeyT, class _ValueT, class _CompareT = std::less<_KeyT>>
  2030. using map = std::map<_KeyT, _ValueT, _CompareT, polymorphic_allocator<std::pair<const _KeyT, _ValueT>>>;
  2031. template <class _KeyT, class _ValueT, class _CompareT = std::less<_KeyT>>
  2032. using multimap = std::multimap<_KeyT, _ValueT, _CompareT, polymorphic_allocator<std::pair<const _KeyT, _ValueT>>>;
  2033. } // namespace pmr
  2034. _LIBCPP_END_NAMESPACE_STD
  2035. #endif
  2036. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  2037. # include <concepts>
  2038. # include <functional>
  2039. # include <iterator>
  2040. # include <utility>
  2041. #endif
  2042. #endif // _LIBCPP_MAP