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