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