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