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