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