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