map 83 KB

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