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