container.h 76 KB

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