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