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