algorithm 34 KB

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