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