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