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