container.h 79 KB

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