]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_algo.h
stl_algo.h (remove_copy, [...]): Declare inline.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_algo.h
1 // Algorithm implementation -*- C++ -*-
2
3 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
51 /** @file bits/stl_algo.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{algorithm}
54 */
55
56 #ifndef _STL_ALGO_H
57 #define _STL_ALGO_H 1
58
59 #include <cstdlib> // for rand
60 #include <bits/algorithmfwd.h>
61 #include <bits/stl_heap.h>
62 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
63 #include <bits/predefined_ops.h>
64
65 #if __cplusplus >= 201103L
66 #include <random> // for std::uniform_int_distribution
67 #endif
68
69 // See concept_check.h for the __glibcxx_*_requires macros.
70
71 namespace std _GLIBCXX_VISIBILITY(default)
72 {
73 _GLIBCXX_BEGIN_NAMESPACE_VERSION
74
75 /// Swaps the median value of *__a, *__b and *__c under __comp to *__a
76 template<typename _Iterator, typename _Compare>
77 void
78 __move_median_first(_Iterator __a, _Iterator __b, _Iterator __c,
79 _Compare __comp)
80 {
81 if (__comp(__a, __b))
82 {
83 if (__comp(__b, __c))
84 std::iter_swap(__a, __b);
85 else if (__comp(__a, __c))
86 std::iter_swap(__a, __c);
87 }
88 else if (__comp(__a, __c))
89 return;
90 else if (__comp(__b, __c))
91 std::iter_swap(__a, __c);
92 else
93 std::iter_swap(__a, __b);
94 }
95
96 /// This is an overload used by find algos for the Input Iterator case.
97 template<typename _InputIterator, typename _Predicate>
98 inline _InputIterator
99 __find_if(_InputIterator __first, _InputIterator __last,
100 _Predicate __pred, input_iterator_tag)
101 {
102 while (__first != __last && !__pred(__first))
103 ++__first;
104 return __first;
105 }
106
107 /// This is an overload used by find algos for the RAI case.
108 template<typename _RandomAccessIterator, typename _Predicate>
109 _RandomAccessIterator
110 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
111 _Predicate __pred, random_access_iterator_tag)
112 {
113 typename iterator_traits<_RandomAccessIterator>::difference_type
114 __trip_count = (__last - __first) >> 2;
115
116 for (; __trip_count > 0; --__trip_count)
117 {
118 if (__pred(__first))
119 return __first;
120 ++__first;
121
122 if (__pred(__first))
123 return __first;
124 ++__first;
125
126 if (__pred(__first))
127 return __first;
128 ++__first;
129
130 if (__pred(__first))
131 return __first;
132 ++__first;
133 }
134
135 switch (__last - __first)
136 {
137 case 3:
138 if (__pred(__first))
139 return __first;
140 ++__first;
141 case 2:
142 if (__pred(__first))
143 return __first;
144 ++__first;
145 case 1:
146 if (__pred(__first))
147 return __first;
148 ++__first;
149 case 0:
150 default:
151 return __last;
152 }
153 }
154
155 template<typename _Iterator, typename _Predicate>
156 inline _Iterator
157 __find_if(_Iterator __first, _Iterator __last, _Predicate __pred)
158 {
159 return __find_if(__first, __last, __pred,
160 std::__iterator_category(__first));
161 }
162
163 /// Provided for stable_partition to use.
164 template<typename _InputIterator, typename _Predicate>
165 inline _InputIterator
166 __find_if_not(_InputIterator __first, _InputIterator __last,
167 _Predicate __pred)
168 {
169 return std::__find_if(__first, __last,
170 __gnu_cxx::__ops::__negate(__pred),
171 std::__iterator_category(__first));
172 }
173
174 /// Like find_if_not(), but uses and updates a count of the
175 /// remaining range length instead of comparing against an end
176 /// iterator.
177 template<typename _InputIterator, typename _Predicate, typename _Distance>
178 _InputIterator
179 __find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred)
180 {
181 for (; __len; --__len, ++__first)
182 if (!__pred(__first))
183 break;
184 return __first;
185 }
186
187 // set_difference
188 // set_intersection
189 // set_symmetric_difference
190 // set_union
191 // for_each
192 // find
193 // find_if
194 // find_first_of
195 // adjacent_find
196 // count
197 // count_if
198 // search
199
200 template<typename _ForwardIterator1, typename _ForwardIterator2,
201 typename _BinaryPredicate>
202 _ForwardIterator1
203 __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
204 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
205 _BinaryPredicate __predicate)
206 {
207 // Test for empty ranges
208 if (__first1 == __last1 || __first2 == __last2)
209 return __first1;
210
211 // Test for a pattern of length 1.
212 _ForwardIterator2 __p1(__first2);
213 if (++__p1 == __last2)
214 return std::__find_if(__first1, __last1,
215 __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
216
217 // General case.
218 _ForwardIterator2 __p;
219 _ForwardIterator1 __current = __first1;
220
221 for (;;)
222 {
223 __first1 =
224 std::__find_if(__first1, __last1,
225 __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
226
227 if (__first1 == __last1)
228 return __last1;
229
230 __p = __p1;
231 __current = __first1;
232 if (++__current == __last1)
233 return __last1;
234
235 while (__predicate(__current, __p))
236 {
237 if (++__p == __last2)
238 return __first1;
239 if (++__current == __last1)
240 return __last1;
241 }
242 ++__first1;
243 }
244 return __first1;
245 }
246
247 // search_n
248
249 /**
250 * This is an helper function for search_n overloaded for forward iterators.
251 */
252 template<typename _ForwardIterator, typename _Integer,
253 typename _UnaryPredicate>
254 _ForwardIterator
255 __search_n_aux(_ForwardIterator __first, _ForwardIterator __last,
256 _Integer __count, _UnaryPredicate __unary_pred,
257 std::forward_iterator_tag)
258 {
259 __first = std::__find_if(__first, __last, __unary_pred);
260 while (__first != __last)
261 {
262 typename iterator_traits<_ForwardIterator>::difference_type
263 __n = __count;
264 _ForwardIterator __i = __first;
265 ++__i;
266 while (__i != __last && __n != 1 && __unary_pred(__i))
267 {
268 ++__i;
269 --__n;
270 }
271 if (__n == 1)
272 return __first;
273 if (__i == __last)
274 return __last;
275 __first = std::__find_if(++__i, __last, __unary_pred);
276 }
277 return __last;
278 }
279
280 /**
281 * This is an helper function for search_n overloaded for random access
282 * iterators.
283 */
284 template<typename _RandomAccessIter, typename _Integer,
285 typename _UnaryPredicate>
286 _RandomAccessIter
287 __search_n_aux(_RandomAccessIter __first, _RandomAccessIter __last,
288 _Integer __count, _UnaryPredicate __unary_pred,
289 std::random_access_iterator_tag)
290 {
291 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
292 _DistanceType;
293
294 _DistanceType __tailSize = __last - __first;
295 _DistanceType __remainder = __count;
296
297 while (__remainder <= __tailSize) // the main loop...
298 {
299 __first += __remainder;
300 __tailSize -= __remainder;
301 // __first here is always pointing to one past the last element of
302 // next possible match.
303 _RandomAccessIter __backTrack = __first;
304 while (__unary_pred(--__backTrack))
305 {
306 if (--__remainder == 0)
307 return (__first - __count); // Success
308 }
309 __remainder = __count + 1 - (__first - __backTrack);
310 }
311 return __last; // Failure
312 }
313
314 template<typename _ForwardIterator, typename _Integer,
315 typename _UnaryPredicate>
316 _ForwardIterator
317 __search_n(_ForwardIterator __first, _ForwardIterator __last,
318 _Integer __count,
319 _UnaryPredicate __unary_pred)
320 {
321 if (__count <= 0)
322 return __first;
323
324 if (__count == 1)
325 return std::__find_if(__first, __last, __unary_pred);
326
327 return std::__search_n_aux(__first, __last, __count, __unary_pred,
328 std::__iterator_category(__first));
329 }
330
331 // find_end for forward iterators.
332 template<typename _ForwardIterator1, typename _ForwardIterator2,
333 typename _BinaryPredicate>
334 _ForwardIterator1
335 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
336 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
337 forward_iterator_tag, forward_iterator_tag,
338 _BinaryPredicate __comp)
339 {
340 if (__first2 == __last2)
341 return __last1;
342
343 _ForwardIterator1 __result = __last1;
344 while (1)
345 {
346 _ForwardIterator1 __new_result
347 = std::__search(__first1, __last1, __first2, __last2, __comp);
348 if (__new_result == __last1)
349 return __result;
350 else
351 {
352 __result = __new_result;
353 __first1 = __new_result;
354 ++__first1;
355 }
356 }
357 }
358
359 // find_end for bidirectional iterators (much faster).
360 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
361 typename _BinaryPredicate>
362 _BidirectionalIterator1
363 __find_end(_BidirectionalIterator1 __first1,
364 _BidirectionalIterator1 __last1,
365 _BidirectionalIterator2 __first2,
366 _BidirectionalIterator2 __last2,
367 bidirectional_iterator_tag, bidirectional_iterator_tag,
368 _BinaryPredicate __comp)
369 {
370 // concept requirements
371 __glibcxx_function_requires(_BidirectionalIteratorConcept<
372 _BidirectionalIterator1>)
373 __glibcxx_function_requires(_BidirectionalIteratorConcept<
374 _BidirectionalIterator2>)
375
376 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
377 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
378
379 _RevIterator1 __rlast1(__first1);
380 _RevIterator2 __rlast2(__first2);
381 _RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1,
382 _RevIterator2(__last2), __rlast2,
383 __comp);
384
385 if (__rresult == __rlast1)
386 return __last1;
387 else
388 {
389 _BidirectionalIterator1 __result = __rresult.base();
390 std::advance(__result, -std::distance(__first2, __last2));
391 return __result;
392 }
393 }
394
395 /**
396 * @brief Find last matching subsequence in a sequence.
397 * @ingroup non_mutating_algorithms
398 * @param __first1 Start of range to search.
399 * @param __last1 End of range to search.
400 * @param __first2 Start of sequence to match.
401 * @param __last2 End of sequence to match.
402 * @return The last iterator @c i in the range
403 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
404 * @p *(__first2+N) for each @c N in the range @p
405 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
406 *
407 * Searches the range @p [__first1,__last1) for a sub-sequence that
408 * compares equal value-by-value with the sequence given by @p
409 * [__first2,__last2) and returns an iterator to the __first
410 * element of the sub-sequence, or @p __last1 if the sub-sequence
411 * is not found. The sub-sequence will be the last such
412 * subsequence contained in [__first1,__last1).
413 *
414 * Because the sub-sequence must lie completely within the range @p
415 * [__first1,__last1) it must start at a position less than @p
416 * __last1-(__last2-__first2) where @p __last2-__first2 is the
417 * length of the sub-sequence. This means that the returned
418 * iterator @c i will be in the range @p
419 * [__first1,__last1-(__last2-__first2))
420 */
421 template<typename _ForwardIterator1, typename _ForwardIterator2>
422 inline _ForwardIterator1
423 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
424 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
425 {
426 // concept requirements
427 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
428 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
429 __glibcxx_function_requires(_EqualOpConcept<
430 typename iterator_traits<_ForwardIterator1>::value_type,
431 typename iterator_traits<_ForwardIterator2>::value_type>)
432 __glibcxx_requires_valid_range(__first1, __last1);
433 __glibcxx_requires_valid_range(__first2, __last2);
434
435 return std::__find_end(__first1, __last1, __first2, __last2,
436 std::__iterator_category(__first1),
437 std::__iterator_category(__first2),
438 __gnu_cxx::__ops::__iter_equal_to_iter());
439 }
440
441 /**
442 * @brief Find last matching subsequence in a sequence using a predicate.
443 * @ingroup non_mutating_algorithms
444 * @param __first1 Start of range to search.
445 * @param __last1 End of range to search.
446 * @param __first2 Start of sequence to match.
447 * @param __last2 End of sequence to match.
448 * @param __comp The predicate to use.
449 * @return The last iterator @c i in the range @p
450 * [__first1,__last1-(__last2-__first2)) such that @c
451 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
452 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
453 * exists.
454 *
455 * Searches the range @p [__first1,__last1) for a sub-sequence that
456 * compares equal value-by-value with the sequence given by @p
457 * [__first2,__last2) using comp as a predicate and returns an
458 * iterator to the first element of the sub-sequence, or @p __last1
459 * if the sub-sequence is not found. The sub-sequence will be the
460 * last such subsequence contained in [__first,__last1).
461 *
462 * Because the sub-sequence must lie completely within the range @p
463 * [__first1,__last1) it must start at a position less than @p
464 * __last1-(__last2-__first2) where @p __last2-__first2 is the
465 * length of the sub-sequence. This means that the returned
466 * iterator @c i will be in the range @p
467 * [__first1,__last1-(__last2-__first2))
468 */
469 template<typename _ForwardIterator1, typename _ForwardIterator2,
470 typename _BinaryPredicate>
471 inline _ForwardIterator1
472 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
473 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
474 _BinaryPredicate __comp)
475 {
476 // concept requirements
477 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
478 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
479 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
480 typename iterator_traits<_ForwardIterator1>::value_type,
481 typename iterator_traits<_ForwardIterator2>::value_type>)
482 __glibcxx_requires_valid_range(__first1, __last1);
483 __glibcxx_requires_valid_range(__first2, __last2);
484
485 return std::__find_end(__first1, __last1, __first2, __last2,
486 std::__iterator_category(__first1),
487 std::__iterator_category(__first2),
488 __gnu_cxx::__ops::__iter_comp_iter(__comp));
489 }
490
491 #if __cplusplus >= 201103L
492 /**
493 * @brief Checks that a predicate is true for all the elements
494 * of a sequence.
495 * @ingroup non_mutating_algorithms
496 * @param __first An input iterator.
497 * @param __last An input iterator.
498 * @param __pred A predicate.
499 * @return True if the check is true, false otherwise.
500 *
501 * Returns true if @p __pred is true for each element in the range
502 * @p [__first,__last), and false otherwise.
503 */
504 template<typename _InputIterator, typename _Predicate>
505 inline bool
506 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
507 { return __last == std::find_if_not(__first, __last, __pred); }
508
509 /**
510 * @brief Checks that a predicate is false for all the elements
511 * of a sequence.
512 * @ingroup non_mutating_algorithms
513 * @param __first An input iterator.
514 * @param __last An input iterator.
515 * @param __pred A predicate.
516 * @return True if the check is true, false otherwise.
517 *
518 * Returns true if @p __pred is false for each element in the range
519 * @p [__first,__last), and false otherwise.
520 */
521 template<typename _InputIterator, typename _Predicate>
522 inline bool
523 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
524 { return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); }
525
526 /**
527 * @brief Checks that a predicate is false for at least an element
528 * of a sequence.
529 * @ingroup non_mutating_algorithms
530 * @param __first An input iterator.
531 * @param __last An input iterator.
532 * @param __pred A predicate.
533 * @return True if the check is true, false otherwise.
534 *
535 * Returns true if an element exists in the range @p
536 * [__first,__last) such that @p __pred is true, and false
537 * otherwise.
538 */
539 template<typename _InputIterator, typename _Predicate>
540 inline bool
541 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
542 { return !std::none_of(__first, __last, __pred); }
543
544 /**
545 * @brief Find the first element in a sequence for which a
546 * predicate is false.
547 * @ingroup non_mutating_algorithms
548 * @param __first An input iterator.
549 * @param __last An input iterator.
550 * @param __pred A predicate.
551 * @return The first iterator @c i in the range @p [__first,__last)
552 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
553 */
554 template<typename _InputIterator, typename _Predicate>
555 inline _InputIterator
556 find_if_not(_InputIterator __first, _InputIterator __last,
557 _Predicate __pred)
558 {
559 // concept requirements
560 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
561 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
562 typename iterator_traits<_InputIterator>::value_type>)
563 __glibcxx_requires_valid_range(__first, __last);
564 return std::__find_if_not(__first, __last,
565 __gnu_cxx::__ops::__pred_iter(__pred));
566 }
567
568 /**
569 * @brief Checks whether the sequence is partitioned.
570 * @ingroup mutating_algorithms
571 * @param __first An input iterator.
572 * @param __last An input iterator.
573 * @param __pred A predicate.
574 * @return True if the range @p [__first,__last) is partioned by @p __pred,
575 * i.e. if all elements that satisfy @p __pred appear before those that
576 * do not.
577 */
578 template<typename _InputIterator, typename _Predicate>
579 inline bool
580 is_partitioned(_InputIterator __first, _InputIterator __last,
581 _Predicate __pred)
582 {
583 __first = std::find_if_not(__first, __last, __pred);
584 return std::none_of(__first, __last, __pred);
585 }
586
587 /**
588 * @brief Find the partition point of a partitioned range.
589 * @ingroup mutating_algorithms
590 * @param __first An iterator.
591 * @param __last Another iterator.
592 * @param __pred A predicate.
593 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
594 * and @p none_of(mid, __last, __pred) are both true.
595 */
596 template<typename _ForwardIterator, typename _Predicate>
597 _ForwardIterator
598 partition_point(_ForwardIterator __first, _ForwardIterator __last,
599 _Predicate __pred)
600 {
601 // concept requirements
602 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
603 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
604 typename iterator_traits<_ForwardIterator>::value_type>)
605
606 // A specific debug-mode test will be necessary...
607 __glibcxx_requires_valid_range(__first, __last);
608
609 typedef typename iterator_traits<_ForwardIterator>::difference_type
610 _DistanceType;
611
612 _DistanceType __len = std::distance(__first, __last);
613 _DistanceType __half;
614 _ForwardIterator __middle;
615
616 while (__len > 0)
617 {
618 __half = __len >> 1;
619 __middle = __first;
620 std::advance(__middle, __half);
621 if (__pred(*__middle))
622 {
623 __first = __middle;
624 ++__first;
625 __len = __len - __half - 1;
626 }
627 else
628 __len = __half;
629 }
630 return __first;
631 }
632 #endif
633
634 template<typename _InputIterator, typename _OutputIterator,
635 typename _Predicate>
636 _OutputIterator
637 __remove_copy_if(_InputIterator __first, _InputIterator __last,
638 _OutputIterator __result, _Predicate __pred)
639 {
640 for (; __first != __last; ++__first)
641 if (!__pred(__first))
642 {
643 *__result = *__first;
644 ++__result;
645 }
646 return __result;
647 }
648
649 /**
650 * @brief Copy a sequence, removing elements of a given value.
651 * @ingroup mutating_algorithms
652 * @param __first An input iterator.
653 * @param __last An input iterator.
654 * @param __result An output iterator.
655 * @param __value The value to be removed.
656 * @return An iterator designating the end of the resulting sequence.
657 *
658 * Copies each element in the range @p [__first,__last) not equal
659 * to @p __value to the range beginning at @p __result.
660 * remove_copy() is stable, so the relative order of elements that
661 * are copied is unchanged.
662 */
663 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
664 inline _OutputIterator
665 remove_copy(_InputIterator __first, _InputIterator __last,
666 _OutputIterator __result, const _Tp& __value)
667 {
668 // concept requirements
669 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
670 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
671 typename iterator_traits<_InputIterator>::value_type>)
672 __glibcxx_function_requires(_EqualOpConcept<
673 typename iterator_traits<_InputIterator>::value_type, _Tp>)
674 __glibcxx_requires_valid_range(__first, __last);
675
676 return std::__remove_copy_if(__first, __last, __result,
677 __gnu_cxx::__ops::__iter_equals_val(__value));
678 }
679
680 /**
681 * @brief Copy a sequence, removing elements for which a predicate is true.
682 * @ingroup mutating_algorithms
683 * @param __first An input iterator.
684 * @param __last An input iterator.
685 * @param __result An output iterator.
686 * @param __pred A predicate.
687 * @return An iterator designating the end of the resulting sequence.
688 *
689 * Copies each element in the range @p [__first,__last) for which
690 * @p __pred returns false to the range beginning at @p __result.
691 *
692 * remove_copy_if() is stable, so the relative order of elements that are
693 * copied is unchanged.
694 */
695 template<typename _InputIterator, typename _OutputIterator,
696 typename _Predicate>
697 inline _OutputIterator
698 remove_copy_if(_InputIterator __first, _InputIterator __last,
699 _OutputIterator __result, _Predicate __pred)
700 {
701 // concept requirements
702 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
703 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
704 typename iterator_traits<_InputIterator>::value_type>)
705 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
706 typename iterator_traits<_InputIterator>::value_type>)
707 __glibcxx_requires_valid_range(__first, __last);
708
709 return std::__remove_copy_if(__first, __last, __result,
710 __gnu_cxx::__ops::__pred_iter(__pred));
711 }
712
713 #if __cplusplus >= 201103L
714 /**
715 * @brief Copy the elements of a sequence for which a predicate is true.
716 * @ingroup mutating_algorithms
717 * @param __first An input iterator.
718 * @param __last An input iterator.
719 * @param __result An output iterator.
720 * @param __pred A predicate.
721 * @return An iterator designating the end of the resulting sequence.
722 *
723 * Copies each element in the range @p [__first,__last) for which
724 * @p __pred returns true to the range beginning at @p __result.
725 *
726 * copy_if() is stable, so the relative order of elements that are
727 * copied is unchanged.
728 */
729 template<typename _InputIterator, typename _OutputIterator,
730 typename _Predicate>
731 _OutputIterator
732 copy_if(_InputIterator __first, _InputIterator __last,
733 _OutputIterator __result, _Predicate __pred)
734 {
735 // concept requirements
736 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
737 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
738 typename iterator_traits<_InputIterator>::value_type>)
739 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
740 typename iterator_traits<_InputIterator>::value_type>)
741 __glibcxx_requires_valid_range(__first, __last);
742
743 for (; __first != __last; ++__first)
744 if (__pred(*__first))
745 {
746 *__result = *__first;
747 ++__result;
748 }
749 return __result;
750 }
751
752 template<typename _InputIterator, typename _Size, typename _OutputIterator>
753 _OutputIterator
754 __copy_n(_InputIterator __first, _Size __n,
755 _OutputIterator __result, input_iterator_tag)
756 {
757 if (__n > 0)
758 {
759 while (true)
760 {
761 *__result = *__first;
762 ++__result;
763 if (--__n > 0)
764 ++__first;
765 else
766 break;
767 }
768 }
769 return __result;
770 }
771
772 template<typename _RandomAccessIterator, typename _Size,
773 typename _OutputIterator>
774 inline _OutputIterator
775 __copy_n(_RandomAccessIterator __first, _Size __n,
776 _OutputIterator __result, random_access_iterator_tag)
777 { return std::copy(__first, __first + __n, __result); }
778
779 /**
780 * @brief Copies the range [first,first+n) into [result,result+n).
781 * @ingroup mutating_algorithms
782 * @param __first An input iterator.
783 * @param __n The number of elements to copy.
784 * @param __result An output iterator.
785 * @return result+n.
786 *
787 * This inline function will boil down to a call to @c memmove whenever
788 * possible. Failing that, if random access iterators are passed, then the
789 * loop count will be known (and therefore a candidate for compiler
790 * optimizations such as unrolling).
791 */
792 template<typename _InputIterator, typename _Size, typename _OutputIterator>
793 inline _OutputIterator
794 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
795 {
796 // concept requirements
797 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
798 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
799 typename iterator_traits<_InputIterator>::value_type>)
800
801 return std::__copy_n(__first, __n, __result,
802 std::__iterator_category(__first));
803 }
804
805 /**
806 * @brief Copy the elements of a sequence to separate output sequences
807 * depending on the truth value of a predicate.
808 * @ingroup mutating_algorithms
809 * @param __first An input iterator.
810 * @param __last An input iterator.
811 * @param __out_true An output iterator.
812 * @param __out_false An output iterator.
813 * @param __pred A predicate.
814 * @return A pair designating the ends of the resulting sequences.
815 *
816 * Copies each element in the range @p [__first,__last) for which
817 * @p __pred returns true to the range beginning at @p out_true
818 * and each element for which @p __pred returns false to @p __out_false.
819 */
820 template<typename _InputIterator, typename _OutputIterator1,
821 typename _OutputIterator2, typename _Predicate>
822 pair<_OutputIterator1, _OutputIterator2>
823 partition_copy(_InputIterator __first, _InputIterator __last,
824 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
825 _Predicate __pred)
826 {
827 // concept requirements
828 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
829 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
830 typename iterator_traits<_InputIterator>::value_type>)
831 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
832 typename iterator_traits<_InputIterator>::value_type>)
833 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
834 typename iterator_traits<_InputIterator>::value_type>)
835 __glibcxx_requires_valid_range(__first, __last);
836
837 for (; __first != __last; ++__first)
838 if (__pred(*__first))
839 {
840 *__out_true = *__first;
841 ++__out_true;
842 }
843 else
844 {
845 *__out_false = *__first;
846 ++__out_false;
847 }
848
849 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
850 }
851 #endif
852
853 template<typename _ForwardIterator, typename _Predicate>
854 _ForwardIterator
855 __remove_if(_ForwardIterator __first, _ForwardIterator __last,
856 _Predicate __pred)
857 {
858 __first = std::__find_if(__first, __last, __pred);
859 if (__first == __last)
860 return __first;
861 _ForwardIterator __result = __first;
862 ++__first;
863 for (; __first != __last; ++__first)
864 if (!__pred(__first))
865 {
866 *__result = _GLIBCXX_MOVE(*__first);
867 ++__result;
868 }
869 return __result;
870 }
871
872 /**
873 * @brief Remove elements from a sequence.
874 * @ingroup mutating_algorithms
875 * @param __first An input iterator.
876 * @param __last An input iterator.
877 * @param __value The value to be removed.
878 * @return An iterator designating the end of the resulting sequence.
879 *
880 * All elements equal to @p __value are removed from the range
881 * @p [__first,__last).
882 *
883 * remove() is stable, so the relative order of elements that are
884 * not removed is unchanged.
885 *
886 * Elements between the end of the resulting sequence and @p __last
887 * are still present, but their value is unspecified.
888 */
889 template<typename _ForwardIterator, typename _Tp>
890 inline _ForwardIterator
891 remove(_ForwardIterator __first, _ForwardIterator __last,
892 const _Tp& __value)
893 {
894 // concept requirements
895 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
896 _ForwardIterator>)
897 __glibcxx_function_requires(_EqualOpConcept<
898 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
899 __glibcxx_requires_valid_range(__first, __last);
900
901 return std::__remove_if(__first, __last,
902 __gnu_cxx::__ops::__iter_equals_val(__value));
903 }
904
905 /**
906 * @brief Remove elements from a sequence using a predicate.
907 * @ingroup mutating_algorithms
908 * @param __first A forward iterator.
909 * @param __last A forward iterator.
910 * @param __pred A predicate.
911 * @return An iterator designating the end of the resulting sequence.
912 *
913 * All elements for which @p __pred returns true are removed from the range
914 * @p [__first,__last).
915 *
916 * remove_if() is stable, so the relative order of elements that are
917 * not removed is unchanged.
918 *
919 * Elements between the end of the resulting sequence and @p __last
920 * are still present, but their value is unspecified.
921 */
922 template<typename _ForwardIterator, typename _Predicate>
923 inline _ForwardIterator
924 remove_if(_ForwardIterator __first, _ForwardIterator __last,
925 _Predicate __pred)
926 {
927 // concept requirements
928 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
929 _ForwardIterator>)
930 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
931 typename iterator_traits<_ForwardIterator>::value_type>)
932 __glibcxx_requires_valid_range(__first, __last);
933
934 return std::__remove_if(__first, __last,
935 __gnu_cxx::__ops::__pred_iter(__pred));
936 }
937
938 template<typename _ForwardIterator, typename _BinaryPredicate>
939 _ForwardIterator
940 __adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
941 _BinaryPredicate __binary_pred)
942 {
943 if (__first == __last)
944 return __last;
945 _ForwardIterator __next = __first;
946 while (++__next != __last)
947 {
948 if (__binary_pred(__first, __next))
949 return __first;
950 __first = __next;
951 }
952 return __last;
953 }
954
955 template<typename _ForwardIterator, typename _BinaryPredicate>
956 _ForwardIterator
957 __unique(_ForwardIterator __first, _ForwardIterator __last,
958 _BinaryPredicate __binary_pred)
959 {
960 // Skip the beginning, if already unique.
961 __first = std::__adjacent_find(__first, __last, __binary_pred);
962 if (__first == __last)
963 return __last;
964
965 // Do the real copy work.
966 _ForwardIterator __dest = __first;
967 ++__first;
968 while (++__first != __last)
969 if (!__binary_pred(__dest, __first))
970 *++__dest = _GLIBCXX_MOVE(*__first);
971 return ++__dest;
972 }
973
974 /**
975 * @brief Remove consecutive duplicate values from a sequence.
976 * @ingroup mutating_algorithms
977 * @param __first A forward iterator.
978 * @param __last A forward iterator.
979 * @return An iterator designating the end of the resulting sequence.
980 *
981 * Removes all but the first element from each group of consecutive
982 * values that compare equal.
983 * unique() is stable, so the relative order of elements that are
984 * not removed is unchanged.
985 * Elements between the end of the resulting sequence and @p __last
986 * are still present, but their value is unspecified.
987 */
988 template<typename _ForwardIterator>
989 inline _ForwardIterator
990 unique(_ForwardIterator __first, _ForwardIterator __last)
991 {
992 // concept requirements
993 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
994 _ForwardIterator>)
995 __glibcxx_function_requires(_EqualityComparableConcept<
996 typename iterator_traits<_ForwardIterator>::value_type>)
997 __glibcxx_requires_valid_range(__first, __last);
998
999 return std::__unique(__first, __last,
1000 __gnu_cxx::__ops::__iter_equal_to_iter());
1001 }
1002
1003 /**
1004 * @brief Remove consecutive values from a sequence using a predicate.
1005 * @ingroup mutating_algorithms
1006 * @param __first A forward iterator.
1007 * @param __last A forward iterator.
1008 * @param __binary_pred A binary predicate.
1009 * @return An iterator designating the end of the resulting sequence.
1010 *
1011 * Removes all but the first element from each group of consecutive
1012 * values for which @p __binary_pred returns true.
1013 * unique() is stable, so the relative order of elements that are
1014 * not removed is unchanged.
1015 * Elements between the end of the resulting sequence and @p __last
1016 * are still present, but their value is unspecified.
1017 */
1018 template<typename _ForwardIterator, typename _BinaryPredicate>
1019 inline _ForwardIterator
1020 unique(_ForwardIterator __first, _ForwardIterator __last,
1021 _BinaryPredicate __binary_pred)
1022 {
1023 // concept requirements
1024 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1025 _ForwardIterator>)
1026 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1027 typename iterator_traits<_ForwardIterator>::value_type,
1028 typename iterator_traits<_ForwardIterator>::value_type>)
1029 __glibcxx_requires_valid_range(__first, __last);
1030
1031 return std::__unique(__first, __last,
1032 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1033 }
1034
1035 /**
1036 * This is an uglified
1037 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1038 * _BinaryPredicate)
1039 * overloaded for forward iterators and output iterator as result.
1040 */
1041 template<typename _ForwardIterator, typename _OutputIterator,
1042 typename _BinaryPredicate>
1043 _OutputIterator
1044 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1045 _OutputIterator __result, _BinaryPredicate __binary_pred,
1046 forward_iterator_tag, output_iterator_tag)
1047 {
1048 // concept requirements -- iterators already checked
1049 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1050 typename iterator_traits<_ForwardIterator>::value_type,
1051 typename iterator_traits<_ForwardIterator>::value_type>)
1052
1053 _ForwardIterator __next = __first;
1054 *__result = *__first;
1055 while (++__next != __last)
1056 if (!__binary_pred(__first, __next))
1057 {
1058 __first = __next;
1059 *++__result = *__first;
1060 }
1061 return ++__result;
1062 }
1063
1064 /**
1065 * This is an uglified
1066 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1067 * _BinaryPredicate)
1068 * overloaded for input iterators and output iterator as result.
1069 */
1070 template<typename _InputIterator, typename _OutputIterator,
1071 typename _BinaryPredicate>
1072 _OutputIterator
1073 __unique_copy(_InputIterator __first, _InputIterator __last,
1074 _OutputIterator __result, _BinaryPredicate __binary_pred,
1075 input_iterator_tag, output_iterator_tag)
1076 {
1077 // concept requirements -- iterators already checked
1078 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1079 typename iterator_traits<_InputIterator>::value_type,
1080 typename iterator_traits<_InputIterator>::value_type>)
1081
1082 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1083 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred))
1084 __rebound_pred
1085 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred);
1086 *__result = __value;
1087 while (++__first != __last)
1088 if (!__rebound_pred(__first, __value))
1089 {
1090 __value = *__first;
1091 *++__result = __value;
1092 }
1093 return ++__result;
1094 }
1095
1096 /**
1097 * This is an uglified
1098 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1099 * _BinaryPredicate)
1100 * overloaded for input iterators and forward iterator as result.
1101 */
1102 template<typename _InputIterator, typename _ForwardIterator,
1103 typename _BinaryPredicate>
1104 _ForwardIterator
1105 __unique_copy(_InputIterator __first, _InputIterator __last,
1106 _ForwardIterator __result, _BinaryPredicate __binary_pred,
1107 input_iterator_tag, forward_iterator_tag)
1108 {
1109 // concept requirements -- iterators already checked
1110 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1111 typename iterator_traits<_ForwardIterator>::value_type,
1112 typename iterator_traits<_InputIterator>::value_type>)
1113 *__result = *__first;
1114 while (++__first != __last)
1115 if (!__binary_pred(__result, __first))
1116 *++__result = *__first;
1117 return ++__result;
1118 }
1119
1120 /**
1121 * This is an uglified reverse(_BidirectionalIterator,
1122 * _BidirectionalIterator)
1123 * overloaded for bidirectional iterators.
1124 */
1125 template<typename _BidirectionalIterator>
1126 void
1127 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1128 bidirectional_iterator_tag)
1129 {
1130 while (true)
1131 if (__first == __last || __first == --__last)
1132 return;
1133 else
1134 {
1135 std::iter_swap(__first, __last);
1136 ++__first;
1137 }
1138 }
1139
1140 /**
1141 * This is an uglified reverse(_BidirectionalIterator,
1142 * _BidirectionalIterator)
1143 * overloaded for random access iterators.
1144 */
1145 template<typename _RandomAccessIterator>
1146 void
1147 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1148 random_access_iterator_tag)
1149 {
1150 if (__first == __last)
1151 return;
1152 --__last;
1153 while (__first < __last)
1154 {
1155 std::iter_swap(__first, __last);
1156 ++__first;
1157 --__last;
1158 }
1159 }
1160
1161 /**
1162 * @brief Reverse a sequence.
1163 * @ingroup mutating_algorithms
1164 * @param __first A bidirectional iterator.
1165 * @param __last A bidirectional iterator.
1166 * @return reverse() returns no value.
1167 *
1168 * Reverses the order of the elements in the range @p [__first,__last),
1169 * so that the first element becomes the last etc.
1170 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse()
1171 * swaps @p *(__first+i) and @p *(__last-(i+1))
1172 */
1173 template<typename _BidirectionalIterator>
1174 inline void
1175 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1176 {
1177 // concept requirements
1178 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1179 _BidirectionalIterator>)
1180 __glibcxx_requires_valid_range(__first, __last);
1181 std::__reverse(__first, __last, std::__iterator_category(__first));
1182 }
1183
1184 /**
1185 * @brief Copy a sequence, reversing its elements.
1186 * @ingroup mutating_algorithms
1187 * @param __first A bidirectional iterator.
1188 * @param __last A bidirectional iterator.
1189 * @param __result An output iterator.
1190 * @return An iterator designating the end of the resulting sequence.
1191 *
1192 * Copies the elements in the range @p [__first,__last) to the
1193 * range @p [__result,__result+(__last-__first)) such that the
1194 * order of the elements is reversed. For every @c i such that @p
1195 * 0<=i<=(__last-__first), @p reverse_copy() performs the
1196 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i).
1197 * The ranges @p [__first,__last) and @p
1198 * [__result,__result+(__last-__first)) must not overlap.
1199 */
1200 template<typename _BidirectionalIterator, typename _OutputIterator>
1201 _OutputIterator
1202 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1203 _OutputIterator __result)
1204 {
1205 // concept requirements
1206 __glibcxx_function_requires(_BidirectionalIteratorConcept<
1207 _BidirectionalIterator>)
1208 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1209 typename iterator_traits<_BidirectionalIterator>::value_type>)
1210 __glibcxx_requires_valid_range(__first, __last);
1211
1212 while (__first != __last)
1213 {
1214 --__last;
1215 *__result = *__last;
1216 ++__result;
1217 }
1218 return __result;
1219 }
1220
1221 /**
1222 * This is a helper function for the rotate algorithm specialized on RAIs.
1223 * It returns the greatest common divisor of two integer values.
1224 */
1225 template<typename _EuclideanRingElement>
1226 _EuclideanRingElement
1227 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1228 {
1229 while (__n != 0)
1230 {
1231 _EuclideanRingElement __t = __m % __n;
1232 __m = __n;
1233 __n = __t;
1234 }
1235 return __m;
1236 }
1237
1238 /// This is a helper function for the rotate algorithm.
1239 template<typename _ForwardIterator>
1240 void
1241 __rotate(_ForwardIterator __first,
1242 _ForwardIterator __middle,
1243 _ForwardIterator __last,
1244 forward_iterator_tag)
1245 {
1246 if (__first == __middle || __last == __middle)
1247 return;
1248
1249 _ForwardIterator __first2 = __middle;
1250 do
1251 {
1252 std::iter_swap(__first, __first2);
1253 ++__first;
1254 ++__first2;
1255 if (__first == __middle)
1256 __middle = __first2;
1257 }
1258 while (__first2 != __last);
1259
1260 __first2 = __middle;
1261
1262 while (__first2 != __last)
1263 {
1264 std::iter_swap(__first, __first2);
1265 ++__first;
1266 ++__first2;
1267 if (__first == __middle)
1268 __middle = __first2;
1269 else if (__first2 == __last)
1270 __first2 = __middle;
1271 }
1272 }
1273
1274 /// This is a helper function for the rotate algorithm.
1275 template<typename _BidirectionalIterator>
1276 void
1277 __rotate(_BidirectionalIterator __first,
1278 _BidirectionalIterator __middle,
1279 _BidirectionalIterator __last,
1280 bidirectional_iterator_tag)
1281 {
1282 // concept requirements
1283 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1284 _BidirectionalIterator>)
1285
1286 if (__first == __middle || __last == __middle)
1287 return;
1288
1289 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1290 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1291
1292 while (__first != __middle && __middle != __last)
1293 {
1294 std::iter_swap(__first, --__last);
1295 ++__first;
1296 }
1297
1298 if (__first == __middle)
1299 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1300 else
1301 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1302 }
1303
1304 /// This is a helper function for the rotate algorithm.
1305 template<typename _RandomAccessIterator>
1306 void
1307 __rotate(_RandomAccessIterator __first,
1308 _RandomAccessIterator __middle,
1309 _RandomAccessIterator __last,
1310 random_access_iterator_tag)
1311 {
1312 // concept requirements
1313 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1314 _RandomAccessIterator>)
1315
1316 if (__first == __middle || __last == __middle)
1317 return;
1318
1319 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1320 _Distance;
1321 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1322 _ValueType;
1323
1324 _Distance __n = __last - __first;
1325 _Distance __k = __middle - __first;
1326
1327 if (__k == __n - __k)
1328 {
1329 std::swap_ranges(__first, __middle, __middle);
1330 return;
1331 }
1332
1333 _RandomAccessIterator __p = __first;
1334
1335 for (;;)
1336 {
1337 if (__k < __n - __k)
1338 {
1339 if (__is_pod(_ValueType) && __k == 1)
1340 {
1341 _ValueType __t = _GLIBCXX_MOVE(*__p);
1342 _GLIBCXX_MOVE3(__p + 1, __p + __n, __p);
1343 *(__p + __n - 1) = _GLIBCXX_MOVE(__t);
1344 return;
1345 }
1346 _RandomAccessIterator __q = __p + __k;
1347 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1348 {
1349 std::iter_swap(__p, __q);
1350 ++__p;
1351 ++__q;
1352 }
1353 __n %= __k;
1354 if (__n == 0)
1355 return;
1356 std::swap(__n, __k);
1357 __k = __n - __k;
1358 }
1359 else
1360 {
1361 __k = __n - __k;
1362 if (__is_pod(_ValueType) && __k == 1)
1363 {
1364 _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1));
1365 _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n);
1366 *__p = _GLIBCXX_MOVE(__t);
1367 return;
1368 }
1369 _RandomAccessIterator __q = __p + __n;
1370 __p = __q - __k;
1371 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1372 {
1373 --__p;
1374 --__q;
1375 std::iter_swap(__p, __q);
1376 }
1377 __n %= __k;
1378 if (__n == 0)
1379 return;
1380 std::swap(__n, __k);
1381 }
1382 }
1383 }
1384
1385 /**
1386 * @brief Rotate the elements of a sequence.
1387 * @ingroup mutating_algorithms
1388 * @param __first A forward iterator.
1389 * @param __middle A forward iterator.
1390 * @param __last A forward iterator.
1391 * @return Nothing.
1392 *
1393 * Rotates the elements of the range @p [__first,__last) by
1394 * @p (__middle - __first) positions so that the element at @p __middle
1395 * is moved to @p __first, the element at @p __middle+1 is moved to
1396 * @p __first+1 and so on for each element in the range
1397 * @p [__first,__last).
1398 *
1399 * This effectively swaps the ranges @p [__first,__middle) and
1400 * @p [__middle,__last).
1401 *
1402 * Performs
1403 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1404 * for each @p n in the range @p [0,__last-__first).
1405 */
1406 template<typename _ForwardIterator>
1407 inline void
1408 rotate(_ForwardIterator __first, _ForwardIterator __middle,
1409 _ForwardIterator __last)
1410 {
1411 // concept requirements
1412 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1413 _ForwardIterator>)
1414 __glibcxx_requires_valid_range(__first, __middle);
1415 __glibcxx_requires_valid_range(__middle, __last);
1416
1417 std::__rotate(__first, __middle, __last,
1418 std::__iterator_category(__first));
1419 }
1420
1421 /**
1422 * @brief Copy a sequence, rotating its elements.
1423 * @ingroup mutating_algorithms
1424 * @param __first A forward iterator.
1425 * @param __middle A forward iterator.
1426 * @param __last A forward iterator.
1427 * @param __result An output iterator.
1428 * @return An iterator designating the end of the resulting sequence.
1429 *
1430 * Copies the elements of the range @p [__first,__last) to the
1431 * range beginning at @result, rotating the copied elements by
1432 * @p (__middle-__first) positions so that the element at @p __middle
1433 * is moved to @p __result, the element at @p __middle+1 is moved
1434 * to @p __result+1 and so on for each element in the range @p
1435 * [__first,__last).
1436 *
1437 * Performs
1438 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1439 * for each @p n in the range @p [0,__last-__first).
1440 */
1441 template<typename _ForwardIterator, typename _OutputIterator>
1442 inline _OutputIterator
1443 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1444 _ForwardIterator __last, _OutputIterator __result)
1445 {
1446 // concept requirements
1447 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1448 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1449 typename iterator_traits<_ForwardIterator>::value_type>)
1450 __glibcxx_requires_valid_range(__first, __middle);
1451 __glibcxx_requires_valid_range(__middle, __last);
1452
1453 return std::copy(__first, __middle,
1454 std::copy(__middle, __last, __result));
1455 }
1456
1457 /// This is a helper function...
1458 template<typename _ForwardIterator, typename _Predicate>
1459 _ForwardIterator
1460 __partition(_ForwardIterator __first, _ForwardIterator __last,
1461 _Predicate __pred, forward_iterator_tag)
1462 {
1463 if (__first == __last)
1464 return __first;
1465
1466 while (__pred(*__first))
1467 if (++__first == __last)
1468 return __first;
1469
1470 _ForwardIterator __next = __first;
1471
1472 while (++__next != __last)
1473 if (__pred(*__next))
1474 {
1475 std::iter_swap(__first, __next);
1476 ++__first;
1477 }
1478
1479 return __first;
1480 }
1481
1482 /// This is a helper function...
1483 template<typename _BidirectionalIterator, typename _Predicate>
1484 _BidirectionalIterator
1485 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
1486 _Predicate __pred, bidirectional_iterator_tag)
1487 {
1488 while (true)
1489 {
1490 while (true)
1491 if (__first == __last)
1492 return __first;
1493 else if (__pred(*__first))
1494 ++__first;
1495 else
1496 break;
1497 --__last;
1498 while (true)
1499 if (__first == __last)
1500 return __first;
1501 else if (!bool(__pred(*__last)))
1502 --__last;
1503 else
1504 break;
1505 std::iter_swap(__first, __last);
1506 ++__first;
1507 }
1508 }
1509
1510 // partition
1511
1512 /// This is a helper function...
1513 /// Requires __len != 0 and !__pred(*__first),
1514 /// same as __stable_partition_adaptive.
1515 template<typename _ForwardIterator, typename _Predicate, typename _Distance>
1516 _ForwardIterator
1517 __inplace_stable_partition(_ForwardIterator __first,
1518 _Predicate __pred, _Distance __len)
1519 {
1520 if (__len == 1)
1521 return __first;
1522 _ForwardIterator __middle = __first;
1523 std::advance(__middle, __len / 2);
1524 _ForwardIterator __left_split =
1525 std::__inplace_stable_partition(__first, __pred, __len / 2);
1526 // Advance past true-predicate values to satisfy this
1527 // function's preconditions.
1528 _Distance __right_len = __len - __len / 2;
1529 _ForwardIterator __right_split =
1530 std::__find_if_not_n(__middle, __right_len, __pred);
1531 if (__right_len)
1532 __right_split = std::__inplace_stable_partition(__middle,
1533 __pred,
1534 __right_len);
1535 std::rotate(__left_split, __middle, __right_split);
1536 std::advance(__left_split, std::distance(__middle, __right_split));
1537 return __left_split;
1538 }
1539
1540 /// This is a helper function...
1541 /// Requires __first != __last and !__pred(__first)
1542 /// and __len == distance(__first, __last).
1543 ///
1544 /// !__pred(__first) allows us to guarantee that we don't
1545 /// move-assign an element onto itself.
1546 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
1547 typename _Distance>
1548 _ForwardIterator
1549 __stable_partition_adaptive(_ForwardIterator __first,
1550 _ForwardIterator __last,
1551 _Predicate __pred, _Distance __len,
1552 _Pointer __buffer,
1553 _Distance __buffer_size)
1554 {
1555 if (__len <= __buffer_size)
1556 {
1557 _ForwardIterator __result1 = __first;
1558 _Pointer __result2 = __buffer;
1559 // The precondition guarantees that !__pred(__first), so
1560 // move that element to the buffer before starting the loop.
1561 // This ensures that we only call __pred once per element.
1562 *__result2 = _GLIBCXX_MOVE(*__first);
1563 ++__result2;
1564 ++__first;
1565 for (; __first != __last; ++__first)
1566 if (__pred(__first))
1567 {
1568 *__result1 = _GLIBCXX_MOVE(*__first);
1569 ++__result1;
1570 }
1571 else
1572 {
1573 *__result2 = _GLIBCXX_MOVE(*__first);
1574 ++__result2;
1575 }
1576 _GLIBCXX_MOVE3(__buffer, __result2, __result1);
1577 return __result1;
1578 }
1579 else
1580 {
1581 _ForwardIterator __middle = __first;
1582 std::advance(__middle, __len / 2);
1583 _ForwardIterator __left_split =
1584 std::__stable_partition_adaptive(__first, __middle, __pred,
1585 __len / 2, __buffer,
1586 __buffer_size);
1587 // Advance past true-predicate values to satisfy this
1588 // function's preconditions.
1589 _Distance __right_len = __len - __len / 2;
1590 _ForwardIterator __right_split =
1591 std::__find_if_not_n(__middle, __right_len, __pred);
1592 if (__right_len)
1593 __right_split =
1594 std::__stable_partition_adaptive(__right_split, __last, __pred,
1595 __right_len,
1596 __buffer, __buffer_size);
1597 std::rotate(__left_split, __middle, __right_split);
1598 std::advance(__left_split, std::distance(__middle, __right_split));
1599 return __left_split;
1600 }
1601 }
1602
1603 template<typename _ForwardIterator, typename _Predicate>
1604 _ForwardIterator
1605 __stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1606 _Predicate __pred)
1607 {
1608 __first = std::__find_if_not(__first, __last, __pred);
1609
1610 if (__first == __last)
1611 return __first;
1612
1613 typedef typename iterator_traits<_ForwardIterator>::value_type
1614 _ValueType;
1615 typedef typename iterator_traits<_ForwardIterator>::difference_type
1616 _DistanceType;
1617
1618 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first, __last);
1619 if (__buf.size() > 0)
1620 return
1621 std::__stable_partition_adaptive(__first, __last, __pred,
1622 _DistanceType(__buf.requested_size()),
1623 __buf.begin(),
1624 _DistanceType(__buf.size()));
1625 else
1626 return
1627 std::__inplace_stable_partition(__first, __pred,
1628 _DistanceType(__buf.requested_size()));
1629 }
1630
1631 /**
1632 * @brief Move elements for which a predicate is true to the beginning
1633 * of a sequence, preserving relative ordering.
1634 * @ingroup mutating_algorithms
1635 * @param __first A forward iterator.
1636 * @param __last A forward iterator.
1637 * @param __pred A predicate functor.
1638 * @return An iterator @p middle such that @p __pred(i) is true for each
1639 * iterator @p i in the range @p [first,middle) and false for each @p i
1640 * in the range @p [middle,last).
1641 *
1642 * Performs the same function as @p partition() with the additional
1643 * guarantee that the relative ordering of elements in each group is
1644 * preserved, so any two elements @p x and @p y in the range
1645 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1646 * relative ordering after calling @p stable_partition().
1647 */
1648 template<typename _ForwardIterator, typename _Predicate>
1649 inline _ForwardIterator
1650 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1651 _Predicate __pred)
1652 {
1653 // concept requirements
1654 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1655 _ForwardIterator>)
1656 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1657 typename iterator_traits<_ForwardIterator>::value_type>)
1658 __glibcxx_requires_valid_range(__first, __last);
1659
1660 return std::__stable_partition(__first, __last,
1661 __gnu_cxx::__ops::__pred_iter(__pred));
1662 }
1663
1664 /// This is a helper function for the sort routines.
1665 template<typename _RandomAccessIterator, typename _Compare>
1666 void
1667 __heap_select(_RandomAccessIterator __first,
1668 _RandomAccessIterator __middle,
1669 _RandomAccessIterator __last, _Compare __comp)
1670 {
1671 std::__make_heap(__first, __middle, __comp);
1672 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1673 if (__comp(__i, __first))
1674 std::__pop_heap(__first, __middle, __i, __comp);
1675 }
1676
1677 // partial_sort
1678
1679 template<typename _InputIterator, typename _RandomAccessIterator,
1680 typename _Compare>
1681 _RandomAccessIterator
1682 __partial_sort_copy(_InputIterator __first, _InputIterator __last,
1683 _RandomAccessIterator __result_first,
1684 _RandomAccessIterator __result_last,
1685 _Compare __comp)
1686 {
1687 typedef typename iterator_traits<_InputIterator>::value_type
1688 _InputValueType;
1689 typedef iterator_traits<_RandomAccessIterator> _RItTraits;
1690 typedef typename _RItTraits::difference_type _DistanceType;
1691
1692 if (__result_first == __result_last)
1693 return __result_last;
1694 _RandomAccessIterator __result_real_last = __result_first;
1695 while (__first != __last && __result_real_last != __result_last)
1696 {
1697 *__result_real_last = *__first;
1698 ++__result_real_last;
1699 ++__first;
1700 }
1701
1702 std::__make_heap(__result_first, __result_real_last, __comp);
1703 while (__first != __last)
1704 {
1705 if (__comp(__first, __result_first))
1706 std::__adjust_heap(__result_first, _DistanceType(0),
1707 _DistanceType(__result_real_last
1708 - __result_first),
1709 _InputValueType(*__first), __comp);
1710 ++__first;
1711 }
1712 std::__sort_heap(__result_first, __result_real_last, __comp);
1713 return __result_real_last;
1714 }
1715
1716 /**
1717 * @brief Copy the smallest elements of a sequence.
1718 * @ingroup sorting_algorithms
1719 * @param __first An iterator.
1720 * @param __last Another iterator.
1721 * @param __result_first A random-access iterator.
1722 * @param __result_last Another random-access iterator.
1723 * @return An iterator indicating the end of the resulting sequence.
1724 *
1725 * Copies and sorts the smallest N values from the range @p [__first,__last)
1726 * to the range beginning at @p __result_first, where the number of
1727 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1728 * @p (__result_last-__result_first).
1729 * After the sort if @e i and @e j are iterators in the range
1730 * @p [__result_first,__result_first+N) such that i precedes j then
1731 * *j<*i is false.
1732 * The value returned is @p __result_first+N.
1733 */
1734 template<typename _InputIterator, typename _RandomAccessIterator>
1735 inline _RandomAccessIterator
1736 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1737 _RandomAccessIterator __result_first,
1738 _RandomAccessIterator __result_last)
1739 {
1740 typedef typename iterator_traits<_InputIterator>::value_type
1741 _InputValueType;
1742 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1743 _OutputValueType;
1744 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1745 _DistanceType;
1746
1747 // concept requirements
1748 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1749 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1750 _OutputValueType>)
1751 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
1752 _OutputValueType>)
1753 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
1754 __glibcxx_requires_valid_range(__first, __last);
1755 __glibcxx_requires_valid_range(__result_first, __result_last);
1756
1757 return std::__partial_sort_copy(__first, __last,
1758 __result_first, __result_last,
1759 __gnu_cxx::__ops::__iter_less_iter());
1760 }
1761
1762 /**
1763 * @brief Copy the smallest elements of a sequence using a predicate for
1764 * comparison.
1765 * @ingroup sorting_algorithms
1766 * @param __first An input iterator.
1767 * @param __last Another input iterator.
1768 * @param __result_first A random-access iterator.
1769 * @param __result_last Another random-access iterator.
1770 * @param __comp A comparison functor.
1771 * @return An iterator indicating the end of the resulting sequence.
1772 *
1773 * Copies and sorts the smallest N values from the range @p [__first,__last)
1774 * to the range beginning at @p result_first, where the number of
1775 * elements to be copied, @p N, is the smaller of @p (__last-__first) and
1776 * @p (__result_last-__result_first).
1777 * After the sort if @e i and @e j are iterators in the range
1778 * @p [__result_first,__result_first+N) such that i precedes j then
1779 * @p __comp(*j,*i) is false.
1780 * The value returned is @p __result_first+N.
1781 */
1782 template<typename _InputIterator, typename _RandomAccessIterator,
1783 typename _Compare>
1784 inline _RandomAccessIterator
1785 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1786 _RandomAccessIterator __result_first,
1787 _RandomAccessIterator __result_last,
1788 _Compare __comp)
1789 {
1790 typedef typename iterator_traits<_InputIterator>::value_type
1791 _InputValueType;
1792 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1793 _OutputValueType;
1794 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1795 _DistanceType;
1796
1797 // concept requirements
1798 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1799 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1800 _RandomAccessIterator>)
1801 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1802 _OutputValueType>)
1803 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
1804 _InputValueType, _OutputValueType>)
1805 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
1806 _OutputValueType, _OutputValueType>)
1807 __glibcxx_requires_valid_range(__first, __last);
1808 __glibcxx_requires_valid_range(__result_first, __result_last);
1809
1810 return std::__partial_sort_copy(__first, __last,
1811 __result_first, __result_last,
1812 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1813 }
1814
1815 /// This is a helper function for the sort routine.
1816 template<typename _RandomAccessIterator, typename _Compare>
1817 void
1818 __unguarded_linear_insert(_RandomAccessIterator __last,
1819 _Compare __comp)
1820 {
1821 typename iterator_traits<_RandomAccessIterator>::value_type
1822 __val = _GLIBCXX_MOVE(*__last);
1823 _RandomAccessIterator __next = __last;
1824 --__next;
1825 while (__comp(__val, __next))
1826 {
1827 *__last = _GLIBCXX_MOVE(*__next);
1828 __last = __next;
1829 --__next;
1830 }
1831 *__last = _GLIBCXX_MOVE(__val);
1832 }
1833
1834 /// This is a helper function for the sort routine.
1835 template<typename _RandomAccessIterator, typename _Compare>
1836 void
1837 __insertion_sort(_RandomAccessIterator __first,
1838 _RandomAccessIterator __last, _Compare __comp)
1839 {
1840 if (__first == __last) return;
1841
1842 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1843 {
1844 if (__comp(__i, __first))
1845 {
1846 typename iterator_traits<_RandomAccessIterator>::value_type
1847 __val = _GLIBCXX_MOVE(*__i);
1848 _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
1849 *__first = _GLIBCXX_MOVE(__val);
1850 }
1851 else
1852 std::__unguarded_linear_insert(__i,
1853 __gnu_cxx::__ops::__val_comp_iter(__comp));
1854 }
1855 }
1856
1857 /// This is a helper function for the sort routine.
1858 template<typename _RandomAccessIterator, typename _Compare>
1859 inline void
1860 __unguarded_insertion_sort(_RandomAccessIterator __first,
1861 _RandomAccessIterator __last, _Compare __comp)
1862 {
1863 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
1864 std::__unguarded_linear_insert(__i,
1865 __gnu_cxx::__ops::__val_comp_iter(__comp));
1866 }
1867
1868 /**
1869 * @doctodo
1870 * This controls some aspect of the sort routines.
1871 */
1872 enum { _S_threshold = 16 };
1873
1874 /// This is a helper function for the sort routine.
1875 template<typename _RandomAccessIterator, typename _Compare>
1876 void
1877 __final_insertion_sort(_RandomAccessIterator __first,
1878 _RandomAccessIterator __last, _Compare __comp)
1879 {
1880 if (__last - __first > int(_S_threshold))
1881 {
1882 std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
1883 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
1884 __comp);
1885 }
1886 else
1887 std::__insertion_sort(__first, __last, __comp);
1888 }
1889
1890 /// This is a helper function...
1891 template<typename _RandomAccessIterator, typename _Compare>
1892 _RandomAccessIterator
1893 __unguarded_partition(_RandomAccessIterator __first,
1894 _RandomAccessIterator __last,
1895 _RandomAccessIterator __pivot, _Compare __comp)
1896 {
1897 while (true)
1898 {
1899 while (__comp(__first, __pivot))
1900 ++__first;
1901 --__last;
1902 while (__comp(__pivot, __last))
1903 --__last;
1904 if (!(__first < __last))
1905 return __first;
1906 std::iter_swap(__first, __last);
1907 ++__first;
1908 }
1909 }
1910
1911 /// This is a helper function...
1912 template<typename _RandomAccessIterator, typename _Compare>
1913 inline _RandomAccessIterator
1914 __unguarded_partition_pivot(_RandomAccessIterator __first,
1915 _RandomAccessIterator __last, _Compare __comp)
1916 {
1917 _RandomAccessIterator __mid = __first + (__last - __first) / 2;
1918 std::__move_median_first(__first, __mid, (__last - 1), __comp);
1919 return std::__unguarded_partition(__first + 1, __last, __first, __comp);
1920 }
1921
1922 template<typename _RandomAccessIterator, typename _Compare>
1923 inline void
1924 __partial_sort(_RandomAccessIterator __first,
1925 _RandomAccessIterator __middle,
1926 _RandomAccessIterator __last,
1927 _Compare __comp)
1928 {
1929 std::__heap_select(__first, __middle, __last, __comp);
1930 std::__sort_heap(__first, __middle, __comp);
1931 }
1932
1933 /// This is a helper function for the sort routine.
1934 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
1935 void
1936 __introsort_loop(_RandomAccessIterator __first,
1937 _RandomAccessIterator __last,
1938 _Size __depth_limit, _Compare __comp)
1939 {
1940 while (__last - __first > int(_S_threshold))
1941 {
1942 if (__depth_limit == 0)
1943 {
1944 std::__partial_sort(__first, __last, __last, __comp);
1945 return;
1946 }
1947 --__depth_limit;
1948 _RandomAccessIterator __cut =
1949 std::__unguarded_partition_pivot(__first, __last, __comp);
1950 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
1951 __last = __cut;
1952 }
1953 }
1954
1955 // sort
1956
1957 template<typename _RandomAccessIterator, typename _Compare>
1958 inline void
1959 __sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
1960 _Compare __comp)
1961 {
1962 if (__first != __last)
1963 {
1964 std::__introsort_loop(__first, __last,
1965 std::__lg(__last - __first) * 2,
1966 __comp);
1967 std::__final_insertion_sort(__first, __last, __comp);
1968 }
1969 }
1970
1971 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
1972 void
1973 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
1974 _RandomAccessIterator __last, _Size __depth_limit,
1975 _Compare __comp)
1976 {
1977 while (__last - __first > 3)
1978 {
1979 if (__depth_limit == 0)
1980 {
1981 std::__heap_select(__first, __nth + 1, __last, __comp);
1982 // Place the nth largest element in its final position.
1983 std::iter_swap(__first, __nth);
1984 return;
1985 }
1986 --__depth_limit;
1987 _RandomAccessIterator __cut =
1988 std::__unguarded_partition_pivot(__first, __last, __comp);
1989 if (__cut <= __nth)
1990 __first = __cut;
1991 else
1992 __last = __cut;
1993 }
1994 std::__insertion_sort(__first, __last, __comp);
1995 }
1996
1997 // nth_element
1998
1999 // lower_bound moved to stl_algobase.h
2000
2001 /**
2002 * @brief Finds the first position in which @p __val could be inserted
2003 * without changing the ordering.
2004 * @ingroup binary_search_algorithms
2005 * @param __first An iterator.
2006 * @param __last Another iterator.
2007 * @param __val The search term.
2008 * @param __comp A functor to use for comparisons.
2009 * @return An iterator pointing to the first element <em>not less
2010 * than</em> @p __val, or end() if every element is less
2011 * than @p __val.
2012 * @ingroup binary_search_algorithms
2013 *
2014 * The comparison function should have the same effects on ordering as
2015 * the function used for the initial sort.
2016 */
2017 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2018 inline _ForwardIterator
2019 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2020 const _Tp& __val, _Compare __comp)
2021 {
2022 typedef typename iterator_traits<_ForwardIterator>::value_type
2023 _ValueType;
2024
2025 // concept requirements
2026 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2027 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2028 _ValueType, _Tp>)
2029 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2030 __val, __comp);
2031
2032 return std::__lower_bound(__first, __last, __val,
2033 __gnu_cxx::__ops::__iter_comp_val(__comp));
2034 }
2035
2036 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2037 _ForwardIterator
2038 __upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2039 const _Tp& __val, _Compare __comp)
2040 {
2041 typedef typename iterator_traits<_ForwardIterator>::difference_type
2042 _DistanceType;
2043
2044 _DistanceType __len = std::distance(__first, __last);
2045
2046 while (__len > 0)
2047 {
2048 _DistanceType __half = __len >> 1;
2049 _ForwardIterator __middle = __first;
2050 std::advance(__middle, __half);
2051 if (__comp(__val, __middle))
2052 __len = __half;
2053 else
2054 {
2055 __first = __middle;
2056 ++__first;
2057 __len = __len - __half - 1;
2058 }
2059 }
2060 return __first;
2061 }
2062
2063 /**
2064 * @brief Finds the last position in which @p __val could be inserted
2065 * without changing the ordering.
2066 * @ingroup binary_search_algorithms
2067 * @param __first An iterator.
2068 * @param __last Another iterator.
2069 * @param __val The search term.
2070 * @return An iterator pointing to the first element greater than @p __val,
2071 * or end() if no elements are greater than @p __val.
2072 * @ingroup binary_search_algorithms
2073 */
2074 template<typename _ForwardIterator, typename _Tp>
2075 inline _ForwardIterator
2076 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2077 const _Tp& __val)
2078 {
2079 typedef typename iterator_traits<_ForwardIterator>::value_type
2080 _ValueType;
2081
2082 // concept requirements
2083 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2084 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2085 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2086
2087 return std::__upper_bound(__first, __last, __val,
2088 __gnu_cxx::__ops::__val_less_iter());
2089 }
2090
2091 /**
2092 * @brief Finds the last position in which @p __val could be inserted
2093 * without changing the ordering.
2094 * @ingroup binary_search_algorithms
2095 * @param __first An iterator.
2096 * @param __last Another iterator.
2097 * @param __val The search term.
2098 * @param __comp A functor to use for comparisons.
2099 * @return An iterator pointing to the first element greater than @p __val,
2100 * or end() if no elements are greater than @p __val.
2101 * @ingroup binary_search_algorithms
2102 *
2103 * The comparison function should have the same effects on ordering as
2104 * the function used for the initial sort.
2105 */
2106 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2107 inline _ForwardIterator
2108 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2109 const _Tp& __val, _Compare __comp)
2110 {
2111 typedef typename iterator_traits<_ForwardIterator>::value_type
2112 _ValueType;
2113
2114 // concept requirements
2115 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2116 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2117 _Tp, _ValueType>)
2118 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2119 __val, __comp);
2120
2121 return std::__upper_bound(__first, __last, __val,
2122 __gnu_cxx::__ops::__val_comp_iter(__comp));
2123 }
2124
2125 template<typename _ForwardIterator, typename _Tp,
2126 typename _CompareItTp, typename _CompareTpIt>
2127 pair<_ForwardIterator, _ForwardIterator>
2128 __equal_range(_ForwardIterator __first, _ForwardIterator __last,
2129 const _Tp& __val,
2130 _CompareItTp __comp_it_val, _CompareTpIt __comp_val_it)
2131 {
2132 typedef typename iterator_traits<_ForwardIterator>::difference_type
2133 _DistanceType;
2134
2135 _DistanceType __len = std::distance(__first, __last);
2136
2137 while (__len > 0)
2138 {
2139 _DistanceType __half = __len >> 1;
2140 _ForwardIterator __middle = __first;
2141 std::advance(__middle, __half);
2142 if (__comp_it_val(__middle, __val))
2143 {
2144 __first = __middle;
2145 ++__first;
2146 __len = __len - __half - 1;
2147 }
2148 else if (__comp_val_it(__val, __middle))
2149 __len = __half;
2150 else
2151 {
2152 _ForwardIterator __left
2153 = std::__lower_bound(__first, __middle, __val, __comp_it_val);
2154 std::advance(__first, __len);
2155 _ForwardIterator __right
2156 = std::__upper_bound(++__middle, __first, __val, __comp_val_it);
2157 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2158 }
2159 }
2160 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2161 }
2162
2163 /**
2164 * @brief Finds the largest subrange in which @p __val could be inserted
2165 * at any place in it without changing the ordering.
2166 * @ingroup binary_search_algorithms
2167 * @param __first An iterator.
2168 * @param __last Another iterator.
2169 * @param __val The search term.
2170 * @return An pair of iterators defining the subrange.
2171 * @ingroup binary_search_algorithms
2172 *
2173 * This is equivalent to
2174 * @code
2175 * std::make_pair(lower_bound(__first, __last, __val),
2176 * upper_bound(__first, __last, __val))
2177 * @endcode
2178 * but does not actually call those functions.
2179 */
2180 template<typename _ForwardIterator, typename _Tp>
2181 inline pair<_ForwardIterator, _ForwardIterator>
2182 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2183 const _Tp& __val)
2184 {
2185 typedef typename iterator_traits<_ForwardIterator>::value_type
2186 _ValueType;
2187
2188 // concept requirements
2189 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2190 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2191 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2192 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2193 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2194
2195 return std::__equal_range(__first, __last, __val,
2196 __gnu_cxx::__ops::__iter_less_val(),
2197 __gnu_cxx::__ops::__val_less_iter());
2198 }
2199
2200 /**
2201 * @brief Finds the largest subrange in which @p __val could be inserted
2202 * at any place in it without changing the ordering.
2203 * @param __first An iterator.
2204 * @param __last Another iterator.
2205 * @param __val The search term.
2206 * @param __comp A functor to use for comparisons.
2207 * @return An pair of iterators defining the subrange.
2208 * @ingroup binary_search_algorithms
2209 *
2210 * This is equivalent to
2211 * @code
2212 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2213 * upper_bound(__first, __last, __val, __comp))
2214 * @endcode
2215 * but does not actually call those functions.
2216 */
2217 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2218 inline pair<_ForwardIterator, _ForwardIterator>
2219 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2220 const _Tp& __val, _Compare __comp)
2221 {
2222 typedef typename iterator_traits<_ForwardIterator>::value_type
2223 _ValueType;
2224
2225 // concept requirements
2226 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2227 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2228 _ValueType, _Tp>)
2229 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2230 _Tp, _ValueType>)
2231 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2232 __val, __comp);
2233 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2234 __val, __comp);
2235
2236 return std::__equal_range(__first, __last, __val,
2237 __gnu_cxx::__ops::__iter_comp_val(__comp),
2238 __gnu_cxx::__ops::__val_comp_iter(__comp));
2239 }
2240
2241 /**
2242 * @brief Determines whether an element exists in a range.
2243 * @ingroup binary_search_algorithms
2244 * @param __first An iterator.
2245 * @param __last Another iterator.
2246 * @param __val The search term.
2247 * @return True if @p __val (or its equivalent) is in [@p
2248 * __first,@p __last ].
2249 *
2250 * Note that this does not actually return an iterator to @p __val. For
2251 * that, use std::find or a container's specialized find member functions.
2252 */
2253 template<typename _ForwardIterator, typename _Tp>
2254 bool
2255 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2256 const _Tp& __val)
2257 {
2258 typedef typename iterator_traits<_ForwardIterator>::value_type
2259 _ValueType;
2260
2261 // concept requirements
2262 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2263 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2264 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2265 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2266
2267 _ForwardIterator __i
2268 = std::__lower_bound(__first, __last, __val,
2269 __gnu_cxx::__ops::__iter_less_val());
2270 return __i != __last && !(__val < *__i);
2271 }
2272
2273 /**
2274 * @brief Determines whether an element exists in a range.
2275 * @ingroup binary_search_algorithms
2276 * @param __first An iterator.
2277 * @param __last Another iterator.
2278 * @param __val The search term.
2279 * @param __comp A functor to use for comparisons.
2280 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2281 *
2282 * Note that this does not actually return an iterator to @p __val. For
2283 * that, use std::find or a container's specialized find member functions.
2284 *
2285 * The comparison function should have the same effects on ordering as
2286 * the function used for the initial sort.
2287 */
2288 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2289 bool
2290 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2291 const _Tp& __val, _Compare __comp)
2292 {
2293 typedef typename iterator_traits<_ForwardIterator>::value_type
2294 _ValueType;
2295
2296 // concept requirements
2297 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2298 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2299 _Tp, _ValueType>)
2300 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2301 __val, __comp);
2302 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2303 __val, __comp);
2304
2305 _ForwardIterator __i
2306 = std::__lower_bound(__first, __last, __val,
2307 __gnu_cxx::__ops::__iter_comp_val(__comp));
2308 return __i != __last && !bool(__comp(__val, *__i));
2309 }
2310
2311 // merge
2312
2313 /// This is a helper function for the __merge_adaptive routines.
2314 template<typename _InputIterator1, typename _InputIterator2,
2315 typename _OutputIterator, typename _Compare>
2316 void
2317 __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1,
2318 _InputIterator2 __first2, _InputIterator2 __last2,
2319 _OutputIterator __result, _Compare __comp)
2320 {
2321 while (__first1 != __last1 && __first2 != __last2)
2322 {
2323 if (__comp(__first2, __first1))
2324 {
2325 *__result = _GLIBCXX_MOVE(*__first2);
2326 ++__first2;
2327 }
2328 else
2329 {
2330 *__result = _GLIBCXX_MOVE(*__first1);
2331 ++__first1;
2332 }
2333 ++__result;
2334 }
2335 if (__first1 != __last1)
2336 _GLIBCXX_MOVE3(__first1, __last1, __result);
2337 }
2338
2339 /// This is a helper function for the __merge_adaptive routines.
2340 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2341 typename _BidirectionalIterator3, typename _Compare>
2342 void
2343 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1,
2344 _BidirectionalIterator1 __last1,
2345 _BidirectionalIterator2 __first2,
2346 _BidirectionalIterator2 __last2,
2347 _BidirectionalIterator3 __result,
2348 _Compare __comp)
2349 {
2350 if (__first1 == __last1)
2351 {
2352 _GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result);
2353 return;
2354 }
2355 else if (__first2 == __last2)
2356 return;
2357
2358 --__last1;
2359 --__last2;
2360 while (true)
2361 {
2362 if (__comp(__last2, __last1))
2363 {
2364 *--__result = _GLIBCXX_MOVE(*__last1);
2365 if (__first1 == __last1)
2366 {
2367 _GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result);
2368 return;
2369 }
2370 --__last1;
2371 }
2372 else
2373 {
2374 *--__result = _GLIBCXX_MOVE(*__last2);
2375 if (__first2 == __last2)
2376 return;
2377 --__last2;
2378 }
2379 }
2380 }
2381
2382 /// This is a helper function for the merge routines.
2383 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2384 typename _Distance>
2385 _BidirectionalIterator1
2386 __rotate_adaptive(_BidirectionalIterator1 __first,
2387 _BidirectionalIterator1 __middle,
2388 _BidirectionalIterator1 __last,
2389 _Distance __len1, _Distance __len2,
2390 _BidirectionalIterator2 __buffer,
2391 _Distance __buffer_size)
2392 {
2393 _BidirectionalIterator2 __buffer_end;
2394 if (__len1 > __len2 && __len2 <= __buffer_size)
2395 {
2396 if (__len2)
2397 {
2398 __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2399 _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last);
2400 return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first);
2401 }
2402 else
2403 return __first;
2404 }
2405 else if (__len1 <= __buffer_size)
2406 {
2407 if (__len1)
2408 {
2409 __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2410 _GLIBCXX_MOVE3(__middle, __last, __first);
2411 return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
2412 }
2413 else
2414 return __last;
2415 }
2416 else
2417 {
2418 std::rotate(__first, __middle, __last);
2419 std::advance(__first, std::distance(__middle, __last));
2420 return __first;
2421 }
2422 }
2423
2424 /// This is a helper function for the merge routines.
2425 template<typename _BidirectionalIterator, typename _Distance,
2426 typename _Pointer, typename _Compare>
2427 void
2428 __merge_adaptive(_BidirectionalIterator __first,
2429 _BidirectionalIterator __middle,
2430 _BidirectionalIterator __last,
2431 _Distance __len1, _Distance __len2,
2432 _Pointer __buffer, _Distance __buffer_size,
2433 _Compare __comp)
2434 {
2435 if (__len1 <= __len2 && __len1 <= __buffer_size)
2436 {
2437 _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2438 std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last,
2439 __first, __comp);
2440 }
2441 else if (__len2 <= __buffer_size)
2442 {
2443 _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2444 std::__move_merge_adaptive_backward(__first, __middle, __buffer,
2445 __buffer_end, __last, __comp);
2446 }
2447 else
2448 {
2449 _BidirectionalIterator __first_cut = __first;
2450 _BidirectionalIterator __second_cut = __middle;
2451 _Distance __len11 = 0;
2452 _Distance __len22 = 0;
2453 if (__len1 > __len2)
2454 {
2455 __len11 = __len1 / 2;
2456 std::advance(__first_cut, __len11);
2457 __second_cut
2458 = std::__lower_bound(__middle, __last, *__first_cut,
2459 __gnu_cxx::__ops::__iter_comp_val(__comp));
2460 __len22 = std::distance(__middle, __second_cut);
2461 }
2462 else
2463 {
2464 __len22 = __len2 / 2;
2465 std::advance(__second_cut, __len22);
2466 __first_cut
2467 = std::__upper_bound(__first, __middle, *__second_cut,
2468 __gnu_cxx::__ops::__val_comp_iter(__comp));
2469 __len11 = std::distance(__first, __first_cut);
2470 }
2471 _BidirectionalIterator __new_middle
2472 = std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2473 __len1 - __len11, __len22, __buffer,
2474 __buffer_size);
2475 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
2476 __len22, __buffer, __buffer_size, __comp);
2477 std::__merge_adaptive(__new_middle, __second_cut, __last,
2478 __len1 - __len11,
2479 __len2 - __len22, __buffer,
2480 __buffer_size, __comp);
2481 }
2482 }
2483
2484 /// This is a helper function for the merge routines.
2485 template<typename _BidirectionalIterator, typename _Distance,
2486 typename _Compare>
2487 void
2488 __merge_without_buffer(_BidirectionalIterator __first,
2489 _BidirectionalIterator __middle,
2490 _BidirectionalIterator __last,
2491 _Distance __len1, _Distance __len2,
2492 _Compare __comp)
2493 {
2494 if (__len1 == 0 || __len2 == 0)
2495 return;
2496 if (__len1 + __len2 == 2)
2497 {
2498 if (__comp(__middle, __first))
2499 std::iter_swap(__first, __middle);
2500 return;
2501 }
2502 _BidirectionalIterator __first_cut = __first;
2503 _BidirectionalIterator __second_cut = __middle;
2504 _Distance __len11 = 0;
2505 _Distance __len22 = 0;
2506 if (__len1 > __len2)
2507 {
2508 __len11 = __len1 / 2;
2509 std::advance(__first_cut, __len11);
2510 __second_cut
2511 = std::__lower_bound(__middle, __last, *__first_cut,
2512 __gnu_cxx::__ops::__iter_comp_val(__comp));
2513 __len22 = std::distance(__middle, __second_cut);
2514 }
2515 else
2516 {
2517 __len22 = __len2 / 2;
2518 std::advance(__second_cut, __len22);
2519 __first_cut
2520 = std::__upper_bound(__first, __middle, *__second_cut,
2521 __gnu_cxx::__ops::__val_comp_iter(__comp));
2522 __len11 = std::distance(__first, __first_cut);
2523 }
2524 std::rotate(__first_cut, __middle, __second_cut);
2525 _BidirectionalIterator __new_middle = __first_cut;
2526 std::advance(__new_middle, std::distance(__middle, __second_cut));
2527 std::__merge_without_buffer(__first, __first_cut, __new_middle,
2528 __len11, __len22, __comp);
2529 std::__merge_without_buffer(__new_middle, __second_cut, __last,
2530 __len1 - __len11, __len2 - __len22, __comp);
2531 }
2532
2533 template<typename _BidirectionalIterator, typename _Compare>
2534 void
2535 __inplace_merge(_BidirectionalIterator __first,
2536 _BidirectionalIterator __middle,
2537 _BidirectionalIterator __last,
2538 _Compare __comp)
2539 {
2540 typedef typename iterator_traits<_BidirectionalIterator>::value_type
2541 _ValueType;
2542 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
2543 _DistanceType;
2544
2545 if (__first == __middle || __middle == __last)
2546 return;
2547
2548 const _DistanceType __len1 = std::distance(__first, __middle);
2549 const _DistanceType __len2 = std::distance(__middle, __last);
2550
2551 typedef _Temporary_buffer<_BidirectionalIterator, _ValueType> _TmpBuf;
2552 _TmpBuf __buf(__first, __last);
2553
2554 if (__buf.begin() == 0)
2555 std::__merge_without_buffer
2556 (__first, __middle, __last, __len1, __len2, __comp);
2557 else
2558 std::__merge_adaptive
2559 (__first, __middle, __last, __len1, __len2, __buf.begin(),
2560 _DistanceType(__buf.size()), __comp);
2561 }
2562
2563 /**
2564 * @brief Merges two sorted ranges in place.
2565 * @ingroup sorting_algorithms
2566 * @param __first An iterator.
2567 * @param __middle Another iterator.
2568 * @param __last Another iterator.
2569 * @return Nothing.
2570 *
2571 * Merges two sorted and consecutive ranges, [__first,__middle) and
2572 * [__middle,__last), and puts the result in [__first,__last). The
2573 * output will be sorted. The sort is @e stable, that is, for
2574 * equivalent elements in the two ranges, elements from the first
2575 * range will always come before elements from the second.
2576 *
2577 * If enough additional memory is available, this takes (__last-__first)-1
2578 * comparisons. Otherwise an NlogN algorithm is used, where N is
2579 * distance(__first,__last).
2580 */
2581 template<typename _BidirectionalIterator>
2582 inline void
2583 inplace_merge(_BidirectionalIterator __first,
2584 _BidirectionalIterator __middle,
2585 _BidirectionalIterator __last)
2586 {
2587 // concept requirements
2588 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
2589 _BidirectionalIterator>)
2590 __glibcxx_function_requires(_LessThanComparableConcept<
2591 typename iterator_traits<_BidirectionalIterator>::value_type>)
2592 __glibcxx_requires_sorted(__first, __middle);
2593 __glibcxx_requires_sorted(__middle, __last);
2594
2595 std::__inplace_merge(__first, __middle, __last,
2596 __gnu_cxx::__ops::__iter_less_iter());
2597 }
2598
2599 /**
2600 * @brief Merges two sorted ranges in place.
2601 * @ingroup sorting_algorithms
2602 * @param __first An iterator.
2603 * @param __middle Another iterator.
2604 * @param __last Another iterator.
2605 * @param __comp A functor to use for comparisons.
2606 * @return Nothing.
2607 *
2608 * Merges two sorted and consecutive ranges, [__first,__middle) and
2609 * [middle,last), and puts the result in [__first,__last). The output will
2610 * be sorted. The sort is @e stable, that is, for equivalent
2611 * elements in the two ranges, elements from the first range will always
2612 * come before elements from the second.
2613 *
2614 * If enough additional memory is available, this takes (__last-__first)-1
2615 * comparisons. Otherwise an NlogN algorithm is used, where N is
2616 * distance(__first,__last).
2617 *
2618 * The comparison function should have the same effects on ordering as
2619 * the function used for the initial sort.
2620 */
2621 template<typename _BidirectionalIterator, typename _Compare>
2622 inline void
2623 inplace_merge(_BidirectionalIterator __first,
2624 _BidirectionalIterator __middle,
2625 _BidirectionalIterator __last,
2626 _Compare __comp)
2627 {
2628 // concept requirements
2629 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
2630 _BidirectionalIterator>)
2631 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2632 typename iterator_traits<_BidirectionalIterator>::value_type,
2633 typename iterator_traits<_BidirectionalIterator>::value_type>)
2634 __glibcxx_requires_sorted_pred(__first, __middle, __comp);
2635 __glibcxx_requires_sorted_pred(__middle, __last, __comp);
2636
2637 std::__inplace_merge(__first, __middle, __last,
2638 __gnu_cxx::__ops::__iter_comp_iter(__comp));
2639 }
2640
2641
2642 /// This is a helper function for the __merge_sort_loop routines.
2643 template<typename _InputIterator, typename _OutputIterator,
2644 typename _Compare>
2645 _OutputIterator
2646 __move_merge(_InputIterator __first1, _InputIterator __last1,
2647 _InputIterator __first2, _InputIterator __last2,
2648 _OutputIterator __result, _Compare __comp)
2649 {
2650 while (__first1 != __last1 && __first2 != __last2)
2651 {
2652 if (__comp(__first2, __first1))
2653 {
2654 *__result = _GLIBCXX_MOVE(*__first2);
2655 ++__first2;
2656 }
2657 else
2658 {
2659 *__result = _GLIBCXX_MOVE(*__first1);
2660 ++__first1;
2661 }
2662 ++__result;
2663 }
2664 return _GLIBCXX_MOVE3(__first2, __last2,
2665 _GLIBCXX_MOVE3(__first1, __last1,
2666 __result));
2667 }
2668
2669 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
2670 typename _Distance, typename _Compare>
2671 void
2672 __merge_sort_loop(_RandomAccessIterator1 __first,
2673 _RandomAccessIterator1 __last,
2674 _RandomAccessIterator2 __result, _Distance __step_size,
2675 _Compare __comp)
2676 {
2677 const _Distance __two_step = 2 * __step_size;
2678
2679 while (__last - __first >= __two_step)
2680 {
2681 __result = std::__move_merge(__first, __first + __step_size,
2682 __first + __step_size,
2683 __first + __two_step,
2684 __result, __comp);
2685 __first += __two_step;
2686 }
2687 __step_size = std::min(_Distance(__last - __first), __step_size);
2688
2689 std::__move_merge(__first, __first + __step_size,
2690 __first + __step_size, __last, __result, __comp);
2691 }
2692
2693 template<typename _RandomAccessIterator, typename _Distance,
2694 typename _Compare>
2695 void
2696 __chunk_insertion_sort(_RandomAccessIterator __first,
2697 _RandomAccessIterator __last,
2698 _Distance __chunk_size, _Compare __comp)
2699 {
2700 while (__last - __first >= __chunk_size)
2701 {
2702 std::__insertion_sort(__first, __first + __chunk_size, __comp);
2703 __first += __chunk_size;
2704 }
2705 std::__insertion_sort(__first, __last, __comp);
2706 }
2707
2708 enum { _S_chunk_size = 7 };
2709
2710 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
2711 void
2712 __merge_sort_with_buffer(_RandomAccessIterator __first,
2713 _RandomAccessIterator __last,
2714 _Pointer __buffer, _Compare __comp)
2715 {
2716 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2717 _Distance;
2718
2719 const _Distance __len = __last - __first;
2720 const _Pointer __buffer_last = __buffer + __len;
2721
2722 _Distance __step_size = _S_chunk_size;
2723 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
2724
2725 while (__step_size < __len)
2726 {
2727 std::__merge_sort_loop(__first, __last, __buffer,
2728 __step_size, __comp);
2729 __step_size *= 2;
2730 std::__merge_sort_loop(__buffer, __buffer_last, __first,
2731 __step_size, __comp);
2732 __step_size *= 2;
2733 }
2734 }
2735
2736 template<typename _RandomAccessIterator, typename _Pointer,
2737 typename _Distance, typename _Compare>
2738 void
2739 __stable_sort_adaptive(_RandomAccessIterator __first,
2740 _RandomAccessIterator __last,
2741 _Pointer __buffer, _Distance __buffer_size,
2742 _Compare __comp)
2743 {
2744 const _Distance __len = (__last - __first + 1) / 2;
2745 const _RandomAccessIterator __middle = __first + __len;
2746 if (__len > __buffer_size)
2747 {
2748 std::__stable_sort_adaptive(__first, __middle, __buffer,
2749 __buffer_size, __comp);
2750 std::__stable_sort_adaptive(__middle, __last, __buffer,
2751 __buffer_size, __comp);
2752 }
2753 else
2754 {
2755 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
2756 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
2757 }
2758 std::__merge_adaptive(__first, __middle, __last,
2759 _Distance(__middle - __first),
2760 _Distance(__last - __middle),
2761 __buffer, __buffer_size,
2762 __comp);
2763 }
2764
2765 /// This is a helper function for the stable sorting routines.
2766 template<typename _RandomAccessIterator, typename _Compare>
2767 void
2768 __inplace_stable_sort(_RandomAccessIterator __first,
2769 _RandomAccessIterator __last, _Compare __comp)
2770 {
2771 if (__last - __first < 15)
2772 {
2773 std::__insertion_sort(__first, __last, __comp);
2774 return;
2775 }
2776 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
2777 std::__inplace_stable_sort(__first, __middle, __comp);
2778 std::__inplace_stable_sort(__middle, __last, __comp);
2779 std::__merge_without_buffer(__first, __middle, __last,
2780 __middle - __first,
2781 __last - __middle,
2782 __comp);
2783 }
2784
2785 // stable_sort
2786
2787 // Set algorithms: includes, set_union, set_intersection, set_difference,
2788 // set_symmetric_difference. All of these algorithms have the precondition
2789 // that their input ranges are sorted and the postcondition that their output
2790 // ranges are sorted.
2791
2792 template<typename _InputIterator1, typename _InputIterator2,
2793 typename _Compare>
2794 bool
2795 __includes(_InputIterator1 __first1, _InputIterator1 __last1,
2796 _InputIterator2 __first2, _InputIterator2 __last2,
2797 _Compare __comp)
2798 {
2799 while (__first1 != __last1 && __first2 != __last2)
2800 if (__comp(__first2, __first1))
2801 return false;
2802 else if (__comp(__first1, __first2))
2803 ++__first1;
2804 else
2805 ++__first1, ++__first2;
2806
2807 return __first2 == __last2;
2808 }
2809
2810 /**
2811 * @brief Determines whether all elements of a sequence exists in a range.
2812 * @param __first1 Start of search range.
2813 * @param __last1 End of search range.
2814 * @param __first2 Start of sequence
2815 * @param __last2 End of sequence.
2816 * @return True if each element in [__first2,__last2) is contained in order
2817 * within [__first1,__last1). False otherwise.
2818 * @ingroup set_algorithms
2819 *
2820 * This operation expects both [__first1,__last1) and
2821 * [__first2,__last2) to be sorted. Searches for the presence of
2822 * each element in [__first2,__last2) within [__first1,__last1).
2823 * The iterators over each range only move forward, so this is a
2824 * linear algorithm. If an element in [__first2,__last2) is not
2825 * found before the search iterator reaches @p __last2, false is
2826 * returned.
2827 */
2828 template<typename _InputIterator1, typename _InputIterator2>
2829 inline bool
2830 includes(_InputIterator1 __first1, _InputIterator1 __last1,
2831 _InputIterator2 __first2, _InputIterator2 __last2)
2832 {
2833 // concept requirements
2834 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
2835 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
2836 __glibcxx_function_requires(_LessThanOpConcept<
2837 typename iterator_traits<_InputIterator1>::value_type,
2838 typename iterator_traits<_InputIterator2>::value_type>)
2839 __glibcxx_function_requires(_LessThanOpConcept<
2840 typename iterator_traits<_InputIterator2>::value_type,
2841 typename iterator_traits<_InputIterator1>::value_type>)
2842 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
2843 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
2844
2845 return std::__includes(__first1, __last1, __first2, __last2,
2846 __gnu_cxx::__ops::__iter_less_iter());
2847 }
2848
2849 /**
2850 * @brief Determines whether all elements of a sequence exists in a range
2851 * using comparison.
2852 * @ingroup set_algorithms
2853 * @param __first1 Start of search range.
2854 * @param __last1 End of search range.
2855 * @param __first2 Start of sequence
2856 * @param __last2 End of sequence.
2857 * @param __comp Comparison function to use.
2858 * @return True if each element in [__first2,__last2) is contained
2859 * in order within [__first1,__last1) according to comp. False
2860 * otherwise. @ingroup set_algorithms
2861 *
2862 * This operation expects both [__first1,__last1) and
2863 * [__first2,__last2) to be sorted. Searches for the presence of
2864 * each element in [__first2,__last2) within [__first1,__last1),
2865 * using comp to decide. The iterators over each range only move
2866 * forward, so this is a linear algorithm. If an element in
2867 * [__first2,__last2) is not found before the search iterator
2868 * reaches @p __last2, false is returned.
2869 */
2870 template<typename _InputIterator1, typename _InputIterator2,
2871 typename _Compare>
2872 inline bool
2873 includes(_InputIterator1 __first1, _InputIterator1 __last1,
2874 _InputIterator2 __first2, _InputIterator2 __last2,
2875 _Compare __comp)
2876 {
2877 // concept requirements
2878 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
2879 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
2880 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2881 typename iterator_traits<_InputIterator1>::value_type,
2882 typename iterator_traits<_InputIterator2>::value_type>)
2883 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2884 typename iterator_traits<_InputIterator2>::value_type,
2885 typename iterator_traits<_InputIterator1>::value_type>)
2886 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
2887 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
2888
2889 return std::__includes(__first1, __last1, __first2, __last2,
2890 __gnu_cxx::__ops::__iter_comp_iter(__comp));
2891 }
2892
2893 // nth_element
2894 // merge
2895 // set_difference
2896 // set_intersection
2897 // set_union
2898 // stable_sort
2899 // set_symmetric_difference
2900 // min_element
2901 // max_element
2902
2903 template<typename _BidirectionalIterator, typename _Compare>
2904 bool
2905 __next_permutation(_BidirectionalIterator __first,
2906 _BidirectionalIterator __last, _Compare __comp)
2907 {
2908 if (__first == __last)
2909 return false;
2910 _BidirectionalIterator __i = __first;
2911 ++__i;
2912 if (__i == __last)
2913 return false;
2914 __i = __last;
2915 --__i;
2916
2917 for(;;)
2918 {
2919 _BidirectionalIterator __ii = __i;
2920 --__i;
2921 if (__comp(__i, __ii))
2922 {
2923 _BidirectionalIterator __j = __last;
2924 while (!__comp(__i, --__j))
2925 {}
2926 std::iter_swap(__i, __j);
2927 std::__reverse(__ii, __last,
2928 std::__iterator_category(__first));
2929 return true;
2930 }
2931 if (__i == __first)
2932 {
2933 std::__reverse(__first, __last,
2934 std::__iterator_category(__first));
2935 return false;
2936 }
2937 }
2938 }
2939
2940 /**
2941 * @brief Permute range into the next @e dictionary ordering.
2942 * @ingroup sorting_algorithms
2943 * @param __first Start of range.
2944 * @param __last End of range.
2945 * @return False if wrapped to first permutation, true otherwise.
2946 *
2947 * Treats all permutations of the range as a set of @e dictionary sorted
2948 * sequences. Permutes the current sequence into the next one of this set.
2949 * Returns true if there are more sequences to generate. If the sequence
2950 * is the largest of the set, the smallest is generated and false returned.
2951 */
2952 template<typename _BidirectionalIterator>
2953 inline bool
2954 next_permutation(_BidirectionalIterator __first,
2955 _BidirectionalIterator __last)
2956 {
2957 // concept requirements
2958 __glibcxx_function_requires(_BidirectionalIteratorConcept<
2959 _BidirectionalIterator>)
2960 __glibcxx_function_requires(_LessThanComparableConcept<
2961 typename iterator_traits<_BidirectionalIterator>::value_type>)
2962 __glibcxx_requires_valid_range(__first, __last);
2963
2964 return std::__next_permutation
2965 (__first, __last, __gnu_cxx::__ops::__iter_less_iter());
2966 }
2967
2968 /**
2969 * @brief Permute range into the next @e dictionary ordering using
2970 * comparison functor.
2971 * @ingroup sorting_algorithms
2972 * @param __first Start of range.
2973 * @param __last End of range.
2974 * @param __comp A comparison functor.
2975 * @return False if wrapped to first permutation, true otherwise.
2976 *
2977 * Treats all permutations of the range [__first,__last) as a set of
2978 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2979 * sequence into the next one of this set. Returns true if there are more
2980 * sequences to generate. If the sequence is the largest of the set, the
2981 * smallest is generated and false returned.
2982 */
2983 template<typename _BidirectionalIterator, typename _Compare>
2984 inline bool
2985 next_permutation(_BidirectionalIterator __first,
2986 _BidirectionalIterator __last, _Compare __comp)
2987 {
2988 // concept requirements
2989 __glibcxx_function_requires(_BidirectionalIteratorConcept<
2990 _BidirectionalIterator>)
2991 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2992 typename iterator_traits<_BidirectionalIterator>::value_type,
2993 typename iterator_traits<_BidirectionalIterator>::value_type>)
2994 __glibcxx_requires_valid_range(__first, __last);
2995
2996 return std::__next_permutation
2997 (__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
2998 }
2999
3000 template<typename _BidirectionalIterator, typename _Compare>
3001 bool
3002 __prev_permutation(_BidirectionalIterator __first,
3003 _BidirectionalIterator __last, _Compare __comp)
3004 {
3005 if (__first == __last)
3006 return false;
3007 _BidirectionalIterator __i = __first;
3008 ++__i;
3009 if (__i == __last)
3010 return false;
3011 __i = __last;
3012 --__i;
3013
3014 for(;;)
3015 {
3016 _BidirectionalIterator __ii = __i;
3017 --__i;
3018 if (__comp(__ii, __i))
3019 {
3020 _BidirectionalIterator __j = __last;
3021 while (!__comp(--__j, __i))
3022 {}
3023 std::iter_swap(__i, __j);
3024 std::__reverse(__ii, __last,
3025 std::__iterator_category(__first));
3026 return true;
3027 }
3028 if (__i == __first)
3029 {
3030 std::__reverse(__first, __last,
3031 std::__iterator_category(__first));
3032 return false;
3033 }
3034 }
3035 }
3036
3037 /**
3038 * @brief Permute range into the previous @e dictionary ordering.
3039 * @ingroup sorting_algorithms
3040 * @param __first Start of range.
3041 * @param __last End of range.
3042 * @return False if wrapped to last permutation, true otherwise.
3043 *
3044 * Treats all permutations of the range as a set of @e dictionary sorted
3045 * sequences. Permutes the current sequence into the previous one of this
3046 * set. Returns true if there are more sequences to generate. If the
3047 * sequence is the smallest of the set, the largest is generated and false
3048 * returned.
3049 */
3050 template<typename _BidirectionalIterator>
3051 inline bool
3052 prev_permutation(_BidirectionalIterator __first,
3053 _BidirectionalIterator __last)
3054 {
3055 // concept requirements
3056 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3057 _BidirectionalIterator>)
3058 __glibcxx_function_requires(_LessThanComparableConcept<
3059 typename iterator_traits<_BidirectionalIterator>::value_type>)
3060 __glibcxx_requires_valid_range(__first, __last);
3061
3062 return std::__prev_permutation(__first, __last,
3063 __gnu_cxx::__ops::__iter_less_iter());
3064 }
3065
3066 /**
3067 * @brief Permute range into the previous @e dictionary ordering using
3068 * comparison functor.
3069 * @ingroup sorting_algorithms
3070 * @param __first Start of range.
3071 * @param __last End of range.
3072 * @param __comp A comparison functor.
3073 * @return False if wrapped to last permutation, true otherwise.
3074 *
3075 * Treats all permutations of the range [__first,__last) as a set of
3076 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3077 * sequence into the previous one of this set. Returns true if there are
3078 * more sequences to generate. If the sequence is the smallest of the set,
3079 * the largest is generated and false returned.
3080 */
3081 template<typename _BidirectionalIterator, typename _Compare>
3082 inline bool
3083 prev_permutation(_BidirectionalIterator __first,
3084 _BidirectionalIterator __last, _Compare __comp)
3085 {
3086 // concept requirements
3087 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3088 _BidirectionalIterator>)
3089 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3090 typename iterator_traits<_BidirectionalIterator>::value_type,
3091 typename iterator_traits<_BidirectionalIterator>::value_type>)
3092 __glibcxx_requires_valid_range(__first, __last);
3093
3094 return std::__prev_permutation(__first, __last,
3095 __gnu_cxx::__ops::__iter_comp_iter(__comp));
3096 }
3097
3098 // replace
3099 // replace_if
3100
3101 template<typename _InputIterator, typename _OutputIterator,
3102 typename _Predicate, typename _Tp>
3103 _OutputIterator
3104 __replace_copy_if(_InputIterator __first, _InputIterator __last,
3105 _OutputIterator __result,
3106 _Predicate __pred, const _Tp& __new_value)
3107 {
3108 for (; __first != __last; ++__first, ++__result)
3109 if (__pred(__first))
3110 *__result = __new_value;
3111 else
3112 *__result = *__first;
3113 return __result;
3114 }
3115
3116 /**
3117 * @brief Copy a sequence, replacing each element of one value with another
3118 * value.
3119 * @param __first An input iterator.
3120 * @param __last An input iterator.
3121 * @param __result An output iterator.
3122 * @param __old_value The value to be replaced.
3123 * @param __new_value The replacement value.
3124 * @return The end of the output sequence, @p result+(last-first).
3125 *
3126 * Copies each element in the input range @p [__first,__last) to the
3127 * output range @p [__result,__result+(__last-__first)) replacing elements
3128 * equal to @p __old_value with @p __new_value.
3129 */
3130 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
3131 inline _OutputIterator
3132 replace_copy(_InputIterator __first, _InputIterator __last,
3133 _OutputIterator __result,
3134 const _Tp& __old_value, const _Tp& __new_value)
3135 {
3136 // concept requirements
3137 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3138 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3139 typename iterator_traits<_InputIterator>::value_type>)
3140 __glibcxx_function_requires(_EqualOpConcept<
3141 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3142 __glibcxx_requires_valid_range(__first, __last);
3143
3144 return std::__replace_copy_if(__first, __last, __result,
3145 __gnu_cxx::__ops::__iter_equals_val(__old_value),
3146 __new_value);
3147 }
3148
3149 /**
3150 * @brief Copy a sequence, replacing each value for which a predicate
3151 * returns true with another value.
3152 * @ingroup mutating_algorithms
3153 * @param __first An input iterator.
3154 * @param __last An input iterator.
3155 * @param __result An output iterator.
3156 * @param __pred A predicate.
3157 * @param __new_value The replacement value.
3158 * @return The end of the output sequence, @p __result+(__last-__first).
3159 *
3160 * Copies each element in the range @p [__first,__last) to the range
3161 * @p [__result,__result+(__last-__first)) replacing elements for which
3162 * @p __pred returns true with @p __new_value.
3163 */
3164 template<typename _InputIterator, typename _OutputIterator,
3165 typename _Predicate, typename _Tp>
3166 inline _OutputIterator
3167 replace_copy_if(_InputIterator __first, _InputIterator __last,
3168 _OutputIterator __result,
3169 _Predicate __pred, const _Tp& __new_value)
3170 {
3171 // concept requirements
3172 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3173 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3174 typename iterator_traits<_InputIterator>::value_type>)
3175 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3176 typename iterator_traits<_InputIterator>::value_type>)
3177 __glibcxx_requires_valid_range(__first, __last);
3178
3179 return std::__replace_copy_if(__first, __last, __result,
3180 __gnu_cxx::__ops::__pred_iter(__pred),
3181 __new_value);
3182 }
3183
3184 template<typename _InputIterator, typename _Predicate>
3185 typename iterator_traits<_InputIterator>::difference_type
3186 __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3187 {
3188 typename iterator_traits<_InputIterator>::difference_type __n = 0;
3189 for (; __first != __last; ++__first)
3190 if (__pred(__first))
3191 ++__n;
3192 return __n;
3193 }
3194
3195 #if __cplusplus >= 201103L
3196 /**
3197 * @brief Determines whether the elements of a sequence are sorted.
3198 * @ingroup sorting_algorithms
3199 * @param __first An iterator.
3200 * @param __last Another iterator.
3201 * @return True if the elements are sorted, false otherwise.
3202 */
3203 template<typename _ForwardIterator>
3204 inline bool
3205 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3206 { return std::is_sorted_until(__first, __last) == __last; }
3207
3208 /**
3209 * @brief Determines whether the elements of a sequence are sorted
3210 * according to a comparison functor.
3211 * @ingroup sorting_algorithms
3212 * @param __first An iterator.
3213 * @param __last Another iterator.
3214 * @param __comp A comparison functor.
3215 * @return True if the elements are sorted, false otherwise.
3216 */
3217 template<typename _ForwardIterator, typename _Compare>
3218 inline bool
3219 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
3220 _Compare __comp)
3221 { return std::is_sorted_until(__first, __last, __comp) == __last; }
3222
3223 template<typename _ForwardIterator, typename _Compare>
3224 _ForwardIterator
3225 __is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3226 _Compare __comp)
3227 {
3228 if (__first == __last)
3229 return __last;
3230
3231 _ForwardIterator __next = __first;
3232 for (++__next; __next != __last; __first = __next, ++__next)
3233 if (__comp(__next, __first))
3234 return __next;
3235 return __next;
3236 }
3237
3238 /**
3239 * @brief Determines the end of a sorted sequence.
3240 * @ingroup sorting_algorithms
3241 * @param __first An iterator.
3242 * @param __last Another iterator.
3243 * @return An iterator pointing to the last iterator i in [__first, __last)
3244 * for which the range [__first, i) is sorted.
3245 */
3246 template<typename _ForwardIterator>
3247 inline _ForwardIterator
3248 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3249 {
3250 // concept requirements
3251 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3252 __glibcxx_function_requires(_LessThanComparableConcept<
3253 typename iterator_traits<_ForwardIterator>::value_type>)
3254 __glibcxx_requires_valid_range(__first, __last);
3255
3256 return std::__is_sorted_until(__first, __last,
3257 __gnu_cxx::__ops::__iter_less_iter());
3258 }
3259
3260 /**
3261 * @brief Determines the end of a sorted sequence using comparison functor.
3262 * @ingroup sorting_algorithms
3263 * @param __first An iterator.
3264 * @param __last Another iterator.
3265 * @param __comp A comparison functor.
3266 * @return An iterator pointing to the last iterator i in [__first, __last)
3267 * for which the range [__first, i) is sorted.
3268 */
3269 template<typename _ForwardIterator, typename _Compare>
3270 inline _ForwardIterator
3271 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3272 _Compare __comp)
3273 {
3274 // concept requirements
3275 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3276 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3277 typename iterator_traits<_ForwardIterator>::value_type,
3278 typename iterator_traits<_ForwardIterator>::value_type>)
3279 __glibcxx_requires_valid_range(__first, __last);
3280
3281 return std::__is_sorted_until(__first, __last,
3282 __gnu_cxx::__ops::__iter_comp_iter(__comp));
3283 }
3284
3285 /**
3286 * @brief Determines min and max at once as an ordered pair.
3287 * @ingroup sorting_algorithms
3288 * @param __a A thing of arbitrary type.
3289 * @param __b Another thing of arbitrary type.
3290 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3291 * __b) otherwise.
3292 */
3293 template<typename _Tp>
3294 inline pair<const _Tp&, const _Tp&>
3295 minmax(const _Tp& __a, const _Tp& __b)
3296 {
3297 // concept requirements
3298 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
3299
3300 return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
3301 : pair<const _Tp&, const _Tp&>(__a, __b);
3302 }
3303
3304 /**
3305 * @brief Determines min and max at once as an ordered pair.
3306 * @ingroup sorting_algorithms
3307 * @param __a A thing of arbitrary type.
3308 * @param __b Another thing of arbitrary type.
3309 * @param __comp A @link comparison_functors comparison functor @endlink.
3310 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3311 * __b) otherwise.
3312 */
3313 template<typename _Tp, typename _Compare>
3314 inline pair<const _Tp&, const _Tp&>
3315 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
3316 {
3317 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
3318 : pair<const _Tp&, const _Tp&>(__a, __b);
3319 }
3320
3321 template<typename _ForwardIterator, typename _Compare>
3322 pair<_ForwardIterator, _ForwardIterator>
3323 __minmax_element(_ForwardIterator __first, _ForwardIterator __last,
3324 _Compare __comp)
3325 {
3326 _ForwardIterator __next = __first;
3327 if (__first == __last
3328 || ++__next == __last)
3329 return std::make_pair(__first, __first);
3330
3331 _ForwardIterator __min, __max;
3332 if (__comp(__next, __first))
3333 {
3334 __min = __next;
3335 __max = __first;
3336 }
3337 else
3338 {
3339 __min = __first;
3340 __max = __next;
3341 }
3342
3343 __first = __next;
3344 ++__first;
3345
3346 while (__first != __last)
3347 {
3348 __next = __first;
3349 if (++__next == __last)
3350 {
3351 if (__comp(__first, __min))
3352 __min = __first;
3353 else if (!__comp(__first, __max))
3354 __max = __first;
3355 break;
3356 }
3357
3358 if (__comp(__next, __first))
3359 {
3360 if (__comp(__next, __min))
3361 __min = __next;
3362 if (!__comp(__first, __max))
3363 __max = __first;
3364 }
3365 else
3366 {
3367 if (__comp(__first, __min))
3368 __min = __first;
3369 if (!__comp(__next, __max))
3370 __max = __next;
3371 }
3372
3373 __first = __next;
3374 ++__first;
3375 }
3376
3377 return std::make_pair(__min, __max);
3378 }
3379
3380 /**
3381 * @brief Return a pair of iterators pointing to the minimum and maximum
3382 * elements in a range.
3383 * @ingroup sorting_algorithms
3384 * @param __first Start of range.
3385 * @param __last End of range.
3386 * @return make_pair(m, M), where m is the first iterator i in
3387 * [__first, __last) such that no other element in the range is
3388 * smaller, and where M is the last iterator i in [__first, __last)
3389 * such that no other element in the range is larger.
3390 */
3391 template<typename _ForwardIterator>
3392 inline pair<_ForwardIterator, _ForwardIterator>
3393 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
3394 {
3395 // concept requirements
3396 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3397 __glibcxx_function_requires(_LessThanComparableConcept<
3398 typename iterator_traits<_ForwardIterator>::value_type>)
3399 __glibcxx_requires_valid_range(__first, __last);
3400
3401 return std::__minmax_element(__first, __last,
3402 __gnu_cxx::__ops::__iter_less_iter());
3403 }
3404
3405 /**
3406 * @brief Return a pair of iterators pointing to the minimum and maximum
3407 * elements in a range.
3408 * @ingroup sorting_algorithms
3409 * @param __first Start of range.
3410 * @param __last End of range.
3411 * @param __comp Comparison functor.
3412 * @return make_pair(m, M), where m is the first iterator i in
3413 * [__first, __last) such that no other element in the range is
3414 * smaller, and where M is the last iterator i in [__first, __last)
3415 * such that no other element in the range is larger.
3416 */
3417 template<typename _ForwardIterator, typename _Compare>
3418 inline pair<_ForwardIterator, _ForwardIterator>
3419 minmax_element(_ForwardIterator __first, _ForwardIterator __last,
3420 _Compare __comp)
3421 {
3422 // concept requirements
3423 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3424 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3425 typename iterator_traits<_ForwardIterator>::value_type,
3426 typename iterator_traits<_ForwardIterator>::value_type>)
3427 __glibcxx_requires_valid_range(__first, __last);
3428
3429 return std::__minmax_element(__first, __last,
3430 __gnu_cxx::__ops::__iter_comp_iter(__comp));
3431 }
3432
3433 // N2722 + DR 915.
3434 template<typename _Tp>
3435 inline _Tp
3436 min(initializer_list<_Tp> __l)
3437 { return *std::min_element(__l.begin(), __l.end()); }
3438
3439 template<typename _Tp, typename _Compare>
3440 inline _Tp
3441 min(initializer_list<_Tp> __l, _Compare __comp)
3442 { return *std::min_element(__l.begin(), __l.end(), __comp); }
3443
3444 template<typename _Tp>
3445 inline _Tp
3446 max(initializer_list<_Tp> __l)
3447 { return *std::max_element(__l.begin(), __l.end()); }
3448
3449 template<typename _Tp, typename _Compare>
3450 inline _Tp
3451 max(initializer_list<_Tp> __l, _Compare __comp)
3452 { return *std::max_element(__l.begin(), __l.end(), __comp); }
3453
3454 template<typename _Tp>
3455 inline pair<_Tp, _Tp>
3456 minmax(initializer_list<_Tp> __l)
3457 {
3458 pair<const _Tp*, const _Tp*> __p =
3459 std::minmax_element(__l.begin(), __l.end());
3460 return std::make_pair(*__p.first, *__p.second);
3461 }
3462
3463 template<typename _Tp, typename _Compare>
3464 inline pair<_Tp, _Tp>
3465 minmax(initializer_list<_Tp> __l, _Compare __comp)
3466 {
3467 pair<const _Tp*, const _Tp*> __p =
3468 std::minmax_element(__l.begin(), __l.end(), __comp);
3469 return std::make_pair(*__p.first, *__p.second);
3470 }
3471
3472 template<typename _ForwardIterator1, typename _ForwardIterator2,
3473 typename _BinaryPredicate>
3474 bool
3475 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3476 _ForwardIterator2 __first2, _BinaryPredicate __pred)
3477 {
3478 // Efficiently compare identical prefixes: O(N) if sequences
3479 // have the same elements in the same order.
3480 for (; __first1 != __last1; ++__first1, ++__first2)
3481 if (!__pred(__first1, __first2))
3482 break;
3483
3484 if (__first1 == __last1)
3485 return true;
3486
3487 // Establish __last2 assuming equal ranges by iterating over the
3488 // rest of the list.
3489 _ForwardIterator2 __last2 = __first2;
3490 std::advance(__last2, std::distance(__first1, __last1));
3491 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
3492 {
3493 if (__scan != std::__find_if(__first1, __scan,
3494 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
3495 continue; // We've seen this one before.
3496
3497 auto __matches
3498 = std::__count_if(__first2, __last2,
3499 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
3500 if (0 == __matches ||
3501 std::__count_if(__scan, __last1,
3502 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
3503 != __matches)
3504 return false;
3505 }
3506 return true;
3507 }
3508
3509 /**
3510 * @brief Checks whether a permutation of the second sequence is equal
3511 * to the first sequence.
3512 * @ingroup non_mutating_algorithms
3513 * @param __first1 Start of first range.
3514 * @param __last1 End of first range.
3515 * @param __first2 Start of second range.
3516 * @return true if there exists a permutation of the elements in the range
3517 * [__first2, __first2 + (__last1 - __first1)), beginning with
3518 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
3519 * returns true; otherwise, returns false.
3520 */
3521 template<typename _ForwardIterator1, typename _ForwardIterator2>
3522 inline bool
3523 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3524 _ForwardIterator2 __first2)
3525 {
3526 // concept requirements
3527 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
3528 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
3529 __glibcxx_function_requires(_EqualOpConcept<
3530 typename iterator_traits<_ForwardIterator1>::value_type,
3531 typename iterator_traits<_ForwardIterator2>::value_type>)
3532 __glibcxx_requires_valid_range(__first1, __last1);
3533
3534 return std::__is_permutation(__first1, __last1, __first2,
3535 __gnu_cxx::__ops::__iter_equal_to_iter());
3536 }
3537
3538 /**
3539 * @brief Checks whether a permutation of the second sequence is equal
3540 * to the first sequence.
3541 * @ingroup non_mutating_algorithms
3542 * @param __first1 Start of first range.
3543 * @param __last1 End of first range.
3544 * @param __first2 Start of second range.
3545 * @param __pred A binary predicate.
3546 * @return true if there exists a permutation of the elements in
3547 * the range [__first2, __first2 + (__last1 - __first1)),
3548 * beginning with ForwardIterator2 begin, such that
3549 * equal(__first1, __last1, __begin, __pred) returns true;
3550 * otherwise, returns false.
3551 */
3552 template<typename _ForwardIterator1, typename _ForwardIterator2,
3553 typename _BinaryPredicate>
3554 inline bool
3555 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3556 _ForwardIterator2 __first2, _BinaryPredicate __pred)
3557 {
3558 // concept requirements
3559 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
3560 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
3561 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3562 typename iterator_traits<_ForwardIterator1>::value_type,
3563 typename iterator_traits<_ForwardIterator2>::value_type>)
3564 __glibcxx_requires_valid_range(__first1, __last1);
3565
3566 return std::__is_permutation(__first1, __last1, __first2,
3567 __gnu_cxx::__ops::__iter_comp_iter(__pred));
3568 }
3569
3570 #if __cplusplus > 201103L
3571 template<typename _ForwardIterator1, typename _ForwardIterator2,
3572 typename _BinaryPredicate>
3573 bool
3574 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3575 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
3576 _BinaryPredicate __pred)
3577 {
3578 using _Cat1
3579 = typename iterator_traits<_ForwardIterator1>::iterator_category;
3580 using _Cat2
3581 = typename iterator_traits<_ForwardIterator2>::iterator_category;
3582 using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>;
3583 using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>;
3584 constexpr bool __ra_iters = _It1_is_RA() && _It2_is_RA();
3585 if (__ra_iters)
3586 {
3587 auto __d1 = std::distance(__first1, __last1);
3588 auto __d2 = std::distance(__first2, __last2);
3589 if (__d1 != __d2)
3590 return false;
3591 }
3592
3593 // Efficiently compare identical prefixes: O(N) if sequences
3594 // have the same elements in the same order.
3595 for (; __first1 != __last1; ++__first1, ++__first2)
3596 if (!__pred(__first1, __first2))
3597 break;
3598
3599 if (__ra_iters)
3600 {
3601 if (__first1 == __last1)
3602 return true;
3603 }
3604 else
3605 {
3606 auto __d1 = std::distance(__first1, __last1);
3607 auto __d2 = std::distance(__first2, __last2);
3608 if (__d1 == 0 && __d2 == 0)
3609 return true;
3610 if (__d1 != __d2)
3611 return false;
3612 }
3613
3614 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
3615 {
3616 if (__scan != std::__find_if(__first1, __scan,
3617 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
3618 continue; // We've seen this one before.
3619
3620 auto __matches = std::__count_if(__first2, __last2,
3621 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
3622 if (0 == __matches
3623 || std::__count_if(__scan, __last1,
3624 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
3625 != __matches)
3626 return false;
3627 }
3628 return true;
3629 }
3630
3631 /**
3632 * @brief Checks whether a permutaion of the second sequence is equal
3633 * to the first sequence.
3634 * @ingroup non_mutating_algorithms
3635 * @param __first1 Start of first range.
3636 * @param __last1 End of first range.
3637 * @param __first2 Start of second range.
3638 * @param __last2 End of first range.
3639 * @return true if there exists a permutation of the elements in the range
3640 * [__first2, __last2), beginning with ForwardIterator2 begin,
3641 * such that equal(__first1, __last1, begin) returns true;
3642 * otherwise, returns false.
3643 */
3644 template<typename _ForwardIterator1, typename _ForwardIterator2>
3645 inline bool
3646 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3647 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
3648 {
3649 __glibcxx_requires_valid_range(__first1, __last1);
3650 __glibcxx_requires_valid_range(__first2, __last2);
3651
3652 return
3653 std::__is_permutation(__first1, __last1, __first2, __last2,
3654 __gnu_cxx::__ops::__iter_equal_to_iter());
3655 }
3656
3657 /**
3658 * @brief Checks whether a permutation of the second sequence is equal
3659 * to the first sequence.
3660 * @ingroup non_mutating_algorithms
3661 * @param __first1 Start of first range.
3662 * @param __last1 End of first range.
3663 * @param __first2 Start of second range.
3664 * @param __last2 End of first range.
3665 * @param __pred A binary predicate.
3666 * @return true if there exists a permutation of the elements in the range
3667 * [__first2, __last2), beginning with ForwardIterator2 begin,
3668 * such that equal(__first1, __last1, __begin, __pred) returns true;
3669 * otherwise, returns false.
3670 */
3671 template<typename _ForwardIterator1, typename _ForwardIterator2,
3672 typename _BinaryPredicate>
3673 inline bool
3674 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3675 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
3676 _BinaryPredicate __pred)
3677 {
3678 __glibcxx_requires_valid_range(__first1, __last1);
3679 __glibcxx_requires_valid_range(__first2, __last2);
3680
3681 return std::__is_permutation(__first1, __last1, __first2, __last2,
3682 __gnu_cxx::__ops::__iter_comp_iter(__pred));
3683 }
3684 #endif
3685
3686 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3687 /**
3688 * @brief Shuffle the elements of a sequence using a uniform random
3689 * number generator.
3690 * @ingroup mutating_algorithms
3691 * @param __first A forward iterator.
3692 * @param __last A forward iterator.
3693 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3694 * @return Nothing.
3695 *
3696 * Reorders the elements in the range @p [__first,__last) using @p __g to
3697 * provide random numbers.
3698 */
3699 template<typename _RandomAccessIterator,
3700 typename _UniformRandomNumberGenerator>
3701 void
3702 shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3703 _UniformRandomNumberGenerator&& __g)
3704 {
3705 // concept requirements
3706 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3707 _RandomAccessIterator>)
3708 __glibcxx_requires_valid_range(__first, __last);
3709
3710 if (__first == __last)
3711 return;
3712
3713 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3714 _DistanceType;
3715
3716 typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
3717 typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
3718 typedef typename __distr_type::param_type __p_type;
3719 __distr_type __d;
3720
3721 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
3722 std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
3723 }
3724 #endif
3725
3726 #endif // C++11
3727
3728 _GLIBCXX_END_NAMESPACE_VERSION
3729
3730 _GLIBCXX_BEGIN_NAMESPACE_ALGO
3731
3732 /**
3733 * @brief Apply a function to every element of a sequence.
3734 * @ingroup non_mutating_algorithms
3735 * @param __first An input iterator.
3736 * @param __last An input iterator.
3737 * @param __f A unary function object.
3738 * @return @p __f (std::move(@p __f) in C++0x).
3739 *
3740 * Applies the function object @p __f to each element in the range
3741 * @p [first,last). @p __f must not modify the order of the sequence.
3742 * If @p __f has a return value it is ignored.
3743 */
3744 template<typename _InputIterator, typename _Function>
3745 _Function
3746 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
3747 {
3748 // concept requirements
3749 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3750 __glibcxx_requires_valid_range(__first, __last);
3751 for (; __first != __last; ++__first)
3752 __f(*__first);
3753 return _GLIBCXX_MOVE(__f);
3754 }
3755
3756 /**
3757 * @brief Find the first occurrence of a value in a sequence.
3758 * @ingroup non_mutating_algorithms
3759 * @param __first An input iterator.
3760 * @param __last An input iterator.
3761 * @param __val The value to find.
3762 * @return The first iterator @c i in the range @p [__first,__last)
3763 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3764 */
3765 template<typename _InputIterator, typename _Tp>
3766 inline _InputIterator
3767 find(_InputIterator __first, _InputIterator __last,
3768 const _Tp& __val)
3769 {
3770 // concept requirements
3771 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3772 __glibcxx_function_requires(_EqualOpConcept<
3773 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3774 __glibcxx_requires_valid_range(__first, __last);
3775 return std::__find_if(__first, __last,
3776 __gnu_cxx::__ops::__iter_equals_val(__val));
3777 }
3778
3779 /**
3780 * @brief Find the first element in a sequence for which a
3781 * predicate is true.
3782 * @ingroup non_mutating_algorithms
3783 * @param __first An input iterator.
3784 * @param __last An input iterator.
3785 * @param __pred A predicate.
3786 * @return The first iterator @c i in the range @p [__first,__last)
3787 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3788 */
3789 template<typename _InputIterator, typename _Predicate>
3790 inline _InputIterator
3791 find_if(_InputIterator __first, _InputIterator __last,
3792 _Predicate __pred)
3793 {
3794 // concept requirements
3795 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3796 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3797 typename iterator_traits<_InputIterator>::value_type>)
3798 __glibcxx_requires_valid_range(__first, __last);
3799
3800 return std::__find_if(__first, __last,
3801 __gnu_cxx::__ops::__pred_iter(__pred));
3802 }
3803
3804 /**
3805 * @brief Find element from a set in a sequence.
3806 * @ingroup non_mutating_algorithms
3807 * @param __first1 Start of range to search.
3808 * @param __last1 End of range to search.
3809 * @param __first2 Start of match candidates.
3810 * @param __last2 End of match candidates.
3811 * @return The first iterator @c i in the range
3812 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3813 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3814 *
3815 * Searches the range @p [__first1,__last1) for an element that is
3816 * equal to some element in the range [__first2,__last2). If
3817 * found, returns an iterator in the range [__first1,__last1),
3818 * otherwise returns @p __last1.
3819 */
3820 template<typename _InputIterator, typename _ForwardIterator>
3821 _InputIterator
3822 find_first_of(_InputIterator __first1, _InputIterator __last1,
3823 _ForwardIterator __first2, _ForwardIterator __last2)
3824 {
3825 // concept requirements
3826 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3827 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3828 __glibcxx_function_requires(_EqualOpConcept<
3829 typename iterator_traits<_InputIterator>::value_type,
3830 typename iterator_traits<_ForwardIterator>::value_type>)
3831 __glibcxx_requires_valid_range(__first1, __last1);
3832 __glibcxx_requires_valid_range(__first2, __last2);
3833
3834 for (; __first1 != __last1; ++__first1)
3835 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
3836 if (*__first1 == *__iter)
3837 return __first1;
3838 return __last1;
3839 }
3840
3841 /**
3842 * @brief Find element from a set in a sequence using a predicate.
3843 * @ingroup non_mutating_algorithms
3844 * @param __first1 Start of range to search.
3845 * @param __last1 End of range to search.
3846 * @param __first2 Start of match candidates.
3847 * @param __last2 End of match candidates.
3848 * @param __comp Predicate to use.
3849 * @return The first iterator @c i in the range
3850 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3851 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3852 * such iterator exists.
3853 *
3854
3855 * Searches the range @p [__first1,__last1) for an element that is
3856 * equal to some element in the range [__first2,__last2). If
3857 * found, returns an iterator in the range [__first1,__last1),
3858 * otherwise returns @p __last1.
3859 */
3860 template<typename _InputIterator, typename _ForwardIterator,
3861 typename _BinaryPredicate>
3862 _InputIterator
3863 find_first_of(_InputIterator __first1, _InputIterator __last1,
3864 _ForwardIterator __first2, _ForwardIterator __last2,
3865 _BinaryPredicate __comp)
3866 {
3867 // concept requirements
3868 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3869 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3870 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3871 typename iterator_traits<_InputIterator>::value_type,
3872 typename iterator_traits<_ForwardIterator>::value_type>)
3873 __glibcxx_requires_valid_range(__first1, __last1);
3874 __glibcxx_requires_valid_range(__first2, __last2);
3875
3876 for (; __first1 != __last1; ++__first1)
3877 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
3878 if (__comp(*__first1, *__iter))
3879 return __first1;
3880 return __last1;
3881 }
3882
3883 /**
3884 * @brief Find two adjacent values in a sequence that are equal.
3885 * @ingroup non_mutating_algorithms
3886 * @param __first A forward iterator.
3887 * @param __last A forward iterator.
3888 * @return The first iterator @c i such that @c i and @c i+1 are both
3889 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
3890 * or @p __last if no such iterator exists.
3891 */
3892 template<typename _ForwardIterator>
3893 inline _ForwardIterator
3894 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
3895 {
3896 // concept requirements
3897 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3898 __glibcxx_function_requires(_EqualityComparableConcept<
3899 typename iterator_traits<_ForwardIterator>::value_type>)
3900 __glibcxx_requires_valid_range(__first, __last);
3901
3902 return std::__adjacent_find(__first, __last,
3903 __gnu_cxx::__ops::__iter_equal_to_iter());
3904 }
3905
3906 /**
3907 * @brief Find two adjacent values in a sequence using a predicate.
3908 * @ingroup non_mutating_algorithms
3909 * @param __first A forward iterator.
3910 * @param __last A forward iterator.
3911 * @param __binary_pred A binary predicate.
3912 * @return The first iterator @c i such that @c i and @c i+1 are both
3913 * valid iterators in @p [__first,__last) and such that
3914 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
3915 * exists.
3916 */
3917 template<typename _ForwardIterator, typename _BinaryPredicate>
3918 inline _ForwardIterator
3919 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
3920 _BinaryPredicate __binary_pred)
3921 {
3922 // concept requirements
3923 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3924 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3925 typename iterator_traits<_ForwardIterator>::value_type,
3926 typename iterator_traits<_ForwardIterator>::value_type>)
3927 __glibcxx_requires_valid_range(__first, __last);
3928
3929 return std::__adjacent_find(__first, __last,
3930 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
3931 }
3932
3933 /**
3934 * @brief Count the number of copies of a value in a sequence.
3935 * @ingroup non_mutating_algorithms
3936 * @param __first An input iterator.
3937 * @param __last An input iterator.
3938 * @param __value The value to be counted.
3939 * @return The number of iterators @c i in the range @p [__first,__last)
3940 * for which @c *i == @p __value
3941 */
3942 template<typename _InputIterator, typename _Tp>
3943 inline typename iterator_traits<_InputIterator>::difference_type
3944 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
3945 {
3946 // concept requirements
3947 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3948 __glibcxx_function_requires(_EqualOpConcept<
3949 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3950 __glibcxx_requires_valid_range(__first, __last);
3951
3952 return std::__count_if(__first, __last,
3953 __gnu_cxx::__ops::__iter_equals_val(__value));
3954 }
3955
3956 /**
3957 * @brief Count the elements of a sequence for which a predicate is true.
3958 * @ingroup non_mutating_algorithms
3959 * @param __first An input iterator.
3960 * @param __last An input iterator.
3961 * @param __pred A predicate.
3962 * @return The number of iterators @c i in the range @p [__first,__last)
3963 * for which @p __pred(*i) is true.
3964 */
3965 template<typename _InputIterator, typename _Predicate>
3966 inline typename iterator_traits<_InputIterator>::difference_type
3967 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3968 {
3969 // concept requirements
3970 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3971 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3972 typename iterator_traits<_InputIterator>::value_type>)
3973 __glibcxx_requires_valid_range(__first, __last);
3974
3975 return std::__count_if(__first, __last,
3976 __gnu_cxx::__ops::__pred_iter(__pred));
3977 }
3978
3979 /**
3980 * @brief Search a sequence for a matching sub-sequence.
3981 * @ingroup non_mutating_algorithms
3982 * @param __first1 A forward iterator.
3983 * @param __last1 A forward iterator.
3984 * @param __first2 A forward iterator.
3985 * @param __last2 A forward iterator.
3986 * @return The first iterator @c i in the range @p
3987 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
3988 * *(__first2+N) for each @c N in the range @p
3989 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
3990 *
3991 * Searches the range @p [__first1,__last1) for a sub-sequence that
3992 * compares equal value-by-value with the sequence given by @p
3993 * [__first2,__last2) and returns an iterator to the first element
3994 * of the sub-sequence, or @p __last1 if the sub-sequence is not
3995 * found.
3996 *
3997 * Because the sub-sequence must lie completely within the range @p
3998 * [__first1,__last1) it must start at a position less than @p
3999 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4000 * length of the sub-sequence.
4001 *
4002 * This means that the returned iterator @c i will be in the range
4003 * @p [__first1,__last1-(__last2-__first2))
4004 */
4005 template<typename _ForwardIterator1, typename _ForwardIterator2>
4006 inline _ForwardIterator1
4007 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4008 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4009 {
4010 // concept requirements
4011 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4012 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4013 __glibcxx_function_requires(_EqualOpConcept<
4014 typename iterator_traits<_ForwardIterator1>::value_type,
4015 typename iterator_traits<_ForwardIterator2>::value_type>)
4016 __glibcxx_requires_valid_range(__first1, __last1);
4017 __glibcxx_requires_valid_range(__first2, __last2);
4018
4019 return std::__search(__first1, __last1, __first2, __last2,
4020 __gnu_cxx::__ops::__iter_equal_to_iter());
4021 }
4022
4023 /**
4024 * @brief Search a sequence for a matching sub-sequence using a predicate.
4025 * @ingroup non_mutating_algorithms
4026 * @param __first1 A forward iterator.
4027 * @param __last1 A forward iterator.
4028 * @param __first2 A forward iterator.
4029 * @param __last2 A forward iterator.
4030 * @param __predicate A binary predicate.
4031 * @return The first iterator @c i in the range
4032 * @p [__first1,__last1-(__last2-__first2)) such that
4033 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range
4034 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists.
4035 *
4036 * Searches the range @p [__first1,__last1) for a sub-sequence that
4037 * compares equal value-by-value with the sequence given by @p
4038 * [__first2,__last2), using @p __predicate to determine equality,
4039 * and returns an iterator to the first element of the
4040 * sub-sequence, or @p __last1 if no such iterator exists.
4041 *
4042 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4043 */
4044 template<typename _ForwardIterator1, typename _ForwardIterator2,
4045 typename _BinaryPredicate>
4046 inline _ForwardIterator1
4047 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4048 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4049 _BinaryPredicate __predicate)
4050 {
4051 // concept requirements
4052 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4053 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4054 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4055 typename iterator_traits<_ForwardIterator1>::value_type,
4056 typename iterator_traits<_ForwardIterator2>::value_type>)
4057 __glibcxx_requires_valid_range(__first1, __last1);
4058 __glibcxx_requires_valid_range(__first2, __last2);
4059
4060 return std::__search(__first1, __last1, __first2, __last2,
4061 __gnu_cxx::__ops::__iter_comp_iter(__predicate));
4062 }
4063
4064 /**
4065 * @brief Search a sequence for a number of consecutive values.
4066 * @ingroup non_mutating_algorithms
4067 * @param __first A forward iterator.
4068 * @param __last A forward iterator.
4069 * @param __count The number of consecutive values.
4070 * @param __val The value to find.
4071 * @return The first iterator @c i in the range @p
4072 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4073 * each @c N in the range @p [0,__count), or @p __last if no such
4074 * iterator exists.
4075 *
4076 * Searches the range @p [__first,__last) for @p count consecutive elements
4077 * equal to @p __val.
4078 */
4079 template<typename _ForwardIterator, typename _Integer, typename _Tp>
4080 inline _ForwardIterator
4081 search_n(_ForwardIterator __first, _ForwardIterator __last,
4082 _Integer __count, const _Tp& __val)
4083 {
4084 // concept requirements
4085 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4086 __glibcxx_function_requires(_EqualOpConcept<
4087 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4088 __glibcxx_requires_valid_range(__first, __last);
4089
4090 return std::__search_n(__first, __last, __count,
4091 __gnu_cxx::__ops::__iter_equals_val(__val));
4092 }
4093
4094
4095 /**
4096 * @brief Search a sequence for a number of consecutive values using a
4097 * predicate.
4098 * @ingroup non_mutating_algorithms
4099 * @param __first A forward iterator.
4100 * @param __last A forward iterator.
4101 * @param __count The number of consecutive values.
4102 * @param __val The value to find.
4103 * @param __binary_pred A binary predicate.
4104 * @return The first iterator @c i in the range @p
4105 * [__first,__last-__count) such that @p
4106 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4107 * @p [0,__count), or @p __last if no such iterator exists.
4108 *
4109 * Searches the range @p [__first,__last) for @p __count
4110 * consecutive elements for which the predicate returns true.
4111 */
4112 template<typename _ForwardIterator, typename _Integer, typename _Tp,
4113 typename _BinaryPredicate>
4114 inline _ForwardIterator
4115 search_n(_ForwardIterator __first, _ForwardIterator __last,
4116 _Integer __count, const _Tp& __val,
4117 _BinaryPredicate __binary_pred)
4118 {
4119 // concept requirements
4120 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4121 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4122 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4123 __glibcxx_requires_valid_range(__first, __last);
4124
4125 return std::__search_n(__first, __last, __count,
4126 __gnu_cxx::__ops::__iter_comp_val(__binary_pred, __val));
4127 }
4128
4129
4130 /**
4131 * @brief Perform an operation on a sequence.
4132 * @ingroup mutating_algorithms
4133 * @param __first An input iterator.
4134 * @param __last An input iterator.
4135 * @param __result An output iterator.
4136 * @param __unary_op A unary operator.
4137 * @return An output iterator equal to @p __result+(__last-__first).
4138 *
4139 * Applies the operator to each element in the input range and assigns
4140 * the results to successive elements of the output sequence.
4141 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4142 * range @p [0,__last-__first).
4143 *
4144 * @p unary_op must not alter its argument.
4145 */
4146 template<typename _InputIterator, typename _OutputIterator,
4147 typename _UnaryOperation>
4148 _OutputIterator
4149 transform(_InputIterator __first, _InputIterator __last,
4150 _OutputIterator __result, _UnaryOperation __unary_op)
4151 {
4152 // concept requirements
4153 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4154 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4155 // "the type returned by a _UnaryOperation"
4156 __typeof__(__unary_op(*__first))>)
4157 __glibcxx_requires_valid_range(__first, __last);
4158
4159 for (; __first != __last; ++__first, ++__result)
4160 *__result = __unary_op(*__first);
4161 return __result;
4162 }
4163
4164 /**
4165 * @brief Perform an operation on corresponding elements of two sequences.
4166 * @ingroup mutating_algorithms
4167 * @param __first1 An input iterator.
4168 * @param __last1 An input iterator.
4169 * @param __first2 An input iterator.
4170 * @param __result An output iterator.
4171 * @param __binary_op A binary operator.
4172 * @return An output iterator equal to @p result+(last-first).
4173 *
4174 * Applies the operator to the corresponding elements in the two
4175 * input ranges and assigns the results to successive elements of the
4176 * output sequence.
4177 * Evaluates @p
4178 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4179 * @c N in the range @p [0,__last1-__first1).
4180 *
4181 * @p binary_op must not alter either of its arguments.
4182 */
4183 template<typename _InputIterator1, typename _InputIterator2,
4184 typename _OutputIterator, typename _BinaryOperation>
4185 _OutputIterator
4186 transform(_InputIterator1 __first1, _InputIterator1 __last1,
4187 _InputIterator2 __first2, _OutputIterator __result,
4188 _BinaryOperation __binary_op)
4189 {
4190 // concept requirements
4191 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4192 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4193 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4194 // "the type returned by a _BinaryOperation"
4195 __typeof__(__binary_op(*__first1,*__first2))>)
4196 __glibcxx_requires_valid_range(__first1, __last1);
4197
4198 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
4199 *__result = __binary_op(*__first1, *__first2);
4200 return __result;
4201 }
4202
4203 /**
4204 * @brief Replace each occurrence of one value in a sequence with another
4205 * value.
4206 * @ingroup mutating_algorithms
4207 * @param __first A forward iterator.
4208 * @param __last A forward iterator.
4209 * @param __old_value The value to be replaced.
4210 * @param __new_value The replacement value.
4211 * @return replace() returns no value.
4212 *
4213 * For each iterator @c i in the range @p [__first,__last) if @c *i ==
4214 * @p __old_value then the assignment @c *i = @p __new_value is performed.
4215 */
4216 template<typename _ForwardIterator, typename _Tp>
4217 void
4218 replace(_ForwardIterator __first, _ForwardIterator __last,
4219 const _Tp& __old_value, const _Tp& __new_value)
4220 {
4221 // concept requirements
4222 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4223 _ForwardIterator>)
4224 __glibcxx_function_requires(_EqualOpConcept<
4225 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4226 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4227 typename iterator_traits<_ForwardIterator>::value_type>)
4228 __glibcxx_requires_valid_range(__first, __last);
4229
4230 for (; __first != __last; ++__first)
4231 if (*__first == __old_value)
4232 *__first = __new_value;
4233 }
4234
4235 /**
4236 * @brief Replace each value in a sequence for which a predicate returns
4237 * true with another value.
4238 * @ingroup mutating_algorithms
4239 * @param __first A forward iterator.
4240 * @param __last A forward iterator.
4241 * @param __pred A predicate.
4242 * @param __new_value The replacement value.
4243 * @return replace_if() returns no value.
4244 *
4245 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i)
4246 * is true then the assignment @c *i = @p __new_value is performed.
4247 */
4248 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4249 void
4250 replace_if(_ForwardIterator __first, _ForwardIterator __last,
4251 _Predicate __pred, const _Tp& __new_value)
4252 {
4253 // concept requirements
4254 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4255 _ForwardIterator>)
4256 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4257 typename iterator_traits<_ForwardIterator>::value_type>)
4258 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4259 typename iterator_traits<_ForwardIterator>::value_type>)
4260 __glibcxx_requires_valid_range(__first, __last);
4261
4262 for (; __first != __last; ++__first)
4263 if (__pred(*__first))
4264 *__first = __new_value;
4265 }
4266
4267 /**
4268 * @brief Assign the result of a function object to each value in a
4269 * sequence.
4270 * @ingroup mutating_algorithms
4271 * @param __first A forward iterator.
4272 * @param __last A forward iterator.
4273 * @param __gen A function object taking no arguments and returning
4274 * std::iterator_traits<_ForwardIterator>::value_type
4275 * @return generate() returns no value.
4276 *
4277 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4278 * @p [__first,__last).
4279 */
4280 template<typename _ForwardIterator, typename _Generator>
4281 void
4282 generate(_ForwardIterator __first, _ForwardIterator __last,
4283 _Generator __gen)
4284 {
4285 // concept requirements
4286 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4287 __glibcxx_function_requires(_GeneratorConcept<_Generator,
4288 typename iterator_traits<_ForwardIterator>::value_type>)
4289 __glibcxx_requires_valid_range(__first, __last);
4290
4291 for (; __first != __last; ++__first)
4292 *__first = __gen();
4293 }
4294
4295 /**
4296 * @brief Assign the result of a function object to each value in a
4297 * sequence.
4298 * @ingroup mutating_algorithms
4299 * @param __first A forward iterator.
4300 * @param __n The length of the sequence.
4301 * @param __gen A function object taking no arguments and returning
4302 * std::iterator_traits<_ForwardIterator>::value_type
4303 * @return The end of the sequence, @p __first+__n
4304 *
4305 * Performs the assignment @c *i = @p __gen() for each @c i in the range
4306 * @p [__first,__first+__n).
4307 *
4308 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4309 * DR 865. More algorithms that throw away information
4310 */
4311 template<typename _OutputIterator, typename _Size, typename _Generator>
4312 _OutputIterator
4313 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
4314 {
4315 // concept requirements
4316 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4317 // "the type returned by a _Generator"
4318 __typeof__(__gen())>)
4319
4320 for (__decltype(__n + 0) __niter = __n;
4321 __niter > 0; --__niter, ++__first)
4322 *__first = __gen();
4323 return __first;
4324 }
4325
4326 /**
4327 * @brief Copy a sequence, removing consecutive duplicate values.
4328 * @ingroup mutating_algorithms
4329 * @param __first An input iterator.
4330 * @param __last An input iterator.
4331 * @param __result An output iterator.
4332 * @return An iterator designating the end of the resulting sequence.
4333 *
4334 * Copies each element in the range @p [__first,__last) to the range
4335 * beginning at @p __result, except that only the first element is copied
4336 * from groups of consecutive elements that compare equal.
4337 * unique_copy() is stable, so the relative order of elements that are
4338 * copied is unchanged.
4339 *
4340 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4341 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4342 *
4343 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4344 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4345 * Assignable?
4346 */
4347 template<typename _InputIterator, typename _OutputIterator>
4348 inline _OutputIterator
4349 unique_copy(_InputIterator __first, _InputIterator __last,
4350 _OutputIterator __result)
4351 {
4352 // concept requirements
4353 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4354 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4355 typename iterator_traits<_InputIterator>::value_type>)
4356 __glibcxx_function_requires(_EqualityComparableConcept<
4357 typename iterator_traits<_InputIterator>::value_type>)
4358 __glibcxx_requires_valid_range(__first, __last);
4359
4360 if (__first == __last)
4361 return __result;
4362 return std::__unique_copy(__first, __last, __result,
4363 __gnu_cxx::__ops::__iter_equal_to_iter(),
4364 std::__iterator_category(__first),
4365 std::__iterator_category(__result));
4366 }
4367
4368 /**
4369 * @brief Copy a sequence, removing consecutive values using a predicate.
4370 * @ingroup mutating_algorithms
4371 * @param __first An input iterator.
4372 * @param __last An input iterator.
4373 * @param __result An output iterator.
4374 * @param __binary_pred A binary predicate.
4375 * @return An iterator designating the end of the resulting sequence.
4376 *
4377 * Copies each element in the range @p [__first,__last) to the range
4378 * beginning at @p __result, except that only the first element is copied
4379 * from groups of consecutive elements for which @p __binary_pred returns
4380 * true.
4381 * unique_copy() is stable, so the relative order of elements that are
4382 * copied is unchanged.
4383 *
4384 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4385 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4386 */
4387 template<typename _InputIterator, typename _OutputIterator,
4388 typename _BinaryPredicate>
4389 inline _OutputIterator
4390 unique_copy(_InputIterator __first, _InputIterator __last,
4391 _OutputIterator __result,
4392 _BinaryPredicate __binary_pred)
4393 {
4394 // concept requirements -- predicates checked later
4395 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4396 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4397 typename iterator_traits<_InputIterator>::value_type>)
4398 __glibcxx_requires_valid_range(__first, __last);
4399
4400 if (__first == __last)
4401 return __result;
4402 return std::__unique_copy(__first, __last, __result,
4403 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred),
4404 std::__iterator_category(__first),
4405 std::__iterator_category(__result));
4406 }
4407
4408 /**
4409 * @brief Randomly shuffle the elements of a sequence.
4410 * @ingroup mutating_algorithms
4411 * @param __first A forward iterator.
4412 * @param __last A forward iterator.
4413 * @return Nothing.
4414 *
4415 * Reorder the elements in the range @p [__first,__last) using a random
4416 * distribution, so that every possible ordering of the sequence is
4417 * equally likely.
4418 */
4419 template<typename _RandomAccessIterator>
4420 inline void
4421 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
4422 {
4423 // concept requirements
4424 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4425 _RandomAccessIterator>)
4426 __glibcxx_requires_valid_range(__first, __last);
4427
4428 if (__first != __last)
4429 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4430 std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
4431 }
4432
4433 /**
4434 * @brief Shuffle the elements of a sequence using a random number
4435 * generator.
4436 * @ingroup mutating_algorithms
4437 * @param __first A forward iterator.
4438 * @param __last A forward iterator.
4439 * @param __rand The RNG functor or function.
4440 * @return Nothing.
4441 *
4442 * Reorders the elements in the range @p [__first,__last) using @p __rand to
4443 * provide a random distribution. Calling @p __rand(N) for a positive
4444 * integer @p N should return a randomly chosen integer from the
4445 * range [0,N).
4446 */
4447 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
4448 void
4449 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4450 #if __cplusplus >= 201103L
4451 _RandomNumberGenerator&& __rand)
4452 #else
4453 _RandomNumberGenerator& __rand)
4454 #endif
4455 {
4456 // concept requirements
4457 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4458 _RandomAccessIterator>)
4459 __glibcxx_requires_valid_range(__first, __last);
4460
4461 if (__first == __last)
4462 return;
4463 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4464 std::iter_swap(__i, __first + __rand((__i - __first) + 1));
4465 }
4466
4467
4468 /**
4469 * @brief Move elements for which a predicate is true to the beginning
4470 * of a sequence.
4471 * @ingroup mutating_algorithms
4472 * @param __first A forward iterator.
4473 * @param __last A forward iterator.
4474 * @param __pred A predicate functor.
4475 * @return An iterator @p middle such that @p __pred(i) is true for each
4476 * iterator @p i in the range @p [__first,middle) and false for each @p i
4477 * in the range @p [middle,__last).
4478 *
4479 * @p __pred must not modify its operand. @p partition() does not preserve
4480 * the relative ordering of elements in each group, use
4481 * @p stable_partition() if this is needed.
4482 */
4483 template<typename _ForwardIterator, typename _Predicate>
4484 inline _ForwardIterator
4485 partition(_ForwardIterator __first, _ForwardIterator __last,
4486 _Predicate __pred)
4487 {
4488 // concept requirements
4489 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4490 _ForwardIterator>)
4491 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4492 typename iterator_traits<_ForwardIterator>::value_type>)
4493 __glibcxx_requires_valid_range(__first, __last);
4494
4495 return std::__partition(__first, __last, __pred,
4496 std::__iterator_category(__first));
4497 }
4498
4499
4500 /**
4501 * @brief Sort the smallest elements of a sequence.
4502 * @ingroup sorting_algorithms
4503 * @param __first An iterator.
4504 * @param __middle Another iterator.
4505 * @param __last Another iterator.
4506 * @return Nothing.
4507 *
4508 * Sorts the smallest @p (__middle-__first) elements in the range
4509 * @p [first,last) and moves them to the range @p [__first,__middle). The
4510 * order of the remaining elements in the range @p [__middle,__last) is
4511 * undefined.
4512 * After the sort if @e i and @e j are iterators in the range
4513 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4514 * the range @p [__middle,__last) then *j<*i and *k<*i are both false.
4515 */
4516 template<typename _RandomAccessIterator>
4517 inline void
4518 partial_sort(_RandomAccessIterator __first,
4519 _RandomAccessIterator __middle,
4520 _RandomAccessIterator __last)
4521 {
4522 // concept requirements
4523 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4524 _RandomAccessIterator>)
4525 __glibcxx_function_requires(_LessThanComparableConcept<
4526 typename iterator_traits<_RandomAccessIterator>::value_type>)
4527 __glibcxx_requires_valid_range(__first, __middle);
4528 __glibcxx_requires_valid_range(__middle, __last);
4529
4530 std::__partial_sort(__first, __middle, __last,
4531 __gnu_cxx::__ops::__iter_less_iter());
4532 }
4533
4534 /**
4535 * @brief Sort the smallest elements of a sequence using a predicate
4536 * for comparison.
4537 * @ingroup sorting_algorithms
4538 * @param __first An iterator.
4539 * @param __middle Another iterator.
4540 * @param __last Another iterator.
4541 * @param __comp A comparison functor.
4542 * @return Nothing.
4543 *
4544 * Sorts the smallest @p (__middle-__first) elements in the range
4545 * @p [__first,__last) and moves them to the range @p [__first,__middle). The
4546 * order of the remaining elements in the range @p [__middle,__last) is
4547 * undefined.
4548 * After the sort if @e i and @e j are iterators in the range
4549 * @p [__first,__middle) such that i precedes j and @e k is an iterator in
4550 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i)
4551 * are both false.
4552 */
4553 template<typename _RandomAccessIterator, typename _Compare>
4554 inline void
4555 partial_sort(_RandomAccessIterator __first,
4556 _RandomAccessIterator __middle,
4557 _RandomAccessIterator __last,
4558 _Compare __comp)
4559 {
4560 // concept requirements
4561 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4562 _RandomAccessIterator>)
4563 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4564 typename iterator_traits<_RandomAccessIterator>::value_type,
4565 typename iterator_traits<_RandomAccessIterator>::value_type>)
4566 __glibcxx_requires_valid_range(__first, __middle);
4567 __glibcxx_requires_valid_range(__middle, __last);
4568
4569 std::__partial_sort(__first, __middle, __last,
4570 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4571 }
4572
4573 /**
4574 * @brief Sort a sequence just enough to find a particular position.
4575 * @ingroup sorting_algorithms
4576 * @param __first An iterator.
4577 * @param __nth Another iterator.
4578 * @param __last Another iterator.
4579 * @return Nothing.
4580 *
4581 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4582 * is the same element that would have been in that position had the
4583 * whole sequence been sorted. The elements either side of @p *__nth are
4584 * not completely sorted, but for any iterator @e i in the range
4585 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4586 * holds that *j < *i is false.
4587 */
4588 template<typename _RandomAccessIterator>
4589 inline void
4590 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4591 _RandomAccessIterator __last)
4592 {
4593 // concept requirements
4594 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4595 _RandomAccessIterator>)
4596 __glibcxx_function_requires(_LessThanComparableConcept<
4597 typename iterator_traits<_RandomAccessIterator>::value_type>)
4598 __glibcxx_requires_valid_range(__first, __nth);
4599 __glibcxx_requires_valid_range(__nth, __last);
4600
4601 if (__first == __last || __nth == __last)
4602 return;
4603
4604 std::__introselect(__first, __nth, __last,
4605 std::__lg(__last - __first) * 2,
4606 __gnu_cxx::__ops::__iter_less_iter());
4607 }
4608
4609 /**
4610 * @brief Sort a sequence just enough to find a particular position
4611 * using a predicate for comparison.
4612 * @ingroup sorting_algorithms
4613 * @param __first An iterator.
4614 * @param __nth Another iterator.
4615 * @param __last Another iterator.
4616 * @param __comp A comparison functor.
4617 * @return Nothing.
4618 *
4619 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth
4620 * is the same element that would have been in that position had the
4621 * whole sequence been sorted. The elements either side of @p *__nth are
4622 * not completely sorted, but for any iterator @e i in the range
4623 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it
4624 * holds that @p __comp(*j,*i) is false.
4625 */
4626 template<typename _RandomAccessIterator, typename _Compare>
4627 inline void
4628 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4629 _RandomAccessIterator __last, _Compare __comp)
4630 {
4631 // concept requirements
4632 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4633 _RandomAccessIterator>)
4634 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4635 typename iterator_traits<_RandomAccessIterator>::value_type,
4636 typename iterator_traits<_RandomAccessIterator>::value_type>)
4637 __glibcxx_requires_valid_range(__first, __nth);
4638 __glibcxx_requires_valid_range(__nth, __last);
4639
4640 if (__first == __last || __nth == __last)
4641 return;
4642
4643 std::__introselect(__first, __nth, __last,
4644 std::__lg(__last - __first) * 2,
4645 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4646 }
4647
4648 /**
4649 * @brief Sort the elements of a sequence.
4650 * @ingroup sorting_algorithms
4651 * @param __first An iterator.
4652 * @param __last Another iterator.
4653 * @return Nothing.
4654 *
4655 * Sorts the elements in the range @p [__first,__last) in ascending order,
4656 * such that for each iterator @e i in the range @p [__first,__last-1),
4657 * *(i+1)<*i is false.
4658 *
4659 * The relative ordering of equivalent elements is not preserved, use
4660 * @p stable_sort() if this is needed.
4661 */
4662 template<typename _RandomAccessIterator>
4663 inline void
4664 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4665 {
4666 // concept requirements
4667 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4668 _RandomAccessIterator>)
4669 __glibcxx_function_requires(_LessThanComparableConcept<
4670 typename iterator_traits<_RandomAccessIterator>::value_type>)
4671 __glibcxx_requires_valid_range(__first, __last);
4672
4673 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter());
4674 }
4675
4676 /**
4677 * @brief Sort the elements of a sequence using a predicate for comparison.
4678 * @ingroup sorting_algorithms
4679 * @param __first An iterator.
4680 * @param __last Another iterator.
4681 * @param __comp A comparison functor.
4682 * @return Nothing.
4683 *
4684 * Sorts the elements in the range @p [__first,__last) in ascending order,
4685 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
4686 * range @p [__first,__last-1).
4687 *
4688 * The relative ordering of equivalent elements is not preserved, use
4689 * @p stable_sort() if this is needed.
4690 */
4691 template<typename _RandomAccessIterator, typename _Compare>
4692 inline void
4693 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4694 _Compare __comp)
4695 {
4696 // concept requirements
4697 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4698 _RandomAccessIterator>)
4699 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4700 typename iterator_traits<_RandomAccessIterator>::value_type,
4701 typename iterator_traits<_RandomAccessIterator>::value_type>)
4702 __glibcxx_requires_valid_range(__first, __last);
4703
4704 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
4705 }
4706
4707 template<typename _InputIterator1, typename _InputIterator2,
4708 typename _OutputIterator, typename _Compare>
4709 _OutputIterator
4710 __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4711 _InputIterator2 __first2, _InputIterator2 __last2,
4712 _OutputIterator __result, _Compare __comp)
4713 {
4714 while (__first1 != __last1 && __first2 != __last2)
4715 {
4716 if (__comp(__first2, __first1))
4717 {
4718 *__result = *__first2;
4719 ++__first2;
4720 }
4721 else
4722 {
4723 *__result = *__first1;
4724 ++__first1;
4725 }
4726 ++__result;
4727 }
4728 return std::copy(__first2, __last2,
4729 std::copy(__first1, __last1, __result));
4730 }
4731
4732 /**
4733 * @brief Merges two sorted ranges.
4734 * @ingroup sorting_algorithms
4735 * @param __first1 An iterator.
4736 * @param __first2 Another iterator.
4737 * @param __last1 Another iterator.
4738 * @param __last2 Another iterator.
4739 * @param __result An iterator pointing to the end of the merged range.
4740 * @return An iterator pointing to the first element <em>not less
4741 * than</em> @e val.
4742 *
4743 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4744 * the sorted range @p [__result, __result + (__last1-__first1) +
4745 * (__last2-__first2)). Both input ranges must be sorted, and the
4746 * output range must not overlap with either of the input ranges.
4747 * The sort is @e stable, that is, for equivalent elements in the
4748 * two ranges, elements from the first range will always come
4749 * before elements from the second.
4750 */
4751 template<typename _InputIterator1, typename _InputIterator2,
4752 typename _OutputIterator>
4753 inline _OutputIterator
4754 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4755 _InputIterator2 __first2, _InputIterator2 __last2,
4756 _OutputIterator __result)
4757 {
4758 // concept requirements
4759 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4760 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4761 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4762 typename iterator_traits<_InputIterator1>::value_type>)
4763 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4764 typename iterator_traits<_InputIterator2>::value_type>)
4765 __glibcxx_function_requires(_LessThanOpConcept<
4766 typename iterator_traits<_InputIterator2>::value_type,
4767 typename iterator_traits<_InputIterator1>::value_type>)
4768 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
4769 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
4770
4771 return _GLIBCXX_STD_A::__merge(__first1, __last1,
4772 __first2, __last2, __result,
4773 __gnu_cxx::__ops::__iter_less_iter());
4774 }
4775
4776 /**
4777 * @brief Merges two sorted ranges.
4778 * @ingroup sorting_algorithms
4779 * @param __first1 An iterator.
4780 * @param __first2 Another iterator.
4781 * @param __last1 Another iterator.
4782 * @param __last2 Another iterator.
4783 * @param __result An iterator pointing to the end of the merged range.
4784 * @param __comp A functor to use for comparisons.
4785 * @return An iterator pointing to the first element "not less
4786 * than" @e val.
4787 *
4788 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4789 * the sorted range @p [__result, __result + (__last1-__first1) +
4790 * (__last2-__first2)). Both input ranges must be sorted, and the
4791 * output range must not overlap with either of the input ranges.
4792 * The sort is @e stable, that is, for equivalent elements in the
4793 * two ranges, elements from the first range will always come
4794 * before elements from the second.
4795 *
4796 * The comparison function should have the same effects on ordering as
4797 * the function used for the initial sort.
4798 */
4799 template<typename _InputIterator1, typename _InputIterator2,
4800 typename _OutputIterator, typename _Compare>
4801 inline _OutputIterator
4802 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4803 _InputIterator2 __first2, _InputIterator2 __last2,
4804 _OutputIterator __result, _Compare __comp)
4805 {
4806 // concept requirements
4807 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4808 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4809 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4810 typename iterator_traits<_InputIterator1>::value_type>)
4811 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4812 typename iterator_traits<_InputIterator2>::value_type>)
4813 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4814 typename iterator_traits<_InputIterator2>::value_type,
4815 typename iterator_traits<_InputIterator1>::value_type>)
4816 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
4817 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
4818
4819 return _GLIBCXX_STD_A::__merge(__first1, __last1,
4820 __first2, __last2, __result,
4821 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4822 }
4823
4824 template<typename _RandomAccessIterator, typename _Compare>
4825 inline void
4826 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4827 _Compare __comp)
4828 {
4829 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4830 _ValueType;
4831 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
4832 _DistanceType;
4833
4834 typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf;
4835 _TmpBuf __buf(__first, __last);
4836
4837 if (__buf.begin() == 0)
4838 std::__inplace_stable_sort(__first, __last, __comp);
4839 else
4840 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
4841 _DistanceType(__buf.size()), __comp);
4842 }
4843
4844 /**
4845 * @brief Sort the elements of a sequence, preserving the relative order
4846 * of equivalent elements.
4847 * @ingroup sorting_algorithms
4848 * @param __first An iterator.
4849 * @param __last Another iterator.
4850 * @return Nothing.
4851 *
4852 * Sorts the elements in the range @p [__first,__last) in ascending order,
4853 * such that for each iterator @p i in the range @p [__first,__last-1),
4854 * @p *(i+1)<*i is false.
4855 *
4856 * The relative ordering of equivalent elements is preserved, so any two
4857 * elements @p x and @p y in the range @p [__first,__last) such that
4858 * @p x<y is false and @p y<x is false will have the same relative
4859 * ordering after calling @p stable_sort().
4860 */
4861 template<typename _RandomAccessIterator>
4862 inline void
4863 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4864 {
4865 // concept requirements
4866 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4867 _RandomAccessIterator>)
4868 __glibcxx_function_requires(_LessThanComparableConcept<
4869 typename iterator_traits<_RandomAccessIterator>::value_type>)
4870 __glibcxx_requires_valid_range(__first, __last);
4871
4872 _GLIBCXX_STD_A::__stable_sort(__first, __last,
4873 __gnu_cxx::__ops::__iter_less_iter());
4874 }
4875
4876 /**
4877 * @brief Sort the elements of a sequence using a predicate for comparison,
4878 * preserving the relative order of equivalent elements.
4879 * @ingroup sorting_algorithms
4880 * @param __first An iterator.
4881 * @param __last Another iterator.
4882 * @param __comp A comparison functor.
4883 * @return Nothing.
4884 *
4885 * Sorts the elements in the range @p [__first,__last) in ascending order,
4886 * such that for each iterator @p i in the range @p [__first,__last-1),
4887 * @p __comp(*(i+1),*i) is false.
4888 *
4889 * The relative ordering of equivalent elements is preserved, so any two
4890 * elements @p x and @p y in the range @p [__first,__last) such that
4891 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
4892 * relative ordering after calling @p stable_sort().
4893 */
4894 template<typename _RandomAccessIterator, typename _Compare>
4895 inline void
4896 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4897 _Compare __comp)
4898 {
4899 // concept requirements
4900 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4901 _RandomAccessIterator>)
4902 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4903 typename iterator_traits<_RandomAccessIterator>::value_type,
4904 typename iterator_traits<_RandomAccessIterator>::value_type>)
4905 __glibcxx_requires_valid_range(__first, __last);
4906
4907 _GLIBCXX_STD_A::__stable_sort(__first, __last,
4908 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4909 }
4910
4911 template<typename _InputIterator1, typename _InputIterator2,
4912 typename _OutputIterator,
4913 typename _Compare>
4914 _OutputIterator
4915 __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4916 _InputIterator2 __first2, _InputIterator2 __last2,
4917 _OutputIterator __result, _Compare __comp)
4918 {
4919 while (__first1 != __last1 && __first2 != __last2)
4920 {
4921 if (__comp(__first1, __first2))
4922 {
4923 *__result = *__first1;
4924 ++__first1;
4925 }
4926 else if (__comp(__first2, __first1))
4927 {
4928 *__result = *__first2;
4929 ++__first2;
4930 }
4931 else
4932 {
4933 *__result = *__first1;
4934 ++__first1;
4935 ++__first2;
4936 }
4937 ++__result;
4938 }
4939 return std::copy(__first2, __last2,
4940 std::copy(__first1, __last1, __result));
4941 }
4942
4943 /**
4944 * @brief Return the union of two sorted ranges.
4945 * @ingroup set_algorithms
4946 * @param __first1 Start of first range.
4947 * @param __last1 End of first range.
4948 * @param __first2 Start of second range.
4949 * @param __last2 End of second range.
4950 * @return End of the output range.
4951 * @ingroup set_algorithms
4952 *
4953 * This operation iterates over both ranges, copying elements present in
4954 * each range in order to the output range. Iterators increment for each
4955 * range. When the current element of one range is less than the other,
4956 * that element is copied and the iterator advanced. If an element is
4957 * contained in both ranges, the element from the first range is copied and
4958 * both ranges advance. The output range may not overlap either input
4959 * range.
4960 */
4961 template<typename _InputIterator1, typename _InputIterator2,
4962 typename _OutputIterator>
4963 inline _OutputIterator
4964 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4965 _InputIterator2 __first2, _InputIterator2 __last2,
4966 _OutputIterator __result)
4967 {
4968 // concept requirements
4969 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4970 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4971 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4972 typename iterator_traits<_InputIterator1>::value_type>)
4973 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4974 typename iterator_traits<_InputIterator2>::value_type>)
4975 __glibcxx_function_requires(_LessThanOpConcept<
4976 typename iterator_traits<_InputIterator1>::value_type,
4977 typename iterator_traits<_InputIterator2>::value_type>)
4978 __glibcxx_function_requires(_LessThanOpConcept<
4979 typename iterator_traits<_InputIterator2>::value_type,
4980 typename iterator_traits<_InputIterator1>::value_type>)
4981 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
4982 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
4983
4984 return _GLIBCXX_STD_A::__set_union(__first1, __last1,
4985 __first2, __last2, __result,
4986 __gnu_cxx::__ops::__iter_less_iter());
4987 }
4988
4989 /**
4990 * @brief Return the union of two sorted ranges using a comparison functor.
4991 * @ingroup set_algorithms
4992 * @param __first1 Start of first range.
4993 * @param __last1 End of first range.
4994 * @param __first2 Start of second range.
4995 * @param __last2 End of second range.
4996 * @param __comp The comparison functor.
4997 * @return End of the output range.
4998 * @ingroup set_algorithms
4999 *
5000 * This operation iterates over both ranges, copying elements present in
5001 * each range in order to the output range. Iterators increment for each
5002 * range. When the current element of one range is less than the other
5003 * according to @p __comp, that element is copied and the iterator advanced.
5004 * If an equivalent element according to @p __comp is contained in both
5005 * ranges, the element from the first range is copied and both ranges
5006 * advance. The output range may not overlap either input range.
5007 */
5008 template<typename _InputIterator1, typename _InputIterator2,
5009 typename _OutputIterator, typename _Compare>
5010 inline _OutputIterator
5011 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5012 _InputIterator2 __first2, _InputIterator2 __last2,
5013 _OutputIterator __result, _Compare __comp)
5014 {
5015 // concept requirements
5016 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5017 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5018 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5019 typename iterator_traits<_InputIterator1>::value_type>)
5020 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5021 typename iterator_traits<_InputIterator2>::value_type>)
5022 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5023 typename iterator_traits<_InputIterator1>::value_type,
5024 typename iterator_traits<_InputIterator2>::value_type>)
5025 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5026 typename iterator_traits<_InputIterator2>::value_type,
5027 typename iterator_traits<_InputIterator1>::value_type>)
5028 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5029 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5030
5031 return _GLIBCXX_STD_A::__set_union(__first1, __last1,
5032 __first2, __last2, __result,
5033 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5034 }
5035
5036 template<typename _InputIterator1, typename _InputIterator2,
5037 typename _OutputIterator,
5038 typename _Compare>
5039 _OutputIterator
5040 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5041 _InputIterator2 __first2, _InputIterator2 __last2,
5042 _OutputIterator __result, _Compare __comp)
5043 {
5044 while (__first1 != __last1 && __first2 != __last2)
5045 if (__comp(__first1, __first2))
5046 ++__first1;
5047 else if (__comp(__first2, __first1))
5048 ++__first2;
5049 else
5050 {
5051 *__result = *__first1;
5052 ++__first1;
5053 ++__first2;
5054 ++__result;
5055 }
5056 return __result;
5057 }
5058
5059 /**
5060 * @brief Return the intersection of two sorted ranges.
5061 * @ingroup set_algorithms
5062 * @param __first1 Start of first range.
5063 * @param __last1 End of first range.
5064 * @param __first2 Start of second range.
5065 * @param __last2 End of second range.
5066 * @return End of the output range.
5067 * @ingroup set_algorithms
5068 *
5069 * This operation iterates over both ranges, copying elements present in
5070 * both ranges in order to the output range. Iterators increment for each
5071 * range. When the current element of one range is less than the other,
5072 * that iterator advances. If an element is contained in both ranges, the
5073 * element from the first range is copied and both ranges advance. The
5074 * output range may not overlap either input range.
5075 */
5076 template<typename _InputIterator1, typename _InputIterator2,
5077 typename _OutputIterator>
5078 inline _OutputIterator
5079 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5080 _InputIterator2 __first2, _InputIterator2 __last2,
5081 _OutputIterator __result)
5082 {
5083 // concept requirements
5084 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5085 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5086 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5087 typename iterator_traits<_InputIterator1>::value_type>)
5088 __glibcxx_function_requires(_LessThanOpConcept<
5089 typename iterator_traits<_InputIterator1>::value_type,
5090 typename iterator_traits<_InputIterator2>::value_type>)
5091 __glibcxx_function_requires(_LessThanOpConcept<
5092 typename iterator_traits<_InputIterator2>::value_type,
5093 typename iterator_traits<_InputIterator1>::value_type>)
5094 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5095 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5096
5097 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
5098 __first2, __last2, __result,
5099 __gnu_cxx::__ops::__iter_less_iter());
5100 }
5101
5102 /**
5103 * @brief Return the intersection of two sorted ranges using comparison
5104 * functor.
5105 * @ingroup set_algorithms
5106 * @param __first1 Start of first range.
5107 * @param __last1 End of first range.
5108 * @param __first2 Start of second range.
5109 * @param __last2 End of second range.
5110 * @param __comp The comparison functor.
5111 * @return End of the output range.
5112 * @ingroup set_algorithms
5113 *
5114 * This operation iterates over both ranges, copying elements present in
5115 * both ranges in order to the output range. Iterators increment for each
5116 * range. When the current element of one range is less than the other
5117 * according to @p __comp, that iterator advances. If an element is
5118 * contained in both ranges according to @p __comp, the element from the
5119 * first range is copied and both ranges advance. The output range may not
5120 * overlap either input range.
5121 */
5122 template<typename _InputIterator1, typename _InputIterator2,
5123 typename _OutputIterator, typename _Compare>
5124 inline _OutputIterator
5125 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5126 _InputIterator2 __first2, _InputIterator2 __last2,
5127 _OutputIterator __result, _Compare __comp)
5128 {
5129 // concept requirements
5130 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5131 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5132 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5133 typename iterator_traits<_InputIterator1>::value_type>)
5134 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5135 typename iterator_traits<_InputIterator1>::value_type,
5136 typename iterator_traits<_InputIterator2>::value_type>)
5137 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5138 typename iterator_traits<_InputIterator2>::value_type,
5139 typename iterator_traits<_InputIterator1>::value_type>)
5140 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5141 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5142
5143 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
5144 __first2, __last2, __result,
5145 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5146 }
5147
5148 template<typename _InputIterator1, typename _InputIterator2,
5149 typename _OutputIterator,
5150 typename _Compare>
5151 _OutputIterator
5152 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5153 _InputIterator2 __first2, _InputIterator2 __last2,
5154 _OutputIterator __result, _Compare __comp)
5155 {
5156 while (__first1 != __last1 && __first2 != __last2)
5157 if (__comp(__first1, __first2))
5158 {
5159 *__result = *__first1;
5160 ++__first1;
5161 ++__result;
5162 }
5163 else if (__comp(__first2, __first1))
5164 ++__first2;
5165 else
5166 {
5167 ++__first1;
5168 ++__first2;
5169 }
5170 return std::copy(__first1, __last1, __result);
5171 }
5172
5173 /**
5174 * @brief Return the difference of two sorted ranges.
5175 * @ingroup set_algorithms
5176 * @param __first1 Start of first range.
5177 * @param __last1 End of first range.
5178 * @param __first2 Start of second range.
5179 * @param __last2 End of second range.
5180 * @return End of the output range.
5181 * @ingroup set_algorithms
5182 *
5183 * This operation iterates over both ranges, copying elements present in
5184 * the first range but not the second in order to the output range.
5185 * Iterators increment for each range. When the current element of the
5186 * first range is less than the second, that element is copied and the
5187 * iterator advances. If the current element of the second range is less,
5188 * the iterator advances, but no element is copied. If an element is
5189 * contained in both ranges, no elements are copied and both ranges
5190 * advance. The output range may not overlap either input range.
5191 */
5192 template<typename _InputIterator1, typename _InputIterator2,
5193 typename _OutputIterator>
5194 inline _OutputIterator
5195 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5196 _InputIterator2 __first2, _InputIterator2 __last2,
5197 _OutputIterator __result)
5198 {
5199 // concept requirements
5200 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5201 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5202 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5203 typename iterator_traits<_InputIterator1>::value_type>)
5204 __glibcxx_function_requires(_LessThanOpConcept<
5205 typename iterator_traits<_InputIterator1>::value_type,
5206 typename iterator_traits<_InputIterator2>::value_type>)
5207 __glibcxx_function_requires(_LessThanOpConcept<
5208 typename iterator_traits<_InputIterator2>::value_type,
5209 typename iterator_traits<_InputIterator1>::value_type>)
5210 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5211 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5212
5213 return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
5214 __first2, __last2, __result,
5215 __gnu_cxx::__ops::__iter_less_iter());
5216 }
5217
5218 /**
5219 * @brief Return the difference of two sorted ranges using comparison
5220 * functor.
5221 * @ingroup set_algorithms
5222 * @param __first1 Start of first range.
5223 * @param __last1 End of first range.
5224 * @param __first2 Start of second range.
5225 * @param __last2 End of second range.
5226 * @param __comp The comparison functor.
5227 * @return End of the output range.
5228 * @ingroup set_algorithms
5229 *
5230 * This operation iterates over both ranges, copying elements present in
5231 * the first range but not the second in order to the output range.
5232 * Iterators increment for each range. When the current element of the
5233 * first range is less than the second according to @p __comp, that element
5234 * is copied and the iterator advances. If the current element of the
5235 * second range is less, no element is copied and the iterator advances.
5236 * If an element is contained in both ranges according to @p __comp, no
5237 * elements are copied and both ranges advance. The output range may not
5238 * overlap either input range.
5239 */
5240 template<typename _InputIterator1, typename _InputIterator2,
5241 typename _OutputIterator, typename _Compare>
5242 inline _OutputIterator
5243 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5244 _InputIterator2 __first2, _InputIterator2 __last2,
5245 _OutputIterator __result, _Compare __comp)
5246 {
5247 // concept requirements
5248 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5249 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5250 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5251 typename iterator_traits<_InputIterator1>::value_type>)
5252 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5253 typename iterator_traits<_InputIterator1>::value_type,
5254 typename iterator_traits<_InputIterator2>::value_type>)
5255 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5256 typename iterator_traits<_InputIterator2>::value_type,
5257 typename iterator_traits<_InputIterator1>::value_type>)
5258 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5259 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5260
5261 return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
5262 __first2, __last2, __result,
5263 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5264 }
5265
5266 template<typename _InputIterator1, typename _InputIterator2,
5267 typename _OutputIterator,
5268 typename _Compare>
5269 _OutputIterator
5270 __set_symmetric_difference(_InputIterator1 __first1,
5271 _InputIterator1 __last1,
5272 _InputIterator2 __first2,
5273 _InputIterator2 __last2,
5274 _OutputIterator __result,
5275 _Compare __comp)
5276 {
5277 while (__first1 != __last1 && __first2 != __last2)
5278 if (__comp(__first1, __first2))
5279 {
5280 *__result = *__first1;
5281 ++__first1;
5282 ++__result;
5283 }
5284 else if (__comp(__first2, __first1))
5285 {
5286 *__result = *__first2;
5287 ++__first2;
5288 ++__result;
5289 }
5290 else
5291 {
5292 ++__first1;
5293 ++__first2;
5294 }
5295 return std::copy(__first2, __last2,
5296 std::copy(__first1, __last1, __result));
5297 }
5298
5299 /**
5300 * @brief Return the symmetric difference of two sorted ranges.
5301 * @ingroup set_algorithms
5302 * @param __first1 Start of first range.
5303 * @param __last1 End of first range.
5304 * @param __first2 Start of second range.
5305 * @param __last2 End of second range.
5306 * @return End of the output range.
5307 * @ingroup set_algorithms
5308 *
5309 * This operation iterates over both ranges, copying elements present in
5310 * one range but not the other in order to the output range. Iterators
5311 * increment for each range. When the current element of one range is less
5312 * than the other, that element is copied and the iterator advances. If an
5313 * element is contained in both ranges, no elements are copied and both
5314 * ranges advance. The output range may not overlap either input range.
5315 */
5316 template<typename _InputIterator1, typename _InputIterator2,
5317 typename _OutputIterator>
5318 inline _OutputIterator
5319 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5320 _InputIterator2 __first2, _InputIterator2 __last2,
5321 _OutputIterator __result)
5322 {
5323 // concept requirements
5324 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5325 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5326 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5327 typename iterator_traits<_InputIterator1>::value_type>)
5328 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5329 typename iterator_traits<_InputIterator2>::value_type>)
5330 __glibcxx_function_requires(_LessThanOpConcept<
5331 typename iterator_traits<_InputIterator1>::value_type,
5332 typename iterator_traits<_InputIterator2>::value_type>)
5333 __glibcxx_function_requires(_LessThanOpConcept<
5334 typename iterator_traits<_InputIterator2>::value_type,
5335 typename iterator_traits<_InputIterator1>::value_type>)
5336 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5337 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5338
5339 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
5340 __first2, __last2, __result,
5341 __gnu_cxx::__ops::__iter_less_iter());
5342 }
5343
5344 /**
5345 * @brief Return the symmetric difference of two sorted ranges using
5346 * comparison functor.
5347 * @ingroup set_algorithms
5348 * @param __first1 Start of first range.
5349 * @param __last1 End of first range.
5350 * @param __first2 Start of second range.
5351 * @param __last2 End of second range.
5352 * @param __comp The comparison functor.
5353 * @return End of the output range.
5354 * @ingroup set_algorithms
5355 *
5356 * This operation iterates over both ranges, copying elements present in
5357 * one range but not the other in order to the output range. Iterators
5358 * increment for each range. When the current element of one range is less
5359 * than the other according to @p comp, that element is copied and the
5360 * iterator advances. If an element is contained in both ranges according
5361 * to @p __comp, no elements are copied and both ranges advance. The output
5362 * range may not overlap either input range.
5363 */
5364 template<typename _InputIterator1, typename _InputIterator2,
5365 typename _OutputIterator, typename _Compare>
5366 inline _OutputIterator
5367 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5368 _InputIterator2 __first2, _InputIterator2 __last2,
5369 _OutputIterator __result,
5370 _Compare __comp)
5371 {
5372 // concept requirements
5373 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5374 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5375 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5376 typename iterator_traits<_InputIterator1>::value_type>)
5377 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5378 typename iterator_traits<_InputIterator2>::value_type>)
5379 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5380 typename iterator_traits<_InputIterator1>::value_type,
5381 typename iterator_traits<_InputIterator2>::value_type>)
5382 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5383 typename iterator_traits<_InputIterator2>::value_type,
5384 typename iterator_traits<_InputIterator1>::value_type>)
5385 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5386 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5387
5388 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
5389 __first2, __last2, __result,
5390 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5391 }
5392
5393 template<typename _ForwardIterator, typename _Compare>
5394 _ForwardIterator
5395 __min_element(_ForwardIterator __first, _ForwardIterator __last,
5396 _Compare __comp)
5397 {
5398 if (__first == __last)
5399 return __first;
5400 _ForwardIterator __result = __first;
5401 while (++__first != __last)
5402 if (__comp(__first, __result))
5403 __result = __first;
5404 return __result;
5405 }
5406
5407 /**
5408 * @brief Return the minimum element in a range.
5409 * @ingroup sorting_algorithms
5410 * @param __first Start of range.
5411 * @param __last End of range.
5412 * @return Iterator referencing the first instance of the smallest value.
5413 */
5414 template<typename _ForwardIterator>
5415 _ForwardIterator
5416 inline min_element(_ForwardIterator __first, _ForwardIterator __last)
5417 {
5418 // concept requirements
5419 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5420 __glibcxx_function_requires(_LessThanComparableConcept<
5421 typename iterator_traits<_ForwardIterator>::value_type>)
5422 __glibcxx_requires_valid_range(__first, __last);
5423
5424 return _GLIBCXX_STD_A::__min_element(__first, __last,
5425 __gnu_cxx::__ops::__iter_less_iter());
5426 }
5427
5428 /**
5429 * @brief Return the minimum element in a range using comparison functor.
5430 * @ingroup sorting_algorithms
5431 * @param __first Start of range.
5432 * @param __last End of range.
5433 * @param __comp Comparison functor.
5434 * @return Iterator referencing the first instance of the smallest value
5435 * according to __comp.
5436 */
5437 template<typename _ForwardIterator, typename _Compare>
5438 inline _ForwardIterator
5439 min_element(_ForwardIterator __first, _ForwardIterator __last,
5440 _Compare __comp)
5441 {
5442 // concept requirements
5443 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5444 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5445 typename iterator_traits<_ForwardIterator>::value_type,
5446 typename iterator_traits<_ForwardIterator>::value_type>)
5447 __glibcxx_requires_valid_range(__first, __last);
5448
5449 return _GLIBCXX_STD_A::__min_element(__first, __last,
5450 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5451 }
5452
5453 template<typename _ForwardIterator, typename _Compare>
5454 _ForwardIterator
5455 __max_element(_ForwardIterator __first, _ForwardIterator __last,
5456 _Compare __comp)
5457 {
5458 if (__first == __last) return __first;
5459 _ForwardIterator __result = __first;
5460 while (++__first != __last)
5461 if (__comp(__result, __first))
5462 __result = __first;
5463 return __result;
5464 }
5465
5466 /**
5467 * @brief Return the maximum element in a range.
5468 * @ingroup sorting_algorithms
5469 * @param __first Start of range.
5470 * @param __last End of range.
5471 * @return Iterator referencing the first instance of the largest value.
5472 */
5473 template<typename _ForwardIterator>
5474 inline _ForwardIterator
5475 max_element(_ForwardIterator __first, _ForwardIterator __last)
5476 {
5477 // concept requirements
5478 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5479 __glibcxx_function_requires(_LessThanComparableConcept<
5480 typename iterator_traits<_ForwardIterator>::value_type>)
5481 __glibcxx_requires_valid_range(__first, __last);
5482
5483 return _GLIBCXX_STD_A::__max_element(__first, __last,
5484 __gnu_cxx::__ops::__iter_less_iter());
5485 }
5486
5487 /**
5488 * @brief Return the maximum element in a range using comparison functor.
5489 * @ingroup sorting_algorithms
5490 * @param __first Start of range.
5491 * @param __last End of range.
5492 * @param __comp Comparison functor.
5493 * @return Iterator referencing the first instance of the largest value
5494 * according to __comp.
5495 */
5496 template<typename _ForwardIterator, typename _Compare>
5497 inline _ForwardIterator
5498 max_element(_ForwardIterator __first, _ForwardIterator __last,
5499 _Compare __comp)
5500 {
5501 // concept requirements
5502 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5503 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5504 typename iterator_traits<_ForwardIterator>::value_type,
5505 typename iterator_traits<_ForwardIterator>::value_type>)
5506 __glibcxx_requires_valid_range(__first, __last);
5507
5508 return _GLIBCXX_STD_A::__max_element(__first, __last,
5509 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5510 }
5511
5512 _GLIBCXX_END_NAMESPACE_ALGO
5513 } // namespace std
5514
5515 #endif /* _STL_ALGO_H */