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