algorithm 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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_ALGORITHM
  10. #define _LIBCPP_ALGORITHM
  11. /*
  12. algorithm synopsis
  13. #include <initializer_list>
  14. namespace std
  15. {
  16. namespace ranges {
  17. template <class I, class F>
  18. struct in_fun_result; // since C++20
  19. template <class I1, class I2>
  20. struct in_in_result; // since C++20
  21. template <class I1, class I2, class O>
  22. struct in_in_out_result; // since C++20
  23. template <class I, class O1, class O2>
  24. struct in_out_out_result; // since C++20
  25. template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
  26. indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less> // since C++20
  27. constexpr I min_element(I first, S last, Comp comp = {}, Proj proj = {});
  28. template<forward_range R, class Proj = identity,
  29. indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less> // since C++20
  30. constexpr borrowed_iterator_t<R> min_element(R&& r, Comp comp = {}, Proj proj = {});
  31. }
  32. template <class InputIterator, class Predicate>
  33. constexpr bool // constexpr in C++20
  34. all_of(InputIterator first, InputIterator last, Predicate pred);
  35. template <class InputIterator, class Predicate>
  36. constexpr bool // constexpr in C++20
  37. any_of(InputIterator first, InputIterator last, Predicate pred);
  38. template <class InputIterator, class Predicate>
  39. constexpr bool // constexpr in C++20
  40. none_of(InputIterator first, InputIterator last, Predicate pred);
  41. template <class InputIterator, class Function>
  42. constexpr Function // constexpr in C++20
  43. for_each(InputIterator first, InputIterator last, Function f);
  44. template<class InputIterator, class Size, class Function>
  45. constexpr InputIterator // constexpr in C++20
  46. for_each_n(InputIterator first, Size n, Function f); // C++17
  47. template <class InputIterator, class T>
  48. constexpr InputIterator // constexpr in C++20
  49. find(InputIterator first, InputIterator last, const T& value);
  50. template <class InputIterator, class Predicate>
  51. constexpr InputIterator // constexpr in C++20
  52. find_if(InputIterator first, InputIterator last, Predicate pred);
  53. template<class InputIterator, class Predicate>
  54. constexpr InputIterator // constexpr in C++20
  55. find_if_not(InputIterator first, InputIterator last, Predicate pred);
  56. template <class ForwardIterator1, class ForwardIterator2>
  57. constexpr ForwardIterator1 // constexpr in C++20
  58. find_end(ForwardIterator1 first1, ForwardIterator1 last1,
  59. ForwardIterator2 first2, ForwardIterator2 last2);
  60. template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  61. constexpr ForwardIterator1 // constexpr in C++20
  62. find_end(ForwardIterator1 first1, ForwardIterator1 last1,
  63. ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
  64. template <class ForwardIterator1, class ForwardIterator2>
  65. constexpr ForwardIterator1 // constexpr in C++20
  66. find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
  67. ForwardIterator2 first2, ForwardIterator2 last2);
  68. template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  69. constexpr ForwardIterator1 // constexpr in C++20
  70. find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
  71. ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
  72. template <class ForwardIterator>
  73. constexpr ForwardIterator // constexpr in C++20
  74. adjacent_find(ForwardIterator first, ForwardIterator last);
  75. template <class ForwardIterator, class BinaryPredicate>
  76. constexpr ForwardIterator // constexpr in C++20
  77. adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
  78. template <class InputIterator, class T>
  79. constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
  80. count(InputIterator first, InputIterator last, const T& value);
  81. template <class InputIterator, class Predicate>
  82. constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
  83. count_if(InputIterator first, InputIterator last, Predicate pred);
  84. template <class InputIterator1, class InputIterator2>
  85. constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
  86. mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
  87. template <class InputIterator1, class InputIterator2>
  88. constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
  89. mismatch(InputIterator1 first1, InputIterator1 last1,
  90. InputIterator2 first2, InputIterator2 last2); // **C++14**
  91. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  92. constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
  93. mismatch(InputIterator1 first1, InputIterator1 last1,
  94. InputIterator2 first2, BinaryPredicate pred);
  95. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  96. constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
  97. mismatch(InputIterator1 first1, InputIterator1 last1,
  98. InputIterator2 first2, InputIterator2 last2,
  99. BinaryPredicate pred); // **C++14**
  100. template <class InputIterator1, class InputIterator2>
  101. constexpr bool // constexpr in C++20
  102. equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
  103. template <class InputIterator1, class InputIterator2>
  104. constexpr bool // constexpr in C++20
  105. equal(InputIterator1 first1, InputIterator1 last1,
  106. InputIterator2 first2, InputIterator2 last2); // **C++14**
  107. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  108. constexpr bool // constexpr in C++20
  109. equal(InputIterator1 first1, InputIterator1 last1,
  110. InputIterator2 first2, BinaryPredicate pred);
  111. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  112. constexpr bool // constexpr in C++20
  113. equal(InputIterator1 first1, InputIterator1 last1,
  114. InputIterator2 first2, InputIterator2 last2,
  115. BinaryPredicate pred); // **C++14**
  116. template<class ForwardIterator1, class ForwardIterator2>
  117. constexpr bool // constexpr in C++20
  118. is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
  119. ForwardIterator2 first2);
  120. template<class ForwardIterator1, class ForwardIterator2>
  121. constexpr bool // constexpr in C++20
  122. is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
  123. ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
  124. template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  125. constexpr bool // constexpr in C++20
  126. is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
  127. ForwardIterator2 first2, BinaryPredicate pred);
  128. template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  129. constexpr bool // constexpr in C++20
  130. is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
  131. ForwardIterator2 first2, ForwardIterator2 last2,
  132. BinaryPredicate pred); // **C++14**
  133. template <class ForwardIterator1, class ForwardIterator2>
  134. constexpr ForwardIterator1 // constexpr in C++20
  135. search(ForwardIterator1 first1, ForwardIterator1 last1,
  136. ForwardIterator2 first2, ForwardIterator2 last2);
  137. template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  138. constexpr ForwardIterator1 // constexpr in C++20
  139. search(ForwardIterator1 first1, ForwardIterator1 last1,
  140. ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
  141. template <class ForwardIterator, class Size, class T>
  142. constexpr ForwardIterator // constexpr in C++20
  143. search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
  144. template <class ForwardIterator, class Size, class T, class BinaryPredicate>
  145. constexpr ForwardIterator // constexpr in C++20
  146. search_n(ForwardIterator first, ForwardIterator last,
  147. Size count, const T& value, BinaryPredicate pred);
  148. template <class InputIterator, class OutputIterator>
  149. constexpr OutputIterator // constexpr in C++20
  150. copy(InputIterator first, InputIterator last, OutputIterator result);
  151. template<class InputIterator, class OutputIterator, class Predicate>
  152. constexpr OutputIterator // constexpr in C++20
  153. copy_if(InputIterator first, InputIterator last,
  154. OutputIterator result, Predicate pred);
  155. template<class InputIterator, class Size, class OutputIterator>
  156. constexpr OutputIterator // constexpr in C++20
  157. copy_n(InputIterator first, Size n, OutputIterator result);
  158. template <class BidirectionalIterator1, class BidirectionalIterator2>
  159. constexpr BidirectionalIterator2 // constexpr in C++20
  160. copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
  161. BidirectionalIterator2 result);
  162. template <class ForwardIterator1, class ForwardIterator2>
  163. constexpr ForwardIterator2 // constexpr in C++20
  164. swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
  165. template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2>
  166. requires indirectly_swappable<I1, I2>
  167. constexpr ranges::swap_ranges_result<I1, I2>
  168. ranges::swap_ranges(I1 first1, S1 last1, I2 first2, S2 last2);
  169. template<input_range R1, input_range R2>
  170. requires indirectly_swappable<iterator_t<R1>, iterator_t<R2>>
  171. constexpr ranges::swap_ranges_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
  172. ranges::swap_ranges(R1&& r1, R2&& r2);
  173. template <class ForwardIterator1, class ForwardIterator2>
  174. constexpr void // constexpr in C++20
  175. iter_swap(ForwardIterator1 a, ForwardIterator2 b);
  176. template <class InputIterator, class OutputIterator, class UnaryOperation>
  177. constexpr OutputIterator // constexpr in C++20
  178. transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
  179. template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
  180. constexpr OutputIterator // constexpr in C++20
  181. transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
  182. OutputIterator result, BinaryOperation binary_op);
  183. template <class ForwardIterator, class T>
  184. constexpr void // constexpr in C++20
  185. replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
  186. template <class ForwardIterator, class Predicate, class T>
  187. constexpr void // constexpr in C++20
  188. replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
  189. template <class InputIterator, class OutputIterator, class T>
  190. constexpr OutputIterator // constexpr in C++20
  191. replace_copy(InputIterator first, InputIterator last, OutputIterator result,
  192. const T& old_value, const T& new_value);
  193. template <class InputIterator, class OutputIterator, class Predicate, class T>
  194. constexpr OutputIterator // constexpr in C++20
  195. replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
  196. template <class ForwardIterator, class T>
  197. constexpr void // constexpr in C++20
  198. fill(ForwardIterator first, ForwardIterator last, const T& value);
  199. template <class OutputIterator, class Size, class T>
  200. constexpr OutputIterator // constexpr in C++20
  201. fill_n(OutputIterator first, Size n, const T& value);
  202. template <class ForwardIterator, class Generator>
  203. constexpr void // constexpr in C++20
  204. generate(ForwardIterator first, ForwardIterator last, Generator gen);
  205. template <class OutputIterator, class Size, class Generator>
  206. constexpr OutputIterator // constexpr in C++20
  207. generate_n(OutputIterator first, Size n, Generator gen);
  208. template <class ForwardIterator, class T>
  209. constexpr ForwardIterator // constexpr in C++20
  210. remove(ForwardIterator first, ForwardIterator last, const T& value);
  211. template <class ForwardIterator, class Predicate>
  212. constexpr ForwardIterator // constexpr in C++20
  213. remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
  214. template <class InputIterator, class OutputIterator, class T>
  215. constexpr OutputIterator // constexpr in C++20
  216. remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
  217. template <class InputIterator, class OutputIterator, class Predicate>
  218. constexpr OutputIterator // constexpr in C++20
  219. remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
  220. template <class ForwardIterator>
  221. constexpr ForwardIterator // constexpr in C++20
  222. unique(ForwardIterator first, ForwardIterator last);
  223. template <class ForwardIterator, class BinaryPredicate>
  224. constexpr ForwardIterator // constexpr in C++20
  225. unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
  226. template <class InputIterator, class OutputIterator>
  227. constexpr OutputIterator // constexpr in C++20
  228. unique_copy(InputIterator first, InputIterator last, OutputIterator result);
  229. template <class InputIterator, class OutputIterator, class BinaryPredicate>
  230. constexpr OutputIterator // constexpr in C++20
  231. unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
  232. template <class BidirectionalIterator>
  233. constexpr void // constexpr in C++20
  234. reverse(BidirectionalIterator first, BidirectionalIterator last);
  235. template <class BidirectionalIterator, class OutputIterator>
  236. constexpr OutputIterator // constexpr in C++20
  237. reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
  238. template <class ForwardIterator>
  239. constexpr ForwardIterator // constexpr in C++20
  240. rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
  241. template <class ForwardIterator, class OutputIterator>
  242. constexpr OutputIterator // constexpr in C++20
  243. rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
  244. template <class RandomAccessIterator>
  245. void
  246. random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
  247. template <class RandomAccessIterator, class RandomNumberGenerator>
  248. void
  249. random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
  250. RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
  251. template<class PopulationIterator, class SampleIterator,
  252. class Distance, class UniformRandomBitGenerator>
  253. SampleIterator sample(PopulationIterator first, PopulationIterator last,
  254. SampleIterator out, Distance n,
  255. UniformRandomBitGenerator&& g); // C++17
  256. template<class RandomAccessIterator, class UniformRandomNumberGenerator>
  257. void shuffle(RandomAccessIterator first, RandomAccessIterator last,
  258. UniformRandomNumberGenerator&& g);
  259. template<class ForwardIterator>
  260. constexpr ForwardIterator
  261. shift_left(ForwardIterator first, ForwardIterator last,
  262. typename iterator_traits<ForwardIterator>::difference_type n); // C++20
  263. template<class ForwardIterator>
  264. constexpr ForwardIterator
  265. shift_right(ForwardIterator first, ForwardIterator last,
  266. typename iterator_traits<ForwardIterator>::difference_type n); // C++20
  267. template <class InputIterator, class Predicate>
  268. constexpr bool // constexpr in C++20
  269. is_partitioned(InputIterator first, InputIterator last, Predicate pred);
  270. template <class ForwardIterator, class Predicate>
  271. constexpr ForwardIterator // constexpr in C++20
  272. partition(ForwardIterator first, ForwardIterator last, Predicate pred);
  273. template <class InputIterator, class OutputIterator1,
  274. class OutputIterator2, class Predicate>
  275. constexpr pair<OutputIterator1, OutputIterator2> // constexpr in C++20
  276. partition_copy(InputIterator first, InputIterator last,
  277. OutputIterator1 out_true, OutputIterator2 out_false,
  278. Predicate pred);
  279. template <class ForwardIterator, class Predicate>
  280. ForwardIterator
  281. stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
  282. template<class ForwardIterator, class Predicate>
  283. constexpr ForwardIterator // constexpr in C++20
  284. partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
  285. template <class ForwardIterator>
  286. constexpr bool // constexpr in C++20
  287. is_sorted(ForwardIterator first, ForwardIterator last);
  288. template <class ForwardIterator, class Compare>
  289. constexpr bool // constexpr in C++20
  290. is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
  291. template<class ForwardIterator>
  292. constexpr ForwardIterator // constexpr in C++20
  293. is_sorted_until(ForwardIterator first, ForwardIterator last);
  294. template <class ForwardIterator, class Compare>
  295. constexpr ForwardIterator // constexpr in C++20
  296. is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
  297. template <class RandomAccessIterator>
  298. constexpr void // constexpr in C++20
  299. sort(RandomAccessIterator first, RandomAccessIterator last);
  300. template <class RandomAccessIterator, class Compare>
  301. constexpr void // constexpr in C++20
  302. sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  303. template <class RandomAccessIterator>
  304. void
  305. stable_sort(RandomAccessIterator first, RandomAccessIterator last);
  306. template <class RandomAccessIterator, class Compare>
  307. void
  308. stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  309. template <class RandomAccessIterator>
  310. constexpr void // constexpr in C++20
  311. partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
  312. template <class RandomAccessIterator, class Compare>
  313. constexpr void // constexpr in C++20
  314. partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
  315. template <class InputIterator, class RandomAccessIterator>
  316. constexpr RandomAccessIterator // constexpr in C++20
  317. partial_sort_copy(InputIterator first, InputIterator last,
  318. RandomAccessIterator result_first, RandomAccessIterator result_last);
  319. template <class InputIterator, class RandomAccessIterator, class Compare>
  320. constexpr RandomAccessIterator // constexpr in C++20
  321. partial_sort_copy(InputIterator first, InputIterator last,
  322. RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
  323. template <class RandomAccessIterator>
  324. constexpr void // constexpr in C++20
  325. nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
  326. template <class RandomAccessIterator, class Compare>
  327. constexpr void // constexpr in C++20
  328. nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
  329. template <class ForwardIterator, class T>
  330. constexpr ForwardIterator // constexpr in C++20
  331. lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
  332. template <class ForwardIterator, class T, class Compare>
  333. constexpr ForwardIterator // constexpr in C++20
  334. lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
  335. template <class ForwardIterator, class T>
  336. constexpr ForwardIterator // constexpr in C++20
  337. upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
  338. template <class ForwardIterator, class T, class Compare>
  339. constexpr ForwardIterator // constexpr in C++20
  340. upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
  341. template <class ForwardIterator, class T>
  342. constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
  343. equal_range(ForwardIterator first, ForwardIterator last, const T& value);
  344. template <class ForwardIterator, class T, class Compare>
  345. constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
  346. equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
  347. template <class ForwardIterator, class T>
  348. constexpr bool // constexpr in C++20
  349. binary_search(ForwardIterator first, ForwardIterator last, const T& value);
  350. template <class ForwardIterator, class T, class Compare>
  351. constexpr bool // constexpr in C++20
  352. binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
  353. template <class InputIterator1, class InputIterator2, class OutputIterator>
  354. constexpr OutputIterator // constexpr in C++20
  355. merge(InputIterator1 first1, InputIterator1 last1,
  356. InputIterator2 first2, InputIterator2 last2, OutputIterator result);
  357. template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
  358. constexpr OutputIterator // constexpr in C++20
  359. merge(InputIterator1 first1, InputIterator1 last1,
  360. InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
  361. template <class BidirectionalIterator>
  362. void
  363. inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
  364. template <class BidirectionalIterator, class Compare>
  365. void
  366. inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
  367. template <class InputIterator1, class InputIterator2>
  368. constexpr bool // constexpr in C++20
  369. includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
  370. template <class InputIterator1, class InputIterator2, class Compare>
  371. constexpr bool // constexpr in C++20
  372. includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
  373. template <class InputIterator1, class InputIterator2, class OutputIterator>
  374. constexpr OutputIterator // constexpr in C++20
  375. set_union(InputIterator1 first1, InputIterator1 last1,
  376. InputIterator2 first2, InputIterator2 last2, OutputIterator result);
  377. template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
  378. constexpr OutputIterator // constexpr in C++20
  379. set_union(InputIterator1 first1, InputIterator1 last1,
  380. InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
  381. template <class InputIterator1, class InputIterator2, class OutputIterator>
  382. constexpr OutputIterator // constexpr in C++20
  383. set_intersection(InputIterator1 first1, InputIterator1 last1,
  384. InputIterator2 first2, InputIterator2 last2, OutputIterator result);
  385. template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
  386. constexpr OutputIterator // constexpr in C++20
  387. set_intersection(InputIterator1 first1, InputIterator1 last1,
  388. InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
  389. template <class InputIterator1, class InputIterator2, class OutputIterator>
  390. constexpr OutputIterator // constexpr in C++20
  391. set_difference(InputIterator1 first1, InputIterator1 last1,
  392. InputIterator2 first2, InputIterator2 last2, OutputIterator result);
  393. template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
  394. constexpr OutputIterator // constexpr in C++20
  395. set_difference(InputIterator1 first1, InputIterator1 last1,
  396. InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
  397. template <class InputIterator1, class InputIterator2, class OutputIterator>
  398. constexpr OutputIterator // constexpr in C++20
  399. set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
  400. InputIterator2 first2, InputIterator2 last2, OutputIterator result);
  401. template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
  402. constexpr OutputIterator // constexpr in C++20
  403. set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
  404. InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
  405. template <class RandomAccessIterator>
  406. constexpr void // constexpr in C++20
  407. push_heap(RandomAccessIterator first, RandomAccessIterator last);
  408. template <class RandomAccessIterator, class Compare>
  409. constexpr void // constexpr in C++20
  410. push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  411. template <class RandomAccessIterator>
  412. constexpr void // constexpr in C++20
  413. pop_heap(RandomAccessIterator first, RandomAccessIterator last);
  414. template <class RandomAccessIterator, class Compare>
  415. constexpr void // constexpr in C++20
  416. pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  417. template <class RandomAccessIterator>
  418. constexpr void // constexpr in C++20
  419. make_heap(RandomAccessIterator first, RandomAccessIterator last);
  420. template <class RandomAccessIterator, class Compare>
  421. constexpr void // constexpr in C++20
  422. make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  423. template <class RandomAccessIterator>
  424. constexpr void // constexpr in C++20
  425. sort_heap(RandomAccessIterator first, RandomAccessIterator last);
  426. template <class RandomAccessIterator, class Compare>
  427. constexpr void // constexpr in C++20
  428. sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
  429. template <class RandomAccessIterator>
  430. constexpr bool // constexpr in C++20
  431. is_heap(RandomAccessIterator first, RandomAccessiterator last);
  432. template <class RandomAccessIterator, class Compare>
  433. constexpr bool // constexpr in C++20
  434. is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
  435. template <class RandomAccessIterator>
  436. constexpr RandomAccessIterator // constexpr in C++20
  437. is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
  438. template <class RandomAccessIterator, class Compare>
  439. constexpr RandomAccessIterator // constexpr in C++20
  440. is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
  441. template <class ForwardIterator>
  442. constexpr ForwardIterator // constexpr in C++14
  443. min_element(ForwardIterator first, ForwardIterator last);
  444. template <class ForwardIterator, class Compare>
  445. constexpr ForwardIterator // constexpr in C++14
  446. min_element(ForwardIterator first, ForwardIterator last, Compare comp);
  447. template <class T>
  448. constexpr const T& // constexpr in C++14
  449. min(const T& a, const T& b);
  450. template <class T, class Compare>
  451. constexpr const T& // constexpr in C++14
  452. min(const T& a, const T& b, Compare comp);
  453. template<class T>
  454. constexpr T // constexpr in C++14
  455. min(initializer_list<T> t);
  456. template<class T, class Compare>
  457. constexpr T // constexpr in C++14
  458. min(initializer_list<T> t, Compare comp);
  459. template<class T>
  460. constexpr const T& clamp(const T& v, const T& lo, const T& hi); // C++17
  461. template<class T, class Compare>
  462. constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp); // C++17
  463. template <class ForwardIterator>
  464. constexpr ForwardIterator // constexpr in C++14
  465. max_element(ForwardIterator first, ForwardIterator last);
  466. template <class ForwardIterator, class Compare>
  467. constexpr ForwardIterator // constexpr in C++14
  468. max_element(ForwardIterator first, ForwardIterator last, Compare comp);
  469. template <class T>
  470. constexpr const T& // constexpr in C++14
  471. max(const T& a, const T& b);
  472. template <class T, class Compare>
  473. constexpr const T& // constexpr in C++14
  474. max(const T& a, const T& b, Compare comp);
  475. template<class T>
  476. constexpr T // constexpr in C++14
  477. max(initializer_list<T> t);
  478. template<class T, class Compare>
  479. constexpr T // constexpr in C++14
  480. max(initializer_list<T> t, Compare comp);
  481. template<class ForwardIterator>
  482. constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++14
  483. minmax_element(ForwardIterator first, ForwardIterator last);
  484. template<class ForwardIterator, class Compare>
  485. constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++14
  486. minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
  487. template<class T>
  488. constexpr pair<const T&, const T&> // constexpr in C++14
  489. minmax(const T& a, const T& b);
  490. template<class T, class Compare>
  491. constexpr pair<const T&, const T&> // constexpr in C++14
  492. minmax(const T& a, const T& b, Compare comp);
  493. template<class T>
  494. constexpr pair<T, T> // constexpr in C++14
  495. minmax(initializer_list<T> t);
  496. template<class T, class Compare>
  497. constexpr pair<T, T> // constexpr in C++14
  498. minmax(initializer_list<T> t, Compare comp);
  499. template <class InputIterator1, class InputIterator2>
  500. constexpr bool // constexpr in C++20
  501. lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
  502. template <class InputIterator1, class InputIterator2, class Compare>
  503. constexpr bool // constexpr in C++20
  504. lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
  505. InputIterator2 first2, InputIterator2 last2, Compare comp);
  506. template <class BidirectionalIterator>
  507. constexpr bool // constexpr in C++20
  508. next_permutation(BidirectionalIterator first, BidirectionalIterator last);
  509. template <class BidirectionalIterator, class Compare>
  510. constexpr bool // constexpr in C++20
  511. next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
  512. template <class BidirectionalIterator>
  513. constexpr bool // constexpr in C++20
  514. prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
  515. template <class BidirectionalIterator, class Compare>
  516. constexpr bool // constexpr in C++20
  517. prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
  518. } // std
  519. */
  520. #include <__bits>
  521. #include <__config>
  522. #include <__debug>
  523. #include <cstddef>
  524. #include <cstring>
  525. #include <functional>
  526. #include <initializer_list>
  527. #include <iterator>
  528. #include <memory>
  529. #include <type_traits>
  530. #include <utility>
  531. #include <version>
  532. #include <__algorithm/adjacent_find.h>
  533. #include <__algorithm/all_of.h>
  534. #include <__algorithm/any_of.h>
  535. #include <__algorithm/binary_search.h>
  536. #include <__algorithm/clamp.h>
  537. #include <__algorithm/comp.h>
  538. #include <__algorithm/comp_ref_type.h>
  539. #include <__algorithm/copy.h>
  540. #include <__algorithm/copy_backward.h>
  541. #include <__algorithm/copy_if.h>
  542. #include <__algorithm/copy_n.h>
  543. #include <__algorithm/count.h>
  544. #include <__algorithm/count_if.h>
  545. #include <__algorithm/equal.h>
  546. #include <__algorithm/equal_range.h>
  547. #include <__algorithm/fill.h>
  548. #include <__algorithm/fill_n.h>
  549. #include <__algorithm/find.h>
  550. #include <__algorithm/find_end.h>
  551. #include <__algorithm/find_first_of.h>
  552. #include <__algorithm/find_if.h>
  553. #include <__algorithm/find_if_not.h>
  554. #include <__algorithm/for_each.h>
  555. #include <__algorithm/for_each_n.h>
  556. #include <__algorithm/generate.h>
  557. #include <__algorithm/generate_n.h>
  558. #include <__algorithm/half_positive.h>
  559. #include <__algorithm/in_fun_result.h>
  560. #include <__algorithm/in_in_out_result.h>
  561. #include <__algorithm/in_in_result.h>
  562. #include <__algorithm/in_out_out_result.h>
  563. #include <__algorithm/in_out_result.h>
  564. #include <__algorithm/includes.h>
  565. #include <__algorithm/inplace_merge.h>
  566. #include <__algorithm/is_heap.h>
  567. #include <__algorithm/is_heap_until.h>
  568. #include <__algorithm/is_partitioned.h>
  569. #include <__algorithm/is_permutation.h>
  570. #include <__algorithm/is_sorted.h>
  571. #include <__algorithm/is_sorted_until.h>
  572. #include <__algorithm/iter_swap.h>
  573. #include <__algorithm/lexicographical_compare.h>
  574. #include <__algorithm/lower_bound.h>
  575. #include <__algorithm/make_heap.h>
  576. #include <__algorithm/max.h>
  577. #include <__algorithm/max_element.h>
  578. #include <__algorithm/merge.h>
  579. #include <__algorithm/min.h>
  580. #include <__algorithm/min_element.h>
  581. #include <__algorithm/minmax.h>
  582. #include <__algorithm/minmax_element.h>
  583. #include <__algorithm/mismatch.h>
  584. #include <__algorithm/move.h>
  585. #include <__algorithm/move_backward.h>
  586. #include <__algorithm/next_permutation.h>
  587. #include <__algorithm/none_of.h>
  588. #include <__algorithm/nth_element.h>
  589. #include <__algorithm/partial_sort.h>
  590. #include <__algorithm/partial_sort_copy.h>
  591. #include <__algorithm/partition.h>
  592. #include <__algorithm/partition_copy.h>
  593. #include <__algorithm/partition_point.h>
  594. #include <__algorithm/pop_heap.h>
  595. #include <__algorithm/prev_permutation.h>
  596. #include <__algorithm/push_heap.h>
  597. #include <__algorithm/ranges_min_element.h>
  598. #include <__algorithm/ranges_swap_ranges.h>
  599. #include <__algorithm/remove.h>
  600. #include <__algorithm/remove_copy.h>
  601. #include <__algorithm/remove_copy_if.h>
  602. #include <__algorithm/remove_if.h>
  603. #include <__algorithm/replace.h>
  604. #include <__algorithm/replace_copy.h>
  605. #include <__algorithm/replace_copy_if.h>
  606. #include <__algorithm/replace_if.h>
  607. #include <__algorithm/reverse.h>
  608. #include <__algorithm/reverse_copy.h>
  609. #include <__algorithm/rotate.h>
  610. #include <__algorithm/rotate_copy.h>
  611. #include <__algorithm/sample.h>
  612. #include <__algorithm/search.h>
  613. #include <__algorithm/search_n.h>
  614. #include <__algorithm/set_difference.h>
  615. #include <__algorithm/set_intersection.h>
  616. #include <__algorithm/set_symmetric_difference.h>
  617. #include <__algorithm/set_union.h>
  618. #include <__algorithm/shift_left.h>
  619. #include <__algorithm/shift_right.h>
  620. #include <__algorithm/shuffle.h>
  621. #include <__algorithm/sift_down.h>
  622. #include <__algorithm/sort.h>
  623. #include <__algorithm/sort_heap.h>
  624. #include <__algorithm/stable_partition.h>
  625. #include <__algorithm/stable_sort.h>
  626. #include <__algorithm/swap_ranges.h>
  627. #include <__algorithm/transform.h>
  628. #include <__algorithm/unique.h>
  629. #include <__algorithm/unique_copy.h>
  630. #include <__algorithm/unwrap_iter.h>
  631. #include <__algorithm/upper_bound.h>
  632. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  633. # pragma GCC system_header
  634. #endif
  635. #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
  636. //# include <__pstl_algorithm>
  637. #endif
  638. #endif // _LIBCPP_ALGORITHM