set 56 KB

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