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