re PR libstdc++/34730 (Legal program doesn't compile with -D_GLIBCXX_DEBUG)
[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/algorithmfwd.h>
67 #include <bits/stl_heap.h>
68 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
69 #include <debug/debug.h>
70
71 // See concept_check.h for the __glibcxx_*_requires macros.
72
73 _GLIBCXX_BEGIN_NAMESPACE(std)
74
75 /**
76 * @brief Find the median of three values.
77 * @param a A value.
78 * @param b A value.
79 * @param c A value.
80 * @return One of @p a, @p b or @p c.
81 *
82 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
83 * then the value returned will be @c m.
84 * This is an SGI extension.
85 * @ingroup SGIextensions
86 */
87 template<typename _Tp>
88 inline const _Tp&
89 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
90 {
91 // concept requirements
92 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
93 if (__a < __b)
94 if (__b < __c)
95 return __b;
96 else if (__a < __c)
97 return __c;
98 else
99 return __a;
100 else if (__a < __c)
101 return __a;
102 else if (__b < __c)
103 return __c;
104 else
105 return __b;
106 }
107
108 /**
109 * @brief Find the median of three values using a predicate for comparison.
110 * @param a A value.
111 * @param b A value.
112 * @param c A value.
113 * @param comp A binary predicate.
114 * @return One of @p a, @p b or @p c.
115 *
116 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
117 * and @p comp(m,n) are both true then the value returned will be @c m.
118 * This is an SGI extension.
119 * @ingroup SGIextensions
120 */
121 template<typename _Tp, typename _Compare>
122 inline const _Tp&
123 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
124 {
125 // concept requirements
126 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
127 _Tp, _Tp>)
128 if (__comp(__a, __b))
129 if (__comp(__b, __c))
130 return __b;
131 else if (__comp(__a, __c))
132 return __c;
133 else
134 return __a;
135 else if (__comp(__a, __c))
136 return __a;
137 else if (__comp(__b, __c))
138 return __c;
139 else
140 return __b;
141 }
142
143 // for_each
144
145 /**
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,
3038 __first + __two_step,
3039 __result);
3040 __first += __two_step;
3041 }
3042
3043 __step_size = std::min(_Distance(__last - __first), __step_size);
3044 _GLIBCXX_STD_P::merge(__first, __first + __step_size,
3045 __first + __step_size, __last,
3046 __result);
3047 }
3048
3049 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3050 typename _Distance, typename _Compare>
3051 void
3052 __merge_sort_loop(_RandomAccessIterator1 __first,
3053 _RandomAccessIterator1 __last,
3054 _RandomAccessIterator2 __result, _Distance __step_size,
3055 _Compare __comp)
3056 {
3057 const _Distance __two_step = 2 * __step_size;
3058
3059 while (__last - __first >= __two_step)
3060 {
3061 __result = _GLIBCXX_STD_P::merge(__first, __first + __step_size,
3062 __first + __step_size, __first + __two_step,
3063 __result,
3064 __comp);
3065 __first += __two_step;
3066 }
3067 __step_size = std::min(_Distance(__last - __first), __step_size);
3068
3069 _GLIBCXX_STD_P::merge(__first, __first + __step_size,
3070 __first + __step_size, __last, __result, __comp);
3071 }
3072
3073 template<typename _RandomAccessIterator, typename _Distance>
3074 void
3075 __chunk_insertion_sort(_RandomAccessIterator __first,
3076 _RandomAccessIterator __last,
3077 _Distance __chunk_size)
3078 {
3079 while (__last - __first >= __chunk_size)
3080 {
3081 std::__insertion_sort(__first, __first + __chunk_size);
3082 __first += __chunk_size;
3083 }
3084 std::__insertion_sort(__first, __last);
3085 }
3086
3087 template<typename _RandomAccessIterator, typename _Distance,
3088 typename _Compare>
3089 void
3090 __chunk_insertion_sort(_RandomAccessIterator __first,
3091 _RandomAccessIterator __last,
3092 _Distance __chunk_size, _Compare __comp)
3093 {
3094 while (__last - __first >= __chunk_size)
3095 {
3096 std::__insertion_sort(__first, __first + __chunk_size, __comp);
3097 __first += __chunk_size;
3098 }
3099 std::__insertion_sort(__first, __last, __comp);
3100 }
3101
3102 enum { _S_chunk_size = 7 };
3103
3104 template<typename _RandomAccessIterator, typename _Pointer>
3105 void
3106 __merge_sort_with_buffer(_RandomAccessIterator __first,
3107 _RandomAccessIterator __last,
3108 _Pointer __buffer)
3109 {
3110 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3111 _Distance;
3112
3113 const _Distance __len = __last - __first;
3114 const _Pointer __buffer_last = __buffer + __len;
3115
3116 _Distance __step_size = _S_chunk_size;
3117 std::__chunk_insertion_sort(__first, __last, __step_size);
3118
3119 while (__step_size < __len)
3120 {
3121 std::__merge_sort_loop(__first, __last, __buffer, __step_size);
3122 __step_size *= 2;
3123 std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
3124 __step_size *= 2;
3125 }
3126 }
3127
3128 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
3129 void
3130 __merge_sort_with_buffer(_RandomAccessIterator __first,
3131 _RandomAccessIterator __last,
3132 _Pointer __buffer, _Compare __comp)
3133 {
3134 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3135 _Distance;
3136
3137 const _Distance __len = __last - __first;
3138 const _Pointer __buffer_last = __buffer + __len;
3139
3140 _Distance __step_size = _S_chunk_size;
3141 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
3142
3143 while (__step_size < __len)
3144 {
3145 std::__merge_sort_loop(__first, __last, __buffer,
3146 __step_size, __comp);
3147 __step_size *= 2;
3148 std::__merge_sort_loop(__buffer, __buffer_last, __first,
3149 __step_size, __comp);
3150 __step_size *= 2;
3151 }
3152 }
3153
3154 template<typename _RandomAccessIterator, typename _Pointer,
3155 typename _Distance>
3156 void
3157 __stable_sort_adaptive(_RandomAccessIterator __first,
3158 _RandomAccessIterator __last,
3159 _Pointer __buffer, _Distance __buffer_size)
3160 {
3161 const _Distance __len = (__last - __first + 1) / 2;
3162 const _RandomAccessIterator __middle = __first + __len;
3163 if (__len > __buffer_size)
3164 {
3165 std::__stable_sort_adaptive(__first, __middle,
3166 __buffer, __buffer_size);
3167 std::__stable_sort_adaptive(__middle, __last,
3168 __buffer, __buffer_size);
3169 }
3170 else
3171 {
3172 std::__merge_sort_with_buffer(__first, __middle, __buffer);
3173 std::__merge_sort_with_buffer(__middle, __last, __buffer);
3174 }
3175 std::__merge_adaptive(__first, __middle, __last,
3176 _Distance(__middle - __first),
3177 _Distance(__last - __middle),
3178 __buffer, __buffer_size);
3179 }
3180
3181 template<typename _RandomAccessIterator, typename _Pointer,
3182 typename _Distance, typename _Compare>
3183 void
3184 __stable_sort_adaptive(_RandomAccessIterator __first,
3185 _RandomAccessIterator __last,
3186 _Pointer __buffer, _Distance __buffer_size,
3187 _Compare __comp)
3188 {
3189 const _Distance __len = (__last - __first + 1) / 2;
3190 const _RandomAccessIterator __middle = __first + __len;
3191 if (__len > __buffer_size)
3192 {
3193 std::__stable_sort_adaptive(__first, __middle, __buffer,
3194 __buffer_size, __comp);
3195 std::__stable_sort_adaptive(__middle, __last, __buffer,
3196 __buffer_size, __comp);
3197 }
3198 else
3199 {
3200 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
3201 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
3202 }
3203 std::__merge_adaptive(__first, __middle, __last,
3204 _Distance(__middle - __first),
3205 _Distance(__last - __middle),
3206 __buffer, __buffer_size,
3207 __comp);
3208 }
3209
3210 /**
3211 * @if maint
3212 * This is a helper function for the stable sorting routines.
3213 * @endif
3214 */
3215 template<typename _RandomAccessIterator>
3216 void
3217 __inplace_stable_sort(_RandomAccessIterator __first,
3218 _RandomAccessIterator __last)
3219 {
3220 if (__last - __first < 15)
3221 {
3222 std::__insertion_sort(__first, __last);
3223 return;
3224 }
3225 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3226 std::__inplace_stable_sort(__first, __middle);
3227 std::__inplace_stable_sort(__middle, __last);
3228 std::__merge_without_buffer(__first, __middle, __last,
3229 __middle - __first,
3230 __last - __middle);
3231 }
3232
3233 /**
3234 * @if maint
3235 * This is a helper function for the stable sorting routines.
3236 * @endif
3237 */
3238 template<typename _RandomAccessIterator, typename _Compare>
3239 void
3240 __inplace_stable_sort(_RandomAccessIterator __first,
3241 _RandomAccessIterator __last, _Compare __comp)
3242 {
3243 if (__last - __first < 15)
3244 {
3245 std::__insertion_sort(__first, __last, __comp);
3246 return;
3247 }
3248 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3249 std::__inplace_stable_sort(__first, __middle, __comp);
3250 std::__inplace_stable_sort(__middle, __last, __comp);
3251 std::__merge_without_buffer(__first, __middle, __last,
3252 __middle - __first,
3253 __last - __middle,
3254 __comp);
3255 }
3256
3257 // stable_sort
3258
3259 // Set algorithms: includes, set_union, set_intersection, set_difference,
3260 // set_symmetric_difference. All of these algorithms have the precondition
3261 // that their input ranges are sorted and the postcondition that their output
3262 // ranges are sorted.
3263
3264 /**
3265 * @brief Determines whether all elements of a sequence exists in a range.
3266 * @param first1 Start of search range.
3267 * @param last1 End of search range.
3268 * @param first2 Start of sequence
3269 * @param last2 End of sequence.
3270 * @return True if each element in [first2,last2) is contained in order
3271 * within [first1,last1). False otherwise.
3272 * @ingroup setoperations
3273 *
3274 * This operation expects both [first1,last1) and [first2,last2) to be
3275 * sorted. Searches for the presence of each element in [first2,last2)
3276 * within [first1,last1). The iterators over each range only move forward,
3277 * so this is a linear algorithm. If an element in [first2,last2) is not
3278 * found before the search iterator reaches @a last2, false is returned.
3279 */
3280 template<typename _InputIterator1, typename _InputIterator2>
3281 bool
3282 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3283 _InputIterator2 __first2, _InputIterator2 __last2)
3284 {
3285 typedef typename iterator_traits<_InputIterator1>::value_type
3286 _ValueType1;
3287 typedef typename iterator_traits<_InputIterator2>::value_type
3288 _ValueType2;
3289
3290 // concept requirements
3291 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3292 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3293 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
3294 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
3295 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
3296 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
3297
3298 while (__first1 != __last1 && __first2 != __last2)
3299 if (*__first2 < *__first1)
3300 return false;
3301 else if(*__first1 < *__first2)
3302 ++__first1;
3303 else
3304 ++__first1, ++__first2;
3305
3306 return __first2 == __last2;
3307 }
3308
3309 /**
3310 * @brief Determines whether all elements of a sequence exists in a range
3311 * using comparison.
3312 * @param first1 Start of search range.
3313 * @param last1 End of search range.
3314 * @param first2 Start of sequence
3315 * @param last2 End of sequence.
3316 * @param comp Comparison function to use.
3317 * @return True if each element in [first2,last2) is contained in order
3318 * within [first1,last1) according to comp. False otherwise.
3319 * @ingroup setoperations
3320 *
3321 * This operation expects both [first1,last1) and [first2,last2) to be
3322 * sorted. Searches for the presence of each element in [first2,last2)
3323 * within [first1,last1), using comp to decide. The iterators over each
3324 * range only move forward, so this is a linear algorithm. If an element
3325 * in [first2,last2) is not found before the search iterator reaches @a
3326 * last2, false is returned.
3327 */
3328 template<typename _InputIterator1, typename _InputIterator2,
3329 typename _Compare>
3330 bool
3331 includes(_InputIterator1 __first1, _InputIterator1 __last1,
3332 _InputIterator2 __first2, _InputIterator2 __last2,
3333 _Compare __comp)
3334 {
3335 typedef typename iterator_traits<_InputIterator1>::value_type
3336 _ValueType1;
3337 typedef typename iterator_traits<_InputIterator2>::value_type
3338 _ValueType2;
3339
3340 // concept requirements
3341 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3342 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3343 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3344 _ValueType1, _ValueType2>)
3345 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3346 _ValueType2, _ValueType1>)
3347 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
3348 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
3349
3350 while (__first1 != __last1 && __first2 != __last2)
3351 if (__comp(*__first2, *__first1))
3352 return false;
3353 else if(__comp(*__first1, *__first2))
3354 ++__first1;
3355 else
3356 ++__first1, ++__first2;
3357
3358 return __first2 == __last2;
3359 }
3360
3361 // nth_element
3362 // merge
3363 // set_difference
3364 // set_intersection
3365 // set_union
3366 // stable_sort
3367 // set_symmetric_difference
3368 // min_element
3369 // max_element
3370
3371 /**
3372 * @brief Permute range into the next "dictionary" ordering.
3373 * @param first Start of range.
3374 * @param last End of range.
3375 * @return False if wrapped to first permutation, true otherwise.
3376 *
3377 * Treats all permutations of the range as a set of "dictionary" sorted
3378 * sequences. Permutes the current sequence into the next one of this set.
3379 * Returns true if there are more sequences to generate. If the sequence
3380 * is the largest of the set, the smallest is generated and false returned.
3381 */
3382 template<typename _BidirectionalIterator>
3383 bool
3384 next_permutation(_BidirectionalIterator __first,
3385 _BidirectionalIterator __last)
3386 {
3387 // concept requirements
3388 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3389 _BidirectionalIterator>)
3390 __glibcxx_function_requires(_LessThanComparableConcept<
3391 typename iterator_traits<_BidirectionalIterator>::value_type>)
3392 __glibcxx_requires_valid_range(__first, __last);
3393
3394 if (__first == __last)
3395 return false;
3396 _BidirectionalIterator __i = __first;
3397 ++__i;
3398 if (__i == __last)
3399 return false;
3400 __i = __last;
3401 --__i;
3402
3403 for(;;)
3404 {
3405 _BidirectionalIterator __ii = __i;
3406 --__i;
3407 if (*__i < *__ii)
3408 {
3409 _BidirectionalIterator __j = __last;
3410 while (!(*__i < *--__j))
3411 {}
3412 std::iter_swap(__i, __j);
3413 std::reverse(__ii, __last);
3414 return true;
3415 }
3416 if (__i == __first)
3417 {
3418 std::reverse(__first, __last);
3419 return false;
3420 }
3421 }
3422 }
3423
3424 /**
3425 * @brief Permute range into the next "dictionary" ordering using
3426 * comparison functor.
3427 * @param first Start of range.
3428 * @param last End of range.
3429 * @param comp A comparison functor.
3430 * @return False if wrapped to first permutation, true otherwise.
3431 *
3432 * Treats all permutations of the range [first,last) as a set of
3433 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3434 * sequence into the next one of this set. Returns true if there are more
3435 * sequences to generate. If the sequence is the largest of the set, the
3436 * smallest is generated and false returned.
3437 */
3438 template<typename _BidirectionalIterator, typename _Compare>
3439 bool
3440 next_permutation(_BidirectionalIterator __first,
3441 _BidirectionalIterator __last, _Compare __comp)
3442 {
3443 // concept requirements
3444 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3445 _BidirectionalIterator>)
3446 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3447 typename iterator_traits<_BidirectionalIterator>::value_type,
3448 typename iterator_traits<_BidirectionalIterator>::value_type>)
3449 __glibcxx_requires_valid_range(__first, __last);
3450
3451 if (__first == __last)
3452 return false;
3453 _BidirectionalIterator __i = __first;
3454 ++__i;
3455 if (__i == __last)
3456 return false;
3457 __i = __last;
3458 --__i;
3459
3460 for(;;)
3461 {
3462 _BidirectionalIterator __ii = __i;
3463 --__i;
3464 if (__comp(*__i, *__ii))
3465 {
3466 _BidirectionalIterator __j = __last;
3467 while (!bool(__comp(*__i, *--__j)))
3468 {}
3469 std::iter_swap(__i, __j);
3470 std::reverse(__ii, __last);
3471 return true;
3472 }
3473 if (__i == __first)
3474 {
3475 std::reverse(__first, __last);
3476 return false;
3477 }
3478 }
3479 }
3480
3481 /**
3482 * @brief Permute range into the previous "dictionary" ordering.
3483 * @param first Start of range.
3484 * @param last End of range.
3485 * @return False if wrapped to last permutation, true otherwise.
3486 *
3487 * Treats all permutations of the range as a set of "dictionary" sorted
3488 * sequences. Permutes the current sequence into the previous one of this
3489 * set. Returns true if there are more sequences to generate. If the
3490 * sequence is the smallest of the set, the largest is generated and false
3491 * returned.
3492 */
3493 template<typename _BidirectionalIterator>
3494 bool
3495 prev_permutation(_BidirectionalIterator __first,
3496 _BidirectionalIterator __last)
3497 {
3498 // concept requirements
3499 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3500 _BidirectionalIterator>)
3501 __glibcxx_function_requires(_LessThanComparableConcept<
3502 typename iterator_traits<_BidirectionalIterator>::value_type>)
3503 __glibcxx_requires_valid_range(__first, __last);
3504
3505 if (__first == __last)
3506 return false;
3507 _BidirectionalIterator __i = __first;
3508 ++__i;
3509 if (__i == __last)
3510 return false;
3511 __i = __last;
3512 --__i;
3513
3514 for(;;)
3515 {
3516 _BidirectionalIterator __ii = __i;
3517 --__i;
3518 if (*__ii < *__i)
3519 {
3520 _BidirectionalIterator __j = __last;
3521 while (!(*--__j < *__i))
3522 {}
3523 std::iter_swap(__i, __j);
3524 std::reverse(__ii, __last);
3525 return true;
3526 }
3527 if (__i == __first)
3528 {
3529 std::reverse(__first, __last);
3530 return false;
3531 }
3532 }
3533 }
3534
3535 /**
3536 * @brief Permute range into the previous "dictionary" ordering using
3537 * comparison functor.
3538 * @param first Start of range.
3539 * @param last End of range.
3540 * @param comp A comparison functor.
3541 * @return False if wrapped to last permutation, true otherwise.
3542 *
3543 * Treats all permutations of the range [first,last) as a set of
3544 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
3545 * sequence into the previous one of this set. Returns true if there are
3546 * more sequences to generate. If the sequence is the smallest of the set,
3547 * the largest is generated and false returned.
3548 */
3549 template<typename _BidirectionalIterator, typename _Compare>
3550 bool
3551 prev_permutation(_BidirectionalIterator __first,
3552 _BidirectionalIterator __last, _Compare __comp)
3553 {
3554 // concept requirements
3555 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3556 _BidirectionalIterator>)
3557 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3558 typename iterator_traits<_BidirectionalIterator>::value_type,
3559 typename iterator_traits<_BidirectionalIterator>::value_type>)
3560 __glibcxx_requires_valid_range(__first, __last);
3561
3562 if (__first == __last)
3563 return false;
3564 _BidirectionalIterator __i = __first;
3565 ++__i;
3566 if (__i == __last)
3567 return false;
3568 __i = __last;
3569 --__i;
3570
3571 for(;;)
3572 {
3573 _BidirectionalIterator __ii = __i;
3574 --__i;
3575 if (__comp(*__ii, *__i))
3576 {
3577 _BidirectionalIterator __j = __last;
3578 while (!bool(__comp(*--__j, *__i)))
3579 {}
3580 std::iter_swap(__i, __j);
3581 std::reverse(__ii, __last);
3582 return true;
3583 }
3584 if (__i == __first)
3585 {
3586 std::reverse(__first, __last);
3587 return false;
3588 }
3589 }
3590 }
3591
3592 // replace
3593 // replace_if
3594
3595 /**
3596 * @brief Copy a sequence, replacing each element of one value with another
3597 * value.
3598 * @param first An input iterator.
3599 * @param last An input iterator.
3600 * @param result An output iterator.
3601 * @param old_value The value to be replaced.
3602 * @param new_value The replacement value.
3603 * @return The end of the output sequence, @p result+(last-first).
3604 *
3605 * Copies each element in the input range @p [first,last) to the
3606 * output range @p [result,result+(last-first)) replacing elements
3607 * equal to @p old_value with @p new_value.
3608 */
3609 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
3610 _OutputIterator
3611 replace_copy(_InputIterator __first, _InputIterator __last,
3612 _OutputIterator __result,
3613 const _Tp& __old_value, const _Tp& __new_value)
3614 {
3615 // concept requirements
3616 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3617 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3618 typename iterator_traits<_InputIterator>::value_type>)
3619 __glibcxx_function_requires(_EqualOpConcept<
3620 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3621 __glibcxx_requires_valid_range(__first, __last);
3622
3623 for (; __first != __last; ++__first, ++__result)
3624 if (*__first == __old_value)
3625 *__result = __new_value;
3626 else
3627 *__result = *__first;
3628 return __result;
3629 }
3630
3631 /**
3632 * @brief Copy a sequence, replacing each value for which a predicate
3633 * returns true with another value.
3634 * @param first An input iterator.
3635 * @param last An input iterator.
3636 * @param result An output iterator.
3637 * @param pred A predicate.
3638 * @param new_value The replacement value.
3639 * @return The end of the output sequence, @p result+(last-first).
3640 *
3641 * Copies each element in the range @p [first,last) to the range
3642 * @p [result,result+(last-first)) replacing elements for which
3643 * @p pred returns true with @p new_value.
3644 */
3645 template<typename _InputIterator, typename _OutputIterator,
3646 typename _Predicate, typename _Tp>
3647 _OutputIterator
3648 replace_copy_if(_InputIterator __first, _InputIterator __last,
3649 _OutputIterator __result,
3650 _Predicate __pred, const _Tp& __new_value)
3651 {
3652 // concept requirements
3653 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3654 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3655 typename iterator_traits<_InputIterator>::value_type>)
3656 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3657 typename iterator_traits<_InputIterator>::value_type>)
3658 __glibcxx_requires_valid_range(__first, __last);
3659
3660 for (; __first != __last; ++__first, ++__result)
3661 if (__pred(*__first))
3662 *__result = __new_value;
3663 else
3664 *__result = *__first;
3665 return __result;
3666 }
3667
3668 #ifdef __GXX_EXPERIMENTAL_CXX0X__
3669 /**
3670 * @brief Determines whether the elements of a sequence are sorted.
3671 * @param first An iterator.
3672 * @param last Another iterator.
3673 * @return True if the elements are sorted, false otherwise.
3674 */
3675 template<typename _ForwardIterator>
3676 inline bool
3677 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3678 { return std::is_sorted_until(__first, __last) == __last; }
3679
3680 /**
3681 * @brief Determines whether the elements of a sequence are sorted
3682 * according to a comparison functor.
3683 * @param first An iterator.
3684 * @param last Another iterator.
3685 * @param comp A comparison functor.
3686 * @return True if the elements are sorted, false otherwise.
3687 */
3688 template<typename _ForwardIterator, typename _Compare>
3689 inline bool
3690 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
3691 _Compare __comp)
3692 { return std::is_sorted_until(__first, __last, __comp) == __last; }
3693
3694 /**
3695 * @brief Determines the end of a sorted sequence.
3696 * @param first An iterator.
3697 * @param last Another iterator.
3698 * @return An iterator pointing to the last iterator i in [first, last)
3699 * for which the range [first, i) is sorted.
3700 */
3701 template<typename _ForwardIterator>
3702 _ForwardIterator
3703 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3704 {
3705 // concept requirements
3706 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3707 __glibcxx_function_requires(_LessThanComparableConcept<
3708 typename iterator_traits<_ForwardIterator>::value_type>)
3709 __glibcxx_requires_valid_range(__first, __last);
3710
3711 if (__first == __last)
3712 return __last;
3713
3714 _ForwardIterator __next = __first;
3715 for (++__next; __next != __last; __first = __next, ++__next)
3716 if (*__next < *__first)
3717 return __next;
3718 return __next;
3719 }
3720
3721 /**
3722 * @brief Determines the end of a sorted sequence using comparison functor.
3723 * @param first An iterator.
3724 * @param last Another iterator.
3725 * @param comp A comparison functor.
3726 * @return An iterator pointing to the last iterator i in [first, last)
3727 * for which the range [first, i) is sorted.
3728 */
3729 template<typename _ForwardIterator, typename _Compare>
3730 _ForwardIterator
3731 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3732 _Compare __comp)
3733 {
3734 // concept requirements
3735 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3736 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3737 typename iterator_traits<_ForwardIterator>::value_type,
3738 typename iterator_traits<_ForwardIterator>::value_type>)
3739 __glibcxx_requires_valid_range(__first, __last);
3740
3741 if (__first == __last)
3742 return __last;
3743
3744 _ForwardIterator __next = __first;
3745 for (++__next; __next != __last; __first = __next, ++__next)
3746 if (__comp(*__next, *__first))
3747 return __next;
3748 return __next;
3749 }
3750
3751 /**
3752 * @brief Determines min and max at once as an ordered pair.
3753 * @param a A thing of arbitrary type.
3754 * @param b Another thing of arbitrary type.
3755 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3756 */
3757 template<typename _Tp>
3758 inline pair<const _Tp&, const _Tp&>
3759 minmax(const _Tp& __a, const _Tp& __b)
3760 {
3761 // concept requirements
3762 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
3763
3764 return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
3765 : pair<const _Tp&, const _Tp&>(__a, __b);
3766 }
3767
3768 /**
3769 * @brief Determines min and max at once as an ordered pair.
3770 * @param a A thing of arbitrary type.
3771 * @param b Another thing of arbitrary type.
3772 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
3773 * @return A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
3774 */
3775 template<typename _Tp, typename _Compare>
3776 inline pair<const _Tp&, const _Tp&>
3777 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
3778 {
3779 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
3780 : pair<const _Tp&, const _Tp&>(__a, __b);
3781 }
3782
3783 /**
3784 * @brief Return a pair of iterators pointing to the minimum and maximum
3785 * elements in a range.
3786 * @param first Start of range.
3787 * @param last End of range.
3788 * @return make_pair(m, M), where m is the first iterator i in
3789 * [first, last) such that no other element in the range is
3790 * smaller, and where M is the last iterator i in [first, last)
3791 * such that no other element in the range is larger.
3792 */
3793 template<typename _ForwardIterator>
3794 pair<_ForwardIterator, _ForwardIterator>
3795 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
3796 {
3797 // concept requirements
3798 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3799 __glibcxx_function_requires(_LessThanComparableConcept<
3800 typename iterator_traits<_ForwardIterator>::value_type>)
3801 __glibcxx_requires_valid_range(__first, __last);
3802
3803 _ForwardIterator __next = __first;
3804 if (__first == __last
3805 || ++__next == __last)
3806 return std::make_pair(__first, __first);
3807
3808 _ForwardIterator __min, __max;
3809 if (*__next < *__first)
3810 {
3811 __min = __next;
3812 __max = __first;
3813 }
3814 else
3815 {
3816 __min = __first;
3817 __max = __next;
3818 }
3819
3820 __first = __next;
3821 ++__first;
3822
3823 while (__first != __last)
3824 {
3825 __next = __first;
3826 if (++__next == __last)
3827 {
3828 if (*__first < *__min)
3829 __min = __first;
3830 else if (!(*__first < *__max))
3831 __max = __first;
3832 break;
3833 }
3834
3835 if (*__next < *__first)
3836 {
3837 if (*__next < *__min)
3838 __min = __next;
3839 if (!(*__first < *__max))
3840 __max = __first;
3841 }
3842 else
3843 {
3844 if (*__first < *__min)
3845 __min = __first;
3846 if (!(*__next < *__max))
3847 __max = __next;
3848 }
3849
3850 __first = __next;
3851 ++__first;
3852 }
3853
3854 return std::make_pair(__min, __max);
3855 }
3856
3857 /**
3858 * @brief Return a pair of iterators pointing to the minimum and maximum
3859 * elements in a range.
3860 * @param first Start of range.
3861 * @param last End of range.
3862 * @param comp Comparison functor.
3863 * @return make_pair(m, M), where m is the first iterator i in
3864 * [first, last) such that no other element in the range is
3865 * smaller, and where M is the last iterator i in [first, last)
3866 * such that no other element in the range is larger.
3867 */
3868 template<typename _ForwardIterator, typename _Compare>
3869 pair<_ForwardIterator, _ForwardIterator>
3870 minmax_element(_ForwardIterator __first, _ForwardIterator __last,
3871 _Compare __comp)
3872 {
3873 // concept requirements
3874 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3875 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3876 typename iterator_traits<_ForwardIterator>::value_type,
3877 typename iterator_traits<_ForwardIterator>::value_type>)
3878 __glibcxx_requires_valid_range(__first, __last);
3879
3880 _ForwardIterator __next = __first;
3881 if (__first == __last
3882 || ++__next == __last)
3883 return std::make_pair(__first, __first);
3884
3885 _ForwardIterator __min, __max;
3886 if (__comp(*__next, *__first))
3887 {
3888 __min = __next;
3889 __max = __first;
3890 }
3891 else
3892 {
3893 __min = __first;
3894 __max = __next;
3895 }
3896
3897 __first = __next;
3898 ++__first;
3899
3900 while (__first != __last)
3901 {
3902 __next = __first;
3903 if (++__next == __last)
3904 {
3905 if (__comp(*__first, *__min))
3906 __min = __first;
3907 else if (!__comp(*__first, *__max))
3908 __max = __first;
3909 break;
3910 }
3911
3912 if (__comp(*__next, *__first))
3913 {
3914 if (__comp(*__next, *__min))
3915 __min = __next;
3916 if (!__comp(*__first, *__max))
3917 __max = __first;
3918 }
3919 else
3920 {
3921 if (__comp(*__first, *__min))
3922 __min = __first;
3923 if (!__comp(*__next, *__max))
3924 __max = __next;
3925 }
3926
3927 __first = __next;
3928 ++__first;
3929 }
3930
3931 return std::make_pair(__min, __max);
3932 }
3933 #endif // __GXX_EXPERIMENTAL_CXX0X__
3934
3935 _GLIBCXX_END_NAMESPACE
3936
3937 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
3938
3939 /**
3940 * @brief Apply a function to every element of a sequence.
3941 * @param first An input iterator.
3942 * @param last An input iterator.
3943 * @param f A unary function object.
3944 * @return @p f.
3945 *
3946 * Applies the function object @p f to each element in the range
3947 * @p [first,last). @p f must not modify the order of the sequence.
3948 * If @p f has a return value it is ignored.
3949 */
3950 template<typename _InputIterator, typename _Function>
3951 _Function
3952 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
3953 {
3954 // concept requirements
3955 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3956 __glibcxx_requires_valid_range(__first, __last);
3957 for (; __first != __last; ++__first)
3958 __f(*__first);
3959 return __f;
3960 }
3961
3962 /**
3963 * @brief Find the first occurrence of a value in a sequence.
3964 * @param first An input iterator.
3965 * @param last An input iterator.
3966 * @param val The value to find.
3967 * @return The first iterator @c i in the range @p [first,last)
3968 * such that @c *i == @p val, or @p last if no such iterator exists.
3969 */
3970 template<typename _InputIterator, typename _Tp>
3971 inline _InputIterator
3972 find(_InputIterator __first, _InputIterator __last,
3973 const _Tp& __val)
3974 {
3975 // concept requirements
3976 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3977 __glibcxx_function_requires(_EqualOpConcept<
3978 typename iterator_traits<_InputIterator>::value_type, _Tp>)
3979 __glibcxx_requires_valid_range(__first, __last);
3980 return std::__find(__first, __last, __val,
3981 std::__iterator_category(__first));
3982 }
3983
3984 /**
3985 * @brief Find the first element in a sequence for which a
3986 * predicate is true.
3987 * @param first An input iterator.
3988 * @param last An input iterator.
3989 * @param pred A predicate.
3990 * @return The first iterator @c i in the range @p [first,last)
3991 * such that @p pred(*i) is true, or @p last if no such iterator exists.
3992 */
3993 template<typename _InputIterator, typename _Predicate>
3994 inline _InputIterator
3995 find_if(_InputIterator __first, _InputIterator __last,
3996 _Predicate __pred)
3997 {
3998 // concept requirements
3999 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4000 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4001 typename iterator_traits<_InputIterator>::value_type>)
4002 __glibcxx_requires_valid_range(__first, __last);
4003 return std::__find_if(__first, __last, __pred,
4004 std::__iterator_category(__first));
4005 }
4006
4007 /**
4008 * @brief Find element from a set in a sequence.
4009 * @param first1 Start of range to search.
4010 * @param last1 End of range to search.
4011 * @param first2 Start of match candidates.
4012 * @param last2 End of match candidates.
4013 * @return The first iterator @c i in the range
4014 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4015 * interator in [first2,last2), or @p last1 if no such iterator exists.
4016 *
4017 * Searches the range @p [first1,last1) for an element that is equal to
4018 * some element in the range [first2,last2). If found, returns an iterator
4019 * in the range [first1,last1), otherwise returns @p last1.
4020 */
4021 template<typename _InputIterator, typename _ForwardIterator>
4022 _InputIterator
4023 find_first_of(_InputIterator __first1, _InputIterator __last1,
4024 _ForwardIterator __first2, _ForwardIterator __last2)
4025 {
4026 // concept requirements
4027 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4028 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4029 __glibcxx_function_requires(_EqualOpConcept<
4030 typename iterator_traits<_InputIterator>::value_type,
4031 typename iterator_traits<_ForwardIterator>::value_type>)
4032 __glibcxx_requires_valid_range(__first1, __last1);
4033 __glibcxx_requires_valid_range(__first2, __last2);
4034
4035 for (; __first1 != __last1; ++__first1)
4036 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4037 if (*__first1 == *__iter)
4038 return __first1;
4039 return __last1;
4040 }
4041
4042 /**
4043 * @brief Find element from a set in a sequence using a predicate.
4044 * @param first1 Start of range to search.
4045 * @param last1 End of range to search.
4046 * @param first2 Start of match candidates.
4047 * @param last2 End of match candidates.
4048 * @param comp Predicate to use.
4049 * @return The first iterator @c i in the range
4050 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4051 * interator in [first2,last2), or @p last1 if no such iterator exists.
4052 *
4053
4054 * Searches the range @p [first1,last1) for an element that is
4055 * equal to some element in the range [first2,last2). If found,
4056 * returns an iterator in the range [first1,last1), otherwise
4057 * returns @p last1.
4058 */
4059 template<typename _InputIterator, typename _ForwardIterator,
4060 typename _BinaryPredicate>
4061 _InputIterator
4062 find_first_of(_InputIterator __first1, _InputIterator __last1,
4063 _ForwardIterator __first2, _ForwardIterator __last2,
4064 _BinaryPredicate __comp)
4065 {
4066 // concept requirements
4067 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4068 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4069 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4070 typename iterator_traits<_InputIterator>::value_type,
4071 typename iterator_traits<_ForwardIterator>::value_type>)
4072 __glibcxx_requires_valid_range(__first1, __last1);
4073 __glibcxx_requires_valid_range(__first2, __last2);
4074
4075 for (; __first1 != __last1; ++__first1)
4076 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4077 if (__comp(*__first1, *__iter))
4078 return __first1;
4079 return __last1;
4080 }
4081
4082 /**
4083 * @brief Find two adjacent values in a sequence that are equal.
4084 * @param first A forward iterator.
4085 * @param last A forward iterator.
4086 * @return The first iterator @c i such that @c i and @c i+1 are both
4087 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
4088 * or @p last if no such iterator exists.
4089 */
4090 template<typename _ForwardIterator>
4091 _ForwardIterator
4092 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
4093 {
4094 // concept requirements
4095 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4096 __glibcxx_function_requires(_EqualityComparableConcept<
4097 typename iterator_traits<_ForwardIterator>::value_type>)
4098 __glibcxx_requires_valid_range(__first, __last);
4099 if (__first == __last)
4100 return __last;
4101 _ForwardIterator __next = __first;
4102 while(++__next != __last)
4103 {
4104 if (*__first == *__next)
4105 return __first;
4106 __first = __next;
4107 }
4108 return __last;
4109 }
4110
4111 /**
4112 * @brief Find two adjacent values in a sequence using a predicate.
4113 * @param first A forward iterator.
4114 * @param last A forward iterator.
4115 * @param binary_pred A binary predicate.
4116 * @return The first iterator @c i such that @c i and @c i+1 are both
4117 * valid iterators in @p [first,last) and such that
4118 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
4119 * exists.
4120 */
4121 template<typename _ForwardIterator, typename _BinaryPredicate>
4122 _ForwardIterator
4123 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
4124 _BinaryPredicate __binary_pred)
4125 {
4126 // concept requirements
4127 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4128 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4129 typename iterator_traits<_ForwardIterator>::value_type,
4130 typename iterator_traits<_ForwardIterator>::value_type>)
4131 __glibcxx_requires_valid_range(__first, __last);
4132 if (__first == __last)
4133 return __last;
4134 _ForwardIterator __next = __first;
4135 while(++__next != __last)
4136 {
4137 if (__binary_pred(*__first, *__next))
4138 return __first;
4139 __first = __next;
4140 }
4141 return __last;
4142 }
4143
4144 /**
4145 * @brief Count the number of copies of a value in a sequence.
4146 * @param first An input iterator.
4147 * @param last An input iterator.
4148 * @param value The value to be counted.
4149 * @return The number of iterators @c i in the range @p [first,last)
4150 * for which @c *i == @p value
4151 */
4152 template<typename _InputIterator, typename _Tp>
4153 typename iterator_traits<_InputIterator>::difference_type
4154 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
4155 {
4156 // concept requirements
4157 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4158 __glibcxx_function_requires(_EqualOpConcept<
4159 typename iterator_traits<_InputIterator>::value_type, _Tp>)
4160 __glibcxx_requires_valid_range(__first, __last);
4161 typename iterator_traits<_InputIterator>::difference_type __n = 0;
4162 for (; __first != __last; ++__first)
4163 if (*__first == __value)
4164 ++__n;
4165 return __n;
4166 }
4167
4168 /**
4169 * @brief Count the elements of a sequence for which a predicate is true.
4170 * @param first An input iterator.
4171 * @param last An input iterator.
4172 * @param pred A predicate.
4173 * @return The number of iterators @c i in the range @p [first,last)
4174 * for which @p pred(*i) is true.
4175 */
4176 template<typename _InputIterator, typename _Predicate>
4177 typename iterator_traits<_InputIterator>::difference_type
4178 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
4179 {
4180 // concept requirements
4181 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4182 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4183 typename iterator_traits<_InputIterator>::value_type>)
4184 __glibcxx_requires_valid_range(__first, __last);
4185 typename iterator_traits<_InputIterator>::difference_type __n = 0;
4186 for (; __first != __last; ++__first)
4187 if (__pred(*__first))
4188 ++__n;
4189 return __n;
4190 }
4191
4192 /**
4193 * @brief Search a sequence for a matching sub-sequence.
4194 * @param first1 A forward iterator.
4195 * @param last1 A forward iterator.
4196 * @param first2 A forward iterator.
4197 * @param last2 A forward iterator.
4198 * @return The first iterator @c i in the range
4199 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4200 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4201 * such iterator exists.
4202 *
4203 * Searches the range @p [first1,last1) for a sub-sequence that compares
4204 * equal value-by-value with the sequence given by @p [first2,last2) and
4205 * returns an iterator to the first element of the sub-sequence, or
4206 * @p last1 if the sub-sequence is not found.
4207 *
4208 * Because the sub-sequence must lie completely within the range
4209 * @p [first1,last1) it must start at a position less than
4210 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4211 * sub-sequence.
4212 * This means that the returned iterator @c i will be in the range
4213 * @p [first1,last1-(last2-first2))
4214 */
4215 template<typename _ForwardIterator1, typename _ForwardIterator2>
4216 _ForwardIterator1
4217 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4218 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4219 {
4220 // concept requirements
4221 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4222 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4223 __glibcxx_function_requires(_EqualOpConcept<
4224 typename iterator_traits<_ForwardIterator1>::value_type,
4225 typename iterator_traits<_ForwardIterator2>::value_type>)
4226 __glibcxx_requires_valid_range(__first1, __last1);
4227 __glibcxx_requires_valid_range(__first2, __last2);
4228
4229 // Test for empty ranges
4230 if (__first1 == __last1 || __first2 == __last2)
4231 return __first1;
4232
4233 // Test for a pattern of length 1.
4234 _ForwardIterator2 __p1(__first2);
4235 if (++__p1 == __last2)
4236 return _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
4237
4238 // General case.
4239 _ForwardIterator2 __p;
4240 _ForwardIterator1 __current = __first1;
4241
4242 for (;;)
4243 {
4244 __first1 = _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
4245 if (__first1 == __last1)
4246 return __last1;
4247
4248 __p = __p1;
4249 __current = __first1;
4250 if (++__current == __last1)
4251 return __last1;
4252
4253 while (*__current == *__p)
4254 {
4255 if (++__p == __last2)
4256 return __first1;
4257 if (++__current == __last1)
4258 return __last1;
4259 }
4260 ++__first1;
4261 }
4262 return __first1;
4263 }
4264
4265 /**
4266 * @brief Search a sequence for a matching sub-sequence using a predicate.
4267 * @param first1 A forward iterator.
4268 * @param last1 A forward iterator.
4269 * @param first2 A forward iterator.
4270 * @param last2 A forward iterator.
4271 * @param predicate A binary predicate.
4272 * @return The first iterator @c i in the range
4273 * @p [first1,last1-(last2-first2)) such that
4274 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
4275 * @p [0,last2-first2), or @p last1 if no such iterator exists.
4276 *
4277 * Searches the range @p [first1,last1) for a sub-sequence that compares
4278 * equal value-by-value with the sequence given by @p [first2,last2),
4279 * using @p predicate to determine equality, and returns an iterator
4280 * to the first element of the sub-sequence, or @p last1 if no such
4281 * iterator exists.
4282 *
4283 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
4284 */
4285 template<typename _ForwardIterator1, typename _ForwardIterator2,
4286 typename _BinaryPredicate>
4287 _ForwardIterator1
4288 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4289 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
4290 _BinaryPredicate __predicate)
4291 {
4292 // concept requirements
4293 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4294 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4295 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4296 typename iterator_traits<_ForwardIterator1>::value_type,
4297 typename iterator_traits<_ForwardIterator2>::value_type>)
4298 __glibcxx_requires_valid_range(__first1, __last1);
4299 __glibcxx_requires_valid_range(__first2, __last2);
4300
4301 // Test for empty ranges
4302 if (__first1 == __last1 || __first2 == __last2)
4303 return __first1;
4304
4305 // Test for a pattern of length 1.
4306 _ForwardIterator2 __p1(__first2);
4307 if (++__p1 == __last2)
4308 {
4309 while (__first1 != __last1
4310 && !bool(__predicate(*__first1, *__first2)))
4311 ++__first1;
4312 return __first1;
4313 }
4314
4315 // General case.
4316 _ForwardIterator2 __p;
4317 _ForwardIterator1 __current = __first1;
4318
4319 for (;;)
4320 {
4321 while (__first1 != __last1
4322 && !bool(__predicate(*__first1, *__first2)))
4323 ++__first1;
4324 if (__first1 == __last1)
4325 return __last1;
4326
4327 __p = __p1;
4328 __current = __first1;
4329 if (++__current == __last1)
4330 return __last1;
4331
4332 while (__predicate(*__current, *__p))
4333 {
4334 if (++__p == __last2)
4335 return __first1;
4336 if (++__current == __last1)
4337 return __last1;
4338 }
4339 ++__first1;
4340 }
4341 return __first1;
4342 }
4343
4344
4345 /**
4346 * @brief Search a sequence for a number of consecutive values.
4347 * @param first A forward iterator.
4348 * @param last A forward iterator.
4349 * @param count The number of consecutive values.
4350 * @param val The value to find.
4351 * @return The first iterator @c i in the range @p [first,last-count)
4352 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
4353 * or @p last if no such iterator exists.
4354 *
4355 * Searches the range @p [first,last) for @p count consecutive elements
4356 * equal to @p val.
4357 */
4358 template<typename _ForwardIterator, typename _Integer, typename _Tp>
4359 _ForwardIterator
4360 search_n(_ForwardIterator __first, _ForwardIterator __last,
4361 _Integer __count, const _Tp& __val)
4362 {
4363 // concept requirements
4364 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4365 __glibcxx_function_requires(_EqualOpConcept<
4366 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4367 __glibcxx_requires_valid_range(__first, __last);
4368
4369 if (__count <= 0)
4370 return __first;
4371 if (__count == 1)
4372 return _GLIBCXX_STD_P::find(__first, __last, __val);
4373 return std::__search_n(__first, __last, __count, __val,
4374 std::__iterator_category(__first));
4375 }
4376
4377
4378 /**
4379 * @brief Search a sequence for a number of consecutive values using a
4380 * predicate.
4381 * @param first A forward iterator.
4382 * @param last A forward iterator.
4383 * @param count The number of consecutive values.
4384 * @param val The value to find.
4385 * @param binary_pred A binary predicate.
4386 * @return The first iterator @c i in the range @p [first,last-count)
4387 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
4388 * range @p [0,count), or @p last if no such iterator exists.
4389 *
4390 * Searches the range @p [first,last) for @p count consecutive elements
4391 * for which the predicate returns true.
4392 */
4393 template<typename _ForwardIterator, typename _Integer, typename _Tp,
4394 typename _BinaryPredicate>
4395 _ForwardIterator
4396 search_n(_ForwardIterator __first, _ForwardIterator __last,
4397 _Integer __count, const _Tp& __val,
4398 _BinaryPredicate __binary_pred)
4399 {
4400 // concept requirements
4401 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4402 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4403 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4404 __glibcxx_requires_valid_range(__first, __last);
4405
4406 if (__count <= 0)
4407 return __first;
4408 if (__count == 1)
4409 {
4410 while (__first != __last && !bool(__binary_pred(*__first, __val)))
4411 ++__first;
4412 return __first;
4413 }
4414 return std::__search_n(__first, __last, __count, __val, __binary_pred,
4415 std::__iterator_category(__first));
4416 }
4417
4418
4419 /**
4420 * @brief Perform an operation on a sequence.
4421 * @param first An input iterator.
4422 * @param last An input iterator.
4423 * @param result An output iterator.
4424 * @param unary_op A unary operator.
4425 * @return An output iterator equal to @p result+(last-first).
4426 *
4427 * Applies the operator to each element in the input range and assigns
4428 * the results to successive elements of the output sequence.
4429 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
4430 * range @p [0,last-first).
4431 *
4432 * @p unary_op must not alter its argument.
4433 */
4434 template<typename _InputIterator, typename _OutputIterator,
4435 typename _UnaryOperation>
4436 _OutputIterator
4437 transform(_InputIterator __first, _InputIterator __last,
4438 _OutputIterator __result, _UnaryOperation __unary_op)
4439 {
4440 // concept requirements
4441 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4442 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4443 // "the type returned by a _UnaryOperation"
4444 __typeof__(__unary_op(*__first))>)
4445 __glibcxx_requires_valid_range(__first, __last);
4446
4447 for (; __first != __last; ++__first, ++__result)
4448 *__result = __unary_op(*__first);
4449 return __result;
4450 }
4451
4452 /**
4453 * @brief Perform an operation on corresponding elements of two sequences.
4454 * @param first1 An input iterator.
4455 * @param last1 An input iterator.
4456 * @param first2 An input iterator.
4457 * @param result An output iterator.
4458 * @param binary_op A binary operator.
4459 * @return An output iterator equal to @p result+(last-first).
4460 *
4461 * Applies the operator to the corresponding elements in the two
4462 * input ranges and assigns the results to successive elements of the
4463 * output sequence.
4464 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
4465 * @c N in the range @p [0,last1-first1).
4466 *
4467 * @p binary_op must not alter either of its arguments.
4468 */
4469 template<typename _InputIterator1, typename _InputIterator2,
4470 typename _OutputIterator, typename _BinaryOperation>
4471 _OutputIterator
4472 transform(_InputIterator1 __first1, _InputIterator1 __last1,
4473 _InputIterator2 __first2, _OutputIterator __result,
4474 _BinaryOperation __binary_op)
4475 {
4476 // concept requirements
4477 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4478 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4479 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4480 // "the type returned by a _BinaryOperation"
4481 __typeof__(__binary_op(*__first1,*__first2))>)
4482 __glibcxx_requires_valid_range(__first1, __last1);
4483
4484 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
4485 *__result = __binary_op(*__first1, *__first2);
4486 return __result;
4487 }
4488
4489 /**
4490 * @brief Replace each occurrence of one value in a sequence with another
4491 * value.
4492 * @param first A forward iterator.
4493 * @param last A forward iterator.
4494 * @param old_value The value to be replaced.
4495 * @param new_value The replacement value.
4496 * @return replace() returns no value.
4497 *
4498 * For each iterator @c i in the range @p [first,last) if @c *i ==
4499 * @p old_value then the assignment @c *i = @p new_value is performed.
4500 */
4501 template<typename _ForwardIterator, typename _Tp>
4502 void
4503 replace(_ForwardIterator __first, _ForwardIterator __last,
4504 const _Tp& __old_value, const _Tp& __new_value)
4505 {
4506 // concept requirements
4507 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4508 _ForwardIterator>)
4509 __glibcxx_function_requires(_EqualOpConcept<
4510 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
4511 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4512 typename iterator_traits<_ForwardIterator>::value_type>)
4513 __glibcxx_requires_valid_range(__first, __last);
4514
4515 for (; __first != __last; ++__first)
4516 if (*__first == __old_value)
4517 *__first = __new_value;
4518 }
4519
4520 /**
4521 * @brief Replace each value in a sequence for which a predicate returns
4522 * true with another value.
4523 * @param first A forward iterator.
4524 * @param last A forward iterator.
4525 * @param pred A predicate.
4526 * @param new_value The replacement value.
4527 * @return replace_if() returns no value.
4528 *
4529 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
4530 * is true then the assignment @c *i = @p new_value is performed.
4531 */
4532 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4533 void
4534 replace_if(_ForwardIterator __first, _ForwardIterator __last,
4535 _Predicate __pred, const _Tp& __new_value)
4536 {
4537 // concept requirements
4538 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4539 _ForwardIterator>)
4540 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4541 typename iterator_traits<_ForwardIterator>::value_type>)
4542 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4543 typename iterator_traits<_ForwardIterator>::value_type>)
4544 __glibcxx_requires_valid_range(__first, __last);
4545
4546 for (; __first != __last; ++__first)
4547 if (__pred(*__first))
4548 *__first = __new_value;
4549 }
4550
4551 /**
4552 * @brief Assign the result of a function object to each value in a
4553 * sequence.
4554 * @param first A forward iterator.
4555 * @param last A forward iterator.
4556 * @param gen A function object taking no arguments and returning
4557 * std::iterator_traits<_ForwardIterator>::value_type
4558 * @return generate() returns no value.
4559 *
4560 * Performs the assignment @c *i = @p gen() for each @c i in the range
4561 * @p [first,last).
4562 */
4563 template<typename _ForwardIterator, typename _Generator>
4564 void
4565 generate(_ForwardIterator __first, _ForwardIterator __last,
4566 _Generator __gen)
4567 {
4568 // concept requirements
4569 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4570 __glibcxx_function_requires(_GeneratorConcept<_Generator,
4571 typename iterator_traits<_ForwardIterator>::value_type>)
4572 __glibcxx_requires_valid_range(__first, __last);
4573
4574 for (; __first != __last; ++__first)
4575 *__first = __gen();
4576 }
4577
4578 /**
4579 * @brief Assign the result of a function object to each value in a
4580 * sequence.
4581 * @param first A forward iterator.
4582 * @param n The length of the sequence.
4583 * @param gen A function object taking no arguments and returning
4584 * std::iterator_traits<_ForwardIterator>::value_type
4585 * @return The end of the sequence, @p first+n
4586 *
4587 * Performs the assignment @c *i = @p gen() for each @c i in the range
4588 * @p [first,first+n).
4589 */
4590 template<typename _OutputIterator, typename _Size, typename _Generator>
4591 _OutputIterator
4592 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
4593 {
4594 // concept requirements
4595 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4596 // "the type returned by a _Generator"
4597 __typeof__(__gen())>)
4598
4599 for (; __n > 0; --__n, ++__first)
4600 *__first = __gen();
4601 return __first;
4602 }
4603
4604
4605 /**
4606 * @brief Copy a sequence, removing consecutive duplicate values.
4607 * @param first An input iterator.
4608 * @param last An input iterator.
4609 * @param result An output iterator.
4610 * @return An iterator designating the end of the resulting sequence.
4611 *
4612 * Copies each element in the range @p [first,last) to the range
4613 * beginning at @p result, except that only the first element is copied
4614 * from groups of consecutive elements that compare equal.
4615 * unique_copy() is stable, so the relative order of elements that are
4616 * copied is unchanged.
4617 *
4618 * @if maint
4619 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4620 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4621 *
4622 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4623 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
4624 * Assignable?
4625 * @endif
4626 */
4627 template<typename _InputIterator, typename _OutputIterator>
4628 inline _OutputIterator
4629 unique_copy(_InputIterator __first, _InputIterator __last,
4630 _OutputIterator __result)
4631 {
4632 // concept requirements
4633 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4634 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4635 typename iterator_traits<_InputIterator>::value_type>)
4636 __glibcxx_function_requires(_EqualityComparableConcept<
4637 typename iterator_traits<_InputIterator>::value_type>)
4638 __glibcxx_requires_valid_range(__first, __last);
4639
4640 if (__first == __last)
4641 return __result;
4642 return std::__unique_copy(__first, __last, __result,
4643 std::__iterator_category(__first),
4644 std::__iterator_category(__result));
4645 }
4646
4647 /**
4648 * @brief Copy a sequence, removing consecutive values using a predicate.
4649 * @param first An input iterator.
4650 * @param last An input iterator.
4651 * @param result An output iterator.
4652 * @param binary_pred A binary predicate.
4653 * @return An iterator designating the end of the resulting sequence.
4654 *
4655 * Copies each element in the range @p [first,last) to the range
4656 * beginning at @p result, except that only the first element is copied
4657 * from groups of consecutive elements for which @p binary_pred returns
4658 * true.
4659 * unique_copy() is stable, so the relative order of elements that are
4660 * copied is unchanged.
4661 *
4662 * @if maint
4663 * _GLIBCXX_RESOLVE_LIB_DEFECTS
4664 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
4665 * @endif
4666 */
4667 template<typename _InputIterator, typename _OutputIterator,
4668 typename _BinaryPredicate>
4669 inline _OutputIterator
4670 unique_copy(_InputIterator __first, _InputIterator __last,
4671 _OutputIterator __result,
4672 _BinaryPredicate __binary_pred)
4673 {
4674 // concept requirements -- predicates checked later
4675 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4676 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4677 typename iterator_traits<_InputIterator>::value_type>)
4678 __glibcxx_requires_valid_range(__first, __last);
4679
4680 if (__first == __last)
4681 return __result;
4682 return std::__unique_copy(__first, __last, __result, __binary_pred,
4683 std::__iterator_category(__first),
4684 std::__iterator_category(__result));
4685 }
4686
4687
4688 /**
4689 * @brief Randomly shuffle the elements of a sequence.
4690 * @param first A forward iterator.
4691 * @param last A forward iterator.
4692 * @return Nothing.
4693 *
4694 * Reorder the elements in the range @p [first,last) using a random
4695 * distribution, so that every possible ordering of the sequence is
4696 * equally likely.
4697 */
4698 template<typename _RandomAccessIterator>
4699 inline void
4700 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
4701 {
4702 // concept requirements
4703 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4704 _RandomAccessIterator>)
4705 __glibcxx_requires_valid_range(__first, __last);
4706
4707 if (__first != __last)
4708 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4709 std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
4710 }
4711
4712 /**
4713 * @brief Shuffle the elements of a sequence using a random number
4714 * generator.
4715 * @param first A forward iterator.
4716 * @param last A forward iterator.
4717 * @param rand The RNG functor or function.
4718 * @return Nothing.
4719 *
4720 * Reorders the elements in the range @p [first,last) using @p rand to
4721 * provide a random distribution. Calling @p rand(N) for a positive
4722 * integer @p N should return a randomly chosen integer from the
4723 * range [0,N).
4724 */
4725 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
4726 void
4727 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4728 _RandomNumberGenerator& __rand)
4729 {
4730 // concept requirements
4731 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4732 _RandomAccessIterator>)
4733 __glibcxx_requires_valid_range(__first, __last);
4734
4735 if (__first == __last)
4736 return;
4737 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4738 std::iter_swap(__i, __first + __rand((__i - __first) + 1));
4739 }
4740
4741
4742 /**
4743 * @brief Move elements for which a predicate is true to the beginning
4744 * of a sequence.
4745 * @param first A forward iterator.
4746 * @param last A forward iterator.
4747 * @param pred A predicate functor.
4748 * @return An iterator @p middle such that @p pred(i) is true for each
4749 * iterator @p i in the range @p [first,middle) and false for each @p i
4750 * in the range @p [middle,last).
4751 *
4752 * @p pred must not modify its operand. @p partition() does not preserve
4753 * the relative ordering of elements in each group, use
4754 * @p stable_partition() if this is needed.
4755 */
4756 template<typename _ForwardIterator, typename _Predicate>
4757 inline _ForwardIterator
4758 partition(_ForwardIterator __first, _ForwardIterator __last,
4759 _Predicate __pred)
4760 {
4761 // concept requirements
4762 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4763 _ForwardIterator>)
4764 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4765 typename iterator_traits<_ForwardIterator>::value_type>)
4766 __glibcxx_requires_valid_range(__first, __last);
4767
4768 return std::__partition(__first, __last, __pred,
4769 std::__iterator_category(__first));
4770 }
4771
4772
4773
4774 /**
4775 * @brief Sort the smallest elements of a sequence.
4776 * @param first An iterator.
4777 * @param middle Another iterator.
4778 * @param last Another iterator.
4779 * @return Nothing.
4780 *
4781 * Sorts the smallest @p (middle-first) elements in the range
4782 * @p [first,last) and moves them to the range @p [first,middle). The
4783 * order of the remaining elements in the range @p [middle,last) is
4784 * undefined.
4785 * After the sort if @p i and @j are iterators in the range
4786 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4787 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
4788 */
4789 template<typename _RandomAccessIterator>
4790 inline void
4791 partial_sort(_RandomAccessIterator __first,
4792 _RandomAccessIterator __middle,
4793 _RandomAccessIterator __last)
4794 {
4795 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4796 _ValueType;
4797
4798 // concept requirements
4799 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4800 _RandomAccessIterator>)
4801 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
4802 __glibcxx_requires_valid_range(__first, __middle);
4803 __glibcxx_requires_valid_range(__middle, __last);
4804
4805 std::__heap_select(__first, __middle, __last);
4806 std::sort_heap(__first, __middle);
4807 }
4808
4809 /**
4810 * @brief Sort the smallest elements of a sequence using a predicate
4811 * for comparison.
4812 * @param first An iterator.
4813 * @param middle Another iterator.
4814 * @param last Another iterator.
4815 * @param comp A comparison functor.
4816 * @return Nothing.
4817 *
4818 * Sorts the smallest @p (middle-first) elements in the range
4819 * @p [first,last) and moves them to the range @p [first,middle). The
4820 * order of the remaining elements in the range @p [middle,last) is
4821 * undefined.
4822 * After the sort if @p i and @j are iterators in the range
4823 * @p [first,middle) such that @i precedes @j and @k is an iterator in
4824 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
4825 * are both false.
4826 */
4827 template<typename _RandomAccessIterator, typename _Compare>
4828 inline void
4829 partial_sort(_RandomAccessIterator __first,
4830 _RandomAccessIterator __middle,
4831 _RandomAccessIterator __last,
4832 _Compare __comp)
4833 {
4834 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4835 _ValueType;
4836
4837 // concept requirements
4838 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4839 _RandomAccessIterator>)
4840 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4841 _ValueType, _ValueType>)
4842 __glibcxx_requires_valid_range(__first, __middle);
4843 __glibcxx_requires_valid_range(__middle, __last);
4844
4845 std::__heap_select(__first, __middle, __last, __comp);
4846 std::sort_heap(__first, __middle, __comp);
4847 }
4848
4849 /**
4850 * @brief Sort a sequence just enough to find a particular position.
4851 * @param first An iterator.
4852 * @param nth Another iterator.
4853 * @param last Another iterator.
4854 * @return Nothing.
4855 *
4856 * Rearranges the elements in the range @p [first,last) so that @p *nth
4857 * is the same element that would have been in that position had the
4858 * whole sequence been sorted.
4859 * whole sequence been sorted. The elements either side of @p *nth are
4860 * not completely sorted, but for any iterator @i in the range
4861 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4862 * holds that @p *j<*i is false.
4863 */
4864 template<typename _RandomAccessIterator>
4865 inline void
4866 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4867 _RandomAccessIterator __last)
4868 {
4869 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4870 _ValueType;
4871
4872 // concept requirements
4873 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4874 _RandomAccessIterator>)
4875 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
4876 __glibcxx_requires_valid_range(__first, __nth);
4877 __glibcxx_requires_valid_range(__nth, __last);
4878
4879 if (__first == __last || __nth == __last)
4880 return;
4881
4882 std::__introselect(__first, __nth, __last,
4883 std::__lg(__last - __first) * 2);
4884 }
4885
4886 /**
4887 * @brief Sort a sequence just enough to find a particular position
4888 * using a predicate for comparison.
4889 * @param first An iterator.
4890 * @param nth Another iterator.
4891 * @param last Another iterator.
4892 * @param comp A comparison functor.
4893 * @return Nothing.
4894 *
4895 * Rearranges the elements in the range @p [first,last) so that @p *nth
4896 * is the same element that would have been in that position had the
4897 * whole sequence been sorted. The elements either side of @p *nth are
4898 * not completely sorted, but for any iterator @i in the range
4899 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4900 * holds that @p comp(*j,*i) is false.
4901 */
4902 template<typename _RandomAccessIterator, typename _Compare>
4903 inline void
4904 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4905 _RandomAccessIterator __last, _Compare __comp)
4906 {
4907 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4908 _ValueType;
4909
4910 // concept requirements
4911 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4912 _RandomAccessIterator>)
4913 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4914 _ValueType, _ValueType>)
4915 __glibcxx_requires_valid_range(__first, __nth);
4916 __glibcxx_requires_valid_range(__nth, __last);
4917
4918 if (__first == __last || __nth == __last)
4919 return;
4920
4921 std::__introselect(__first, __nth, __last,
4922 std::__lg(__last - __first) * 2, __comp);
4923 }
4924
4925
4926 /**
4927 * @brief Sort the elements of a sequence.
4928 * @param first An iterator.
4929 * @param last Another iterator.
4930 * @return Nothing.
4931 *
4932 * Sorts the elements in the range @p [first,last) in ascending order,
4933 * such that @p *(i+1)<*i is false for each iterator @p i in the range
4934 * @p [first,last-1).
4935 *
4936 * The relative ordering of equivalent elements is not preserved, use
4937 * @p stable_sort() if this is needed.
4938 */
4939 template<typename _RandomAccessIterator>
4940 inline void
4941 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4942 {
4943 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4944 _ValueType;
4945
4946 // concept requirements
4947 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4948 _RandomAccessIterator>)
4949 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
4950 __glibcxx_requires_valid_range(__first, __last);
4951
4952 if (__first != __last)
4953 {
4954 std::__introsort_loop(__first, __last,
4955 std::__lg(__last - __first) * 2);
4956 std::__final_insertion_sort(__first, __last);
4957 }
4958 }
4959
4960 /**
4961 * @brief Sort the elements of a sequence using a predicate for comparison.
4962 * @param first An iterator.
4963 * @param last Another iterator.
4964 * @param comp A comparison functor.
4965 * @return Nothing.
4966 *
4967 * Sorts the elements in the range @p [first,last) in ascending order,
4968 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
4969 * range @p [first,last-1).
4970 *
4971 * The relative ordering of equivalent elements is not preserved, use
4972 * @p stable_sort() if this is needed.
4973 */
4974 template<typename _RandomAccessIterator, typename _Compare>
4975 inline void
4976 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4977 _Compare __comp)
4978 {
4979 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4980 _ValueType;
4981
4982 // concept requirements
4983 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4984 _RandomAccessIterator>)
4985 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
4986 _ValueType>)
4987 __glibcxx_requires_valid_range(__first, __last);
4988
4989 if (__first != __last)
4990 {
4991 std::__introsort_loop(__first, __last,
4992 std::__lg(__last - __first) * 2, __comp);
4993 std::__final_insertion_sort(__first, __last, __comp);
4994 }
4995 }
4996
4997 /**
4998 * @brief Merges two sorted ranges.
4999 * @param first1 An iterator.
5000 * @param first2 Another iterator.
5001 * @param last1 Another iterator.
5002 * @param last2 Another iterator.
5003 * @param result An iterator pointing to the end of the merged range.
5004 * @return An iterator pointing to the first element "not less
5005 * than" @a val.
5006 *
5007 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5008 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5009 * must be sorted, and the output range must not overlap with either of
5010 * the input ranges. The sort is @e stable, that is, for equivalent
5011 * elements in the two ranges, elements from the first range will always
5012 * come before elements from the second.
5013 */
5014 template<typename _InputIterator1, typename _InputIterator2,
5015 typename _OutputIterator>
5016 _OutputIterator
5017 merge(_InputIterator1 __first1, _InputIterator1 __last1,
5018 _InputIterator2 __first2, _InputIterator2 __last2,
5019 _OutputIterator __result)
5020 {
5021 typedef typename iterator_traits<_InputIterator1>::value_type
5022 _ValueType1;
5023 typedef typename iterator_traits<_InputIterator2>::value_type
5024 _ValueType2;
5025
5026 // concept requirements
5027 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5028 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5029 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5030 _ValueType1>)
5031 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5032 _ValueType2>)
5033 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5034 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5035 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5036
5037 while (__first1 != __last1 && __first2 != __last2)
5038 {
5039 if (*__first2 < *__first1)
5040 {
5041 *__result = *__first2;
5042 ++__first2;
5043 }
5044 else
5045 {
5046 *__result = *__first1;
5047 ++__first1;
5048 }
5049 ++__result;
5050 }
5051 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5052 __result));
5053 }
5054
5055 /**
5056 * @brief Merges two sorted ranges.
5057 * @param first1 An iterator.
5058 * @param first2 Another iterator.
5059 * @param last1 Another iterator.
5060 * @param last2 Another iterator.
5061 * @param result An iterator pointing to the end of the merged range.
5062 * @param comp A functor to use for comparisons.
5063 * @return An iterator pointing to the first element "not less
5064 * than" @a val.
5065 *
5066 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
5067 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
5068 * must be sorted, and the output range must not overlap with either of
5069 * the input ranges. The sort is @e stable, that is, for equivalent
5070 * elements in the two ranges, elements from the first range will always
5071 * come before elements from the second.
5072 *
5073 * The comparison function should have the same effects on ordering as
5074 * the function used for the initial sort.
5075 */
5076 template<typename _InputIterator1, typename _InputIterator2,
5077 typename _OutputIterator, typename _Compare>
5078 _OutputIterator
5079 merge(_InputIterator1 __first1, _InputIterator1 __last1,
5080 _InputIterator2 __first2, _InputIterator2 __last2,
5081 _OutputIterator __result, _Compare __comp)
5082 {
5083 typedef typename iterator_traits<_InputIterator1>::value_type
5084 _ValueType1;
5085 typedef typename iterator_traits<_InputIterator2>::value_type
5086 _ValueType2;
5087
5088 // concept requirements
5089 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5090 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5091 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5092 _ValueType1>)
5093 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5094 _ValueType2>)
5095 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5096 _ValueType2, _ValueType1>)
5097 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5098 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5099
5100 while (__first1 != __last1 && __first2 != __last2)
5101 {
5102 if (__comp(*__first2, *__first1))
5103 {
5104 *__result = *__first2;
5105 ++__first2;
5106 }
5107 else
5108 {
5109 *__result = *__first1;
5110 ++__first1;
5111 }
5112 ++__result;
5113 }
5114 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5115 __result));
5116 }
5117
5118
5119 /**
5120 * @brief Sort the elements of a sequence, preserving the relative order
5121 * of equivalent elements.
5122 * @param first An iterator.
5123 * @param last Another iterator.
5124 * @return Nothing.
5125 *
5126 * Sorts the elements in the range @p [first,last) in ascending order,
5127 * such that @p *(i+1)<*i is false for each iterator @p i in the range
5128 * @p [first,last-1).
5129 *
5130 * The relative ordering of equivalent elements is preserved, so any two
5131 * elements @p x and @p y in the range @p [first,last) such that
5132 * @p x<y is false and @p y<x is false will have the same relative
5133 * ordering after calling @p stable_sort().
5134 */
5135 template<typename _RandomAccessIterator>
5136 inline void
5137 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5138 {
5139 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5140 _ValueType;
5141 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5142 _DistanceType;
5143
5144 // concept requirements
5145 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5146 _RandomAccessIterator>)
5147 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
5148 __glibcxx_requires_valid_range(__first, __last);
5149
5150 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5151 __last);
5152 if (__buf.begin() == 0)
5153 std::__inplace_stable_sort(__first, __last);
5154 else
5155 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5156 _DistanceType(__buf.size()));
5157 }
5158
5159 /**
5160 * @brief Sort the elements of a sequence using a predicate for comparison,
5161 * preserving the relative order of equivalent elements.
5162 * @param first An iterator.
5163 * @param last Another iterator.
5164 * @param comp A comparison functor.
5165 * @return Nothing.
5166 *
5167 * Sorts the elements in the range @p [first,last) in ascending order,
5168 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
5169 * range @p [first,last-1).
5170 *
5171 * The relative ordering of equivalent elements is preserved, so any two
5172 * elements @p x and @p y in the range @p [first,last) such that
5173 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
5174 * relative ordering after calling @p stable_sort().
5175 */
5176 template<typename _RandomAccessIterator, typename _Compare>
5177 inline void
5178 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5179 _Compare __comp)
5180 {
5181 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5182 _ValueType;
5183 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5184 _DistanceType;
5185
5186 // concept requirements
5187 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5188 _RandomAccessIterator>)
5189 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5190 _ValueType,
5191 _ValueType>)
5192 __glibcxx_requires_valid_range(__first, __last);
5193
5194 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
5195 __last);
5196 if (__buf.begin() == 0)
5197 std::__inplace_stable_sort(__first, __last, __comp);
5198 else
5199 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
5200 _DistanceType(__buf.size()), __comp);
5201 }
5202
5203
5204 /**
5205 * @brief Return the union of two sorted ranges.
5206 * @param first1 Start of first range.
5207 * @param last1 End of first range.
5208 * @param first2 Start of second range.
5209 * @param last2 End of second range.
5210 * @return End of the output range.
5211 * @ingroup setoperations
5212 *
5213 * This operation iterates over both ranges, copying elements present in
5214 * each range in order to the output range. Iterators increment for each
5215 * range. When the current element of one range is less than the other,
5216 * that element is copied and the iterator advanced. If an element is
5217 * contained in both ranges, the element from the first range is copied and
5218 * both ranges advance. The output range may not overlap either input
5219 * range.
5220 */
5221 template<typename _InputIterator1, typename _InputIterator2,
5222 typename _OutputIterator>
5223 _OutputIterator
5224 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5225 _InputIterator2 __first2, _InputIterator2 __last2,
5226 _OutputIterator __result)
5227 {
5228 typedef typename iterator_traits<_InputIterator1>::value_type
5229 _ValueType1;
5230 typedef typename iterator_traits<_InputIterator2>::value_type
5231 _ValueType2;
5232
5233 // concept requirements
5234 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5235 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5236 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5237 _ValueType1>)
5238 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5239 _ValueType2>)
5240 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5241 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5242 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5243 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5244
5245 while (__first1 != __last1 && __first2 != __last2)
5246 {
5247 if (*__first1 < *__first2)
5248 {
5249 *__result = *__first1;
5250 ++__first1;
5251 }
5252 else if (*__first2 < *__first1)
5253 {
5254 *__result = *__first2;
5255 ++__first2;
5256 }
5257 else
5258 {
5259 *__result = *__first1;
5260 ++__first1;
5261 ++__first2;
5262 }
5263 ++__result;
5264 }
5265 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5266 __result));
5267 }
5268
5269 /**
5270 * @brief Return the union of two sorted ranges using a comparison functor.
5271 * @param first1 Start of first range.
5272 * @param last1 End of first range.
5273 * @param first2 Start of second range.
5274 * @param last2 End of second range.
5275 * @param comp The comparison functor.
5276 * @return End of the output range.
5277 * @ingroup setoperations
5278 *
5279 * This operation iterates over both ranges, copying elements present in
5280 * each range in order to the output range. Iterators increment for each
5281 * range. When the current element of one range is less than the other
5282 * according to @a comp, that element is copied and the iterator advanced.
5283 * If an equivalent element according to @a comp is contained in both
5284 * ranges, the element from the first range is copied and both ranges
5285 * advance. The output range may not overlap either input range.
5286 */
5287 template<typename _InputIterator1, typename _InputIterator2,
5288 typename _OutputIterator, typename _Compare>
5289 _OutputIterator
5290 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5291 _InputIterator2 __first2, _InputIterator2 __last2,
5292 _OutputIterator __result, _Compare __comp)
5293 {
5294 typedef typename iterator_traits<_InputIterator1>::value_type
5295 _ValueType1;
5296 typedef typename iterator_traits<_InputIterator2>::value_type
5297 _ValueType2;
5298
5299 // concept requirements
5300 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5301 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5302 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5303 _ValueType1>)
5304 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5305 _ValueType2>)
5306 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5307 _ValueType1, _ValueType2>)
5308 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5309 _ValueType2, _ValueType1>)
5310 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5311 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5312
5313 while (__first1 != __last1 && __first2 != __last2)
5314 {
5315 if (__comp(*__first1, *__first2))
5316 {
5317 *__result = *__first1;
5318 ++__first1;
5319 }
5320 else if (__comp(*__first2, *__first1))
5321 {
5322 *__result = *__first2;
5323 ++__first2;
5324 }
5325 else
5326 {
5327 *__result = *__first1;
5328 ++__first1;
5329 ++__first2;
5330 }
5331 ++__result;
5332 }
5333 return std::copy(__first2, __last2, std::copy(__first1, __last1,
5334 __result));
5335 }
5336
5337 /**
5338 * @brief Return the intersection of two sorted ranges.
5339 * @param first1 Start of first range.
5340 * @param last1 End of first range.
5341 * @param first2 Start of second range.
5342 * @param last2 End of second range.
5343 * @return End of the output range.
5344 * @ingroup setoperations
5345 *
5346 * This operation iterates over both ranges, copying elements present in
5347 * both ranges in order to the output range. Iterators increment for each
5348 * range. When the current element of one range is less than the other,
5349 * that iterator advances. If an element is contained in both ranges, the
5350 * element from the first range is copied and both ranges advance. The
5351 * output range may not overlap either input range.
5352 */
5353 template<typename _InputIterator1, typename _InputIterator2,
5354 typename _OutputIterator>
5355 _OutputIterator
5356 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5357 _InputIterator2 __first2, _InputIterator2 __last2,
5358 _OutputIterator __result)
5359 {
5360 typedef typename iterator_traits<_InputIterator1>::value_type
5361 _ValueType1;
5362 typedef typename iterator_traits<_InputIterator2>::value_type
5363 _ValueType2;
5364
5365 // concept requirements
5366 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5367 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5368 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5369 _ValueType1>)
5370 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5371 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5372 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5373 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5374
5375 while (__first1 != __last1 && __first2 != __last2)
5376 if (*__first1 < *__first2)
5377 ++__first1;
5378 else if (*__first2 < *__first1)
5379 ++__first2;
5380 else
5381 {
5382 *__result = *__first1;
5383 ++__first1;
5384 ++__first2;
5385 ++__result;
5386 }
5387 return __result;
5388 }
5389
5390 /**
5391 * @brief Return the intersection of two sorted ranges using comparison
5392 * functor.
5393 * @param first1 Start of first range.
5394 * @param last1 End of first range.
5395 * @param first2 Start of second range.
5396 * @param last2 End of second range.
5397 * @param comp The comparison functor.
5398 * @return End of the output range.
5399 * @ingroup setoperations
5400 *
5401 * This operation iterates over both ranges, copying elements present in
5402 * both ranges in order to the output range. Iterators increment for each
5403 * range. When the current element of one range is less than the other
5404 * according to @a comp, that iterator advances. If an element is
5405 * contained in both ranges according to @a comp, the element from the
5406 * first range is copied and both ranges advance. The output range may not
5407 * overlap either input range.
5408 */
5409 template<typename _InputIterator1, typename _InputIterator2,
5410 typename _OutputIterator, typename _Compare>
5411 _OutputIterator
5412 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5413 _InputIterator2 __first2, _InputIterator2 __last2,
5414 _OutputIterator __result, _Compare __comp)
5415 {
5416 typedef typename iterator_traits<_InputIterator1>::value_type
5417 _ValueType1;
5418 typedef typename iterator_traits<_InputIterator2>::value_type
5419 _ValueType2;
5420
5421 // concept requirements
5422 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5423 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5424 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5425 _ValueType1>)
5426 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5427 _ValueType1, _ValueType2>)
5428 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5429 _ValueType2, _ValueType1>)
5430 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5431 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5432
5433 while (__first1 != __last1 && __first2 != __last2)
5434 if (__comp(*__first1, *__first2))
5435 ++__first1;
5436 else if (__comp(*__first2, *__first1))
5437 ++__first2;
5438 else
5439 {
5440 *__result = *__first1;
5441 ++__first1;
5442 ++__first2;
5443 ++__result;
5444 }
5445 return __result;
5446 }
5447
5448 /**
5449 * @brief Return the difference of two sorted ranges.
5450 * @param first1 Start of first range.
5451 * @param last1 End of first range.
5452 * @param first2 Start of second range.
5453 * @param last2 End of second range.
5454 * @return End of the output range.
5455 * @ingroup setoperations
5456 *
5457 * This operation iterates over both ranges, copying elements present in
5458 * the first range but not the second in order to the output range.
5459 * Iterators increment for each range. When the current element of the
5460 * first range is less than the second, that element is copied and the
5461 * iterator advances. If the current element of the second range is less,
5462 * the iterator advances, but no element is copied. If an element is
5463 * contained in both ranges, no elements are copied and both ranges
5464 * advance. The output range may not overlap either input range.
5465 */
5466 template<typename _InputIterator1, typename _InputIterator2,
5467 typename _OutputIterator>
5468 _OutputIterator
5469 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5470 _InputIterator2 __first2, _InputIterator2 __last2,
5471 _OutputIterator __result)
5472 {
5473 typedef typename iterator_traits<_InputIterator1>::value_type
5474 _ValueType1;
5475 typedef typename iterator_traits<_InputIterator2>::value_type
5476 _ValueType2;
5477
5478 // concept requirements
5479 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5480 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5481 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5482 _ValueType1>)
5483 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5484 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5485 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5486 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5487
5488 while (__first1 != __last1 && __first2 != __last2)
5489 if (*__first1 < *__first2)
5490 {
5491 *__result = *__first1;
5492 ++__first1;
5493 ++__result;
5494 }
5495 else if (*__first2 < *__first1)
5496 ++__first2;
5497 else
5498 {
5499 ++__first1;
5500 ++__first2;
5501 }
5502 return std::copy(__first1, __last1, __result);
5503 }
5504
5505 /**
5506 * @brief Return the difference of two sorted ranges using comparison
5507 * functor.
5508 * @param first1 Start of first range.
5509 * @param last1 End of first range.
5510 * @param first2 Start of second range.
5511 * @param last2 End of second range.
5512 * @param comp The comparison functor.
5513 * @return End of the output range.
5514 * @ingroup setoperations
5515 *
5516 * This operation iterates over both ranges, copying elements present in
5517 * the first range but not the second in order to the output range.
5518 * Iterators increment for each range. When the current element of the
5519 * first range is less than the second according to @a comp, that element
5520 * is copied and the iterator advances. If the current element of the
5521 * second range is less, no element is copied and the iterator advances.
5522 * If an element is contained in both ranges according to @a comp, no
5523 * elements are copied and both ranges advance. The output range may not
5524 * overlap either input range.
5525 */
5526 template<typename _InputIterator1, typename _InputIterator2,
5527 typename _OutputIterator, typename _Compare>
5528 _OutputIterator
5529 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5530 _InputIterator2 __first2, _InputIterator2 __last2,
5531 _OutputIterator __result, _Compare __comp)
5532 {
5533 typedef typename iterator_traits<_InputIterator1>::value_type
5534 _ValueType1;
5535 typedef typename iterator_traits<_InputIterator2>::value_type
5536 _ValueType2;
5537
5538 // concept requirements
5539 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5540 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5541 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5542 _ValueType1>)
5543 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5544 _ValueType1, _ValueType2>)
5545 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5546 _ValueType2, _ValueType1>)
5547 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5548 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5549
5550 while (__first1 != __last1 && __first2 != __last2)
5551 if (__comp(*__first1, *__first2))
5552 {
5553 *__result = *__first1;
5554 ++__first1;
5555 ++__result;
5556 }
5557 else if (__comp(*__first2, *__first1))
5558 ++__first2;
5559 else
5560 {
5561 ++__first1;
5562 ++__first2;
5563 }
5564 return std::copy(__first1, __last1, __result);
5565 }
5566
5567 /**
5568 * @brief Return the symmetric difference of two sorted ranges.
5569 * @param first1 Start of first range.
5570 * @param last1 End of first range.
5571 * @param first2 Start of second range.
5572 * @param last2 End of second range.
5573 * @return End of the output range.
5574 * @ingroup setoperations
5575 *
5576 * This operation iterates over both ranges, copying elements present in
5577 * one range but not the other in order to the output range. Iterators
5578 * increment for each range. When the current element of one range is less
5579 * than the other, that element is copied and the iterator advances. If an
5580 * element is contained in both ranges, no elements are copied and both
5581 * ranges advance. The output range may not overlap either input range.
5582 */
5583 template<typename _InputIterator1, typename _InputIterator2,
5584 typename _OutputIterator>
5585 _OutputIterator
5586 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5587 _InputIterator2 __first2, _InputIterator2 __last2,
5588 _OutputIterator __result)
5589 {
5590 typedef typename iterator_traits<_InputIterator1>::value_type
5591 _ValueType1;
5592 typedef typename iterator_traits<_InputIterator2>::value_type
5593 _ValueType2;
5594
5595 // concept requirements
5596 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5597 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5598 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5599 _ValueType1>)
5600 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5601 _ValueType2>)
5602 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
5603 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
5604 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5605 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5606
5607 while (__first1 != __last1 && __first2 != __last2)
5608 if (*__first1 < *__first2)
5609 {
5610 *__result = *__first1;
5611 ++__first1;
5612 ++__result;
5613 }
5614 else if (*__first2 < *__first1)
5615 {
5616 *__result = *__first2;
5617 ++__first2;
5618 ++__result;
5619 }
5620 else
5621 {
5622 ++__first1;
5623 ++__first2;
5624 }
5625 return std::copy(__first2, __last2, std::copy(__first1,
5626 __last1, __result));
5627 }
5628
5629 /**
5630 * @brief Return the symmetric difference of two sorted ranges using
5631 * comparison functor.
5632 * @param first1 Start of first range.
5633 * @param last1 End of first range.
5634 * @param first2 Start of second range.
5635 * @param last2 End of second range.
5636 * @param comp The comparison functor.
5637 * @return End of the output range.
5638 * @ingroup setoperations
5639 *
5640 * This operation iterates over both ranges, copying elements present in
5641 * one range but not the other in order to the output range. Iterators
5642 * increment for each range. When the current element of one range is less
5643 * than the other according to @a comp, that element is copied and the
5644 * iterator advances. If an element is contained in both ranges according
5645 * to @a comp, no elements are copied and both ranges advance. The output
5646 * range may not overlap either input range.
5647 */
5648 template<typename _InputIterator1, typename _InputIterator2,
5649 typename _OutputIterator, typename _Compare>
5650 _OutputIterator
5651 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5652 _InputIterator2 __first2, _InputIterator2 __last2,
5653 _OutputIterator __result,
5654 _Compare __comp)
5655 {
5656 typedef typename iterator_traits<_InputIterator1>::value_type
5657 _ValueType1;
5658 typedef typename iterator_traits<_InputIterator2>::value_type
5659 _ValueType2;
5660
5661 // concept requirements
5662 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5663 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5664 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5665 _ValueType1>)
5666 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5667 _ValueType2>)
5668 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5669 _ValueType1, _ValueType2>)
5670 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5671 _ValueType2, _ValueType1>)
5672 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5673 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5674
5675 while (__first1 != __last1 && __first2 != __last2)
5676 if (__comp(*__first1, *__first2))
5677 {
5678 *__result = *__first1;
5679 ++__first1;
5680 ++__result;
5681 }
5682 else if (__comp(*__first2, *__first1))
5683 {
5684 *__result = *__first2;
5685 ++__first2;
5686 ++__result;
5687 }
5688 else
5689 {
5690 ++__first1;
5691 ++__first2;
5692 }
5693 return std::copy(__first2, __last2,
5694 std::copy(__first1, __last1, __result));
5695 }
5696
5697
5698 /**
5699 * @brief Return the minimum element in a range.
5700 * @param first Start of range.
5701 * @param last End of range.
5702 * @return Iterator referencing the first instance of the smallest value.
5703 */
5704 template<typename _ForwardIterator>
5705 _ForwardIterator
5706 min_element(_ForwardIterator __first, _ForwardIterator __last)
5707 {
5708 // concept requirements
5709 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5710 __glibcxx_function_requires(_LessThanComparableConcept<
5711 typename iterator_traits<_ForwardIterator>::value_type>)
5712 __glibcxx_requires_valid_range(__first, __last);
5713
5714 if (__first == __last)
5715 return __first;
5716 _ForwardIterator __result = __first;
5717 while (++__first != __last)
5718 if (*__first < *__result)
5719 __result = __first;
5720 return __result;
5721 }
5722
5723 /**
5724 * @brief Return the minimum element in a range using comparison functor.
5725 * @param first Start of range.
5726 * @param last End of range.
5727 * @param comp Comparison functor.
5728 * @return Iterator referencing the first instance of the smallest value
5729 * according to comp.
5730 */
5731 template<typename _ForwardIterator, typename _Compare>
5732 _ForwardIterator
5733 min_element(_ForwardIterator __first, _ForwardIterator __last,
5734 _Compare __comp)
5735 {
5736 // concept requirements
5737 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5738 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5739 typename iterator_traits<_ForwardIterator>::value_type,
5740 typename iterator_traits<_ForwardIterator>::value_type>)
5741 __glibcxx_requires_valid_range(__first, __last);
5742
5743 if (__first == __last)
5744 return __first;
5745 _ForwardIterator __result = __first;
5746 while (++__first != __last)
5747 if (__comp(*__first, *__result))
5748 __result = __first;
5749 return __result;
5750 }
5751
5752 /**
5753 * @brief Return the maximum element in a range.
5754 * @param first Start of range.
5755 * @param last End of range.
5756 * @return Iterator referencing the first instance of the largest value.
5757 */
5758 template<typename _ForwardIterator>
5759 _ForwardIterator
5760 max_element(_ForwardIterator __first, _ForwardIterator __last)
5761 {
5762 // concept requirements
5763 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5764 __glibcxx_function_requires(_LessThanComparableConcept<
5765 typename iterator_traits<_ForwardIterator>::value_type>)
5766 __glibcxx_requires_valid_range(__first, __last);
5767
5768 if (__first == __last)
5769 return __first;
5770 _ForwardIterator __result = __first;
5771 while (++__first != __last)
5772 if (*__result < *__first)
5773 __result = __first;
5774 return __result;
5775 }
5776
5777 /**
5778 * @brief Return the maximum element in a range using comparison functor.
5779 * @param first Start of range.
5780 * @param last End of range.
5781 * @param comp Comparison functor.
5782 * @return Iterator referencing the first instance of the largest value
5783 * according to comp.
5784 */
5785 template<typename _ForwardIterator, typename _Compare>
5786 _ForwardIterator
5787 max_element(_ForwardIterator __first, _ForwardIterator __last,
5788 _Compare __comp)
5789 {
5790 // concept requirements
5791 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5792 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5793 typename iterator_traits<_ForwardIterator>::value_type,
5794 typename iterator_traits<_ForwardIterator>::value_type>)
5795 __glibcxx_requires_valid_range(__first, __last);
5796
5797 if (__first == __last) return __first;
5798 _ForwardIterator __result = __first;
5799 while (++__first != __last)
5800 if (__comp(*__result, *__first))
5801 __result = __first;
5802 return __result;
5803 }
5804
5805 _GLIBCXX_END_NESTED_NAMESPACE
5806
5807 #endif /* _STL_ALGO_H */