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