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