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