container.h 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: container.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file provides Container-based versions of algorithmic functions
  20. // within the C++ standard library. The following standard library sets of
  21. // functions are covered within this file:
  22. //
  23. // * Algorithmic <iterator> functions
  24. // * Algorithmic <numeric> functions
  25. // * <algorithm> functions
  26. //
  27. // The standard library functions operate on iterator ranges; the functions
  28. // within this API operate on containers, though many return iterator ranges.
  29. //
  30. // All functions within this API are named with a `c_` prefix. Calls such as
  31. // `absl::c_xx(container, ...) are equivalent to std:: functions such as
  32. // `std::xx(std::begin(cont), std::end(cont), ...)`. Functions that act on
  33. // iterators but not conceptually on iterator ranges (e.g. `std::iter_swap`)
  34. // have no equivalent here.
  35. //
  36. // For template parameter and variable naming, `C` indicates the container type
  37. // to which the function is applied, `Pred` indicates the predicate object type
  38. // to be used by the function and `T` indicates the applicable element type.
  39. #ifndef ABSL_ALGORITHM_CONTAINER_H_
  40. #define ABSL_ALGORITHM_CONTAINER_H_
  41. #include <algorithm>
  42. #include <cassert>
  43. #include <iterator>
  44. #include <numeric>
  45. #include <type_traits>
  46. #include <unordered_map>
  47. #include <unordered_set>
  48. #include <utility>
  49. #include <vector>
  50. #include "absl/algorithm/algorithm.h"
  51. #include "absl/base/macros.h"
  52. #include "absl/meta/type_traits.h"
  53. namespace absl {
  54. ABSL_NAMESPACE_BEGIN
  55. namespace container_algorithm_internal {
  56. // NOTE: it is important to defer to ADL lookup for building with C++ modules,
  57. // especially for headers like <valarray> which are not visible from this file
  58. // but specialize std::begin and std::end.
  59. using std::begin;
  60. using std::end;
  61. // The type of the iterator given by begin(c) (possibly std::begin(c)).
  62. // ContainerIter<const vector<T>> gives vector<T>::const_iterator,
  63. // while ContainerIter<vector<T>> gives vector<T>::iterator.
  64. template <typename C>
  65. using ContainerIter = decltype(begin(std::declval<C&>()));
  66. // An MSVC bug involving template parameter substitution requires us to use
  67. // decltype() here instead of just std::pair.
  68. template <typename C1, typename C2>
  69. using ContainerIterPairType =
  70. decltype(std::make_pair(ContainerIter<C1>(), ContainerIter<C2>()));
  71. template <typename C>
  72. using ContainerDifferenceType =
  73. decltype(std::distance(std::declval<ContainerIter<C>>(),
  74. std::declval<ContainerIter<C>>()));
  75. template <typename C>
  76. using ContainerPointerType =
  77. typename std::iterator_traits<ContainerIter<C>>::pointer;
  78. // container_algorithm_internal::c_begin and
  79. // container_algorithm_internal::c_end are abbreviations for proper ADL
  80. // lookup of std::begin and std::end, i.e.
  81. // using std::begin;
  82. // using std::end;
  83. // std::foo(begin(c), end(c);
  84. // becomes
  85. // std::foo(container_algorithm_internal::begin(c),
  86. // container_algorithm_internal::end(c));
  87. // These are meant for internal use only.
  88. template <typename C>
  89. ContainerIter<C> c_begin(C& c) { return begin(c); }
  90. template <typename C>
  91. ContainerIter<C> c_end(C& c) { return end(c); }
  92. template <typename T>
  93. struct IsUnorderedContainer : std::false_type {};
  94. template <class Key, class T, class Hash, class KeyEqual, class Allocator>
  95. struct IsUnorderedContainer<
  96. std::unordered_map<Key, T, Hash, KeyEqual, Allocator>> : std::true_type {};
  97. template <class Key, class Hash, class KeyEqual, class Allocator>
  98. struct IsUnorderedContainer<std::unordered_set<Key, Hash, KeyEqual, Allocator>>
  99. : std::true_type {};
  100. // container_algorithm_internal::c_size. It is meant for internal use only.
  101. template <class C>
  102. auto c_size(C& c) -> decltype(c.size()) {
  103. return c.size();
  104. }
  105. template <class T, std::size_t N>
  106. constexpr std::size_t c_size(T (&)[N]) {
  107. return N;
  108. }
  109. } // namespace container_algorithm_internal
  110. // PUBLIC API
  111. //------------------------------------------------------------------------------
  112. // Abseil algorithm.h functions
  113. //------------------------------------------------------------------------------
  114. // c_linear_search()
  115. //
  116. // Container-based version of absl::linear_search() for performing a linear
  117. // search within a container.
  118. template <typename C, typename EqualityComparable>
  119. bool c_linear_search(const C& c, EqualityComparable&& value) {
  120. return linear_search(container_algorithm_internal::c_begin(c),
  121. container_algorithm_internal::c_end(c),
  122. std::forward<EqualityComparable>(value));
  123. }
  124. //------------------------------------------------------------------------------
  125. // <iterator> algorithms
  126. //------------------------------------------------------------------------------
  127. // c_distance()
  128. //
  129. // Container-based version of the <iterator> `std::distance()` function to
  130. // return the number of elements within a container.
  131. template <typename C>
  132. container_algorithm_internal::ContainerDifferenceType<const C> c_distance(
  133. const C& c) {
  134. return std::distance(container_algorithm_internal::c_begin(c),
  135. container_algorithm_internal::c_end(c));
  136. }
  137. //------------------------------------------------------------------------------
  138. // <algorithm> Non-modifying sequence operations
  139. //------------------------------------------------------------------------------
  140. // c_all_of()
  141. //
  142. // Container-based version of the <algorithm> `std::all_of()` function to
  143. // test a condition on all elements within a container.
  144. template <typename C, typename Pred>
  145. bool c_all_of(const C& c, Pred&& pred) {
  146. return std::all_of(container_algorithm_internal::c_begin(c),
  147. container_algorithm_internal::c_end(c),
  148. std::forward<Pred>(pred));
  149. }
  150. // c_any_of()
  151. //
  152. // Container-based version of the <algorithm> `std::any_of()` function to
  153. // test if any element in a container fulfills a condition.
  154. template <typename C, typename Pred>
  155. bool c_any_of(const C& c, Pred&& pred) {
  156. return std::any_of(container_algorithm_internal::c_begin(c),
  157. container_algorithm_internal::c_end(c),
  158. std::forward<Pred>(pred));
  159. }
  160. // c_none_of()
  161. //
  162. // Container-based version of the <algorithm> `std::none_of()` function to
  163. // test if no elements in a container fulfil a condition.
  164. template <typename C, typename Pred>
  165. bool c_none_of(const C& c, Pred&& pred) {
  166. return std::none_of(container_algorithm_internal::c_begin(c),
  167. container_algorithm_internal::c_end(c),
  168. std::forward<Pred>(pred));
  169. }
  170. // c_for_each()
  171. //
  172. // Container-based version of the <algorithm> `std::for_each()` function to
  173. // apply a function to a container's elements.
  174. template <typename C, typename Function>
  175. decay_t<Function> c_for_each(C&& c, Function&& f) {
  176. return std::for_each(container_algorithm_internal::c_begin(c),
  177. container_algorithm_internal::c_end(c),
  178. std::forward<Function>(f));
  179. }
  180. // c_find()
  181. //
  182. // Container-based version of the <algorithm> `std::find()` function to find
  183. // the first element containing the passed value within a container value.
  184. template <typename C, typename T>
  185. container_algorithm_internal::ContainerIter<C> c_find(C& c, T&& value) {
  186. return std::find(container_algorithm_internal::c_begin(c),
  187. container_algorithm_internal::c_end(c),
  188. std::forward<T>(value));
  189. }
  190. // c_find_if()
  191. //
  192. // Container-based version of the <algorithm> `std::find_if()` function to find
  193. // the first element in a container matching the given condition.
  194. template <typename C, typename Pred>
  195. container_algorithm_internal::ContainerIter<C> c_find_if(C& c, Pred&& pred) {
  196. return std::find_if(container_algorithm_internal::c_begin(c),
  197. container_algorithm_internal::c_end(c),
  198. std::forward<Pred>(pred));
  199. }
  200. // c_find_if_not()
  201. //
  202. // Container-based version of the <algorithm> `std::find_if_not()` function to
  203. // find the first element in a container not matching the given condition.
  204. template <typename C, typename Pred>
  205. container_algorithm_internal::ContainerIter<C> c_find_if_not(C& c,
  206. Pred&& pred) {
  207. return std::find_if_not(container_algorithm_internal::c_begin(c),
  208. container_algorithm_internal::c_end(c),
  209. std::forward<Pred>(pred));
  210. }
  211. // c_find_end()
  212. //
  213. // Container-based version of the <algorithm> `std::find_end()` function to
  214. // find the last subsequence within a container.
  215. template <typename Sequence1, typename Sequence2>
  216. container_algorithm_internal::ContainerIter<Sequence1> c_find_end(
  217. Sequence1& sequence, Sequence2& subsequence) {
  218. return std::find_end(container_algorithm_internal::c_begin(sequence),
  219. container_algorithm_internal::c_end(sequence),
  220. container_algorithm_internal::c_begin(subsequence),
  221. container_algorithm_internal::c_end(subsequence));
  222. }
  223. // Overload of c_find_end() for using a predicate evaluation other than `==` as
  224. // the function's test condition.
  225. template <typename Sequence1, typename Sequence2, typename BinaryPredicate>
  226. container_algorithm_internal::ContainerIter<Sequence1> c_find_end(
  227. Sequence1& sequence, Sequence2& subsequence, BinaryPredicate&& pred) {
  228. return std::find_end(container_algorithm_internal::c_begin(sequence),
  229. container_algorithm_internal::c_end(sequence),
  230. container_algorithm_internal::c_begin(subsequence),
  231. container_algorithm_internal::c_end(subsequence),
  232. std::forward<BinaryPredicate>(pred));
  233. }
  234. // c_find_first_of()
  235. //
  236. // Container-based version of the <algorithm> `std::find_first_of()` function to
  237. // find the first element within the container that is also within the options
  238. // container.
  239. template <typename C1, typename C2>
  240. container_algorithm_internal::ContainerIter<C1> c_find_first_of(C1& container,
  241. C2& options) {
  242. return std::find_first_of(container_algorithm_internal::c_begin(container),
  243. container_algorithm_internal::c_end(container),
  244. container_algorithm_internal::c_begin(options),
  245. container_algorithm_internal::c_end(options));
  246. }
  247. // Overload of c_find_first_of() for using a predicate evaluation other than
  248. // `==` as the function's test condition.
  249. template <typename C1, typename C2, typename BinaryPredicate>
  250. container_algorithm_internal::ContainerIter<C1> c_find_first_of(
  251. C1& container, C2& options, BinaryPredicate&& pred) {
  252. return std::find_first_of(container_algorithm_internal::c_begin(container),
  253. container_algorithm_internal::c_end(container),
  254. container_algorithm_internal::c_begin(options),
  255. container_algorithm_internal::c_end(options),
  256. std::forward<BinaryPredicate>(pred));
  257. }
  258. // c_adjacent_find()
  259. //
  260. // Container-based version of the <algorithm> `std::adjacent_find()` function to
  261. // find equal adjacent elements within a container.
  262. template <typename Sequence>
  263. container_algorithm_internal::ContainerIter<Sequence> c_adjacent_find(
  264. Sequence& sequence) {
  265. return std::adjacent_find(container_algorithm_internal::c_begin(sequence),
  266. container_algorithm_internal::c_end(sequence));
  267. }
  268. // Overload of c_adjacent_find() for using a predicate evaluation other than
  269. // `==` as the function's test condition.
  270. template <typename Sequence, typename BinaryPredicate>
  271. container_algorithm_internal::ContainerIter<Sequence> c_adjacent_find(
  272. Sequence& sequence, BinaryPredicate&& pred) {
  273. return std::adjacent_find(container_algorithm_internal::c_begin(sequence),
  274. container_algorithm_internal::c_end(sequence),
  275. std::forward<BinaryPredicate>(pred));
  276. }
  277. // c_count()
  278. //
  279. // Container-based version of the <algorithm> `std::count()` function to count
  280. // values that match within a container.
  281. template <typename C, typename T>
  282. container_algorithm_internal::ContainerDifferenceType<const C> c_count(
  283. const C& c, T&& value) {
  284. return std::count(container_algorithm_internal::c_begin(c),
  285. container_algorithm_internal::c_end(c),
  286. std::forward<T>(value));
  287. }
  288. // c_count_if()
  289. //
  290. // Container-based version of the <algorithm> `std::count_if()` function to
  291. // count values matching a condition within a container.
  292. template <typename C, typename Pred>
  293. container_algorithm_internal::ContainerDifferenceType<const C> c_count_if(
  294. const C& c, Pred&& pred) {
  295. return std::count_if(container_algorithm_internal::c_begin(c),
  296. container_algorithm_internal::c_end(c),
  297. std::forward<Pred>(pred));
  298. }
  299. // c_mismatch()
  300. //
  301. // Container-based version of the <algorithm> `std::mismatch()` function to
  302. // return the first element where two ordered containers differ.
  303. template <typename C1, typename C2>
  304. container_algorithm_internal::ContainerIterPairType<C1, C2>
  305. c_mismatch(C1& c1, C2& c2) {
  306. return std::mismatch(container_algorithm_internal::c_begin(c1),
  307. container_algorithm_internal::c_end(c1),
  308. container_algorithm_internal::c_begin(c2));
  309. }
  310. // Overload of c_mismatch() for using a predicate evaluation other than `==` as
  311. // the function's test condition.
  312. template <typename C1, typename C2, typename BinaryPredicate>
  313. container_algorithm_internal::ContainerIterPairType<C1, C2>
  314. c_mismatch(C1& c1, C2& c2, BinaryPredicate&& pred) {
  315. return std::mismatch(container_algorithm_internal::c_begin(c1),
  316. container_algorithm_internal::c_end(c1),
  317. container_algorithm_internal::c_begin(c2),
  318. std::forward<BinaryPredicate>(pred));
  319. }
  320. // c_equal()
  321. //
  322. // Container-based version of the <algorithm> `std::equal()` function to
  323. // test whether two containers are equal.
  324. //
  325. // NOTE: the semantics of c_equal() are slightly different than those of
  326. // equal(): while the latter iterates over the second container only up to the
  327. // size of the first container, c_equal() also checks whether the container
  328. // sizes are equal. This better matches expectations about c_equal() based on
  329. // its signature.
  330. //
  331. // Example:
  332. // vector v1 = <1, 2, 3>;
  333. // vector v2 = <1, 2, 3, 4>;
  334. // equal(std::begin(v1), std::end(v1), std::begin(v2)) returns true
  335. // c_equal(v1, v2) returns false
  336. template <typename C1, typename C2>
  337. bool c_equal(const C1& c1, const C2& c2) {
  338. return ((container_algorithm_internal::c_size(c1) ==
  339. container_algorithm_internal::c_size(c2)) &&
  340. std::equal(container_algorithm_internal::c_begin(c1),
  341. container_algorithm_internal::c_end(c1),
  342. container_algorithm_internal::c_begin(c2)));
  343. }
  344. // Overload of c_equal() for using a predicate evaluation other than `==` as
  345. // the function's test condition.
  346. template <typename C1, typename C2, typename BinaryPredicate>
  347. bool c_equal(const C1& c1, const C2& c2, BinaryPredicate&& pred) {
  348. return ((container_algorithm_internal::c_size(c1) ==
  349. container_algorithm_internal::c_size(c2)) &&
  350. std::equal(container_algorithm_internal::c_begin(c1),
  351. container_algorithm_internal::c_end(c1),
  352. container_algorithm_internal::c_begin(c2),
  353. std::forward<BinaryPredicate>(pred)));
  354. }
  355. // c_is_permutation()
  356. //
  357. // Container-based version of the <algorithm> `std::is_permutation()` function
  358. // to test whether a container is a permutation of another.
  359. template <typename C1, typename C2>
  360. bool c_is_permutation(const C1& c1, const C2& c2) {
  361. using std::begin;
  362. using std::end;
  363. return c1.size() == c2.size() &&
  364. std::is_permutation(begin(c1), end(c1), begin(c2));
  365. }
  366. // Overload of c_is_permutation() for using a predicate evaluation other than
  367. // `==` as the function's test condition.
  368. template <typename C1, typename C2, typename BinaryPredicate>
  369. bool c_is_permutation(const C1& c1, const C2& c2, BinaryPredicate&& pred) {
  370. using std::begin;
  371. using std::end;
  372. return c1.size() == c2.size() &&
  373. std::is_permutation(begin(c1), end(c1), begin(c2),
  374. std::forward<BinaryPredicate>(pred));
  375. }
  376. // c_search()
  377. //
  378. // Container-based version of the <algorithm> `std::search()` function to search
  379. // a container for a subsequence.
  380. template <typename Sequence1, typename Sequence2>
  381. container_algorithm_internal::ContainerIter<Sequence1> c_search(
  382. Sequence1& sequence, Sequence2& subsequence) {
  383. return std::search(container_algorithm_internal::c_begin(sequence),
  384. container_algorithm_internal::c_end(sequence),
  385. container_algorithm_internal::c_begin(subsequence),
  386. container_algorithm_internal::c_end(subsequence));
  387. }
  388. // Overload of c_search() for using a predicate evaluation other than
  389. // `==` as the function's test condition.
  390. template <typename Sequence1, typename Sequence2, typename BinaryPredicate>
  391. container_algorithm_internal::ContainerIter<Sequence1> c_search(
  392. Sequence1& sequence, Sequence2& subsequence, BinaryPredicate&& pred) {
  393. return std::search(container_algorithm_internal::c_begin(sequence),
  394. container_algorithm_internal::c_end(sequence),
  395. container_algorithm_internal::c_begin(subsequence),
  396. container_algorithm_internal::c_end(subsequence),
  397. std::forward<BinaryPredicate>(pred));
  398. }
  399. // c_search_n()
  400. //
  401. // Container-based version of the <algorithm> `std::search_n()` function to
  402. // search a container for the first sequence of N elements.
  403. template <typename Sequence, typename Size, typename T>
  404. container_algorithm_internal::ContainerIter<Sequence> c_search_n(
  405. Sequence& sequence, Size count, T&& value) {
  406. return std::search_n(container_algorithm_internal::c_begin(sequence),
  407. container_algorithm_internal::c_end(sequence), count,
  408. std::forward<T>(value));
  409. }
  410. // Overload of c_search_n() for using a predicate evaluation other than
  411. // `==` as the function's test condition.
  412. template <typename Sequence, typename Size, typename T,
  413. typename BinaryPredicate>
  414. container_algorithm_internal::ContainerIter<Sequence> c_search_n(
  415. Sequence& sequence, Size count, T&& value, BinaryPredicate&& pred) {
  416. return std::search_n(container_algorithm_internal::c_begin(sequence),
  417. container_algorithm_internal::c_end(sequence), count,
  418. std::forward<T>(value),
  419. std::forward<BinaryPredicate>(pred));
  420. }
  421. //------------------------------------------------------------------------------
  422. // <algorithm> Modifying sequence operations
  423. //------------------------------------------------------------------------------
  424. // c_copy()
  425. //
  426. // Container-based version of the <algorithm> `std::copy()` function to copy a
  427. // container's elements into an iterator.
  428. template <typename InputSequence, typename OutputIterator>
  429. OutputIterator c_copy(const InputSequence& input, OutputIterator output) {
  430. return std::copy(container_algorithm_internal::c_begin(input),
  431. container_algorithm_internal::c_end(input), output);
  432. }
  433. // c_copy_n()
  434. //
  435. // Container-based version of the <algorithm> `std::copy_n()` function to copy a
  436. // container's first N elements into an iterator.
  437. template <typename C, typename Size, typename OutputIterator>
  438. OutputIterator c_copy_n(const C& input, Size n, OutputIterator output) {
  439. return std::copy_n(container_algorithm_internal::c_begin(input), n, output);
  440. }
  441. // c_copy_if()
  442. //
  443. // Container-based version of the <algorithm> `std::copy_if()` function to copy
  444. // a container's elements satisfying some condition into an iterator.
  445. template <typename InputSequence, typename OutputIterator, typename Pred>
  446. OutputIterator c_copy_if(const InputSequence& input, OutputIterator output,
  447. Pred&& pred) {
  448. return std::copy_if(container_algorithm_internal::c_begin(input),
  449. container_algorithm_internal::c_end(input), output,
  450. std::forward<Pred>(pred));
  451. }
  452. // c_copy_backward()
  453. //
  454. // Container-based version of the <algorithm> `std::copy_backward()` function to
  455. // copy a container's elements in reverse order into an iterator.
  456. template <typename C, typename BidirectionalIterator>
  457. BidirectionalIterator c_copy_backward(const C& src,
  458. BidirectionalIterator dest) {
  459. return std::copy_backward(container_algorithm_internal::c_begin(src),
  460. container_algorithm_internal::c_end(src), dest);
  461. }
  462. // c_move()
  463. //
  464. // Container-based version of the <algorithm> `std::move()` function to move
  465. // a container's elements into an iterator.
  466. template <typename C, typename OutputIterator>
  467. OutputIterator c_move(C&& src, OutputIterator dest) {
  468. return std::move(container_algorithm_internal::c_begin(src),
  469. container_algorithm_internal::c_end(src), dest);
  470. }
  471. // c_move_backward()
  472. //
  473. // Container-based version of the <algorithm> `std::move_backward()` function to
  474. // move a container's elements into an iterator in reverse order.
  475. template <typename C, typename BidirectionalIterator>
  476. BidirectionalIterator c_move_backward(C&& src, BidirectionalIterator dest) {
  477. return std::move_backward(container_algorithm_internal::c_begin(src),
  478. container_algorithm_internal::c_end(src), dest);
  479. }
  480. // c_swap_ranges()
  481. //
  482. // Container-based version of the <algorithm> `std::swap_ranges()` function to
  483. // swap a container's elements with another container's elements.
  484. template <typename C1, typename C2>
  485. container_algorithm_internal::ContainerIter<C2> c_swap_ranges(C1& c1, C2& c2) {
  486. return std::swap_ranges(container_algorithm_internal::c_begin(c1),
  487. container_algorithm_internal::c_end(c1),
  488. container_algorithm_internal::c_begin(c2));
  489. }
  490. // c_transform()
  491. //
  492. // Container-based version of the <algorithm> `std::transform()` function to
  493. // transform a container's elements using the unary operation, storing the
  494. // result in an iterator pointing to the last transformed element in the output
  495. // range.
  496. template <typename InputSequence, typename OutputIterator, typename UnaryOp>
  497. OutputIterator c_transform(const InputSequence& input, OutputIterator output,
  498. UnaryOp&& unary_op) {
  499. return std::transform(container_algorithm_internal::c_begin(input),
  500. container_algorithm_internal::c_end(input), output,
  501. std::forward<UnaryOp>(unary_op));
  502. }
  503. // Overload of c_transform() for performing a transformation using a binary
  504. // predicate.
  505. template <typename InputSequence1, typename InputSequence2,
  506. typename OutputIterator, typename BinaryOp>
  507. OutputIterator c_transform(const InputSequence1& input1,
  508. const InputSequence2& input2, OutputIterator output,
  509. BinaryOp&& binary_op) {
  510. return std::transform(container_algorithm_internal::c_begin(input1),
  511. container_algorithm_internal::c_end(input1),
  512. container_algorithm_internal::c_begin(input2), output,
  513. std::forward<BinaryOp>(binary_op));
  514. }
  515. // c_replace()
  516. //
  517. // Container-based version of the <algorithm> `std::replace()` function to
  518. // replace a container's elements of some value with a new value. The container
  519. // is modified in place.
  520. template <typename Sequence, typename T>
  521. void c_replace(Sequence& sequence, const T& old_value, const T& new_value) {
  522. std::replace(container_algorithm_internal::c_begin(sequence),
  523. container_algorithm_internal::c_end(sequence), old_value,
  524. new_value);
  525. }
  526. // c_replace_if()
  527. //
  528. // Container-based version of the <algorithm> `std::replace_if()` function to
  529. // replace a container's elements of some value with a new value based on some
  530. // condition. The container is modified in place.
  531. template <typename C, typename Pred, typename T>
  532. void c_replace_if(C& c, Pred&& pred, T&& new_value) {
  533. std::replace_if(container_algorithm_internal::c_begin(c),
  534. container_algorithm_internal::c_end(c),
  535. std::forward<Pred>(pred), std::forward<T>(new_value));
  536. }
  537. // c_replace_copy()
  538. //
  539. // Container-based version of the <algorithm> `std::replace_copy()` function to
  540. // replace a container's elements of some value with a new value and return the
  541. // results within an iterator.
  542. template <typename C, typename OutputIterator, typename T>
  543. OutputIterator c_replace_copy(const C& c, OutputIterator result, T&& old_value,
  544. T&& new_value) {
  545. return std::replace_copy(container_algorithm_internal::c_begin(c),
  546. container_algorithm_internal::c_end(c), result,
  547. std::forward<T>(old_value),
  548. std::forward<T>(new_value));
  549. }
  550. // c_replace_copy_if()
  551. //
  552. // Container-based version of the <algorithm> `std::replace_copy_if()` function
  553. // to replace a container's elements of some value with a new value based on
  554. // some condition, and return the results within an iterator.
  555. template <typename C, typename OutputIterator, typename Pred, typename T>
  556. OutputIterator c_replace_copy_if(const C& c, OutputIterator result, Pred&& pred,
  557. T&& new_value) {
  558. return std::replace_copy_if(container_algorithm_internal::c_begin(c),
  559. container_algorithm_internal::c_end(c), result,
  560. std::forward<Pred>(pred),
  561. std::forward<T>(new_value));
  562. }
  563. // c_fill()
  564. //
  565. // Container-based version of the <algorithm> `std::fill()` function to fill a
  566. // container with some value.
  567. template <typename C, typename T>
  568. void c_fill(C& c, T&& value) {
  569. std::fill(container_algorithm_internal::c_begin(c),
  570. container_algorithm_internal::c_end(c), std::forward<T>(value));
  571. }
  572. // c_fill_n()
  573. //
  574. // Container-based version of the <algorithm> `std::fill_n()` function to fill
  575. // the first N elements in a container with some value.
  576. template <typename C, typename Size, typename T>
  577. void c_fill_n(C& c, Size n, T&& value) {
  578. std::fill_n(container_algorithm_internal::c_begin(c), n,
  579. std::forward<T>(value));
  580. }
  581. // c_generate()
  582. //
  583. // Container-based version of the <algorithm> `std::generate()` function to
  584. // assign a container's elements to the values provided by the given generator.
  585. template <typename C, typename Generator>
  586. void c_generate(C& c, Generator&& gen) {
  587. std::generate(container_algorithm_internal::c_begin(c),
  588. container_algorithm_internal::c_end(c),
  589. std::forward<Generator>(gen));
  590. }
  591. // c_generate_n()
  592. //
  593. // Container-based version of the <algorithm> `std::generate_n()` function to
  594. // assign a container's first N elements to the values provided by the given
  595. // generator.
  596. template <typename C, typename Size, typename Generator>
  597. container_algorithm_internal::ContainerIter<C> c_generate_n(C& c, Size n,
  598. Generator&& gen) {
  599. return std::generate_n(container_algorithm_internal::c_begin(c), n,
  600. std::forward<Generator>(gen));
  601. }
  602. // Note: `c_xx()` <algorithm> container versions for `remove()`, `remove_if()`,
  603. // and `unique()` are omitted, because it's not clear whether or not such
  604. // functions should call erase on their supplied sequences afterwards. Either
  605. // behavior would be surprising for a different set of users.
  606. // c_remove_copy()
  607. //
  608. // Container-based version of the <algorithm> `std::remove_copy()` function to
  609. // copy a container's elements while removing any elements matching the given
  610. // `value`.
  611. template <typename C, typename OutputIterator, typename T>
  612. OutputIterator c_remove_copy(const C& c, OutputIterator result, T&& value) {
  613. return std::remove_copy(container_algorithm_internal::c_begin(c),
  614. container_algorithm_internal::c_end(c), result,
  615. std::forward<T>(value));
  616. }
  617. // c_remove_copy_if()
  618. //
  619. // Container-based version of the <algorithm> `std::remove_copy_if()` function
  620. // to copy a container's elements while removing any elements matching the given
  621. // condition.
  622. template <typename C, typename OutputIterator, typename Pred>
  623. OutputIterator c_remove_copy_if(const C& c, OutputIterator result,
  624. Pred&& pred) {
  625. return std::remove_copy_if(container_algorithm_internal::c_begin(c),
  626. container_algorithm_internal::c_end(c), result,
  627. std::forward<Pred>(pred));
  628. }
  629. // c_unique_copy()
  630. //
  631. // Container-based version of the <algorithm> `std::unique_copy()` function to
  632. // copy a container's elements while removing any elements containing duplicate
  633. // values.
  634. template <typename C, typename OutputIterator>
  635. OutputIterator c_unique_copy(const C& c, OutputIterator result) {
  636. return std::unique_copy(container_algorithm_internal::c_begin(c),
  637. container_algorithm_internal::c_end(c), result);
  638. }
  639. // Overload of c_unique_copy() for using a predicate evaluation other than
  640. // `==` for comparing uniqueness of the element values.
  641. template <typename C, typename OutputIterator, typename BinaryPredicate>
  642. OutputIterator c_unique_copy(const C& c, OutputIterator result,
  643. BinaryPredicate&& pred) {
  644. return std::unique_copy(container_algorithm_internal::c_begin(c),
  645. container_algorithm_internal::c_end(c), result,
  646. std::forward<BinaryPredicate>(pred));
  647. }
  648. // c_reverse()
  649. //
  650. // Container-based version of the <algorithm> `std::reverse()` function to
  651. // reverse a container's elements.
  652. template <typename Sequence>
  653. void c_reverse(Sequence& sequence) {
  654. std::reverse(container_algorithm_internal::c_begin(sequence),
  655. container_algorithm_internal::c_end(sequence));
  656. }
  657. // c_reverse_copy()
  658. //
  659. // Container-based version of the <algorithm> `std::reverse()` function to
  660. // reverse a container's elements and write them to an iterator range.
  661. template <typename C, typename OutputIterator>
  662. OutputIterator c_reverse_copy(const C& sequence, OutputIterator result) {
  663. return std::reverse_copy(container_algorithm_internal::c_begin(sequence),
  664. container_algorithm_internal::c_end(sequence),
  665. result);
  666. }
  667. // c_rotate()
  668. //
  669. // Container-based version of the <algorithm> `std::rotate()` function to
  670. // shift a container's elements leftward such that the `middle` element becomes
  671. // the first element in the container.
  672. template <typename C,
  673. typename Iterator = container_algorithm_internal::ContainerIter<C>>
  674. Iterator c_rotate(C& sequence, Iterator middle) {
  675. return absl::rotate(container_algorithm_internal::c_begin(sequence), middle,
  676. container_algorithm_internal::c_end(sequence));
  677. }
  678. // c_rotate_copy()
  679. //
  680. // Container-based version of the <algorithm> `std::rotate_copy()` function to
  681. // shift a container's elements leftward such that the `middle` element becomes
  682. // the first element in a new iterator range.
  683. template <typename C, typename OutputIterator>
  684. OutputIterator c_rotate_copy(
  685. const C& sequence,
  686. container_algorithm_internal::ContainerIter<const C> middle,
  687. OutputIterator result) {
  688. return std::rotate_copy(container_algorithm_internal::c_begin(sequence),
  689. middle, container_algorithm_internal::c_end(sequence),
  690. result);
  691. }
  692. // c_shuffle()
  693. //
  694. // Container-based version of the <algorithm> `std::shuffle()` function to
  695. // randomly shuffle elements within the container using a `gen()` uniform random
  696. // number generator.
  697. template <typename RandomAccessContainer, typename UniformRandomBitGenerator>
  698. void c_shuffle(RandomAccessContainer& c, UniformRandomBitGenerator&& gen) {
  699. std::shuffle(container_algorithm_internal::c_begin(c),
  700. container_algorithm_internal::c_end(c),
  701. std::forward<UniformRandomBitGenerator>(gen));
  702. }
  703. //------------------------------------------------------------------------------
  704. // <algorithm> Partition functions
  705. //------------------------------------------------------------------------------
  706. // c_is_partitioned()
  707. //
  708. // Container-based version of the <algorithm> `std::is_partitioned()` function
  709. // to test whether all elements in the container for which `pred` returns `true`
  710. // precede those for which `pred` is `false`.
  711. template <typename C, typename Pred>
  712. bool c_is_partitioned(const C& c, Pred&& pred) {
  713. return std::is_partitioned(container_algorithm_internal::c_begin(c),
  714. container_algorithm_internal::c_end(c),
  715. std::forward<Pred>(pred));
  716. }
  717. // c_partition()
  718. //
  719. // Container-based version of the <algorithm> `std::partition()` function
  720. // to rearrange all elements in a container in such a way that all elements for
  721. // which `pred` returns `true` precede all those for which it returns `false`,
  722. // returning an iterator to the first element of the second group.
  723. template <typename C, typename Pred>
  724. container_algorithm_internal::ContainerIter<C> c_partition(C& c, Pred&& pred) {
  725. return std::partition(container_algorithm_internal::c_begin(c),
  726. container_algorithm_internal::c_end(c),
  727. std::forward<Pred>(pred));
  728. }
  729. // c_stable_partition()
  730. //
  731. // Container-based version of the <algorithm> `std::stable_partition()` function
  732. // to rearrange all elements in a container in such a way that all elements for
  733. // which `pred` returns `true` precede all those for which it returns `false`,
  734. // preserving the relative ordering between the two groups. The function returns
  735. // an iterator to the first element of the second group.
  736. template <typename C, typename Pred>
  737. container_algorithm_internal::ContainerIter<C> c_stable_partition(C& c,
  738. Pred&& pred) {
  739. return std::stable_partition(container_algorithm_internal::c_begin(c),
  740. container_algorithm_internal::c_end(c),
  741. std::forward<Pred>(pred));
  742. }
  743. // c_partition_copy()
  744. //
  745. // Container-based version of the <algorithm> `std::partition_copy()` function
  746. // to partition a container's elements and return them into two iterators: one
  747. // for which `pred` returns `true`, and one for which `pred` returns `false.`
  748. template <typename C, typename OutputIterator1, typename OutputIterator2,
  749. typename Pred>
  750. std::pair<OutputIterator1, OutputIterator2> c_partition_copy(
  751. const C& c, OutputIterator1 out_true, OutputIterator2 out_false,
  752. Pred&& pred) {
  753. return std::partition_copy(container_algorithm_internal::c_begin(c),
  754. container_algorithm_internal::c_end(c), out_true,
  755. out_false, std::forward<Pred>(pred));
  756. }
  757. // c_partition_point()
  758. //
  759. // Container-based version of the <algorithm> `std::partition_point()` function
  760. // to return the first element of an already partitioned container for which
  761. // the given `pred` is not `true`.
  762. template <typename C, typename Pred>
  763. container_algorithm_internal::ContainerIter<C> c_partition_point(C& c,
  764. Pred&& pred) {
  765. return std::partition_point(container_algorithm_internal::c_begin(c),
  766. container_algorithm_internal::c_end(c),
  767. std::forward<Pred>(pred));
  768. }
  769. //------------------------------------------------------------------------------
  770. // <algorithm> Sorting functions
  771. //------------------------------------------------------------------------------
  772. // c_sort()
  773. //
  774. // Container-based version of the <algorithm> `std::sort()` function
  775. // to sort elements in ascending order of their values.
  776. template <typename C>
  777. void c_sort(C& c) {
  778. std::sort(container_algorithm_internal::c_begin(c),
  779. container_algorithm_internal::c_end(c));
  780. }
  781. // Overload of c_sort() for performing a `comp` comparison other than the
  782. // default `operator<`.
  783. template <typename C, typename Compare>
  784. void c_sort(C& c, Compare&& comp) {
  785. std::sort(container_algorithm_internal::c_begin(c),
  786. container_algorithm_internal::c_end(c),
  787. std::forward<Compare>(comp));
  788. }
  789. // c_stable_sort()
  790. //
  791. // Container-based version of the <algorithm> `std::stable_sort()` function
  792. // to sort elements in ascending order of their values, preserving the order
  793. // of equivalents.
  794. template <typename C>
  795. void c_stable_sort(C& c) {
  796. std::stable_sort(container_algorithm_internal::c_begin(c),
  797. container_algorithm_internal::c_end(c));
  798. }
  799. // Overload of c_stable_sort() for performing a `comp` comparison other than the
  800. // default `operator<`.
  801. template <typename C, typename Compare>
  802. void c_stable_sort(C& c, Compare&& comp) {
  803. std::stable_sort(container_algorithm_internal::c_begin(c),
  804. container_algorithm_internal::c_end(c),
  805. std::forward<Compare>(comp));
  806. }
  807. // c_is_sorted()
  808. //
  809. // Container-based version of the <algorithm> `std::is_sorted()` function
  810. // to evaluate whether the given container is sorted in ascending order.
  811. template <typename C>
  812. bool c_is_sorted(const C& c) {
  813. return std::is_sorted(container_algorithm_internal::c_begin(c),
  814. container_algorithm_internal::c_end(c));
  815. }
  816. // c_is_sorted() overload for performing a `comp` comparison other than the
  817. // default `operator<`.
  818. template <typename C, typename Compare>
  819. bool c_is_sorted(const C& c, Compare&& comp) {
  820. return std::is_sorted(container_algorithm_internal::c_begin(c),
  821. container_algorithm_internal::c_end(c),
  822. std::forward<Compare>(comp));
  823. }
  824. // c_partial_sort()
  825. //
  826. // Container-based version of the <algorithm> `std::partial_sort()` function
  827. // to rearrange elements within a container such that elements before `middle`
  828. // are sorted in ascending order.
  829. template <typename RandomAccessContainer>
  830. void c_partial_sort(
  831. RandomAccessContainer& sequence,
  832. container_algorithm_internal::ContainerIter<RandomAccessContainer> middle) {
  833. std::partial_sort(container_algorithm_internal::c_begin(sequence), middle,
  834. container_algorithm_internal::c_end(sequence));
  835. }
  836. // Overload of c_partial_sort() for performing a `comp` comparison other than
  837. // the default `operator<`.
  838. template <typename RandomAccessContainer, typename Compare>
  839. void c_partial_sort(
  840. RandomAccessContainer& sequence,
  841. container_algorithm_internal::ContainerIter<RandomAccessContainer> middle,
  842. Compare&& comp) {
  843. std::partial_sort(container_algorithm_internal::c_begin(sequence), middle,
  844. container_algorithm_internal::c_end(sequence),
  845. std::forward<Compare>(comp));
  846. }
  847. // c_partial_sort_copy()
  848. //
  849. // Container-based version of the <algorithm> `std::partial_sort_copy()`
  850. // function to sort the elements in the given range `result` within the larger
  851. // `sequence` in ascending order (and using `result` as the output parameter).
  852. // At most min(result.last - result.first, sequence.last - sequence.first)
  853. // elements from the sequence will be stored in the result.
  854. template <typename C, typename RandomAccessContainer>
  855. container_algorithm_internal::ContainerIter<RandomAccessContainer>
  856. c_partial_sort_copy(const C& sequence, RandomAccessContainer& result) {
  857. return std::partial_sort_copy(container_algorithm_internal::c_begin(sequence),
  858. container_algorithm_internal::c_end(sequence),
  859. container_algorithm_internal::c_begin(result),
  860. container_algorithm_internal::c_end(result));
  861. }
  862. // Overload of c_partial_sort_copy() for performing a `comp` comparison other
  863. // than the default `operator<`.
  864. template <typename C, typename RandomAccessContainer, typename Compare>
  865. container_algorithm_internal::ContainerIter<RandomAccessContainer>
  866. c_partial_sort_copy(const C& sequence, RandomAccessContainer& result,
  867. Compare&& comp) {
  868. return std::partial_sort_copy(container_algorithm_internal::c_begin(sequence),
  869. container_algorithm_internal::c_end(sequence),
  870. container_algorithm_internal::c_begin(result),
  871. container_algorithm_internal::c_end(result),
  872. std::forward<Compare>(comp));
  873. }
  874. // c_is_sorted_until()
  875. //
  876. // Container-based version of the <algorithm> `std::is_sorted_until()` function
  877. // to return the first element within a container that is not sorted in
  878. // ascending order as an iterator.
  879. template <typename C>
  880. container_algorithm_internal::ContainerIter<C> c_is_sorted_until(C& c) {
  881. return std::is_sorted_until(container_algorithm_internal::c_begin(c),
  882. container_algorithm_internal::c_end(c));
  883. }
  884. // Overload of c_is_sorted_until() for performing a `comp` comparison other than
  885. // the default `operator<`.
  886. template <typename C, typename Compare>
  887. container_algorithm_internal::ContainerIter<C> c_is_sorted_until(
  888. C& c, Compare&& comp) {
  889. return std::is_sorted_until(container_algorithm_internal::c_begin(c),
  890. container_algorithm_internal::c_end(c),
  891. std::forward<Compare>(comp));
  892. }
  893. // c_nth_element()
  894. //
  895. // Container-based version of the <algorithm> `std::nth_element()` function
  896. // to rearrange the elements within a container such that the `nth` element
  897. // would be in that position in an ordered sequence; other elements may be in
  898. // any order, except that all preceding `nth` will be less than that element,
  899. // and all following `nth` will be greater than that element.
  900. template <typename RandomAccessContainer>
  901. void c_nth_element(
  902. RandomAccessContainer& sequence,
  903. container_algorithm_internal::ContainerIter<RandomAccessContainer> nth) {
  904. std::nth_element(container_algorithm_internal::c_begin(sequence), nth,
  905. container_algorithm_internal::c_end(sequence));
  906. }
  907. // Overload of c_nth_element() for performing a `comp` comparison other than
  908. // the default `operator<`.
  909. template <typename RandomAccessContainer, typename Compare>
  910. void c_nth_element(
  911. RandomAccessContainer& sequence,
  912. container_algorithm_internal::ContainerIter<RandomAccessContainer> nth,
  913. Compare&& comp) {
  914. std::nth_element(container_algorithm_internal::c_begin(sequence), nth,
  915. container_algorithm_internal::c_end(sequence),
  916. std::forward<Compare>(comp));
  917. }
  918. //------------------------------------------------------------------------------
  919. // <algorithm> Binary Search
  920. //------------------------------------------------------------------------------
  921. // c_lower_bound()
  922. //
  923. // Container-based version of the <algorithm> `std::lower_bound()` function
  924. // to return an iterator pointing to the first element in a sorted container
  925. // which does not compare less than `value`.
  926. template <typename Sequence, typename T>
  927. container_algorithm_internal::ContainerIter<Sequence> c_lower_bound(
  928. Sequence& sequence, T&& value) {
  929. return std::lower_bound(container_algorithm_internal::c_begin(sequence),
  930. container_algorithm_internal::c_end(sequence),
  931. std::forward<T>(value));
  932. }
  933. // Overload of c_lower_bound() for performing a `comp` comparison other than
  934. // the default `operator<`.
  935. template <typename Sequence, typename T, typename Compare>
  936. container_algorithm_internal::ContainerIter<Sequence> c_lower_bound(
  937. Sequence& sequence, T&& value, Compare&& comp) {
  938. return std::lower_bound(container_algorithm_internal::c_begin(sequence),
  939. container_algorithm_internal::c_end(sequence),
  940. std::forward<T>(value), std::forward<Compare>(comp));
  941. }
  942. // c_upper_bound()
  943. //
  944. // Container-based version of the <algorithm> `std::upper_bound()` function
  945. // to return an iterator pointing to the first element in a sorted container
  946. // which is greater than `value`.
  947. template <typename Sequence, typename T>
  948. container_algorithm_internal::ContainerIter<Sequence> c_upper_bound(
  949. Sequence& sequence, T&& value) {
  950. return std::upper_bound(container_algorithm_internal::c_begin(sequence),
  951. container_algorithm_internal::c_end(sequence),
  952. std::forward<T>(value));
  953. }
  954. // Overload of c_upper_bound() for performing a `comp` comparison other than
  955. // the default `operator<`.
  956. template <typename Sequence, typename T, typename Compare>
  957. container_algorithm_internal::ContainerIter<Sequence> c_upper_bound(
  958. Sequence& sequence, T&& value, Compare&& comp) {
  959. return std::upper_bound(container_algorithm_internal::c_begin(sequence),
  960. container_algorithm_internal::c_end(sequence),
  961. std::forward<T>(value), std::forward<Compare>(comp));
  962. }
  963. // c_equal_range()
  964. //
  965. // Container-based version of the <algorithm> `std::equal_range()` function
  966. // to return an iterator pair pointing to the first and last elements in a
  967. // sorted container which compare equal to `value`.
  968. template <typename Sequence, typename T>
  969. container_algorithm_internal::ContainerIterPairType<Sequence, Sequence>
  970. c_equal_range(Sequence& sequence, T&& value) {
  971. return std::equal_range(container_algorithm_internal::c_begin(sequence),
  972. container_algorithm_internal::c_end(sequence),
  973. std::forward<T>(value));
  974. }
  975. // Overload of c_equal_range() for performing a `comp` comparison other than
  976. // the default `operator<`.
  977. template <typename Sequence, typename T, typename Compare>
  978. container_algorithm_internal::ContainerIterPairType<Sequence, Sequence>
  979. c_equal_range(Sequence& sequence, T&& value, Compare&& comp) {
  980. return std::equal_range(container_algorithm_internal::c_begin(sequence),
  981. container_algorithm_internal::c_end(sequence),
  982. std::forward<T>(value), std::forward<Compare>(comp));
  983. }
  984. // c_binary_search()
  985. //
  986. // Container-based version of the <algorithm> `std::binary_search()` function
  987. // to test if any element in the sorted container contains a value equivalent to
  988. // 'value'.
  989. template <typename Sequence, typename T>
  990. bool c_binary_search(Sequence&& sequence, T&& value) {
  991. return std::binary_search(container_algorithm_internal::c_begin(sequence),
  992. container_algorithm_internal::c_end(sequence),
  993. std::forward<T>(value));
  994. }
  995. // Overload of c_binary_search() for performing a `comp` comparison other than
  996. // the default `operator<`.
  997. template <typename Sequence, typename T, typename Compare>
  998. bool c_binary_search(Sequence&& sequence, T&& value, Compare&& comp) {
  999. return std::binary_search(container_algorithm_internal::c_begin(sequence),
  1000. container_algorithm_internal::c_end(sequence),
  1001. std::forward<T>(value),
  1002. std::forward<Compare>(comp));
  1003. }
  1004. //------------------------------------------------------------------------------
  1005. // <algorithm> Merge functions
  1006. //------------------------------------------------------------------------------
  1007. // c_merge()
  1008. //
  1009. // Container-based version of the <algorithm> `std::merge()` function
  1010. // to merge two sorted containers into a single sorted iterator.
  1011. template <typename C1, typename C2, typename OutputIterator>
  1012. OutputIterator c_merge(const C1& c1, const C2& c2, OutputIterator result) {
  1013. return std::merge(container_algorithm_internal::c_begin(c1),
  1014. container_algorithm_internal::c_end(c1),
  1015. container_algorithm_internal::c_begin(c2),
  1016. container_algorithm_internal::c_end(c2), result);
  1017. }
  1018. // Overload of c_merge() for performing a `comp` comparison other than
  1019. // the default `operator<`.
  1020. template <typename C1, typename C2, typename OutputIterator, typename Compare>
  1021. OutputIterator c_merge(const C1& c1, const C2& c2, OutputIterator result,
  1022. Compare&& comp) {
  1023. return std::merge(container_algorithm_internal::c_begin(c1),
  1024. container_algorithm_internal::c_end(c1),
  1025. container_algorithm_internal::c_begin(c2),
  1026. container_algorithm_internal::c_end(c2), result,
  1027. std::forward<Compare>(comp));
  1028. }
  1029. // c_inplace_merge()
  1030. //
  1031. // Container-based version of the <algorithm> `std::inplace_merge()` function
  1032. // to merge a supplied iterator `middle` into a container.
  1033. template <typename C>
  1034. void c_inplace_merge(C& c,
  1035. container_algorithm_internal::ContainerIter<C> middle) {
  1036. std::inplace_merge(container_algorithm_internal::c_begin(c), middle,
  1037. container_algorithm_internal::c_end(c));
  1038. }
  1039. // Overload of c_inplace_merge() for performing a merge using a `comp` other
  1040. // than `operator<`.
  1041. template <typename C, typename Compare>
  1042. void c_inplace_merge(C& c,
  1043. container_algorithm_internal::ContainerIter<C> middle,
  1044. Compare&& comp) {
  1045. std::inplace_merge(container_algorithm_internal::c_begin(c), middle,
  1046. container_algorithm_internal::c_end(c),
  1047. std::forward<Compare>(comp));
  1048. }
  1049. // c_includes()
  1050. //
  1051. // Container-based version of the <algorithm> `std::includes()` function
  1052. // to test whether a sorted container `c1` entirely contains another sorted
  1053. // container `c2`.
  1054. template <typename C1, typename C2>
  1055. bool c_includes(const C1& c1, const C2& c2) {
  1056. return std::includes(container_algorithm_internal::c_begin(c1),
  1057. container_algorithm_internal::c_end(c1),
  1058. container_algorithm_internal::c_begin(c2),
  1059. container_algorithm_internal::c_end(c2));
  1060. }
  1061. // Overload of c_includes() for performing a merge using a `comp` other than
  1062. // `operator<`.
  1063. template <typename C1, typename C2, typename Compare>
  1064. bool c_includes(const C1& c1, const C2& c2, Compare&& comp) {
  1065. return std::includes(container_algorithm_internal::c_begin(c1),
  1066. container_algorithm_internal::c_end(c1),
  1067. container_algorithm_internal::c_begin(c2),
  1068. container_algorithm_internal::c_end(c2),
  1069. std::forward<Compare>(comp));
  1070. }
  1071. // c_set_union()
  1072. //
  1073. // Container-based version of the <algorithm> `std::set_union()` function
  1074. // to return an iterator containing the union of two containers; duplicate
  1075. // values are not copied into the output.
  1076. template <typename C1, typename C2, typename OutputIterator,
  1077. typename = typename std::enable_if<
  1078. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1079. void>::type,
  1080. typename = typename std::enable_if<
  1081. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1082. void>::type>
  1083. OutputIterator c_set_union(const C1& c1, const C2& c2, OutputIterator output) {
  1084. return std::set_union(container_algorithm_internal::c_begin(c1),
  1085. container_algorithm_internal::c_end(c1),
  1086. container_algorithm_internal::c_begin(c2),
  1087. container_algorithm_internal::c_end(c2), output);
  1088. }
  1089. // Overload of c_set_union() for performing a merge using a `comp` other than
  1090. // `operator<`.
  1091. template <typename C1, typename C2, typename OutputIterator, typename Compare,
  1092. typename = typename std::enable_if<
  1093. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1094. void>::type,
  1095. typename = typename std::enable_if<
  1096. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1097. void>::type>
  1098. OutputIterator c_set_union(const C1& c1, const C2& c2, OutputIterator output,
  1099. Compare&& comp) {
  1100. return std::set_union(container_algorithm_internal::c_begin(c1),
  1101. container_algorithm_internal::c_end(c1),
  1102. container_algorithm_internal::c_begin(c2),
  1103. container_algorithm_internal::c_end(c2), output,
  1104. std::forward<Compare>(comp));
  1105. }
  1106. // c_set_intersection()
  1107. //
  1108. // Container-based version of the <algorithm> `std::set_intersection()` function
  1109. // to return an iterator containing the intersection of two containers.
  1110. template <typename C1, typename C2, typename OutputIterator,
  1111. typename = typename std::enable_if<
  1112. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1113. void>::type,
  1114. typename = typename std::enable_if<
  1115. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1116. void>::type>
  1117. OutputIterator c_set_intersection(const C1& c1, const C2& c2,
  1118. OutputIterator output) {
  1119. return std::set_intersection(container_algorithm_internal::c_begin(c1),
  1120. container_algorithm_internal::c_end(c1),
  1121. container_algorithm_internal::c_begin(c2),
  1122. container_algorithm_internal::c_end(c2), output);
  1123. }
  1124. // Overload of c_set_intersection() for performing a merge using a `comp` other
  1125. // than `operator<`.
  1126. template <typename C1, typename C2, typename OutputIterator, typename Compare,
  1127. typename = typename std::enable_if<
  1128. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1129. void>::type,
  1130. typename = typename std::enable_if<
  1131. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1132. void>::type>
  1133. OutputIterator c_set_intersection(const C1& c1, const C2& c2,
  1134. OutputIterator output, Compare&& comp) {
  1135. return std::set_intersection(container_algorithm_internal::c_begin(c1),
  1136. container_algorithm_internal::c_end(c1),
  1137. container_algorithm_internal::c_begin(c2),
  1138. container_algorithm_internal::c_end(c2), output,
  1139. std::forward<Compare>(comp));
  1140. }
  1141. // c_set_difference()
  1142. //
  1143. // Container-based version of the <algorithm> `std::set_difference()` function
  1144. // to return an iterator containing elements present in the first container but
  1145. // not in the second.
  1146. template <typename C1, typename C2, typename OutputIterator,
  1147. typename = typename std::enable_if<
  1148. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1149. void>::type,
  1150. typename = typename std::enable_if<
  1151. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1152. void>::type>
  1153. OutputIterator c_set_difference(const C1& c1, const C2& c2,
  1154. OutputIterator output) {
  1155. return std::set_difference(container_algorithm_internal::c_begin(c1),
  1156. container_algorithm_internal::c_end(c1),
  1157. container_algorithm_internal::c_begin(c2),
  1158. container_algorithm_internal::c_end(c2), output);
  1159. }
  1160. // Overload of c_set_difference() for performing a merge using a `comp` other
  1161. // than `operator<`.
  1162. template <typename C1, typename C2, typename OutputIterator, typename Compare,
  1163. typename = typename std::enable_if<
  1164. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1165. void>::type,
  1166. typename = typename std::enable_if<
  1167. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1168. void>::type>
  1169. OutputIterator c_set_difference(const C1& c1, const C2& c2,
  1170. OutputIterator output, Compare&& comp) {
  1171. return std::set_difference(container_algorithm_internal::c_begin(c1),
  1172. container_algorithm_internal::c_end(c1),
  1173. container_algorithm_internal::c_begin(c2),
  1174. container_algorithm_internal::c_end(c2), output,
  1175. std::forward<Compare>(comp));
  1176. }
  1177. // c_set_symmetric_difference()
  1178. //
  1179. // Container-based version of the <algorithm> `std::set_symmetric_difference()`
  1180. // function to return an iterator containing elements present in either one
  1181. // container or the other, but not both.
  1182. template <typename C1, typename C2, typename OutputIterator,
  1183. typename = typename std::enable_if<
  1184. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1185. void>::type,
  1186. typename = typename std::enable_if<
  1187. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1188. void>::type>
  1189. OutputIterator c_set_symmetric_difference(const C1& c1, const C2& c2,
  1190. OutputIterator output) {
  1191. return std::set_symmetric_difference(
  1192. container_algorithm_internal::c_begin(c1),
  1193. container_algorithm_internal::c_end(c1),
  1194. container_algorithm_internal::c_begin(c2),
  1195. container_algorithm_internal::c_end(c2), output);
  1196. }
  1197. // Overload of c_set_symmetric_difference() for performing a merge using a
  1198. // `comp` other than `operator<`.
  1199. template <typename C1, typename C2, typename OutputIterator, typename Compare,
  1200. typename = typename std::enable_if<
  1201. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1202. void>::type,
  1203. typename = typename std::enable_if<
  1204. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1205. void>::type>
  1206. OutputIterator c_set_symmetric_difference(const C1& c1, const C2& c2,
  1207. OutputIterator output,
  1208. Compare&& comp) {
  1209. return std::set_symmetric_difference(
  1210. container_algorithm_internal::c_begin(c1),
  1211. container_algorithm_internal::c_end(c1),
  1212. container_algorithm_internal::c_begin(c2),
  1213. container_algorithm_internal::c_end(c2), output,
  1214. std::forward<Compare>(comp));
  1215. }
  1216. //------------------------------------------------------------------------------
  1217. // <algorithm> Heap functions
  1218. //------------------------------------------------------------------------------
  1219. // c_push_heap()
  1220. //
  1221. // Container-based version of the <algorithm> `std::push_heap()` function
  1222. // to push a value onto a container heap.
  1223. template <typename RandomAccessContainer>
  1224. void c_push_heap(RandomAccessContainer& sequence) {
  1225. std::push_heap(container_algorithm_internal::c_begin(sequence),
  1226. container_algorithm_internal::c_end(sequence));
  1227. }
  1228. // Overload of c_push_heap() for performing a push operation on a heap using a
  1229. // `comp` other than `operator<`.
  1230. template <typename RandomAccessContainer, typename Compare>
  1231. void c_push_heap(RandomAccessContainer& sequence, Compare&& comp) {
  1232. std::push_heap(container_algorithm_internal::c_begin(sequence),
  1233. container_algorithm_internal::c_end(sequence),
  1234. std::forward<Compare>(comp));
  1235. }
  1236. // c_pop_heap()
  1237. //
  1238. // Container-based version of the <algorithm> `std::pop_heap()` function
  1239. // to pop a value from a heap container.
  1240. template <typename RandomAccessContainer>
  1241. void c_pop_heap(RandomAccessContainer& sequence) {
  1242. std::pop_heap(container_algorithm_internal::c_begin(sequence),
  1243. container_algorithm_internal::c_end(sequence));
  1244. }
  1245. // Overload of c_pop_heap() for performing a pop operation on a heap using a
  1246. // `comp` other than `operator<`.
  1247. template <typename RandomAccessContainer, typename Compare>
  1248. void c_pop_heap(RandomAccessContainer& sequence, Compare&& comp) {
  1249. std::pop_heap(container_algorithm_internal::c_begin(sequence),
  1250. container_algorithm_internal::c_end(sequence),
  1251. std::forward<Compare>(comp));
  1252. }
  1253. // c_make_heap()
  1254. //
  1255. // Container-based version of the <algorithm> `std::make_heap()` function
  1256. // to make a container a heap.
  1257. template <typename RandomAccessContainer>
  1258. void c_make_heap(RandomAccessContainer& sequence) {
  1259. std::make_heap(container_algorithm_internal::c_begin(sequence),
  1260. container_algorithm_internal::c_end(sequence));
  1261. }
  1262. // Overload of c_make_heap() for performing heap comparisons using a
  1263. // `comp` other than `operator<`
  1264. template <typename RandomAccessContainer, typename Compare>
  1265. void c_make_heap(RandomAccessContainer& sequence, Compare&& comp) {
  1266. std::make_heap(container_algorithm_internal::c_begin(sequence),
  1267. container_algorithm_internal::c_end(sequence),
  1268. std::forward<Compare>(comp));
  1269. }
  1270. // c_sort_heap()
  1271. //
  1272. // Container-based version of the <algorithm> `std::sort_heap()` function
  1273. // to sort a heap into ascending order (after which it is no longer a heap).
  1274. template <typename RandomAccessContainer>
  1275. void c_sort_heap(RandomAccessContainer& sequence) {
  1276. std::sort_heap(container_algorithm_internal::c_begin(sequence),
  1277. container_algorithm_internal::c_end(sequence));
  1278. }
  1279. // Overload of c_sort_heap() for performing heap comparisons using a
  1280. // `comp` other than `operator<`
  1281. template <typename RandomAccessContainer, typename Compare>
  1282. void c_sort_heap(RandomAccessContainer& sequence, Compare&& comp) {
  1283. std::sort_heap(container_algorithm_internal::c_begin(sequence),
  1284. container_algorithm_internal::c_end(sequence),
  1285. std::forward<Compare>(comp));
  1286. }
  1287. // c_is_heap()
  1288. //
  1289. // Container-based version of the <algorithm> `std::is_heap()` function
  1290. // to check whether the given container is a heap.
  1291. template <typename RandomAccessContainer>
  1292. bool c_is_heap(const RandomAccessContainer& sequence) {
  1293. return std::is_heap(container_algorithm_internal::c_begin(sequence),
  1294. container_algorithm_internal::c_end(sequence));
  1295. }
  1296. // Overload of c_is_heap() for performing heap comparisons using a
  1297. // `comp` other than `operator<`
  1298. template <typename RandomAccessContainer, typename Compare>
  1299. bool c_is_heap(const RandomAccessContainer& sequence, Compare&& comp) {
  1300. return std::is_heap(container_algorithm_internal::c_begin(sequence),
  1301. container_algorithm_internal::c_end(sequence),
  1302. std::forward<Compare>(comp));
  1303. }
  1304. // c_is_heap_until()
  1305. //
  1306. // Container-based version of the <algorithm> `std::is_heap_until()` function
  1307. // to find the first element in a given container which is not in heap order.
  1308. template <typename RandomAccessContainer>
  1309. container_algorithm_internal::ContainerIter<RandomAccessContainer>
  1310. c_is_heap_until(RandomAccessContainer& sequence) {
  1311. return std::is_heap_until(container_algorithm_internal::c_begin(sequence),
  1312. container_algorithm_internal::c_end(sequence));
  1313. }
  1314. // Overload of c_is_heap_until() for performing heap comparisons using a
  1315. // `comp` other than `operator<`
  1316. template <typename RandomAccessContainer, typename Compare>
  1317. container_algorithm_internal::ContainerIter<RandomAccessContainer>
  1318. c_is_heap_until(RandomAccessContainer& sequence, Compare&& comp) {
  1319. return std::is_heap_until(container_algorithm_internal::c_begin(sequence),
  1320. container_algorithm_internal::c_end(sequence),
  1321. std::forward<Compare>(comp));
  1322. }
  1323. //------------------------------------------------------------------------------
  1324. // <algorithm> Min/max
  1325. //------------------------------------------------------------------------------
  1326. // c_min_element()
  1327. //
  1328. // Container-based version of the <algorithm> `std::min_element()` function
  1329. // to return an iterator pointing to the element with the smallest value, using
  1330. // `operator<` to make the comparisons.
  1331. template <typename Sequence>
  1332. container_algorithm_internal::ContainerIter<Sequence> c_min_element(
  1333. Sequence& sequence) {
  1334. return std::min_element(container_algorithm_internal::c_begin(sequence),
  1335. container_algorithm_internal::c_end(sequence));
  1336. }
  1337. // Overload of c_min_element() for performing a `comp` comparison other than
  1338. // `operator<`.
  1339. template <typename Sequence, typename Compare>
  1340. container_algorithm_internal::ContainerIter<Sequence> c_min_element(
  1341. Sequence& sequence, Compare&& comp) {
  1342. return std::min_element(container_algorithm_internal::c_begin(sequence),
  1343. container_algorithm_internal::c_end(sequence),
  1344. std::forward<Compare>(comp));
  1345. }
  1346. // c_max_element()
  1347. //
  1348. // Container-based version of the <algorithm> `std::max_element()` function
  1349. // to return an iterator pointing to the element with the largest value, using
  1350. // `operator<` to make the comparisons.
  1351. template <typename Sequence>
  1352. container_algorithm_internal::ContainerIter<Sequence> c_max_element(
  1353. Sequence& sequence) {
  1354. return std::max_element(container_algorithm_internal::c_begin(sequence),
  1355. container_algorithm_internal::c_end(sequence));
  1356. }
  1357. // Overload of c_max_element() for performing a `comp` comparison other than
  1358. // `operator<`.
  1359. template <typename Sequence, typename Compare>
  1360. container_algorithm_internal::ContainerIter<Sequence> c_max_element(
  1361. Sequence& sequence, Compare&& comp) {
  1362. return std::max_element(container_algorithm_internal::c_begin(sequence),
  1363. container_algorithm_internal::c_end(sequence),
  1364. std::forward<Compare>(comp));
  1365. }
  1366. // c_minmax_element()
  1367. //
  1368. // Container-based version of the <algorithm> `std::minmax_element()` function
  1369. // to return a pair of iterators pointing to the elements containing the
  1370. // smallest and largest values, respectively, using `operator<` to make the
  1371. // comparisons.
  1372. template <typename C>
  1373. container_algorithm_internal::ContainerIterPairType<C, C>
  1374. c_minmax_element(C& c) {
  1375. return std::minmax_element(container_algorithm_internal::c_begin(c),
  1376. container_algorithm_internal::c_end(c));
  1377. }
  1378. // Overload of c_minmax_element() for performing `comp` comparisons other than
  1379. // `operator<`.
  1380. template <typename C, typename Compare>
  1381. container_algorithm_internal::ContainerIterPairType<C, C>
  1382. c_minmax_element(C& c, Compare&& comp) {
  1383. return std::minmax_element(container_algorithm_internal::c_begin(c),
  1384. container_algorithm_internal::c_end(c),
  1385. std::forward<Compare>(comp));
  1386. }
  1387. //------------------------------------------------------------------------------
  1388. // <algorithm> Lexicographical Comparisons
  1389. //------------------------------------------------------------------------------
  1390. // c_lexicographical_compare()
  1391. //
  1392. // Container-based version of the <algorithm> `std::lexicographical_compare()`
  1393. // function to lexicographically compare (e.g. sort words alphabetically) two
  1394. // container sequences. The comparison is performed using `operator<`. Note
  1395. // that capital letters ("A-Z") have ASCII values less than lowercase letters
  1396. // ("a-z").
  1397. template <typename Sequence1, typename Sequence2>
  1398. bool c_lexicographical_compare(Sequence1&& sequence1, Sequence2&& sequence2) {
  1399. return std::lexicographical_compare(
  1400. container_algorithm_internal::c_begin(sequence1),
  1401. container_algorithm_internal::c_end(sequence1),
  1402. container_algorithm_internal::c_begin(sequence2),
  1403. container_algorithm_internal::c_end(sequence2));
  1404. }
  1405. // Overload of c_lexicographical_compare() for performing a lexicographical
  1406. // comparison using a `comp` operator instead of `operator<`.
  1407. template <typename Sequence1, typename Sequence2, typename Compare>
  1408. bool c_lexicographical_compare(Sequence1&& sequence1, Sequence2&& sequence2,
  1409. Compare&& comp) {
  1410. return std::lexicographical_compare(
  1411. container_algorithm_internal::c_begin(sequence1),
  1412. container_algorithm_internal::c_end(sequence1),
  1413. container_algorithm_internal::c_begin(sequence2),
  1414. container_algorithm_internal::c_end(sequence2),
  1415. std::forward<Compare>(comp));
  1416. }
  1417. // c_next_permutation()
  1418. //
  1419. // Container-based version of the <algorithm> `std::next_permutation()` function
  1420. // to rearrange a container's elements into the next lexicographically greater
  1421. // permutation.
  1422. template <typename C>
  1423. bool c_next_permutation(C& c) {
  1424. return std::next_permutation(container_algorithm_internal::c_begin(c),
  1425. container_algorithm_internal::c_end(c));
  1426. }
  1427. // Overload of c_next_permutation() for performing a lexicographical
  1428. // comparison using a `comp` operator instead of `operator<`.
  1429. template <typename C, typename Compare>
  1430. bool c_next_permutation(C& c, Compare&& comp) {
  1431. return std::next_permutation(container_algorithm_internal::c_begin(c),
  1432. container_algorithm_internal::c_end(c),
  1433. std::forward<Compare>(comp));
  1434. }
  1435. // c_prev_permutation()
  1436. //
  1437. // Container-based version of the <algorithm> `std::prev_permutation()` function
  1438. // to rearrange a container's elements into the next lexicographically lesser
  1439. // permutation.
  1440. template <typename C>
  1441. bool c_prev_permutation(C& c) {
  1442. return std::prev_permutation(container_algorithm_internal::c_begin(c),
  1443. container_algorithm_internal::c_end(c));
  1444. }
  1445. // Overload of c_prev_permutation() for performing a lexicographical
  1446. // comparison using a `comp` operator instead of `operator<`.
  1447. template <typename C, typename Compare>
  1448. bool c_prev_permutation(C& c, Compare&& comp) {
  1449. return std::prev_permutation(container_algorithm_internal::c_begin(c),
  1450. container_algorithm_internal::c_end(c),
  1451. std::forward<Compare>(comp));
  1452. }
  1453. //------------------------------------------------------------------------------
  1454. // <numeric> algorithms
  1455. //------------------------------------------------------------------------------
  1456. // c_iota()
  1457. //
  1458. // Container-based version of the <algorithm> `std::iota()` function
  1459. // to compute successive values of `value`, as if incremented with `++value`
  1460. // after each element is written. and write them to the container.
  1461. template <typename Sequence, typename T>
  1462. void c_iota(Sequence& sequence, T&& value) {
  1463. std::iota(container_algorithm_internal::c_begin(sequence),
  1464. container_algorithm_internal::c_end(sequence),
  1465. std::forward<T>(value));
  1466. }
  1467. // c_accumulate()
  1468. //
  1469. // Container-based version of the <algorithm> `std::accumulate()` function
  1470. // to accumulate the element values of a container to `init` and return that
  1471. // accumulation by value.
  1472. //
  1473. // Note: Due to a language technicality this function has return type
  1474. // absl::decay_t<T>. As a user of this function you can casually read
  1475. // this as "returns T by value" and assume it does the right thing.
  1476. template <typename Sequence, typename T>
  1477. decay_t<T> c_accumulate(const Sequence& sequence, T&& init) {
  1478. return std::accumulate(container_algorithm_internal::c_begin(sequence),
  1479. container_algorithm_internal::c_end(sequence),
  1480. std::forward<T>(init));
  1481. }
  1482. // Overload of c_accumulate() for using a binary operations other than
  1483. // addition for computing the accumulation.
  1484. template <typename Sequence, typename T, typename BinaryOp>
  1485. decay_t<T> c_accumulate(const Sequence& sequence, T&& init,
  1486. BinaryOp&& binary_op) {
  1487. return std::accumulate(container_algorithm_internal::c_begin(sequence),
  1488. container_algorithm_internal::c_end(sequence),
  1489. std::forward<T>(init),
  1490. std::forward<BinaryOp>(binary_op));
  1491. }
  1492. // c_inner_product()
  1493. //
  1494. // Container-based version of the <algorithm> `std::inner_product()` function
  1495. // to compute the cumulative inner product of container element pairs.
  1496. //
  1497. // Note: Due to a language technicality this function has return type
  1498. // absl::decay_t<T>. As a user of this function you can casually read
  1499. // this as "returns T by value" and assume it does the right thing.
  1500. template <typename Sequence1, typename Sequence2, typename T>
  1501. decay_t<T> c_inner_product(const Sequence1& factors1, const Sequence2& factors2,
  1502. T&& sum) {
  1503. return std::inner_product(container_algorithm_internal::c_begin(factors1),
  1504. container_algorithm_internal::c_end(factors1),
  1505. container_algorithm_internal::c_begin(factors2),
  1506. std::forward<T>(sum));
  1507. }
  1508. // Overload of c_inner_product() for using binary operations other than
  1509. // `operator+` (for computing the accumulation) and `operator*` (for computing
  1510. // the product between the two container's element pair).
  1511. template <typename Sequence1, typename Sequence2, typename T,
  1512. typename BinaryOp1, typename BinaryOp2>
  1513. decay_t<T> c_inner_product(const Sequence1& factors1, const Sequence2& factors2,
  1514. T&& sum, BinaryOp1&& op1, BinaryOp2&& op2) {
  1515. return std::inner_product(container_algorithm_internal::c_begin(factors1),
  1516. container_algorithm_internal::c_end(factors1),
  1517. container_algorithm_internal::c_begin(factors2),
  1518. std::forward<T>(sum), std::forward<BinaryOp1>(op1),
  1519. std::forward<BinaryOp2>(op2));
  1520. }
  1521. // c_adjacent_difference()
  1522. //
  1523. // Container-based version of the <algorithm> `std::adjacent_difference()`
  1524. // function to compute the difference between each element and the one preceding
  1525. // it and write it to an iterator.
  1526. template <typename InputSequence, typename OutputIt>
  1527. OutputIt c_adjacent_difference(const InputSequence& input,
  1528. OutputIt output_first) {
  1529. return std::adjacent_difference(container_algorithm_internal::c_begin(input),
  1530. container_algorithm_internal::c_end(input),
  1531. output_first);
  1532. }
  1533. // Overload of c_adjacent_difference() for using a binary operation other than
  1534. // subtraction to compute the adjacent difference.
  1535. template <typename InputSequence, typename OutputIt, typename BinaryOp>
  1536. OutputIt c_adjacent_difference(const InputSequence& input,
  1537. OutputIt output_first, BinaryOp&& op) {
  1538. return std::adjacent_difference(container_algorithm_internal::c_begin(input),
  1539. container_algorithm_internal::c_end(input),
  1540. output_first, std::forward<BinaryOp>(op));
  1541. }
  1542. // c_partial_sum()
  1543. //
  1544. // Container-based version of the <algorithm> `std::partial_sum()` function
  1545. // to compute the partial sum of the elements in a sequence and write them
  1546. // to an iterator. The partial sum is the sum of all element values so far in
  1547. // the sequence.
  1548. template <typename InputSequence, typename OutputIt>
  1549. OutputIt c_partial_sum(const InputSequence& input, OutputIt output_first) {
  1550. return std::partial_sum(container_algorithm_internal::c_begin(input),
  1551. container_algorithm_internal::c_end(input),
  1552. output_first);
  1553. }
  1554. // Overload of c_partial_sum() for using a binary operation other than addition
  1555. // to compute the "partial sum".
  1556. template <typename InputSequence, typename OutputIt, typename BinaryOp>
  1557. OutputIt c_partial_sum(const InputSequence& input, OutputIt output_first,
  1558. BinaryOp&& op) {
  1559. return std::partial_sum(container_algorithm_internal::c_begin(input),
  1560. container_algorithm_internal::c_end(input),
  1561. output_first, std::forward<BinaryOp>(op));
  1562. }
  1563. ABSL_NAMESPACE_END
  1564. } // namespace absl
  1565. #endif // ABSL_ALGORITHM_CONTAINER_H_