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