container.h 76 KB

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