set 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  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_SET
  10. #define _LIBCPP_SET
  11. /*
  12. set synopsis
  13. namespace std
  14. {
  15. template <class Key, class Compare = less<Key>,
  16. class Allocator = allocator<Key>>
  17. class set
  18. {
  19. public:
  20. // types:
  21. typedef Key key_type;
  22. typedef key_type value_type;
  23. typedef Compare key_compare;
  24. typedef key_compare value_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::size_type size_type;
  29. typedef typename allocator_type::difference_type difference_type;
  30. typedef typename allocator_type::pointer pointer;
  31. typedef typename allocator_type::const_pointer const_pointer;
  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. // construct/copy/destroy:
  39. set()
  40. noexcept(
  41. is_nothrow_default_constructible<allocator_type>::value &&
  42. is_nothrow_default_constructible<key_compare>::value &&
  43. is_nothrow_copy_constructible<key_compare>::value);
  44. explicit set(const value_compare& comp);
  45. set(const value_compare& comp, const allocator_type& a);
  46. template <class InputIterator>
  47. set(InputIterator first, InputIterator last,
  48. const value_compare& comp = value_compare());
  49. template <class InputIterator>
  50. set(InputIterator first, InputIterator last, const value_compare& comp,
  51. const allocator_type& a);
  52. template<container-compatible-range<value_type> R>
  53. set(from_range_t, R&& rg, const Compare& comp = Compare(), const Allocator& = Allocator()); // C++23
  54. set(const set& s);
  55. set(set&& s)
  56. noexcept(
  57. is_nothrow_move_constructible<allocator_type>::value &&
  58. is_nothrow_move_constructible<key_compare>::value);
  59. explicit set(const allocator_type& a);
  60. set(const set& s, const allocator_type& a);
  61. set(set&& s, const allocator_type& a);
  62. set(initializer_list<value_type> il, const value_compare& comp = value_compare());
  63. set(initializer_list<value_type> il, const value_compare& comp,
  64. const allocator_type& a);
  65. template <class InputIterator>
  66. set(InputIterator first, InputIterator last, const allocator_type& a)
  67. : set(first, last, Compare(), a) {} // C++14
  68. template<container-compatible-range<value_type> R>
  69. set(from_range_t, R&& rg, const Allocator& a))
  70. : set(from_range, std::forward<R>(rg), Compare(), a) { } // C++23
  71. set(initializer_list<value_type> il, const allocator_type& a)
  72. : set(il, Compare(), a) {} // C++14
  73. ~set();
  74. set& operator=(const set& s);
  75. set& operator=(set&& s)
  76. noexcept(
  77. allocator_type::propagate_on_container_move_assignment::value &&
  78. is_nothrow_move_assignable<allocator_type>::value &&
  79. is_nothrow_move_assignable<key_compare>::value);
  80. set& operator=(initializer_list<value_type> il);
  81. // iterators:
  82. iterator begin() noexcept;
  83. const_iterator begin() const noexcept;
  84. iterator end() noexcept;
  85. const_iterator end() const noexcept;
  86. reverse_iterator rbegin() noexcept;
  87. const_reverse_iterator rbegin() const noexcept;
  88. reverse_iterator rend() noexcept;
  89. const_reverse_iterator rend() const noexcept;
  90. const_iterator cbegin() const noexcept;
  91. const_iterator cend() const noexcept;
  92. const_reverse_iterator crbegin() const noexcept;
  93. const_reverse_iterator crend() const noexcept;
  94. // capacity:
  95. bool empty() const noexcept;
  96. size_type size() const noexcept;
  97. size_type max_size() const noexcept;
  98. // modifiers:
  99. template <class... Args>
  100. pair<iterator, bool> emplace(Args&&... args);
  101. template <class... Args>
  102. iterator emplace_hint(const_iterator position, Args&&... args);
  103. pair<iterator,bool> insert(const value_type& v);
  104. pair<iterator,bool> insert(value_type&& v);
  105. iterator insert(const_iterator position, const value_type& v);
  106. iterator insert(const_iterator position, value_type&& v);
  107. template <class InputIterator>
  108. void insert(InputIterator first, InputIterator last);
  109. template<container-compatible-range<value_type> R>
  110. void insert_range(R&& rg); // C++23
  111. void insert(initializer_list<value_type> il);
  112. node_type extract(const_iterator position); // C++17
  113. node_type extract(const key_type& x); // C++17
  114. insert_return_type insert(node_type&& nh); // C++17
  115. iterator insert(const_iterator hint, node_type&& nh); // C++17
  116. iterator erase(const_iterator position);
  117. iterator erase(iterator position); // C++14
  118. size_type erase(const key_type& k);
  119. iterator erase(const_iterator first, const_iterator last);
  120. void clear() noexcept;
  121. template<class C2>
  122. void merge(set<Key, C2, Allocator>& source); // C++17
  123. template<class C2>
  124. void merge(set<Key, C2, Allocator>&& source); // C++17
  125. template<class C2>
  126. void merge(multiset<Key, C2, Allocator>& source); // C++17
  127. template<class C2>
  128. void merge(multiset<Key, C2, Allocator>&& source); // C++17
  129. void swap(set& s)
  130. noexcept(
  131. __is_nothrow_swappable<key_compare>::value &&
  132. (!allocator_type::propagate_on_container_swap::value ||
  133. __is_nothrow_swappable<allocator_type>::value));
  134. // observers:
  135. allocator_type get_allocator() const noexcept;
  136. key_compare key_comp() const;
  137. value_compare value_comp() const;
  138. // set operations:
  139. iterator find(const key_type& k);
  140. const_iterator find(const key_type& k) const;
  141. template<typename K>
  142. iterator find(const K& x);
  143. template<typename K>
  144. const_iterator find(const K& x) const; // C++14
  145. template<typename K>
  146. size_type count(const K& x) const; // C++14
  147. size_type count(const key_type& k) const;
  148. bool contains(const key_type& x) const; // C++20
  149. template<class K> bool contains(const K& x) const; // C++20
  150. iterator lower_bound(const key_type& k);
  151. const_iterator lower_bound(const key_type& k) const;
  152. template<typename K>
  153. iterator lower_bound(const K& x); // C++14
  154. template<typename K>
  155. const_iterator lower_bound(const K& x) const; // C++14
  156. iterator upper_bound(const key_type& k);
  157. const_iterator upper_bound(const key_type& k) const;
  158. template<typename K>
  159. iterator upper_bound(const K& x); // C++14
  160. template<typename K>
  161. const_iterator upper_bound(const K& x) const; // C++14
  162. pair<iterator,iterator> equal_range(const key_type& k);
  163. pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
  164. template<typename K>
  165. pair<iterator,iterator> equal_range(const K& x); // C++14
  166. template<typename K>
  167. pair<const_iterator,const_iterator> equal_range(const K& x) const; // C++14
  168. };
  169. template <class InputIterator,
  170. class Compare = less<typename iterator_traits<InputIterator>::value_type>,
  171. class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
  172. set(InputIterator, InputIterator,
  173. Compare = Compare(), Allocator = Allocator())
  174. -> set<typename iterator_traits<InputIterator>::value_type, Compare, Allocator>; // C++17
  175. template<ranges::input_range R, class Compare = less<ranges::range_value_t<R>>,
  176. class Allocator = allocator<ranges::range_value_t<R>>>
  177. set(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())
  178. -> set<ranges::range_value_t<R>, Compare, Allocator>; // C++23
  179. template<class Key, class Compare = less<Key>, class Allocator = allocator<Key>>
  180. set(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
  181. -> set<Key, Compare, Allocator>; // C++17
  182. template<class InputIterator, class Allocator>
  183. set(InputIterator, InputIterator, Allocator)
  184. -> set<typename iterator_traits<InputIterator>::value_type,
  185. less<typename iterator_traits<InputIterator>::value_type>, Allocator>; // C++17
  186. template<ranges::input_range R, class Allocator>
  187. set(from_range_t, R&&, Allocator)
  188. -> set<ranges::range_value_t<R>, less<ranges::range_value_t<R>>, Allocator>; // C++23
  189. template<class Key, class Allocator>
  190. set(initializer_list<Key>, Allocator) -> set<Key, less<Key>, Allocator>; // C++17
  191. template <class Key, class Compare, class Allocator>
  192. bool
  193. operator==(const set<Key, Compare, Allocator>& x,
  194. const set<Key, Compare, Allocator>& y);
  195. template <class Key, class Compare, class Allocator>
  196. bool
  197. operator< (const set<Key, Compare, Allocator>& x,
  198. const set<Key, Compare, Allocator>& y); // removed in C++20
  199. template <class Key, class Compare, class Allocator>
  200. bool
  201. operator!=(const set<Key, Compare, Allocator>& x,
  202. const set<Key, Compare, Allocator>& y); // removed in C++20
  203. template <class Key, class Compare, class Allocator>
  204. bool
  205. operator> (const set<Key, Compare, Allocator>& x,
  206. const set<Key, Compare, Allocator>& y); // removed in C++20
  207. template <class Key, class Compare, class Allocator>
  208. bool
  209. operator>=(const set<Key, Compare, Allocator>& x,
  210. const set<Key, Compare, Allocator>& y); // removed in C++20
  211. template <class Key, class Compare, class Allocator>
  212. bool
  213. operator<=(const set<Key, Compare, Allocator>& x,
  214. const set<Key, Compare, Allocator>& y); // removed in C++20
  215. template<class Key, class Compare, class Allocator>
  216. synth-three-way-result<Key> operator<=>(const set<Key, Compare, Allocator>& x,
  217. const set<Key, Compare, Allocator>& y); // since C++20
  218. // specialized algorithms:
  219. template <class Key, class Compare, class Allocator>
  220. void
  221. swap(set<Key, Compare, Allocator>& x, set<Key, Compare, Allocator>& y)
  222. noexcept(noexcept(x.swap(y)));
  223. template <class Key, class Compare, class Allocator, class Predicate>
  224. typename set<Key, Compare, Allocator>::size_type
  225. erase_if(set<Key, Compare, Allocator>& c, Predicate pred); // C++20
  226. template <class Key, class Compare = less<Key>,
  227. class Allocator = allocator<Key>>
  228. class multiset
  229. {
  230. public:
  231. // types:
  232. typedef Key key_type;
  233. typedef key_type value_type;
  234. typedef Compare key_compare;
  235. typedef key_compare value_compare;
  236. typedef Allocator allocator_type;
  237. typedef typename allocator_type::reference reference;
  238. typedef typename allocator_type::const_reference const_reference;
  239. typedef typename allocator_type::size_type size_type;
  240. typedef typename allocator_type::difference_type difference_type;
  241. typedef typename allocator_type::pointer pointer;
  242. typedef typename allocator_type::const_pointer const_pointer;
  243. typedef implementation-defined iterator;
  244. typedef implementation-defined const_iterator;
  245. typedef std::reverse_iterator<iterator> reverse_iterator;
  246. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  247. typedef unspecified node_type; // C++17
  248. // construct/copy/destroy:
  249. multiset()
  250. noexcept(
  251. is_nothrow_default_constructible<allocator_type>::value &&
  252. is_nothrow_default_constructible<key_compare>::value &&
  253. is_nothrow_copy_constructible<key_compare>::value);
  254. explicit multiset(const value_compare& comp);
  255. multiset(const value_compare& comp, const allocator_type& a);
  256. template <class InputIterator>
  257. multiset(InputIterator first, InputIterator last,
  258. const value_compare& comp = value_compare());
  259. template <class InputIterator>
  260. multiset(InputIterator first, InputIterator last,
  261. const value_compare& comp, const allocator_type& a);
  262. template<container-compatible-range<value_type> R>
  263. multiset(from_range_t, R&& rg,
  264. const Compare& comp = Compare(), const Allocator& = Allocator()); // C++23
  265. multiset(const multiset& s);
  266. multiset(multiset&& s)
  267. noexcept(
  268. is_nothrow_move_constructible<allocator_type>::value &&
  269. is_nothrow_move_constructible<key_compare>::value);
  270. explicit multiset(const allocator_type& a);
  271. multiset(const multiset& s, const allocator_type& a);
  272. multiset(multiset&& s, const allocator_type& a);
  273. multiset(initializer_list<value_type> il, const value_compare& comp = value_compare());
  274. multiset(initializer_list<value_type> il, const value_compare& comp,
  275. const allocator_type& a);
  276. template <class InputIterator>
  277. multiset(InputIterator first, InputIterator last, const allocator_type& a)
  278. : set(first, last, Compare(), a) {} // C++14
  279. template<container-compatible-range<value_type> R>
  280. multiset(from_range_t, R&& rg, const Allocator& a))
  281. : multiset(from_range, std::forward<R>(rg), Compare(), a) { } // C++23
  282. multiset(initializer_list<value_type> il, const allocator_type& a)
  283. : set(il, Compare(), a) {} // C++14
  284. ~multiset();
  285. multiset& operator=(const multiset& s);
  286. multiset& operator=(multiset&& s)
  287. noexcept(
  288. allocator_type::propagate_on_container_move_assignment::value &&
  289. is_nothrow_move_assignable<allocator_type>::value &&
  290. is_nothrow_move_assignable<key_compare>::value);
  291. multiset& operator=(initializer_list<value_type> il);
  292. // iterators:
  293. iterator begin() noexcept;
  294. const_iterator begin() const noexcept;
  295. iterator end() noexcept;
  296. const_iterator end() const noexcept;
  297. reverse_iterator rbegin() noexcept;
  298. const_reverse_iterator rbegin() const noexcept;
  299. reverse_iterator rend() noexcept;
  300. const_reverse_iterator rend() const noexcept;
  301. const_iterator cbegin() const noexcept;
  302. const_iterator cend() const noexcept;
  303. const_reverse_iterator crbegin() const noexcept;
  304. const_reverse_iterator crend() const noexcept;
  305. // capacity:
  306. bool empty() const noexcept;
  307. size_type size() const noexcept;
  308. size_type max_size() const noexcept;
  309. // modifiers:
  310. template <class... Args>
  311. iterator emplace(Args&&... args);
  312. template <class... Args>
  313. iterator emplace_hint(const_iterator position, Args&&... args);
  314. iterator insert(const value_type& v);
  315. iterator insert(value_type&& v);
  316. iterator insert(const_iterator position, const value_type& v);
  317. iterator insert(const_iterator position, value_type&& v);
  318. template <class InputIterator>
  319. void insert(InputIterator first, InputIterator last);
  320. template<container-compatible-range<value_type> R>
  321. void insert_range(R&& rg); // C++23
  322. void insert(initializer_list<value_type> il);
  323. node_type extract(const_iterator position); // C++17
  324. node_type extract(const key_type& x); // C++17
  325. iterator insert(node_type&& nh); // C++17
  326. iterator insert(const_iterator hint, node_type&& nh); // C++17
  327. iterator erase(const_iterator position);
  328. iterator erase(iterator position); // C++14
  329. size_type erase(const key_type& k);
  330. iterator erase(const_iterator first, const_iterator last);
  331. void clear() noexcept;
  332. template<class C2>
  333. void merge(multiset<Key, C2, Allocator>& source); // C++17
  334. template<class C2>
  335. void merge(multiset<Key, C2, Allocator>&& source); // C++17
  336. template<class C2>
  337. void merge(set<Key, C2, Allocator>& source); // C++17
  338. template<class C2>
  339. void merge(set<Key, C2, Allocator>&& source); // C++17
  340. void swap(multiset& s)
  341. noexcept(
  342. __is_nothrow_swappable<key_compare>::value &&
  343. (!allocator_type::propagate_on_container_swap::value ||
  344. __is_nothrow_swappable<allocator_type>::value));
  345. // observers:
  346. allocator_type get_allocator() const noexcept;
  347. key_compare key_comp() const;
  348. value_compare value_comp() const;
  349. // set operations:
  350. iterator find(const key_type& k);
  351. const_iterator find(const key_type& k) const;
  352. template<typename K>
  353. iterator find(const K& x);
  354. template<typename K>
  355. const_iterator find(const K& x) const; // C++14
  356. template<typename K>
  357. size_type count(const K& x) const; // C++14
  358. size_type count(const key_type& k) const;
  359. bool contains(const key_type& x) const; // C++20
  360. template<class K> bool contains(const K& x) const; // C++20
  361. iterator lower_bound(const key_type& k);
  362. const_iterator lower_bound(const key_type& k) const;
  363. template<typename K>
  364. iterator lower_bound(const K& x); // C++14
  365. template<typename K>
  366. const_iterator lower_bound(const K& x) const; // C++14
  367. iterator upper_bound(const key_type& k);
  368. const_iterator upper_bound(const key_type& k) const;
  369. template<typename K>
  370. iterator upper_bound(const K& x); // C++14
  371. template<typename K>
  372. const_iterator upper_bound(const K& x) const; // C++14
  373. pair<iterator,iterator> equal_range(const key_type& k);
  374. pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
  375. template<typename K>
  376. pair<iterator,iterator> equal_range(const K& x); // C++14
  377. template<typename K>
  378. pair<const_iterator,const_iterator> equal_range(const K& x) const; // C++14
  379. };
  380. template <class InputIterator,
  381. class Compare = less<typename iterator_traits<InputIterator>::value_type>,
  382. class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
  383. multiset(InputIterator, InputIterator,
  384. Compare = Compare(), Allocator = Allocator())
  385. -> multiset<typename iterator_traits<InputIterator>::value_type, Compare, Allocator>; // C++17
  386. template<ranges::input_range R, class Compare = less<ranges::range_value_t<R>>,
  387. class Allocator = allocator<ranges::range_value_t<R>>>
  388. multiset(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())
  389. -> multiset<ranges::range_value_t<R>, Compare, Allocator>;
  390. template<class Key, class Compare = less<Key>, class Allocator = allocator<Key>>
  391. multiset(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
  392. -> multiset<Key, Compare, Allocator>; // C++17
  393. template<class InputIterator, class Allocator>
  394. multiset(InputIterator, InputIterator, Allocator)
  395. -> multiset<typename iterator_traits<InputIterator>::value_type,
  396. less<typename iterator_traits<InputIterator>::value_type>, Allocator>; // C++17
  397. template<ranges::input_range R, class Allocator>
  398. multiset(from_range_t, R&&, Allocator)
  399. -> multiset<ranges::range_value_t<R>, less<ranges::range_value_t<R>>, Allocator>;
  400. template<class Key, class Allocator>
  401. multiset(initializer_list<Key>, Allocator) -> multiset<Key, less<Key>, Allocator>; // C++17
  402. template <class Key, class Compare, class Allocator>
  403. bool
  404. operator==(const multiset<Key, Compare, Allocator>& x,
  405. const multiset<Key, Compare, Allocator>& y);
  406. template <class Key, class Compare, class Allocator>
  407. bool
  408. operator< (const multiset<Key, Compare, Allocator>& x,
  409. const multiset<Key, Compare, Allocator>& y); // removed in C++20
  410. template <class Key, class Compare, class Allocator>
  411. bool
  412. operator!=(const multiset<Key, Compare, Allocator>& x,
  413. const multiset<Key, Compare, Allocator>& y); // removed in C++20
  414. template <class Key, class Compare, class Allocator>
  415. bool
  416. operator> (const multiset<Key, Compare, Allocator>& x,
  417. const multiset<Key, Compare, Allocator>& y); // removed in C++20
  418. template <class Key, class Compare, class Allocator>
  419. bool
  420. operator>=(const multiset<Key, Compare, Allocator>& x,
  421. const multiset<Key, Compare, Allocator>& y); // removed in C++20
  422. template <class Key, class Compare, class Allocator>
  423. bool
  424. operator<=(const multiset<Key, Compare, Allocator>& x,
  425. const multiset<Key, Compare, Allocator>& y); // removed in C++20
  426. template<class Key, class Compare, class Allocator>
  427. synth-three-way-result<Key> operator<=>(const multiset<Key, Compare, Allocator>& x,
  428. const multiset<Key, Compare, Allocator>& y); // since C++20
  429. // specialized algorithms:
  430. template <class Key, class Compare, class Allocator>
  431. void
  432. swap(multiset<Key, Compare, Allocator>& x, multiset<Key, Compare, Allocator>& y)
  433. noexcept(noexcept(x.swap(y)));
  434. template <class Key, class Compare, class Allocator, class Predicate>
  435. typename multiset<Key, Compare, Allocator>::size_type
  436. erase_if(multiset<Key, Compare, Allocator>& c, Predicate pred); // C++20
  437. } // std
  438. */
  439. #include <__algorithm/equal.h>
  440. #include <__algorithm/lexicographical_compare.h>
  441. #include <__algorithm/lexicographical_compare_three_way.h>
  442. #include <__assert> // all public C++ headers provide the assertion handler
  443. #include <__availability>
  444. #include <__config>
  445. #include <__functional/is_transparent.h>
  446. #include <__functional/operations.h>
  447. #include <__iterator/erase_if_container.h>
  448. #include <__iterator/iterator_traits.h>
  449. #include <__iterator/ranges_iterator_traits.h>
  450. #include <__iterator/reverse_iterator.h>
  451. #include <__memory/allocator.h>
  452. #include <__memory_resource/polymorphic_allocator.h>
  453. #include <__node_handle>
  454. #include <__ranges/concepts.h>
  455. #include <__ranges/container_compatible_range.h>
  456. #include <__ranges/from_range.h>
  457. #include <__tree>
  458. #include <__type_traits/is_allocator.h>
  459. #include <__utility/forward.h>
  460. #include <version>
  461. // standard-mandated includes
  462. // [iterator.range]
  463. #include <__iterator/access.h>
  464. #include <__iterator/data.h>
  465. #include <__iterator/empty.h>
  466. #include <__iterator/reverse_access.h>
  467. #include <__iterator/size.h>
  468. // [associative.set.syn]
  469. #include <compare>
  470. #include <initializer_list>
  471. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  472. # pragma GCC system_header
  473. #endif
  474. _LIBCPP_BEGIN_NAMESPACE_STD
  475. template <class _Key, class _Compare, class _Allocator>
  476. class multiset;
  477. template <class _Key, class _Compare = less<_Key>,
  478. class _Allocator = allocator<_Key> >
  479. class _LIBCPP_TEMPLATE_VIS set
  480. {
  481. public:
  482. // types:
  483. typedef _Key key_type;
  484. typedef key_type value_type;
  485. typedef __type_identity_t<_Compare> key_compare;
  486. typedef key_compare value_compare;
  487. typedef __type_identity_t<_Allocator> allocator_type;
  488. typedef value_type& reference;
  489. typedef const value_type& const_reference;
  490. static_assert((is_same<typename allocator_type::value_type, value_type>::value),
  491. "Allocator::value_type must be same type as value_type");
  492. private:
  493. typedef __tree<value_type, value_compare, allocator_type> __base;
  494. typedef allocator_traits<allocator_type> __alloc_traits;
  495. static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
  496. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  497. "original allocator");
  498. __base __tree_;
  499. public:
  500. typedef typename __base::pointer pointer;
  501. typedef typename __base::const_pointer const_pointer;
  502. typedef typename __base::size_type size_type;
  503. typedef typename __base::difference_type difference_type;
  504. typedef typename __base::const_iterator iterator;
  505. typedef typename __base::const_iterator const_iterator;
  506. typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
  507. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  508. #if _LIBCPP_STD_VER >= 17
  509. typedef __set_node_handle<typename __base::__node, allocator_type> node_type;
  510. typedef __insert_return_type<iterator, node_type> insert_return_type;
  511. #endif
  512. template <class _Key2, class _Compare2, class _Alloc2>
  513. friend class _LIBCPP_TEMPLATE_VIS set;
  514. template <class _Key2, class _Compare2, class _Alloc2>
  515. friend class _LIBCPP_TEMPLATE_VIS multiset;
  516. _LIBCPP_INLINE_VISIBILITY
  517. set()
  518. _NOEXCEPT_(
  519. is_nothrow_default_constructible<allocator_type>::value &&
  520. is_nothrow_default_constructible<key_compare>::value &&
  521. is_nothrow_copy_constructible<key_compare>::value)
  522. : __tree_(value_compare()) {}
  523. _LIBCPP_INLINE_VISIBILITY
  524. explicit set(const value_compare& __comp)
  525. _NOEXCEPT_(
  526. is_nothrow_default_constructible<allocator_type>::value &&
  527. is_nothrow_copy_constructible<key_compare>::value)
  528. : __tree_(__comp) {}
  529. _LIBCPP_INLINE_VISIBILITY
  530. explicit set(const value_compare& __comp, const allocator_type& __a)
  531. : __tree_(__comp, __a) {}
  532. template <class _InputIterator>
  533. _LIBCPP_INLINE_VISIBILITY
  534. set(_InputIterator __f, _InputIterator __l,
  535. const value_compare& __comp = value_compare())
  536. : __tree_(__comp)
  537. {
  538. insert(__f, __l);
  539. }
  540. template <class _InputIterator>
  541. _LIBCPP_INLINE_VISIBILITY
  542. set(_InputIterator __f, _InputIterator __l, const value_compare& __comp,
  543. const allocator_type& __a)
  544. : __tree_(__comp, __a)
  545. {
  546. insert(__f, __l);
  547. }
  548. #if _LIBCPP_STD_VER >= 23
  549. template <_ContainerCompatibleRange<value_type> _Range>
  550. _LIBCPP_HIDE_FROM_ABI
  551. set(from_range_t, _Range&& __range, const key_compare& __comp = key_compare(),
  552. const allocator_type& __a = allocator_type())
  553. : __tree_(__comp, __a) {
  554. insert_range(std::forward<_Range>(__range));
  555. }
  556. #endif
  557. #if _LIBCPP_STD_VER >= 14
  558. template <class _InputIterator>
  559. _LIBCPP_INLINE_VISIBILITY
  560. set(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
  561. : set(__f, __l, key_compare(), __a) {}
  562. #endif
  563. #if _LIBCPP_STD_VER >= 23
  564. template <_ContainerCompatibleRange<value_type> _Range>
  565. _LIBCPP_HIDE_FROM_ABI
  566. set(from_range_t, _Range&& __range, const allocator_type& __a)
  567. : set(from_range, std::forward<_Range>(__range), key_compare(), __a) {}
  568. #endif
  569. _LIBCPP_INLINE_VISIBILITY
  570. set(const set& __s)
  571. : __tree_(__s.__tree_)
  572. {
  573. insert(__s.begin(), __s.end());
  574. }
  575. _LIBCPP_INLINE_VISIBILITY
  576. set& operator=(const set& __s)
  577. {
  578. __tree_ = __s.__tree_;
  579. return *this;
  580. }
  581. #ifndef _LIBCPP_CXX03_LANG
  582. _LIBCPP_INLINE_VISIBILITY
  583. set(set&& __s)
  584. _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
  585. : __tree_(_VSTD::move(__s.__tree_)) {}
  586. #endif // _LIBCPP_CXX03_LANG
  587. _LIBCPP_INLINE_VISIBILITY
  588. explicit set(const allocator_type& __a)
  589. : __tree_(__a) {}
  590. _LIBCPP_INLINE_VISIBILITY
  591. set(const set& __s, const allocator_type& __a)
  592. : __tree_(__s.__tree_.value_comp(), __a)
  593. {
  594. insert(__s.begin(), __s.end());
  595. }
  596. #ifndef _LIBCPP_CXX03_LANG
  597. _LIBCPP_HIDE_FROM_ABI set(set&& __s, const allocator_type& __a);
  598. _LIBCPP_INLINE_VISIBILITY
  599. set(initializer_list<value_type> __il, const value_compare& __comp = value_compare())
  600. : __tree_(__comp)
  601. {
  602. insert(__il.begin(), __il.end());
  603. }
  604. _LIBCPP_INLINE_VISIBILITY
  605. set(initializer_list<value_type> __il, const value_compare& __comp,
  606. const allocator_type& __a)
  607. : __tree_(__comp, __a)
  608. {
  609. insert(__il.begin(), __il.end());
  610. }
  611. #if _LIBCPP_STD_VER >= 14
  612. _LIBCPP_INLINE_VISIBILITY
  613. set(initializer_list<value_type> __il, const allocator_type& __a)
  614. : set(__il, key_compare(), __a) {}
  615. #endif
  616. _LIBCPP_INLINE_VISIBILITY
  617. set& operator=(initializer_list<value_type> __il)
  618. {
  619. __tree_.__assign_unique(__il.begin(), __il.end());
  620. return *this;
  621. }
  622. _LIBCPP_INLINE_VISIBILITY
  623. set& operator=(set&& __s)
  624. _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
  625. {
  626. __tree_ = _VSTD::move(__s.__tree_);
  627. return *this;
  628. }
  629. #endif // _LIBCPP_CXX03_LANG
  630. _LIBCPP_INLINE_VISIBILITY
  631. ~set() {
  632. static_assert(sizeof(__diagnose_non_const_comparator<_Key, _Compare>()), "");
  633. }
  634. _LIBCPP_INLINE_VISIBILITY
  635. iterator begin() _NOEXCEPT {return __tree_.begin();}
  636. _LIBCPP_INLINE_VISIBILITY
  637. const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
  638. _LIBCPP_INLINE_VISIBILITY
  639. iterator end() _NOEXCEPT {return __tree_.end();}
  640. _LIBCPP_INLINE_VISIBILITY
  641. const_iterator end() const _NOEXCEPT {return __tree_.end();}
  642. _LIBCPP_INLINE_VISIBILITY
  643. reverse_iterator rbegin() _NOEXCEPT
  644. {return reverse_iterator(end());}
  645. _LIBCPP_INLINE_VISIBILITY
  646. const_reverse_iterator rbegin() const _NOEXCEPT
  647. {return const_reverse_iterator(end());}
  648. _LIBCPP_INLINE_VISIBILITY
  649. reverse_iterator rend() _NOEXCEPT
  650. {return reverse_iterator(begin());}
  651. _LIBCPP_INLINE_VISIBILITY
  652. const_reverse_iterator rend() const _NOEXCEPT
  653. {return const_reverse_iterator(begin());}
  654. _LIBCPP_INLINE_VISIBILITY
  655. const_iterator cbegin() const _NOEXCEPT {return begin();}
  656. _LIBCPP_INLINE_VISIBILITY
  657. const_iterator cend() const _NOEXCEPT {return end();}
  658. _LIBCPP_INLINE_VISIBILITY
  659. const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
  660. _LIBCPP_INLINE_VISIBILITY
  661. const_reverse_iterator crend() const _NOEXCEPT {return rend();}
  662. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  663. bool empty() const _NOEXCEPT {return __tree_.size() == 0;}
  664. _LIBCPP_INLINE_VISIBILITY
  665. size_type size() const _NOEXCEPT {return __tree_.size();}
  666. _LIBCPP_INLINE_VISIBILITY
  667. size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
  668. // modifiers:
  669. #ifndef _LIBCPP_CXX03_LANG
  670. template <class... _Args>
  671. _LIBCPP_INLINE_VISIBILITY
  672. pair<iterator, bool> emplace(_Args&&... __args)
  673. {return __tree_.__emplace_unique(_VSTD::forward<_Args>(__args)...);}
  674. template <class... _Args>
  675. _LIBCPP_INLINE_VISIBILITY
  676. iterator emplace_hint(const_iterator __p, _Args&&... __args)
  677. {return __tree_.__emplace_hint_unique(__p, _VSTD::forward<_Args>(__args)...);}
  678. #endif // _LIBCPP_CXX03_LANG
  679. _LIBCPP_INLINE_VISIBILITY
  680. pair<iterator,bool> insert(const value_type& __v)
  681. {return __tree_.__insert_unique(__v);}
  682. _LIBCPP_INLINE_VISIBILITY
  683. iterator insert(const_iterator __p, const value_type& __v)
  684. {return __tree_.__insert_unique(__p, __v);}
  685. template <class _InputIterator>
  686. _LIBCPP_INLINE_VISIBILITY
  687. void insert(_InputIterator __f, _InputIterator __l)
  688. {
  689. for (const_iterator __e = cend(); __f != __l; ++__f)
  690. __tree_.__insert_unique(__e, *__f);
  691. }
  692. #if _LIBCPP_STD_VER >= 23
  693. template <_ContainerCompatibleRange<value_type> _Range>
  694. _LIBCPP_HIDE_FROM_ABI
  695. void insert_range(_Range&& __range) {
  696. const_iterator __end = cend();
  697. for (auto&& __element : __range) {
  698. __tree_.__insert_unique(__end, std::forward<decltype(__element)>(__element));
  699. }
  700. }
  701. #endif
  702. #ifndef _LIBCPP_CXX03_LANG
  703. _LIBCPP_INLINE_VISIBILITY
  704. pair<iterator,bool> insert(value_type&& __v)
  705. {return __tree_.__insert_unique(_VSTD::move(__v));}
  706. _LIBCPP_INLINE_VISIBILITY
  707. iterator insert(const_iterator __p, value_type&& __v)
  708. {return __tree_.__insert_unique(__p, _VSTD::move(__v));}
  709. _LIBCPP_INLINE_VISIBILITY
  710. void insert(initializer_list<value_type> __il)
  711. {insert(__il.begin(), __il.end());}
  712. #endif // _LIBCPP_CXX03_LANG
  713. _LIBCPP_INLINE_VISIBILITY
  714. iterator erase(const_iterator __p) {return __tree_.erase(__p);}
  715. _LIBCPP_INLINE_VISIBILITY
  716. size_type erase(const key_type& __k)
  717. {return __tree_.__erase_unique(__k);}
  718. _LIBCPP_INLINE_VISIBILITY
  719. iterator erase(const_iterator __f, const_iterator __l)
  720. {return __tree_.erase(__f, __l);}
  721. _LIBCPP_REINITIALIZES_OBJECT _LIBCPP_INLINE_VISIBILITY
  722. void clear() _NOEXCEPT {__tree_.clear();}
  723. #if _LIBCPP_STD_VER >= 17
  724. _LIBCPP_INLINE_VISIBILITY
  725. insert_return_type insert(node_type&& __nh)
  726. {
  727. _LIBCPP_ASSERT_UNCATEGORIZED(__nh.empty() || __nh.get_allocator() == get_allocator(),
  728. "node_type with incompatible allocator passed to set::insert()");
  729. return __tree_.template __node_handle_insert_unique<
  730. node_type, insert_return_type>(_VSTD::move(__nh));
  731. }
  732. _LIBCPP_INLINE_VISIBILITY
  733. iterator insert(const_iterator __hint, node_type&& __nh)
  734. {
  735. _LIBCPP_ASSERT_UNCATEGORIZED(__nh.empty() || __nh.get_allocator() == get_allocator(),
  736. "node_type with incompatible allocator passed to set::insert()");
  737. return __tree_.template __node_handle_insert_unique<node_type>(
  738. __hint, _VSTD::move(__nh));
  739. }
  740. _LIBCPP_INLINE_VISIBILITY
  741. node_type extract(key_type const& __key)
  742. {
  743. return __tree_.template __node_handle_extract<node_type>(__key);
  744. }
  745. _LIBCPP_INLINE_VISIBILITY
  746. node_type extract(const_iterator __it)
  747. {
  748. return __tree_.template __node_handle_extract<node_type>(__it);
  749. }
  750. template <class _Compare2>
  751. _LIBCPP_INLINE_VISIBILITY
  752. void merge(set<key_type, _Compare2, allocator_type>& __source)
  753. {
  754. _LIBCPP_ASSERT_UNCATEGORIZED(__source.get_allocator() == get_allocator(),
  755. "merging container with incompatible allocator");
  756. __tree_.__node_handle_merge_unique(__source.__tree_);
  757. }
  758. template <class _Compare2>
  759. _LIBCPP_INLINE_VISIBILITY
  760. void merge(set<key_type, _Compare2, allocator_type>&& __source)
  761. {
  762. _LIBCPP_ASSERT_UNCATEGORIZED(__source.get_allocator() == get_allocator(),
  763. "merging container with incompatible allocator");
  764. __tree_.__node_handle_merge_unique(__source.__tree_);
  765. }
  766. template <class _Compare2>
  767. _LIBCPP_INLINE_VISIBILITY
  768. void merge(multiset<key_type, _Compare2, allocator_type>& __source)
  769. {
  770. _LIBCPP_ASSERT_UNCATEGORIZED(__source.get_allocator() == get_allocator(),
  771. "merging container with incompatible allocator");
  772. __tree_.__node_handle_merge_unique(__source.__tree_);
  773. }
  774. template <class _Compare2>
  775. _LIBCPP_INLINE_VISIBILITY
  776. void merge(multiset<key_type, _Compare2, allocator_type>&& __source)
  777. {
  778. _LIBCPP_ASSERT_UNCATEGORIZED(__source.get_allocator() == get_allocator(),
  779. "merging container with incompatible allocator");
  780. __tree_.__node_handle_merge_unique(__source.__tree_);
  781. }
  782. #endif
  783. _LIBCPP_INLINE_VISIBILITY
  784. void swap(set& __s) _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
  785. {__tree_.swap(__s.__tree_);}
  786. _LIBCPP_INLINE_VISIBILITY
  787. allocator_type get_allocator() const _NOEXCEPT {return __tree_.__alloc();}
  788. _LIBCPP_INLINE_VISIBILITY
  789. key_compare key_comp() const {return __tree_.value_comp();}
  790. _LIBCPP_INLINE_VISIBILITY
  791. value_compare value_comp() const {return __tree_.value_comp();}
  792. // set operations:
  793. _LIBCPP_INLINE_VISIBILITY
  794. iterator find(const key_type& __k) {return __tree_.find(__k);}
  795. _LIBCPP_INLINE_VISIBILITY
  796. const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
  797. #if _LIBCPP_STD_VER >= 14
  798. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  799. _LIBCPP_INLINE_VISIBILITY
  800. iterator
  801. find(const _K2& __k) {return __tree_.find(__k);}
  802. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  803. _LIBCPP_INLINE_VISIBILITY
  804. const_iterator
  805. find(const _K2& __k) const {return __tree_.find(__k);}
  806. #endif
  807. _LIBCPP_INLINE_VISIBILITY
  808. size_type count(const key_type& __k) const
  809. {return __tree_.__count_unique(__k);}
  810. #if _LIBCPP_STD_VER >= 14
  811. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  812. _LIBCPP_INLINE_VISIBILITY
  813. size_type
  814. count(const _K2& __k) const {return __tree_.__count_multi(__k);}
  815. #endif
  816. #if _LIBCPP_STD_VER >= 20
  817. _LIBCPP_INLINE_VISIBILITY
  818. bool contains(const key_type& __k) const {return find(__k) != end();}
  819. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  820. _LIBCPP_INLINE_VISIBILITY
  821. bool
  822. contains(const _K2& __k) const { return find(__k) != end(); }
  823. #endif // _LIBCPP_STD_VER >= 20
  824. _LIBCPP_INLINE_VISIBILITY
  825. iterator lower_bound(const key_type& __k)
  826. {return __tree_.lower_bound(__k);}
  827. _LIBCPP_INLINE_VISIBILITY
  828. const_iterator lower_bound(const key_type& __k) const
  829. {return __tree_.lower_bound(__k);}
  830. #if _LIBCPP_STD_VER >= 14
  831. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  832. _LIBCPP_INLINE_VISIBILITY
  833. iterator
  834. lower_bound(const _K2& __k) {return __tree_.lower_bound(__k);}
  835. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  836. _LIBCPP_INLINE_VISIBILITY
  837. const_iterator
  838. lower_bound(const _K2& __k) const {return __tree_.lower_bound(__k);}
  839. #endif
  840. _LIBCPP_INLINE_VISIBILITY
  841. iterator upper_bound(const key_type& __k)
  842. {return __tree_.upper_bound(__k);}
  843. _LIBCPP_INLINE_VISIBILITY
  844. const_iterator upper_bound(const key_type& __k) const
  845. {return __tree_.upper_bound(__k);}
  846. #if _LIBCPP_STD_VER >= 14
  847. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  848. _LIBCPP_INLINE_VISIBILITY
  849. iterator
  850. upper_bound(const _K2& __k) {return __tree_.upper_bound(__k);}
  851. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  852. _LIBCPP_INLINE_VISIBILITY
  853. const_iterator
  854. upper_bound(const _K2& __k) const {return __tree_.upper_bound(__k);}
  855. #endif
  856. _LIBCPP_INLINE_VISIBILITY
  857. pair<iterator,iterator> equal_range(const key_type& __k)
  858. {return __tree_.__equal_range_unique(__k);}
  859. _LIBCPP_INLINE_VISIBILITY
  860. pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
  861. {return __tree_.__equal_range_unique(__k);}
  862. #if _LIBCPP_STD_VER >= 14
  863. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  864. _LIBCPP_INLINE_VISIBILITY
  865. pair<iterator,iterator>
  866. equal_range(const _K2& __k) {return __tree_.__equal_range_multi(__k);}
  867. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  868. _LIBCPP_INLINE_VISIBILITY
  869. pair<const_iterator,const_iterator>
  870. equal_range(const _K2& __k) const {return __tree_.__equal_range_multi(__k);}
  871. #endif
  872. };
  873. #if _LIBCPP_STD_VER >= 17
  874. template<class _InputIterator,
  875. class _Compare = less<__iter_value_type<_InputIterator>>,
  876. class _Allocator = allocator<__iter_value_type<_InputIterator>>,
  877. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value, void>,
  878. class = enable_if_t<__is_allocator<_Allocator>::value, void>,
  879. class = enable_if_t<!__is_allocator<_Compare>::value, void>>
  880. set(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
  881. -> set<__iter_value_type<_InputIterator>, _Compare, _Allocator>;
  882. #if _LIBCPP_STD_VER >= 23
  883. template <ranges::input_range _Range, class _Compare = less<ranges::range_value_t<_Range>>,
  884. class _Allocator = allocator<ranges::range_value_t<_Range>>,
  885. class = enable_if_t<__is_allocator<_Allocator>::value, void>,
  886. class = enable_if_t<!__is_allocator<_Compare>::value, void>>
  887. set(from_range_t, _Range&&, _Compare = _Compare(), _Allocator = _Allocator())
  888. -> set<ranges::range_value_t<_Range>, _Compare, _Allocator>;
  889. #endif
  890. template<class _Key, class _Compare = less<_Key>,
  891. class _Allocator = allocator<_Key>,
  892. class = enable_if_t<!__is_allocator<_Compare>::value, void>,
  893. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  894. set(initializer_list<_Key>, _Compare = _Compare(), _Allocator = _Allocator())
  895. -> set<_Key, _Compare, _Allocator>;
  896. template<class _InputIterator, class _Allocator,
  897. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value, void>,
  898. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  899. set(_InputIterator, _InputIterator, _Allocator)
  900. -> set<__iter_value_type<_InputIterator>,
  901. less<__iter_value_type<_InputIterator>>, _Allocator>;
  902. #if _LIBCPP_STD_VER >= 23
  903. template <ranges::input_range _Range, class _Allocator,
  904. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  905. set(from_range_t, _Range&&, _Allocator)
  906. -> set<ranges::range_value_t<_Range>, less<ranges::range_value_t<_Range>>, _Allocator>;
  907. #endif
  908. template<class _Key, class _Allocator,
  909. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  910. set(initializer_list<_Key>, _Allocator)
  911. -> set<_Key, less<_Key>, _Allocator>;
  912. #endif
  913. #ifndef _LIBCPP_CXX03_LANG
  914. template <class _Key, class _Compare, class _Allocator>
  915. set<_Key, _Compare, _Allocator>::set(set&& __s, const allocator_type& __a)
  916. : __tree_(_VSTD::move(__s.__tree_), __a)
  917. {
  918. if (__a != __s.get_allocator())
  919. {
  920. const_iterator __e = cend();
  921. while (!__s.empty())
  922. insert(__e, _VSTD::move(__s.__tree_.remove(__s.begin())->__value_));
  923. }
  924. }
  925. #endif // _LIBCPP_CXX03_LANG
  926. template <class _Key, class _Compare, class _Allocator>
  927. inline _LIBCPP_INLINE_VISIBILITY
  928. bool
  929. operator==(const set<_Key, _Compare, _Allocator>& __x,
  930. const set<_Key, _Compare, _Allocator>& __y)
  931. {
  932. return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
  933. }
  934. #if _LIBCPP_STD_VER <= 17
  935. template <class _Key, class _Compare, class _Allocator>
  936. inline _LIBCPP_INLINE_VISIBILITY
  937. bool
  938. operator< (const set<_Key, _Compare, _Allocator>& __x,
  939. const set<_Key, _Compare, _Allocator>& __y)
  940. {
  941. return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  942. }
  943. template <class _Key, class _Compare, class _Allocator>
  944. inline _LIBCPP_INLINE_VISIBILITY
  945. bool
  946. operator!=(const set<_Key, _Compare, _Allocator>& __x,
  947. const set<_Key, _Compare, _Allocator>& __y)
  948. {
  949. return !(__x == __y);
  950. }
  951. template <class _Key, class _Compare, class _Allocator>
  952. inline _LIBCPP_INLINE_VISIBILITY
  953. bool
  954. operator> (const set<_Key, _Compare, _Allocator>& __x,
  955. const set<_Key, _Compare, _Allocator>& __y)
  956. {
  957. return __y < __x;
  958. }
  959. template <class _Key, class _Compare, class _Allocator>
  960. inline _LIBCPP_INLINE_VISIBILITY
  961. bool
  962. operator>=(const set<_Key, _Compare, _Allocator>& __x,
  963. const set<_Key, _Compare, _Allocator>& __y)
  964. {
  965. return !(__x < __y);
  966. }
  967. template <class _Key, class _Compare, class _Allocator>
  968. inline _LIBCPP_INLINE_VISIBILITY
  969. bool
  970. operator<=(const set<_Key, _Compare, _Allocator>& __x,
  971. const set<_Key, _Compare, _Allocator>& __y)
  972. {
  973. return !(__y < __x);
  974. }
  975. #else // _LIBCPP_STD_VER <= 17
  976. template <class _Key, class _Allocator>
  977. _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Key>
  978. operator<=>(const set<_Key, _Allocator>& __x, const set<_Key, _Allocator>& __y) {
  979. return std::lexicographical_compare_three_way(
  980. __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Key, _Key>);
  981. }
  982. #endif // _LIBCPP_STD_VER <= 17
  983. // specialized algorithms:
  984. template <class _Key, class _Compare, class _Allocator>
  985. inline _LIBCPP_INLINE_VISIBILITY
  986. void
  987. swap(set<_Key, _Compare, _Allocator>& __x,
  988. set<_Key, _Compare, _Allocator>& __y)
  989. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  990. {
  991. __x.swap(__y);
  992. }
  993. #if _LIBCPP_STD_VER >= 20
  994. template <class _Key, class _Compare, class _Allocator, class _Predicate>
  995. inline _LIBCPP_INLINE_VISIBILITY
  996. typename set<_Key, _Compare, _Allocator>::size_type
  997. erase_if(set<_Key, _Compare, _Allocator>& __c, _Predicate __pred) {
  998. return _VSTD::__libcpp_erase_if_container(__c, __pred);
  999. }
  1000. #endif
  1001. template <class _Key, class _Compare = less<_Key>,
  1002. class _Allocator = allocator<_Key> >
  1003. class _LIBCPP_TEMPLATE_VIS multiset
  1004. {
  1005. public:
  1006. // types:
  1007. typedef _Key key_type;
  1008. typedef key_type value_type;
  1009. typedef __type_identity_t<_Compare> key_compare;
  1010. typedef key_compare value_compare;
  1011. typedef __type_identity_t<_Allocator> allocator_type;
  1012. typedef value_type& reference;
  1013. typedef const value_type& const_reference;
  1014. static_assert((is_same<typename allocator_type::value_type, value_type>::value),
  1015. "Allocator::value_type must be same type as value_type");
  1016. private:
  1017. typedef __tree<value_type, value_compare, allocator_type> __base;
  1018. typedef allocator_traits<allocator_type> __alloc_traits;
  1019. static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
  1020. "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
  1021. "original allocator");
  1022. __base __tree_;
  1023. public:
  1024. typedef typename __base::pointer pointer;
  1025. typedef typename __base::const_pointer const_pointer;
  1026. typedef typename __base::size_type size_type;
  1027. typedef typename __base::difference_type difference_type;
  1028. typedef typename __base::const_iterator iterator;
  1029. typedef typename __base::const_iterator const_iterator;
  1030. typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
  1031. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  1032. #if _LIBCPP_STD_VER >= 17
  1033. typedef __set_node_handle<typename __base::__node, allocator_type> node_type;
  1034. #endif
  1035. template <class _Key2, class _Compare2, class _Alloc2>
  1036. friend class _LIBCPP_TEMPLATE_VIS set;
  1037. template <class _Key2, class _Compare2, class _Alloc2>
  1038. friend class _LIBCPP_TEMPLATE_VIS multiset;
  1039. // construct/copy/destroy:
  1040. _LIBCPP_INLINE_VISIBILITY
  1041. multiset()
  1042. _NOEXCEPT_(
  1043. is_nothrow_default_constructible<allocator_type>::value &&
  1044. is_nothrow_default_constructible<key_compare>::value &&
  1045. is_nothrow_copy_constructible<key_compare>::value)
  1046. : __tree_(value_compare()) {}
  1047. _LIBCPP_INLINE_VISIBILITY
  1048. explicit multiset(const value_compare& __comp)
  1049. _NOEXCEPT_(
  1050. is_nothrow_default_constructible<allocator_type>::value &&
  1051. is_nothrow_copy_constructible<key_compare>::value)
  1052. : __tree_(__comp) {}
  1053. _LIBCPP_INLINE_VISIBILITY
  1054. explicit multiset(const value_compare& __comp, const allocator_type& __a)
  1055. : __tree_(__comp, __a) {}
  1056. template <class _InputIterator>
  1057. _LIBCPP_INLINE_VISIBILITY
  1058. multiset(_InputIterator __f, _InputIterator __l,
  1059. const value_compare& __comp = value_compare())
  1060. : __tree_(__comp)
  1061. {
  1062. insert(__f, __l);
  1063. }
  1064. #if _LIBCPP_STD_VER >= 14
  1065. template <class _InputIterator>
  1066. _LIBCPP_INLINE_VISIBILITY
  1067. multiset(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
  1068. : multiset(__f, __l, key_compare(), __a) {}
  1069. #endif
  1070. template <class _InputIterator>
  1071. _LIBCPP_INLINE_VISIBILITY
  1072. multiset(_InputIterator __f, _InputIterator __l,
  1073. const value_compare& __comp, const allocator_type& __a)
  1074. : __tree_(__comp, __a)
  1075. {
  1076. insert(__f, __l);
  1077. }
  1078. #if _LIBCPP_STD_VER >= 23
  1079. template <_ContainerCompatibleRange<value_type> _Range>
  1080. _LIBCPP_HIDE_FROM_ABI
  1081. multiset(from_range_t, _Range&& __range, const key_compare& __comp = key_compare(),
  1082. const allocator_type& __a = allocator_type())
  1083. : __tree_(__comp, __a) {
  1084. insert_range(std::forward<_Range>(__range));
  1085. }
  1086. template <_ContainerCompatibleRange<value_type> _Range>
  1087. _LIBCPP_HIDE_FROM_ABI
  1088. multiset(from_range_t, _Range&& __range, const allocator_type& __a)
  1089. : multiset(from_range, std::forward<_Range>(__range), key_compare(), __a) {}
  1090. #endif
  1091. _LIBCPP_INLINE_VISIBILITY
  1092. multiset(const multiset& __s)
  1093. : __tree_(__s.__tree_.value_comp(),
  1094. __alloc_traits::select_on_container_copy_construction(__s.__tree_.__alloc()))
  1095. {
  1096. insert(__s.begin(), __s.end());
  1097. }
  1098. _LIBCPP_INLINE_VISIBILITY
  1099. multiset& operator=(const multiset& __s)
  1100. {
  1101. __tree_ = __s.__tree_;
  1102. return *this;
  1103. }
  1104. #ifndef _LIBCPP_CXX03_LANG
  1105. _LIBCPP_INLINE_VISIBILITY
  1106. multiset(multiset&& __s)
  1107. _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
  1108. : __tree_(_VSTD::move(__s.__tree_)) {}
  1109. _LIBCPP_HIDE_FROM_ABI multiset(multiset&& __s, const allocator_type& __a);
  1110. #endif // _LIBCPP_CXX03_LANG
  1111. _LIBCPP_INLINE_VISIBILITY
  1112. explicit multiset(const allocator_type& __a)
  1113. : __tree_(__a) {}
  1114. _LIBCPP_INLINE_VISIBILITY
  1115. multiset(const multiset& __s, const allocator_type& __a)
  1116. : __tree_(__s.__tree_.value_comp(), __a)
  1117. {
  1118. insert(__s.begin(), __s.end());
  1119. }
  1120. #ifndef _LIBCPP_CXX03_LANG
  1121. _LIBCPP_INLINE_VISIBILITY
  1122. multiset(initializer_list<value_type> __il, const value_compare& __comp = value_compare())
  1123. : __tree_(__comp)
  1124. {
  1125. insert(__il.begin(), __il.end());
  1126. }
  1127. _LIBCPP_INLINE_VISIBILITY
  1128. multiset(initializer_list<value_type> __il, const value_compare& __comp,
  1129. const allocator_type& __a)
  1130. : __tree_(__comp, __a)
  1131. {
  1132. insert(__il.begin(), __il.end());
  1133. }
  1134. #if _LIBCPP_STD_VER >= 14
  1135. _LIBCPP_INLINE_VISIBILITY
  1136. multiset(initializer_list<value_type> __il, const allocator_type& __a)
  1137. : multiset(__il, key_compare(), __a) {}
  1138. #endif
  1139. _LIBCPP_INLINE_VISIBILITY
  1140. multiset& operator=(initializer_list<value_type> __il)
  1141. {
  1142. __tree_.__assign_multi(__il.begin(), __il.end());
  1143. return *this;
  1144. }
  1145. _LIBCPP_INLINE_VISIBILITY
  1146. multiset& operator=(multiset&& __s)
  1147. _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
  1148. {
  1149. __tree_ = _VSTD::move(__s.__tree_);
  1150. return *this;
  1151. }
  1152. #endif // _LIBCPP_CXX03_LANG
  1153. _LIBCPP_INLINE_VISIBILITY
  1154. ~multiset() {
  1155. static_assert(sizeof(__diagnose_non_const_comparator<_Key, _Compare>()), "");
  1156. }
  1157. _LIBCPP_INLINE_VISIBILITY
  1158. iterator begin() _NOEXCEPT {return __tree_.begin();}
  1159. _LIBCPP_INLINE_VISIBILITY
  1160. const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
  1161. _LIBCPP_INLINE_VISIBILITY
  1162. iterator end() _NOEXCEPT {return __tree_.end();}
  1163. _LIBCPP_INLINE_VISIBILITY
  1164. const_iterator end() const _NOEXCEPT {return __tree_.end();}
  1165. _LIBCPP_INLINE_VISIBILITY
  1166. reverse_iterator rbegin() _NOEXCEPT
  1167. {return reverse_iterator(end());}
  1168. _LIBCPP_INLINE_VISIBILITY
  1169. const_reverse_iterator rbegin() const _NOEXCEPT
  1170. {return const_reverse_iterator(end());}
  1171. _LIBCPP_INLINE_VISIBILITY
  1172. reverse_iterator rend() _NOEXCEPT
  1173. {return reverse_iterator(begin());}
  1174. _LIBCPP_INLINE_VISIBILITY
  1175. const_reverse_iterator rend() const _NOEXCEPT
  1176. {return const_reverse_iterator(begin());}
  1177. _LIBCPP_INLINE_VISIBILITY
  1178. const_iterator cbegin() const _NOEXCEPT {return begin();}
  1179. _LIBCPP_INLINE_VISIBILITY
  1180. const_iterator cend() const _NOEXCEPT {return end();}
  1181. _LIBCPP_INLINE_VISIBILITY
  1182. const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
  1183. _LIBCPP_INLINE_VISIBILITY
  1184. const_reverse_iterator crend() const _NOEXCEPT {return rend();}
  1185. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  1186. bool empty() const _NOEXCEPT {return __tree_.size() == 0;}
  1187. _LIBCPP_INLINE_VISIBILITY
  1188. size_type size() const _NOEXCEPT {return __tree_.size();}
  1189. _LIBCPP_INLINE_VISIBILITY
  1190. size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
  1191. // modifiers:
  1192. #ifndef _LIBCPP_CXX03_LANG
  1193. template <class... _Args>
  1194. _LIBCPP_INLINE_VISIBILITY
  1195. iterator emplace(_Args&&... __args)
  1196. {return __tree_.__emplace_multi(_VSTD::forward<_Args>(__args)...);}
  1197. template <class... _Args>
  1198. _LIBCPP_INLINE_VISIBILITY
  1199. iterator emplace_hint(const_iterator __p, _Args&&... __args)
  1200. {return __tree_.__emplace_hint_multi(__p, _VSTD::forward<_Args>(__args)...);}
  1201. #endif // _LIBCPP_CXX03_LANG
  1202. _LIBCPP_INLINE_VISIBILITY
  1203. iterator insert(const value_type& __v)
  1204. {return __tree_.__insert_multi(__v);}
  1205. _LIBCPP_INLINE_VISIBILITY
  1206. iterator insert(const_iterator __p, const value_type& __v)
  1207. {return __tree_.__insert_multi(__p, __v);}
  1208. template <class _InputIterator>
  1209. _LIBCPP_INLINE_VISIBILITY
  1210. void insert(_InputIterator __f, _InputIterator __l)
  1211. {
  1212. for (const_iterator __e = cend(); __f != __l; ++__f)
  1213. __tree_.__insert_multi(__e, *__f);
  1214. }
  1215. #if _LIBCPP_STD_VER >= 23
  1216. template <_ContainerCompatibleRange<value_type> _Range>
  1217. _LIBCPP_HIDE_FROM_ABI
  1218. void insert_range(_Range&& __range) {
  1219. const_iterator __end = cend();
  1220. for (auto&& __element : __range) {
  1221. __tree_.__insert_multi(__end, std::forward<decltype(__element)>(__element));
  1222. }
  1223. }
  1224. #endif
  1225. #ifndef _LIBCPP_CXX03_LANG
  1226. _LIBCPP_INLINE_VISIBILITY
  1227. iterator insert(value_type&& __v)
  1228. {return __tree_.__insert_multi(_VSTD::move(__v));}
  1229. _LIBCPP_INLINE_VISIBILITY
  1230. iterator insert(const_iterator __p, value_type&& __v)
  1231. {return __tree_.__insert_multi(__p, _VSTD::move(__v));}
  1232. _LIBCPP_INLINE_VISIBILITY
  1233. void insert(initializer_list<value_type> __il)
  1234. {insert(__il.begin(), __il.end());}
  1235. #endif // _LIBCPP_CXX03_LANG
  1236. _LIBCPP_INLINE_VISIBILITY
  1237. iterator erase(const_iterator __p) {return __tree_.erase(__p);}
  1238. _LIBCPP_INLINE_VISIBILITY
  1239. size_type erase(const key_type& __k) {return __tree_.__erase_multi(__k);}
  1240. _LIBCPP_INLINE_VISIBILITY
  1241. iterator erase(const_iterator __f, const_iterator __l)
  1242. {return __tree_.erase(__f, __l);}
  1243. _LIBCPP_REINITIALIZES_OBJECT _LIBCPP_INLINE_VISIBILITY
  1244. void clear() _NOEXCEPT {__tree_.clear();}
  1245. #if _LIBCPP_STD_VER >= 17
  1246. _LIBCPP_INLINE_VISIBILITY
  1247. iterator insert(node_type&& __nh)
  1248. {
  1249. _LIBCPP_ASSERT_UNCATEGORIZED(__nh.empty() || __nh.get_allocator() == get_allocator(),
  1250. "node_type with incompatible allocator passed to multiset::insert()");
  1251. return __tree_.template __node_handle_insert_multi<node_type>(
  1252. _VSTD::move(__nh));
  1253. }
  1254. _LIBCPP_INLINE_VISIBILITY
  1255. iterator insert(const_iterator __hint, node_type&& __nh)
  1256. {
  1257. _LIBCPP_ASSERT_UNCATEGORIZED(__nh.empty() || __nh.get_allocator() == get_allocator(),
  1258. "node_type with incompatible allocator passed to multiset::insert()");
  1259. return __tree_.template __node_handle_insert_multi<node_type>(
  1260. __hint, _VSTD::move(__nh));
  1261. }
  1262. _LIBCPP_INLINE_VISIBILITY
  1263. node_type extract(key_type const& __key)
  1264. {
  1265. return __tree_.template __node_handle_extract<node_type>(__key);
  1266. }
  1267. _LIBCPP_INLINE_VISIBILITY
  1268. node_type extract(const_iterator __it)
  1269. {
  1270. return __tree_.template __node_handle_extract<node_type>(__it);
  1271. }
  1272. template <class _Compare2>
  1273. _LIBCPP_INLINE_VISIBILITY
  1274. void merge(multiset<key_type, _Compare2, allocator_type>& __source)
  1275. {
  1276. _LIBCPP_ASSERT_UNCATEGORIZED(__source.get_allocator() == get_allocator(),
  1277. "merging container with incompatible allocator");
  1278. __tree_.__node_handle_merge_multi(__source.__tree_);
  1279. }
  1280. template <class _Compare2>
  1281. _LIBCPP_INLINE_VISIBILITY
  1282. void merge(multiset<key_type, _Compare2, allocator_type>&& __source)
  1283. {
  1284. _LIBCPP_ASSERT_UNCATEGORIZED(__source.get_allocator() == get_allocator(),
  1285. "merging container with incompatible allocator");
  1286. __tree_.__node_handle_merge_multi(__source.__tree_);
  1287. }
  1288. template <class _Compare2>
  1289. _LIBCPP_INLINE_VISIBILITY
  1290. void merge(set<key_type, _Compare2, allocator_type>& __source)
  1291. {
  1292. _LIBCPP_ASSERT_UNCATEGORIZED(__source.get_allocator() == get_allocator(),
  1293. "merging container with incompatible allocator");
  1294. __tree_.__node_handle_merge_multi(__source.__tree_);
  1295. }
  1296. template <class _Compare2>
  1297. _LIBCPP_INLINE_VISIBILITY
  1298. void merge(set<key_type, _Compare2, allocator_type>&& __source)
  1299. {
  1300. _LIBCPP_ASSERT_UNCATEGORIZED(__source.get_allocator() == get_allocator(),
  1301. "merging container with incompatible allocator");
  1302. __tree_.__node_handle_merge_multi(__source.__tree_);
  1303. }
  1304. #endif
  1305. _LIBCPP_INLINE_VISIBILITY
  1306. void swap(multiset& __s)
  1307. _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
  1308. {__tree_.swap(__s.__tree_);}
  1309. _LIBCPP_INLINE_VISIBILITY
  1310. allocator_type get_allocator() const _NOEXCEPT {return __tree_.__alloc();}
  1311. _LIBCPP_INLINE_VISIBILITY
  1312. key_compare key_comp() const {return __tree_.value_comp();}
  1313. _LIBCPP_INLINE_VISIBILITY
  1314. value_compare value_comp() const {return __tree_.value_comp();}
  1315. // set operations:
  1316. _LIBCPP_INLINE_VISIBILITY
  1317. iterator find(const key_type& __k) {return __tree_.find(__k);}
  1318. _LIBCPP_INLINE_VISIBILITY
  1319. const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
  1320. #if _LIBCPP_STD_VER >= 14
  1321. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1322. _LIBCPP_INLINE_VISIBILITY
  1323. iterator
  1324. find(const _K2& __k) {return __tree_.find(__k);}
  1325. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1326. _LIBCPP_INLINE_VISIBILITY
  1327. const_iterator
  1328. find(const _K2& __k) const {return __tree_.find(__k);}
  1329. #endif
  1330. _LIBCPP_INLINE_VISIBILITY
  1331. size_type count(const key_type& __k) const
  1332. {return __tree_.__count_multi(__k);}
  1333. #if _LIBCPP_STD_VER >= 14
  1334. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1335. _LIBCPP_INLINE_VISIBILITY
  1336. size_type
  1337. count(const _K2& __k) const {return __tree_.__count_multi(__k);}
  1338. #endif
  1339. #if _LIBCPP_STD_VER >= 20
  1340. _LIBCPP_INLINE_VISIBILITY
  1341. bool contains(const key_type& __k) const {return find(__k) != end();}
  1342. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1343. _LIBCPP_INLINE_VISIBILITY
  1344. bool
  1345. contains(const _K2& __k) const { return find(__k) != end(); }
  1346. #endif // _LIBCPP_STD_VER >= 20
  1347. _LIBCPP_INLINE_VISIBILITY
  1348. iterator lower_bound(const key_type& __k)
  1349. {return __tree_.lower_bound(__k);}
  1350. _LIBCPP_INLINE_VISIBILITY
  1351. const_iterator lower_bound(const key_type& __k) const
  1352. {return __tree_.lower_bound(__k);}
  1353. #if _LIBCPP_STD_VER >= 14
  1354. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1355. _LIBCPP_INLINE_VISIBILITY
  1356. iterator
  1357. lower_bound(const _K2& __k) {return __tree_.lower_bound(__k);}
  1358. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1359. _LIBCPP_INLINE_VISIBILITY
  1360. const_iterator
  1361. lower_bound(const _K2& __k) const {return __tree_.lower_bound(__k);}
  1362. #endif
  1363. _LIBCPP_INLINE_VISIBILITY
  1364. iterator upper_bound(const key_type& __k)
  1365. {return __tree_.upper_bound(__k);}
  1366. _LIBCPP_INLINE_VISIBILITY
  1367. const_iterator upper_bound(const key_type& __k) const
  1368. {return __tree_.upper_bound(__k);}
  1369. #if _LIBCPP_STD_VER >= 14
  1370. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1371. _LIBCPP_INLINE_VISIBILITY
  1372. iterator
  1373. upper_bound(const _K2& __k) {return __tree_.upper_bound(__k);}
  1374. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1375. _LIBCPP_INLINE_VISIBILITY
  1376. const_iterator
  1377. upper_bound(const _K2& __k) const {return __tree_.upper_bound(__k);}
  1378. #endif
  1379. _LIBCPP_INLINE_VISIBILITY
  1380. pair<iterator,iterator> equal_range(const key_type& __k)
  1381. {return __tree_.__equal_range_multi(__k);}
  1382. _LIBCPP_INLINE_VISIBILITY
  1383. pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
  1384. {return __tree_.__equal_range_multi(__k);}
  1385. #if _LIBCPP_STD_VER >= 14
  1386. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1387. _LIBCPP_INLINE_VISIBILITY
  1388. pair<iterator,iterator>
  1389. equal_range(const _K2& __k) {return __tree_.__equal_range_multi(__k);}
  1390. template <typename _K2, enable_if_t<__is_transparent<_Compare, _K2>::value, int> = 0>
  1391. _LIBCPP_INLINE_VISIBILITY
  1392. pair<const_iterator,const_iterator>
  1393. equal_range(const _K2& __k) const {return __tree_.__equal_range_multi(__k);}
  1394. #endif
  1395. };
  1396. #if _LIBCPP_STD_VER >= 17
  1397. template<class _InputIterator,
  1398. class _Compare = less<__iter_value_type<_InputIterator>>,
  1399. class _Allocator = allocator<__iter_value_type<_InputIterator>>,
  1400. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value, void>,
  1401. class = enable_if_t<__is_allocator<_Allocator>::value, void>,
  1402. class = enable_if_t<!__is_allocator<_Compare>::value, void>>
  1403. multiset(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
  1404. -> multiset<__iter_value_type<_InputIterator>, _Compare, _Allocator>;
  1405. #if _LIBCPP_STD_VER >= 23
  1406. template <ranges::input_range _Range, class _Compare = less<ranges::range_value_t<_Range>>,
  1407. class _Allocator = allocator<ranges::range_value_t<_Range>>,
  1408. class = enable_if_t<__is_allocator<_Allocator>::value, void>,
  1409. class = enable_if_t<!__is_allocator<_Compare>::value, void>>
  1410. multiset(from_range_t, _Range&&, _Compare = _Compare(), _Allocator = _Allocator())
  1411. -> multiset<ranges::range_value_t<_Range>, _Compare, _Allocator>;
  1412. #endif
  1413. template<class _Key, class _Compare = less<_Key>,
  1414. class _Allocator = allocator<_Key>,
  1415. class = enable_if_t<__is_allocator<_Allocator>::value, void>,
  1416. class = enable_if_t<!__is_allocator<_Compare>::value, void>>
  1417. multiset(initializer_list<_Key>, _Compare = _Compare(), _Allocator = _Allocator())
  1418. -> multiset<_Key, _Compare, _Allocator>;
  1419. template<class _InputIterator, class _Allocator,
  1420. class = enable_if_t<__has_input_iterator_category<_InputIterator>::value, void>,
  1421. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1422. multiset(_InputIterator, _InputIterator, _Allocator)
  1423. -> multiset<__iter_value_type<_InputIterator>,
  1424. less<__iter_value_type<_InputIterator>>, _Allocator>;
  1425. #if _LIBCPP_STD_VER >= 23
  1426. template <ranges::input_range _Range, class _Allocator,
  1427. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1428. multiset(from_range_t, _Range&&, _Allocator)
  1429. -> multiset<ranges::range_value_t<_Range>, less<ranges::range_value_t<_Range>>, _Allocator>;
  1430. #endif
  1431. template<class _Key, class _Allocator,
  1432. class = enable_if_t<__is_allocator<_Allocator>::value, void>>
  1433. multiset(initializer_list<_Key>, _Allocator)
  1434. -> multiset<_Key, less<_Key>, _Allocator>;
  1435. #endif
  1436. #ifndef _LIBCPP_CXX03_LANG
  1437. template <class _Key, class _Compare, class _Allocator>
  1438. multiset<_Key, _Compare, _Allocator>::multiset(multiset&& __s, const allocator_type& __a)
  1439. : __tree_(_VSTD::move(__s.__tree_), __a)
  1440. {
  1441. if (__a != __s.get_allocator())
  1442. {
  1443. const_iterator __e = cend();
  1444. while (!__s.empty())
  1445. insert(__e, _VSTD::move(__s.__tree_.remove(__s.begin())->__value_));
  1446. }
  1447. }
  1448. #endif // _LIBCPP_CXX03_LANG
  1449. template <class _Key, class _Compare, class _Allocator>
  1450. inline _LIBCPP_INLINE_VISIBILITY
  1451. bool
  1452. operator==(const multiset<_Key, _Compare, _Allocator>& __x,
  1453. const multiset<_Key, _Compare, _Allocator>& __y)
  1454. {
  1455. return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
  1456. }
  1457. #if _LIBCPP_STD_VER <= 17
  1458. template <class _Key, class _Compare, class _Allocator>
  1459. inline _LIBCPP_INLINE_VISIBILITY
  1460. bool
  1461. operator< (const multiset<_Key, _Compare, _Allocator>& __x,
  1462. const multiset<_Key, _Compare, _Allocator>& __y)
  1463. {
  1464. return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  1465. }
  1466. template <class _Key, class _Compare, class _Allocator>
  1467. inline _LIBCPP_INLINE_VISIBILITY
  1468. bool
  1469. operator!=(const multiset<_Key, _Compare, _Allocator>& __x,
  1470. const multiset<_Key, _Compare, _Allocator>& __y)
  1471. {
  1472. return !(__x == __y);
  1473. }
  1474. template <class _Key, class _Compare, class _Allocator>
  1475. inline _LIBCPP_INLINE_VISIBILITY
  1476. bool
  1477. operator> (const multiset<_Key, _Compare, _Allocator>& __x,
  1478. const multiset<_Key, _Compare, _Allocator>& __y)
  1479. {
  1480. return __y < __x;
  1481. }
  1482. template <class _Key, class _Compare, class _Allocator>
  1483. inline _LIBCPP_INLINE_VISIBILITY
  1484. bool
  1485. operator>=(const multiset<_Key, _Compare, _Allocator>& __x,
  1486. const multiset<_Key, _Compare, _Allocator>& __y)
  1487. {
  1488. return !(__x < __y);
  1489. }
  1490. template <class _Key, class _Compare, class _Allocator>
  1491. inline _LIBCPP_INLINE_VISIBILITY
  1492. bool
  1493. operator<=(const multiset<_Key, _Compare, _Allocator>& __x,
  1494. const multiset<_Key, _Compare, _Allocator>& __y)
  1495. {
  1496. return !(__y < __x);
  1497. }
  1498. #else // _LIBCPP_STD_VER <= 17
  1499. template <class _Key, class _Allocator>
  1500. _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Key>
  1501. operator<=>(const multiset<_Key, _Allocator>& __x, const multiset<_Key, _Allocator>& __y) {
  1502. return std::lexicographical_compare_three_way(
  1503. __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Key, _Key>);
  1504. }
  1505. #endif // _LIBCPP_STD_VER <= 17
  1506. template <class _Key, class _Compare, class _Allocator>
  1507. inline _LIBCPP_INLINE_VISIBILITY
  1508. void
  1509. swap(multiset<_Key, _Compare, _Allocator>& __x,
  1510. multiset<_Key, _Compare, _Allocator>& __y)
  1511. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  1512. {
  1513. __x.swap(__y);
  1514. }
  1515. #if _LIBCPP_STD_VER >= 20
  1516. template <class _Key, class _Compare, class _Allocator, class _Predicate>
  1517. inline _LIBCPP_INLINE_VISIBILITY
  1518. typename multiset<_Key, _Compare, _Allocator>::size_type
  1519. erase_if(multiset<_Key, _Compare, _Allocator>& __c, _Predicate __pred) {
  1520. return _VSTD::__libcpp_erase_if_container(__c, __pred);
  1521. }
  1522. #endif
  1523. _LIBCPP_END_NAMESPACE_STD
  1524. #if _LIBCPP_STD_VER >= 17
  1525. _LIBCPP_BEGIN_NAMESPACE_STD
  1526. namespace pmr {
  1527. template <class _KeyT, class _CompareT = std::less<_KeyT>>
  1528. using set _LIBCPP_AVAILABILITY_PMR = std::set<_KeyT, _CompareT, polymorphic_allocator<_KeyT>>;
  1529. template <class _KeyT, class _CompareT = std::less<_KeyT>>
  1530. using multiset _LIBCPP_AVAILABILITY_PMR = std::multiset<_KeyT, _CompareT, polymorphic_allocator<_KeyT>>;
  1531. } // namespace pmr
  1532. _LIBCPP_END_NAMESPACE_STD
  1533. #endif
  1534. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  1535. # include <concepts>
  1536. # include <cstdlib>
  1537. # include <functional>
  1538. # include <iterator>
  1539. # include <type_traits>
  1540. #endif
  1541. #endif // _LIBCPP_SET