map 86 KB

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