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