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