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