algorithm 35 KB

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