00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifndef _STL_ALGO_H
00058 #define _STL_ALGO_H 1
00059
00060 #include <cstdlib>
00061 #include <bits/algorithmfwd.h>
00062 #include <bits/stl_heap.h>
00063 #include <bits/stl_tempbuf.h>
00064 #include <debug/debug.h>
00065 #include <initializer_list>
00066
00067
00068
00069 _GLIBCXX_BEGIN_NAMESPACE(std)
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 template<typename _Tp>
00084 inline const _Tp&
00085 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
00086 {
00087
00088 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
00089 if (__a < __b)
00090 if (__b < __c)
00091 return __b;
00092 else if (__a < __c)
00093 return __c;
00094 else
00095 return __a;
00096 else if (__a < __c)
00097 return __a;
00098 else if (__b < __c)
00099 return __c;
00100 else
00101 return __b;
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 template<typename _Tp, typename _Compare>
00118 inline const _Tp&
00119 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
00120 {
00121
00122 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
00123 _Tp, _Tp>)
00124 if (__comp(__a, __b))
00125 if (__comp(__b, __c))
00126 return __b;
00127 else if (__comp(__a, __c))
00128 return __c;
00129 else
00130 return __a;
00131 else if (__comp(__a, __c))
00132 return __a;
00133 else if (__comp(__b, __c))
00134 return __c;
00135 else
00136 return __b;
00137 }
00138
00139
00140
00141
00142 template<typename _InputIterator, typename _Tp>
00143 inline _InputIterator
00144 __find(_InputIterator __first, _InputIterator __last,
00145 const _Tp& __val, input_iterator_tag)
00146 {
00147 while (__first != __last && !(*__first == __val))
00148 ++__first;
00149 return __first;
00150 }
00151
00152
00153 template<typename _InputIterator, typename _Predicate>
00154 inline _InputIterator
00155 __find_if(_InputIterator __first, _InputIterator __last,
00156 _Predicate __pred, input_iterator_tag)
00157 {
00158 while (__first != __last && !bool(__pred(*__first)))
00159 ++__first;
00160 return __first;
00161 }
00162
00163
00164 template<typename _RandomAccessIterator, typename _Tp>
00165 _RandomAccessIterator
00166 __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
00167 const _Tp& __val, random_access_iterator_tag)
00168 {
00169 typename iterator_traits<_RandomAccessIterator>::difference_type
00170 __trip_count = (__last - __first) >> 2;
00171
00172 for (; __trip_count > 0; --__trip_count)
00173 {
00174 if (*__first == __val)
00175 return __first;
00176 ++__first;
00177
00178 if (*__first == __val)
00179 return __first;
00180 ++__first;
00181
00182 if (*__first == __val)
00183 return __first;
00184 ++__first;
00185
00186 if (*__first == __val)
00187 return __first;
00188 ++__first;
00189 }
00190
00191 switch (__last - __first)
00192 {
00193 case 3:
00194 if (*__first == __val)
00195 return __first;
00196 ++__first;
00197 case 2:
00198 if (*__first == __val)
00199 return __first;
00200 ++__first;
00201 case 1:
00202 if (*__first == __val)
00203 return __first;
00204 ++__first;
00205 case 0:
00206 default:
00207 return __last;
00208 }
00209 }
00210
00211
00212 template<typename _RandomAccessIterator, typename _Predicate>
00213 _RandomAccessIterator
00214 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
00215 _Predicate __pred, random_access_iterator_tag)
00216 {
00217 typename iterator_traits<_RandomAccessIterator>::difference_type
00218 __trip_count = (__last - __first) >> 2;
00219
00220 for (; __trip_count > 0; --__trip_count)
00221 {
00222 if (__pred(*__first))
00223 return __first;
00224 ++__first;
00225
00226 if (__pred(*__first))
00227 return __first;
00228 ++__first;
00229
00230 if (__pred(*__first))
00231 return __first;
00232 ++__first;
00233
00234 if (__pred(*__first))
00235 return __first;
00236 ++__first;
00237 }
00238
00239 switch (__last - __first)
00240 {
00241 case 3:
00242 if (__pred(*__first))
00243 return __first;
00244 ++__first;
00245 case 2:
00246 if (__pred(*__first))
00247 return __first;
00248 ++__first;
00249 case 1:
00250 if (__pred(*__first))
00251 return __first;
00252 ++__first;
00253 case 0:
00254 default:
00255 return __last;
00256 }
00257 }
00258
00259 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00260
00261 template<typename _InputIterator, typename _Predicate>
00262 inline _InputIterator
00263 __find_if_not(_InputIterator __first, _InputIterator __last,
00264 _Predicate __pred, input_iterator_tag)
00265 {
00266 while (__first != __last && bool(__pred(*__first)))
00267 ++__first;
00268 return __first;
00269 }
00270
00271
00272 template<typename _RandomAccessIterator, typename _Predicate>
00273 _RandomAccessIterator
00274 __find_if_not(_RandomAccessIterator __first, _RandomAccessIterator __last,
00275 _Predicate __pred, random_access_iterator_tag)
00276 {
00277 typename iterator_traits<_RandomAccessIterator>::difference_type
00278 __trip_count = (__last - __first) >> 2;
00279
00280 for (; __trip_count > 0; --__trip_count)
00281 {
00282 if (!bool(__pred(*__first)))
00283 return __first;
00284 ++__first;
00285
00286 if (!bool(__pred(*__first)))
00287 return __first;
00288 ++__first;
00289
00290 if (!bool(__pred(*__first)))
00291 return __first;
00292 ++__first;
00293
00294 if (!bool(__pred(*__first)))
00295 return __first;
00296 ++__first;
00297 }
00298
00299 switch (__last - __first)
00300 {
00301 case 3:
00302 if (!bool(__pred(*__first)))
00303 return __first;
00304 ++__first;
00305 case 2:
00306 if (!bool(__pred(*__first)))
00307 return __first;
00308 ++__first;
00309 case 1:
00310 if (!bool(__pred(*__first)))
00311 return __first;
00312 ++__first;
00313 case 0:
00314 default:
00315 return __last;
00316 }
00317 }
00318 #endif
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 template<typename _ForwardIterator, typename _Integer, typename _Tp>
00339 _ForwardIterator
00340 __search_n(_ForwardIterator __first, _ForwardIterator __last,
00341 _Integer __count, const _Tp& __val,
00342 std::forward_iterator_tag)
00343 {
00344 __first = _GLIBCXX_STD_P::find(__first, __last, __val);
00345 while (__first != __last)
00346 {
00347 typename iterator_traits<_ForwardIterator>::difference_type
00348 __n = __count;
00349 _ForwardIterator __i = __first;
00350 ++__i;
00351 while (__i != __last && __n != 1 && *__i == __val)
00352 {
00353 ++__i;
00354 --__n;
00355 }
00356 if (__n == 1)
00357 return __first;
00358 if (__i == __last)
00359 return __last;
00360 __first = _GLIBCXX_STD_P::find(++__i, __last, __val);
00361 }
00362 return __last;
00363 }
00364
00365
00366
00367
00368
00369
00370 template<typename _RandomAccessIter, typename _Integer, typename _Tp>
00371 _RandomAccessIter
00372 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
00373 _Integer __count, const _Tp& __val,
00374 std::random_access_iterator_tag)
00375 {
00376
00377 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
00378 _DistanceType;
00379
00380 _DistanceType __tailSize = __last - __first;
00381 const _DistanceType __pattSize = __count;
00382
00383 if (__tailSize < __pattSize)
00384 return __last;
00385
00386 const _DistanceType __skipOffset = __pattSize - 1;
00387 _RandomAccessIter __lookAhead = __first + __skipOffset;
00388 __tailSize -= __pattSize;
00389
00390 while (1)
00391 {
00392
00393
00394 while (!(*__lookAhead == __val))
00395 {
00396 if (__tailSize < __pattSize)
00397 return __last;
00398 __lookAhead += __pattSize;
00399 __tailSize -= __pattSize;
00400 }
00401 _DistanceType __remainder = __skipOffset;
00402 for (_RandomAccessIter __backTrack = __lookAhead - 1;
00403 *__backTrack == __val; --__backTrack)
00404 {
00405 if (--__remainder == 0)
00406 return (__lookAhead - __skipOffset);
00407 }
00408 if (__remainder > __tailSize)
00409 return __last;
00410 __lookAhead += __remainder;
00411 __tailSize -= __remainder;
00412 }
00413 }
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 template<typename _ForwardIterator, typename _Integer, typename _Tp,
00424 typename _BinaryPredicate>
00425 _ForwardIterator
00426 __search_n(_ForwardIterator __first, _ForwardIterator __last,
00427 _Integer __count, const _Tp& __val,
00428 _BinaryPredicate __binary_pred, std::forward_iterator_tag)
00429 {
00430 while (__first != __last && !bool(__binary_pred(*__first, __val)))
00431 ++__first;
00432
00433 while (__first != __last)
00434 {
00435 typename iterator_traits<_ForwardIterator>::difference_type
00436 __n = __count;
00437 _ForwardIterator __i = __first;
00438 ++__i;
00439 while (__i != __last && __n != 1 && bool(__binary_pred(*__i, __val)))
00440 {
00441 ++__i;
00442 --__n;
00443 }
00444 if (__n == 1)
00445 return __first;
00446 if (__i == __last)
00447 return __last;
00448 __first = ++__i;
00449 while (__first != __last
00450 && !bool(__binary_pred(*__first, __val)))
00451 ++__first;
00452 }
00453 return __last;
00454 }
00455
00456
00457
00458
00459
00460
00461
00462 template<typename _RandomAccessIter, typename _Integer, typename _Tp,
00463 typename _BinaryPredicate>
00464 _RandomAccessIter
00465 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
00466 _Integer __count, const _Tp& __val,
00467 _BinaryPredicate __binary_pred, std::random_access_iterator_tag)
00468 {
00469
00470 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
00471 _DistanceType;
00472
00473 _DistanceType __tailSize = __last - __first;
00474 const _DistanceType __pattSize = __count;
00475
00476 if (__tailSize < __pattSize)
00477 return __last;
00478
00479 const _DistanceType __skipOffset = __pattSize - 1;
00480 _RandomAccessIter __lookAhead = __first + __skipOffset;
00481 __tailSize -= __pattSize;
00482
00483 while (1)
00484 {
00485
00486
00487 while (!bool(__binary_pred(*__lookAhead, __val)))
00488 {
00489 if (__tailSize < __pattSize)
00490 return __last;
00491 __lookAhead += __pattSize;
00492 __tailSize -= __pattSize;
00493 }
00494 _DistanceType __remainder = __skipOffset;
00495 for (_RandomAccessIter __backTrack = __lookAhead - 1;
00496 __binary_pred(*__backTrack, __val); --__backTrack)
00497 {
00498 if (--__remainder == 0)
00499 return (__lookAhead - __skipOffset);
00500 }
00501 if (__remainder > __tailSize)
00502 return __last;
00503 __lookAhead += __remainder;
00504 __tailSize -= __remainder;
00505 }
00506 }
00507
00508
00509 template<typename _ForwardIterator1, typename _ForwardIterator2>
00510 _ForwardIterator1
00511 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
00512 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
00513 forward_iterator_tag, forward_iterator_tag)
00514 {
00515 if (__first2 == __last2)
00516 return __last1;
00517 else
00518 {
00519 _ForwardIterator1 __result = __last1;
00520 while (1)
00521 {
00522 _ForwardIterator1 __new_result
00523 = _GLIBCXX_STD_P::search(__first1, __last1, __first2, __last2);
00524 if (__new_result == __last1)
00525 return __result;
00526 else
00527 {
00528 __result = __new_result;
00529 __first1 = __new_result;
00530 ++__first1;
00531 }
00532 }
00533 }
00534 }
00535
00536 template<typename _ForwardIterator1, typename _ForwardIterator2,
00537 typename _BinaryPredicate>
00538 _ForwardIterator1
00539 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
00540 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
00541 forward_iterator_tag, forward_iterator_tag,
00542 _BinaryPredicate __comp)
00543 {
00544 if (__first2 == __last2)
00545 return __last1;
00546 else
00547 {
00548 _ForwardIterator1 __result = __last1;
00549 while (1)
00550 {
00551 _ForwardIterator1 __new_result
00552 = _GLIBCXX_STD_P::search(__first1, __last1, __first2,
00553 __last2, __comp);
00554 if (__new_result == __last1)
00555 return __result;
00556 else
00557 {
00558 __result = __new_result;
00559 __first1 = __new_result;
00560 ++__first1;
00561 }
00562 }
00563 }
00564 }
00565
00566
00567 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
00568 _BidirectionalIterator1
00569 __find_end(_BidirectionalIterator1 __first1,
00570 _BidirectionalIterator1 __last1,
00571 _BidirectionalIterator2 __first2,
00572 _BidirectionalIterator2 __last2,
00573 bidirectional_iterator_tag, bidirectional_iterator_tag)
00574 {
00575
00576 __glibcxx_function_requires(_BidirectionalIteratorConcept<
00577 _BidirectionalIterator1>)
00578 __glibcxx_function_requires(_BidirectionalIteratorConcept<
00579 _BidirectionalIterator2>)
00580
00581 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
00582 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
00583
00584 _RevIterator1 __rlast1(__first1);
00585 _RevIterator2 __rlast2(__first2);
00586 _RevIterator1 __rresult = _GLIBCXX_STD_P::search(_RevIterator1(__last1),
00587 __rlast1,
00588 _RevIterator2(__last2),
00589 __rlast2);
00590
00591 if (__rresult == __rlast1)
00592 return __last1;
00593 else
00594 {
00595 _BidirectionalIterator1 __result = __rresult.base();
00596 std::advance(__result, -std::distance(__first2, __last2));
00597 return __result;
00598 }
00599 }
00600
00601 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
00602 typename _BinaryPredicate>
00603 _BidirectionalIterator1
00604 __find_end(_BidirectionalIterator1 __first1,
00605 _BidirectionalIterator1 __last1,
00606 _BidirectionalIterator2 __first2,
00607 _BidirectionalIterator2 __last2,
00608 bidirectional_iterator_tag, bidirectional_iterator_tag,
00609 _BinaryPredicate __comp)
00610 {
00611
00612 __glibcxx_function_requires(_BidirectionalIteratorConcept<
00613 _BidirectionalIterator1>)
00614 __glibcxx_function_requires(_BidirectionalIteratorConcept<
00615 _BidirectionalIterator2>)
00616
00617 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
00618 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
00619
00620 _RevIterator1 __rlast1(__first1);
00621 _RevIterator2 __rlast2(__first2);
00622 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
00623 _RevIterator2(__last2), __rlast2,
00624 __comp);
00625
00626 if (__rresult == __rlast1)
00627 return __last1;
00628 else
00629 {
00630 _BidirectionalIterator1 __result = __rresult.base();
00631 std::advance(__result, -std::distance(__first2, __last2));
00632 return __result;
00633 }
00634 }
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661 template<typename _ForwardIterator1, typename _ForwardIterator2>
00662 inline _ForwardIterator1
00663 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
00664 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
00665 {
00666
00667 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
00668 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
00669 __glibcxx_function_requires(_EqualOpConcept<
00670 typename iterator_traits<_ForwardIterator1>::value_type,
00671 typename iterator_traits<_ForwardIterator2>::value_type>)
00672 __glibcxx_requires_valid_range(__first1, __last1);
00673 __glibcxx_requires_valid_range(__first2, __last2);
00674
00675 return std::__find_end(__first1, __last1, __first2, __last2,
00676 std::__iterator_category(__first1),
00677 std::__iterator_category(__first2));
00678 }
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707 template<typename _ForwardIterator1, typename _ForwardIterator2,
00708 typename _BinaryPredicate>
00709 inline _ForwardIterator1
00710 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
00711 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
00712 _BinaryPredicate __comp)
00713 {
00714
00715 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
00716 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
00717 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
00718 typename iterator_traits<_ForwardIterator1>::value_type,
00719 typename iterator_traits<_ForwardIterator2>::value_type>)
00720 __glibcxx_requires_valid_range(__first1, __last1);
00721 __glibcxx_requires_valid_range(__first2, __last2);
00722
00723 return std::__find_end(__first1, __last1, __first2, __last2,
00724 std::__iterator_category(__first1),
00725 std::__iterator_category(__first2),
00726 __comp);
00727 }
00728
00729 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742 template<typename _InputIterator, typename _Predicate>
00743 inline bool
00744 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
00745 { return __last == std::find_if_not(__first, __last, __pred); }
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759 template<typename _InputIterator, typename _Predicate>
00760 inline bool
00761 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
00762 { return __last == _GLIBCXX_STD_P::find_if(__first, __last, __pred); }
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776 template<typename _InputIterator, typename _Predicate>
00777 inline bool
00778 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
00779 { return !std::none_of(__first, __last, __pred); }
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791 template<typename _InputIterator, typename _Predicate>
00792 inline _InputIterator
00793 find_if_not(_InputIterator __first, _InputIterator __last,
00794 _Predicate __pred)
00795 {
00796
00797 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00798 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
00799 typename iterator_traits<_InputIterator>::value_type>)
00800 __glibcxx_requires_valid_range(__first, __last);
00801 return std::__find_if_not(__first, __last, __pred,
00802 std::__iterator_category(__first));
00803 }
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815 template<typename _InputIterator, typename _Predicate>
00816 inline bool
00817 is_partitioned(_InputIterator __first, _InputIterator __last,
00818 _Predicate __pred)
00819 {
00820 __first = std::find_if_not(__first, __last, __pred);
00821 return std::none_of(__first, __last, __pred);
00822 }
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833 template<typename _ForwardIterator, typename _Predicate>
00834 _ForwardIterator
00835 partition_point(_ForwardIterator __first, _ForwardIterator __last,
00836 _Predicate __pred)
00837 {
00838
00839 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
00840 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
00841 typename iterator_traits<_ForwardIterator>::value_type>)
00842
00843
00844 __glibcxx_requires_valid_range(__first, __last);
00845
00846 typedef typename iterator_traits<_ForwardIterator>::difference_type
00847 _DistanceType;
00848
00849 _DistanceType __len = std::distance(__first, __last);
00850 _DistanceType __half;
00851 _ForwardIterator __middle;
00852
00853 while (__len > 0)
00854 {
00855 __half = __len >> 1;
00856 __middle = __first;
00857 std::advance(__middle, __half);
00858 if (__pred(*__middle))
00859 {
00860 __first = __middle;
00861 ++__first;
00862 __len = __len - __half - 1;
00863 }
00864 else
00865 __len = __half;
00866 }
00867 return __first;
00868 }
00869 #endif
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
00887 _OutputIterator
00888 remove_copy(_InputIterator __first, _InputIterator __last,
00889 _OutputIterator __result, const _Tp& __value)
00890 {
00891
00892 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00893 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00894 typename iterator_traits<_InputIterator>::value_type>)
00895 __glibcxx_function_requires(_EqualOpConcept<
00896 typename iterator_traits<_InputIterator>::value_type, _Tp>)
00897 __glibcxx_requires_valid_range(__first, __last);
00898
00899 for (; __first != __last; ++__first)
00900 if (!(*__first == __value))
00901 {
00902 *__result = *__first;
00903 ++__result;
00904 }
00905 return __result;
00906 }
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923 template<typename _InputIterator, typename _OutputIterator,
00924 typename _Predicate>
00925 _OutputIterator
00926 remove_copy_if(_InputIterator __first, _InputIterator __last,
00927 _OutputIterator __result, _Predicate __pred)
00928 {
00929
00930 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00931 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00932 typename iterator_traits<_InputIterator>::value_type>)
00933 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
00934 typename iterator_traits<_InputIterator>::value_type>)
00935 __glibcxx_requires_valid_range(__first, __last);
00936
00937 for (; __first != __last; ++__first)
00938 if (!bool(__pred(*__first)))
00939 {
00940 *__result = *__first;
00941 ++__result;
00942 }
00943 return __result;
00944 }
00945
00946 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962 template<typename _InputIterator, typename _OutputIterator,
00963 typename _Predicate>
00964 _OutputIterator
00965 copy_if(_InputIterator __first, _InputIterator __last,
00966 _OutputIterator __result, _Predicate __pred)
00967 {
00968
00969 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00970 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00971 typename iterator_traits<_InputIterator>::value_type>)
00972 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
00973 typename iterator_traits<_InputIterator>::value_type>)
00974 __glibcxx_requires_valid_range(__first, __last);
00975
00976 for (; __first != __last; ++__first)
00977 if (__pred(*__first))
00978 {
00979 *__result = *__first;
00980 ++__result;
00981 }
00982 return __result;
00983 }
00984
00985
00986 template<typename _InputIterator, typename _Size, typename _OutputIterator>
00987 _OutputIterator
00988 __copy_n(_InputIterator __first, _Size __n,
00989 _OutputIterator __result, input_iterator_tag)
00990 {
00991 for (; __n > 0; --__n)
00992 {
00993 *__result = *__first;
00994 ++__first;
00995 ++__result;
00996 }
00997 return __result;
00998 }
00999
01000 template<typename _RandomAccessIterator, typename _Size,
01001 typename _OutputIterator>
01002 inline _OutputIterator
01003 __copy_n(_RandomAccessIterator __first, _Size __n,
01004 _OutputIterator __result, random_access_iterator_tag)
01005 { return std::copy(__first, __first + __n, __result); }
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020 template<typename _InputIterator, typename _Size, typename _OutputIterator>
01021 inline _OutputIterator
01022 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
01023 {
01024
01025 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
01026 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
01027 typename iterator_traits<_InputIterator>::value_type>)
01028
01029 return std::__copy_n(__first, __n, __result,
01030 std::__iterator_category(__first));
01031 }
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048 template<typename _InputIterator, typename _OutputIterator1,
01049 typename _OutputIterator2, typename _Predicate>
01050 pair<_OutputIterator1, _OutputIterator2>
01051 partition_copy(_InputIterator __first, _InputIterator __last,
01052 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
01053 _Predicate __pred)
01054 {
01055
01056 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
01057 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
01058 typename iterator_traits<_InputIterator>::value_type>)
01059 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
01060 typename iterator_traits<_InputIterator>::value_type>)
01061 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
01062 typename iterator_traits<_InputIterator>::value_type>)
01063 __glibcxx_requires_valid_range(__first, __last);
01064
01065 for (; __first != __last; ++__first)
01066 if (__pred(*__first))
01067 {
01068 *__out_true = *__first;
01069 ++__out_true;
01070 }
01071 else
01072 {
01073 *__out_false = *__first;
01074 ++__out_false;
01075 }
01076
01077 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
01078 }
01079 #endif
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098 template<typename _ForwardIterator, typename _Tp>
01099 _ForwardIterator
01100 remove(_ForwardIterator __first, _ForwardIterator __last,
01101 const _Tp& __value)
01102 {
01103
01104 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
01105 _ForwardIterator>)
01106 __glibcxx_function_requires(_EqualOpConcept<
01107 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
01108 __glibcxx_requires_valid_range(__first, __last);
01109
01110 __first = _GLIBCXX_STD_P::find(__first, __last, __value);
01111 if(__first == __last)
01112 return __first;
01113 _ForwardIterator __result = __first;
01114 ++__first;
01115 for(; __first != __last; ++__first)
01116 if(!(*__first == __value))
01117 {
01118 *__result = _GLIBCXX_MOVE(*__first);
01119 ++__result;
01120 }
01121 return __result;
01122 }
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141 template<typename _ForwardIterator, typename _Predicate>
01142 _ForwardIterator
01143 remove_if(_ForwardIterator __first, _ForwardIterator __last,
01144 _Predicate __pred)
01145 {
01146
01147 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
01148 _ForwardIterator>)
01149 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
01150 typename iterator_traits<_ForwardIterator>::value_type>)
01151 __glibcxx_requires_valid_range(__first, __last);
01152
01153 __first = _GLIBCXX_STD_P::find_if(__first, __last, __pred);
01154 if(__first == __last)
01155 return __first;
01156 _ForwardIterator __result = __first;
01157 ++__first;
01158 for(; __first != __last; ++__first)
01159 if(!bool(__pred(*__first)))
01160 {
01161 *__result = _GLIBCXX_MOVE(*__first);
01162 ++__result;
01163 }
01164 return __result;
01165 }
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181 template<typename _ForwardIterator>
01182 _ForwardIterator
01183 unique(_ForwardIterator __first, _ForwardIterator __last)
01184 {
01185
01186 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
01187 _ForwardIterator>)
01188 __glibcxx_function_requires(_EqualityComparableConcept<
01189 typename iterator_traits<_ForwardIterator>::value_type>)
01190 __glibcxx_requires_valid_range(__first, __last);
01191
01192
01193 __first = _GLIBCXX_STD_P::adjacent_find(__first, __last);
01194 if (__first == __last)
01195 return __last;
01196
01197
01198 _ForwardIterator __dest = __first;
01199 ++__first;
01200 while (++__first != __last)
01201 if (!(*__dest == *__first))
01202 *++__dest = _GLIBCXX_MOVE(*__first);
01203 return ++__dest;
01204 }
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221 template<typename _ForwardIterator, typename _BinaryPredicate>
01222 _ForwardIterator
01223 unique(_ForwardIterator __first, _ForwardIterator __last,
01224 _BinaryPredicate __binary_pred)
01225 {
01226
01227 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
01228 _ForwardIterator>)
01229 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
01230 typename iterator_traits<_ForwardIterator>::value_type,
01231 typename iterator_traits<_ForwardIterator>::value_type>)
01232 __glibcxx_requires_valid_range(__first, __last);
01233
01234
01235 __first = _GLIBCXX_STD_P::adjacent_find(__first, __last, __binary_pred);
01236 if (__first == __last)
01237 return __last;
01238
01239
01240 _ForwardIterator __dest = __first;
01241 ++__first;
01242 while (++__first != __last)
01243 if (!bool(__binary_pred(*__dest, *__first)))
01244 *++__dest = _GLIBCXX_MOVE(*__first);
01245 return ++__dest;
01246 }
01247
01248
01249
01250
01251
01252
01253 template<typename _ForwardIterator, typename _OutputIterator>
01254 _OutputIterator
01255 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
01256 _OutputIterator __result,
01257 forward_iterator_tag, output_iterator_tag)
01258 {
01259
01260 _ForwardIterator __next = __first;
01261 *__result = *__first;
01262 while (++__next != __last)
01263 if (!(*__first == *__next))
01264 {
01265 __first = __next;
01266 *++__result = *__first;
01267 }
01268 return ++__result;
01269 }
01270
01271
01272
01273
01274
01275
01276 template<typename _InputIterator, typename _OutputIterator>
01277 _OutputIterator
01278 __unique_copy(_InputIterator __first, _InputIterator __last,
01279 _OutputIterator __result,
01280 input_iterator_tag, output_iterator_tag)
01281 {
01282
01283 typename iterator_traits<_InputIterator>::value_type __value = *__first;
01284 *__result = __value;
01285 while (++__first != __last)
01286 if (!(__value == *__first))
01287 {
01288 __value = *__first;
01289 *++__result = __value;
01290 }
01291 return ++__result;
01292 }
01293
01294
01295
01296
01297
01298
01299 template<typename _InputIterator, typename _ForwardIterator>
01300 _ForwardIterator
01301 __unique_copy(_InputIterator __first, _InputIterator __last,
01302 _ForwardIterator __result,
01303 input_iterator_tag, forward_iterator_tag)
01304 {
01305
01306 *__result = *__first;
01307 while (++__first != __last)
01308 if (!(*__result == *__first))
01309 *++__result = *__first;
01310 return ++__result;
01311 }
01312
01313
01314
01315
01316
01317
01318
01319 template<typename _ForwardIterator, typename _OutputIterator,
01320 typename _BinaryPredicate>
01321 _OutputIterator
01322 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
01323 _OutputIterator __result, _BinaryPredicate __binary_pred,
01324 forward_iterator_tag, output_iterator_tag)
01325 {
01326
01327 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
01328 typename iterator_traits<_ForwardIterator>::value_type,
01329 typename iterator_traits<_ForwardIterator>::value_type>)
01330
01331 _ForwardIterator __next = __first;
01332 *__result = *__first;
01333 while (++__next != __last)
01334 if (!bool(__binary_pred(*__first, *__next)))
01335 {
01336 __first = __next;
01337 *++__result = *__first;
01338 }
01339 return ++__result;
01340 }
01341
01342
01343
01344
01345
01346
01347
01348 template<typename _InputIterator, typename _OutputIterator,
01349 typename _BinaryPredicate>
01350 _OutputIterator
01351 __unique_copy(_InputIterator __first, _InputIterator __last,
01352 _OutputIterator __result, _BinaryPredicate __binary_pred,
01353 input_iterator_tag, output_iterator_tag)
01354 {
01355
01356 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
01357 typename iterator_traits<_InputIterator>::value_type,
01358 typename iterator_traits<_InputIterator>::value_type>)
01359
01360 typename iterator_traits<_InputIterator>::value_type __value = *__first;
01361 *__result = __value;
01362 while (++__first != __last)
01363 if (!bool(__binary_pred(__value, *__first)))
01364 {
01365 __value = *__first;
01366 *++__result = __value;
01367 }
01368 return ++__result;
01369 }
01370
01371
01372
01373
01374
01375
01376
01377 template<typename _InputIterator, typename _ForwardIterator,
01378 typename _BinaryPredicate>
01379 _ForwardIterator
01380 __unique_copy(_InputIterator __first, _InputIterator __last,
01381 _ForwardIterator __result, _BinaryPredicate __binary_pred,
01382 input_iterator_tag, forward_iterator_tag)
01383 {
01384
01385 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
01386 typename iterator_traits<_ForwardIterator>::value_type,
01387 typename iterator_traits<_InputIterator>::value_type>)
01388
01389 *__result = *__first;
01390 while (++__first != __last)
01391 if (!bool(__binary_pred(*__result, *__first)))
01392 *++__result = *__first;
01393 return ++__result;
01394 }
01395
01396
01397
01398
01399
01400
01401 template<typename _BidirectionalIterator>
01402 void
01403 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
01404 bidirectional_iterator_tag)
01405 {
01406 while (true)
01407 if (__first == __last || __first == --__last)
01408 return;
01409 else
01410 {
01411 std::iter_swap(__first, __last);
01412 ++__first;
01413 }
01414 }
01415
01416
01417
01418
01419
01420
01421 template<typename _RandomAccessIterator>
01422 void
01423 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
01424 random_access_iterator_tag)
01425 {
01426 if (__first == __last)
01427 return;
01428 --__last;
01429 while (__first < __last)
01430 {
01431 std::iter_swap(__first, __last);
01432 ++__first;
01433 --__last;
01434 }
01435 }
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447
01448
01449 template<typename _BidirectionalIterator>
01450 inline void
01451 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
01452 {
01453
01454 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
01455 _BidirectionalIterator>)
01456 __glibcxx_requires_valid_range(__first, __last);
01457 std::__reverse(__first, __last, std::__iterator_category(__first));
01458 }
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476 template<typename _BidirectionalIterator, typename _OutputIterator>
01477 _OutputIterator
01478 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
01479 _OutputIterator __result)
01480 {
01481
01482 __glibcxx_function_requires(_BidirectionalIteratorConcept<
01483 _BidirectionalIterator>)
01484 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
01485 typename iterator_traits<_BidirectionalIterator>::value_type>)
01486 __glibcxx_requires_valid_range(__first, __last);
01487
01488 while (__first != __last)
01489 {
01490 --__last;
01491 *__result = *__last;
01492 ++__result;
01493 }
01494 return __result;
01495 }
01496
01497
01498
01499
01500
01501 template<typename _EuclideanRingElement>
01502 _EuclideanRingElement
01503 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
01504 {
01505 while (__n != 0)
01506 {
01507 _EuclideanRingElement __t = __m % __n;
01508 __m = __n;
01509 __n = __t;
01510 }
01511 return __m;
01512 }
01513
01514
01515 template<typename _ForwardIterator>
01516 void
01517 __rotate(_ForwardIterator __first,
01518 _ForwardIterator __middle,
01519 _ForwardIterator __last,
01520 forward_iterator_tag)
01521 {
01522 if (__first == __middle || __last == __middle)
01523 return;
01524
01525 _ForwardIterator __first2 = __middle;
01526 do
01527 {
01528 std::iter_swap(__first, __first2);
01529 ++__first;
01530 ++__first2;
01531 if (__first == __middle)
01532 __middle = __first2;
01533 }
01534 while (__first2 != __last);
01535
01536 __first2 = __middle;
01537
01538 while (__first2 != __last)
01539 {
01540 std::iter_swap(__first, __first2);
01541 ++__first;
01542 ++__first2;
01543 if (__first == __middle)
01544 __middle = __first2;
01545 else if (__first2 == __last)
01546 __first2 = __middle;
01547 }
01548 }
01549
01550
01551 template<typename _BidirectionalIterator>
01552 void
01553 __rotate(_BidirectionalIterator __first,
01554 _BidirectionalIterator __middle,
01555 _BidirectionalIterator __last,
01556 bidirectional_iterator_tag)
01557 {
01558
01559 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
01560 _BidirectionalIterator>)
01561
01562 if (__first == __middle || __last == __middle)
01563 return;
01564
01565 std::__reverse(__first, __middle, bidirectional_iterator_tag());
01566 std::__reverse(__middle, __last, bidirectional_iterator_tag());
01567
01568 while (__first != __middle && __middle != __last)
01569 {
01570 std::iter_swap(__first, --__last);
01571 ++__first;
01572 }
01573
01574 if (__first == __middle)
01575 std::__reverse(__middle, __last, bidirectional_iterator_tag());
01576 else
01577 std::__reverse(__first, __middle, bidirectional_iterator_tag());
01578 }
01579
01580
01581 template<typename _RandomAccessIterator>
01582 void
01583 __rotate(_RandomAccessIterator __first,
01584 _RandomAccessIterator __middle,
01585 _RandomAccessIterator __last,
01586 random_access_iterator_tag)
01587 {
01588
01589 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
01590 _RandomAccessIterator>)
01591
01592 if (__first == __middle || __last == __middle)
01593 return;
01594
01595 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
01596 _Distance;
01597 typedef typename iterator_traits<_RandomAccessIterator>::value_type
01598 _ValueType;
01599
01600 const _Distance __n = __last - __first;
01601 const _Distance __k = __middle - __first;
01602 const _Distance __l = __n - __k;
01603
01604 if (__k == __l)
01605 {
01606 std::swap_ranges(__first, __middle, __middle);
01607 return;
01608 }
01609
01610 const _Distance __d = std::__gcd(__n, __k);
01611
01612 for (_Distance __i = 0; __i < __d; __i++)
01613 {
01614 _ValueType __tmp = _GLIBCXX_MOVE(*__first);
01615 _RandomAccessIterator __p = __first;
01616
01617 if (__k < __l)
01618 {
01619 for (_Distance __j = 0; __j < __l / __d; __j++)
01620 {
01621 if (__p > __first + __l)
01622 {
01623 *__p = _GLIBCXX_MOVE(*(__p - __l));
01624 __p -= __l;
01625 }
01626
01627 *__p = _GLIBCXX_MOVE(*(__p + __k));
01628 __p += __k;
01629 }
01630 }
01631 else
01632 {
01633 for (_Distance __j = 0; __j < __k / __d - 1; __j ++)
01634 {
01635 if (__p < __last - __k)
01636 {
01637 *__p = _GLIBCXX_MOVE(*(__p + __k));
01638 __p += __k;
01639 }
01640 *__p = _GLIBCXX_MOVE(*(__p - __l));
01641 __p -= __l;
01642 }
01643 }
01644
01645 *__p = _GLIBCXX_MOVE(__tmp);
01646 ++__first;
01647 }
01648 }
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660
01661
01662
01663
01664
01665
01666
01667
01668
01669 template<typename _ForwardIterator>
01670 inline void
01671 rotate(_ForwardIterator __first, _ForwardIterator __middle,
01672 _ForwardIterator __last)
01673 {
01674
01675 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
01676 _ForwardIterator>)
01677 __glibcxx_requires_valid_range(__first, __middle);
01678 __glibcxx_requires_valid_range(__middle, __last);
01679
01680 typedef typename iterator_traits<_ForwardIterator>::iterator_category
01681 _IterType;
01682 std::__rotate(__first, __middle, __last, _IterType());
01683 }
01684
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694
01695
01696
01697
01698
01699
01700
01701
01702
01703 template<typename _ForwardIterator, typename _OutputIterator>
01704 _OutputIterator
01705 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
01706 _ForwardIterator __last, _OutputIterator __result)
01707 {
01708
01709 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
01710 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
01711 typename iterator_traits<_ForwardIterator>::value_type>)
01712 __glibcxx_requires_valid_range(__first, __middle);
01713 __glibcxx_requires_valid_range(__middle, __last);
01714
01715 return std::copy(__first, __middle,
01716 std::copy(__middle, __last, __result));
01717 }
01718
01719
01720 template<typename _ForwardIterator, typename _Predicate>
01721 _ForwardIterator
01722 __partition(_ForwardIterator __first, _ForwardIterator __last,
01723 _Predicate __pred, forward_iterator_tag)
01724 {
01725 if (__first == __last)
01726 return __first;
01727
01728 while (__pred(*__first))
01729 if (++__first == __last)
01730 return __first;
01731
01732 _ForwardIterator __next = __first;
01733
01734 while (++__next != __last)
01735 if (__pred(*__next))
01736 {
01737 std::iter_swap(__first, __next);
01738 ++__first;
01739 }
01740
01741 return __first;
01742 }
01743
01744
01745 template<typename _BidirectionalIterator, typename _Predicate>
01746 _BidirectionalIterator
01747 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
01748 _Predicate __pred, bidirectional_iterator_tag)
01749 {
01750 while (true)
01751 {
01752 while (true)
01753 if (__first == __last)
01754 return __first;
01755 else if (__pred(*__first))
01756 ++__first;
01757 else
01758 break;
01759 --__last;
01760 while (true)
01761 if (__first == __last)
01762 return __first;
01763 else if (!bool(__pred(*__last)))
01764 --__last;
01765 else
01766 break;
01767 std::iter_swap(__first, __last);
01768 ++__first;
01769 }
01770 }
01771
01772
01773
01774
01775 template<typename _ForwardIterator, typename _Predicate, typename _Distance>
01776 _ForwardIterator
01777 __inplace_stable_partition(_ForwardIterator __first,
01778 _ForwardIterator __last,
01779 _Predicate __pred, _Distance __len)
01780 {
01781 if (__len == 1)
01782 return __pred(*__first) ? __last : __first;
01783 _ForwardIterator __middle = __first;
01784 std::advance(__middle, __len / 2);
01785 _ForwardIterator __begin = std::__inplace_stable_partition(__first,
01786 __middle,
01787 __pred,
01788 __len / 2);
01789 _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last,
01790 __pred,
01791 __len
01792 - __len / 2);
01793 std::rotate(__begin, __middle, __end);
01794 std::advance(__begin, std::distance(__middle, __end));
01795 return __begin;
01796 }
01797
01798
01799 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
01800 typename _Distance>
01801 _ForwardIterator
01802 __stable_partition_adaptive(_ForwardIterator __first,
01803 _ForwardIterator __last,
01804 _Predicate __pred, _Distance __len,
01805 _Pointer __buffer,
01806 _Distance __buffer_size)
01807 {
01808 if (__len <= __buffer_size)
01809 {
01810 _ForwardIterator __result1 = __first;
01811 _Pointer __result2 = __buffer;
01812 for (; __first != __last; ++__first)
01813 if (__pred(*__first))
01814 {
01815 *__result1 = *__first;
01816 ++__result1;
01817 }
01818 else
01819 {
01820 *__result2 = *__first;
01821 ++__result2;
01822 }
01823 std::copy(__buffer, __result2, __result1);
01824 return __result1;
01825 }
01826 else
01827 {
01828 _ForwardIterator __middle = __first;
01829 std::advance(__middle, __len / 2);
01830 _ForwardIterator __begin =
01831 std::__stable_partition_adaptive(__first, __middle, __pred,
01832 __len / 2, __buffer,
01833 __buffer_size);
01834 _ForwardIterator __end =
01835 std::__stable_partition_adaptive(__middle, __last, __pred,
01836 __len - __len / 2,
01837 __buffer, __buffer_size);
01838 std::rotate(__begin, __middle, __end);
01839 std::advance(__begin, std::distance(__middle, __end));
01840 return __begin;
01841 }
01842 }
01843
01844
01845
01846
01847
01848
01849
01850
01851
01852
01853
01854
01855
01856
01857
01858
01859
01860
01861 template<typename _ForwardIterator, typename _Predicate>
01862 _ForwardIterator
01863 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
01864 _Predicate __pred)
01865 {
01866
01867 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
01868 _ForwardIterator>)
01869 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
01870 typename iterator_traits<_ForwardIterator>::value_type>)
01871 __glibcxx_requires_valid_range(__first, __last);
01872
01873 if (__first == __last)
01874 return __first;
01875 else
01876 {
01877 typedef typename iterator_traits<_ForwardIterator>::value_type
01878 _ValueType;
01879 typedef typename iterator_traits<_ForwardIterator>::difference_type
01880 _DistanceType;
01881
01882 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
01883 __last);
01884 if (__buf.size() > 0)
01885 return
01886 std::__stable_partition_adaptive(__first, __last, __pred,
01887 _DistanceType(__buf.requested_size()),
01888 __buf.begin(),
01889 _DistanceType(__buf.size()));
01890 else
01891 return
01892 std::__inplace_stable_partition(__first, __last, __pred,
01893 _DistanceType(__buf.requested_size()));
01894 }
01895 }
01896
01897
01898 template<typename _RandomAccessIterator>
01899 void
01900 __heap_select(_RandomAccessIterator __first,
01901 _RandomAccessIterator __middle,
01902 _RandomAccessIterator __last)
01903 {
01904 std::make_heap(__first, __middle);
01905 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
01906 if (*__i < *__first)
01907 std::__pop_heap(__first, __middle, __i);
01908 }
01909
01910
01911 template<typename _RandomAccessIterator, typename _Compare>
01912 void
01913 __heap_select(_RandomAccessIterator __first,
01914 _RandomAccessIterator __middle,
01915 _RandomAccessIterator __last, _Compare __comp)
01916 {
01917 std::make_heap(__first, __middle, __comp);
01918 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
01919 if (__comp(*__i, *__first))
01920 std::__pop_heap(__first, __middle, __i, __comp);
01921 }
01922
01923
01924
01925
01926
01927
01928
01929
01930
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940
01941
01942
01943 template<typename _InputIterator, typename _RandomAccessIterator>
01944 _RandomAccessIterator
01945 partial_sort_copy(_InputIterator __first, _InputIterator __last,
01946 _RandomAccessIterator __result_first,
01947 _RandomAccessIterator __result_last)
01948 {
01949 typedef typename iterator_traits<_InputIterator>::value_type
01950 _InputValueType;
01951 typedef typename iterator_traits<_RandomAccessIterator>::value_type
01952 _OutputValueType;
01953 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
01954 _DistanceType;
01955
01956
01957 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
01958 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
01959 _OutputValueType>)
01960 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
01961 _OutputValueType>)
01962 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
01963 __glibcxx_requires_valid_range(__first, __last);
01964 __glibcxx_requires_valid_range(__result_first, __result_last);
01965
01966 if (__result_first == __result_last)
01967 return __result_last;
01968 _RandomAccessIterator __result_real_last = __result_first;
01969 while(__first != __last && __result_real_last != __result_last)
01970 {
01971 *__result_real_last = *__first;
01972 ++__result_real_last;
01973 ++__first;
01974 }
01975 std::make_heap(__result_first, __result_real_last);
01976 while (__first != __last)
01977 {
01978 if (*__first < *__result_first)
01979 std::__adjust_heap(__result_first, _DistanceType(0),
01980 _DistanceType(__result_real_last
01981 - __result_first),
01982 _InputValueType(*__first));
01983 ++__first;
01984 }
01985 std::sort_heap(__result_first, __result_real_last);
01986 return __result_real_last;
01987 }
01988
01989
01990
01991
01992
01993
01994
01995
01996
01997
01998
01999
02000
02001
02002
02003
02004
02005
02006
02007
02008
02009 template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
02010 _RandomAccessIterator
02011 partial_sort_copy(_InputIterator __first, _InputIterator __last,
02012 _RandomAccessIterator __result_first,
02013 _RandomAccessIterator __result_last,
02014 _Compare __comp)
02015 {
02016 typedef typename iterator_traits<_InputIterator>::value_type
02017 _InputValueType;
02018 typedef typename iterator_traits<_RandomAccessIterator>::value_type
02019 _OutputValueType;
02020 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
02021 _DistanceType;
02022
02023
02024 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
02025 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
02026 _RandomAccessIterator>)
02027 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
02028 _OutputValueType>)
02029 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02030 _InputValueType, _OutputValueType>)
02031 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02032 _OutputValueType, _OutputValueType>)
02033 __glibcxx_requires_valid_range(__first, __last);
02034 __glibcxx_requires_valid_range(__result_first, __result_last);
02035
02036 if (__result_first == __result_last)
02037 return __result_last;
02038 _RandomAccessIterator __result_real_last = __result_first;
02039 while(__first != __last && __result_real_last != __result_last)
02040 {
02041 *__result_real_last = *__first;
02042 ++__result_real_last;
02043 ++__first;
02044 }
02045 std::make_heap(__result_first, __result_real_last, __comp);
02046 while (__first != __last)
02047 {
02048 if (__comp(*__first, *__result_first))
02049 std::__adjust_heap(__result_first, _DistanceType(0),
02050 _DistanceType(__result_real_last
02051 - __result_first),
02052 _InputValueType(*__first),
02053 __comp);
02054 ++__first;
02055 }
02056 std::sort_heap(__result_first, __result_real_last, __comp);
02057 return __result_real_last;
02058 }
02059
02060
02061 template<typename _RandomAccessIterator, typename _Tp>
02062 void
02063 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val)
02064 {
02065 _RandomAccessIterator __next = __last;
02066 --__next;
02067 while (__val < *__next)
02068 {
02069 *__last = *__next;
02070 __last = __next;
02071 --__next;
02072 }
02073 *__last = __val;
02074 }
02075
02076
02077 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
02078 void
02079 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val,
02080 _Compare __comp)
02081 {
02082 _RandomAccessIterator __next = __last;
02083 --__next;
02084 while (__comp(__val, *__next))
02085 {
02086 *__last = *__next;
02087 __last = __next;
02088 --__next;
02089 }
02090 *__last = __val;
02091 }
02092
02093
02094 template<typename _RandomAccessIterator>
02095 void
02096 __insertion_sort(_RandomAccessIterator __first,
02097 _RandomAccessIterator __last)
02098 {
02099 if (__first == __last)
02100 return;
02101
02102 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
02103 {
02104 typename iterator_traits<_RandomAccessIterator>::value_type
02105 __val = *__i;
02106 if (__val < *__first)
02107 {
02108 std::copy_backward(__first, __i, __i + 1);
02109 *__first = __val;
02110 }
02111 else
02112 std::__unguarded_linear_insert(__i, __val);
02113 }
02114 }
02115
02116
02117 template<typename _RandomAccessIterator, typename _Compare>
02118 void
02119 __insertion_sort(_RandomAccessIterator __first,
02120 _RandomAccessIterator __last, _Compare __comp)
02121 {
02122 if (__first == __last) return;
02123
02124 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
02125 {
02126 typename iterator_traits<_RandomAccessIterator>::value_type
02127 __val = *__i;
02128 if (__comp(__val, *__first))
02129 {
02130 std::copy_backward(__first, __i, __i + 1);
02131 *__first = __val;
02132 }
02133 else
02134 std::__unguarded_linear_insert(__i, __val, __comp);
02135 }
02136 }
02137
02138
02139 template<typename _RandomAccessIterator>
02140 inline void
02141 __unguarded_insertion_sort(_RandomAccessIterator __first,
02142 _RandomAccessIterator __last)
02143 {
02144 typedef typename iterator_traits<_RandomAccessIterator>::value_type
02145 _ValueType;
02146
02147 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
02148 std::__unguarded_linear_insert(__i, _ValueType(*__i));
02149 }
02150
02151
02152 template<typename _RandomAccessIterator, typename _Compare>
02153 inline void
02154 __unguarded_insertion_sort(_RandomAccessIterator __first,
02155 _RandomAccessIterator __last, _Compare __comp)
02156 {
02157 typedef typename iterator_traits<_RandomAccessIterator>::value_type
02158 _ValueType;
02159
02160 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
02161 std::__unguarded_linear_insert(__i, _ValueType(*__i), __comp);
02162 }
02163
02164
02165
02166
02167
02168 enum { _S_threshold = 16 };
02169
02170
02171 template<typename _RandomAccessIterator>
02172 void
02173 __final_insertion_sort(_RandomAccessIterator __first,
02174 _RandomAccessIterator __last)
02175 {
02176 if (__last - __first > int(_S_threshold))
02177 {
02178 std::__insertion_sort(__first, __first + int(_S_threshold));
02179 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last);
02180 }
02181 else
02182 std::__insertion_sort(__first, __last);
02183 }
02184
02185
02186 template<typename _RandomAccessIterator, typename _Compare>
02187 void
02188 __final_insertion_sort(_RandomAccessIterator __first,
02189 _RandomAccessIterator __last, _Compare __comp)
02190 {
02191 if (__last - __first > int(_S_threshold))
02192 {
02193 std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
02194 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
02195 __comp);
02196 }
02197 else
02198 std::__insertion_sort(__first, __last, __comp);
02199 }
02200
02201
02202 template<typename _RandomAccessIterator, typename _Tp>
02203 _RandomAccessIterator
02204 __unguarded_partition(_RandomAccessIterator __first,
02205 _RandomAccessIterator __last, _Tp __pivot)
02206 {
02207 while (true)
02208 {
02209 while (*__first < __pivot)
02210 ++__first;
02211 --__last;
02212 while (__pivot < *__last)
02213 --__last;
02214 if (!(__first < __last))
02215 return __first;
02216 std::iter_swap(__first, __last);
02217 ++__first;
02218 }
02219 }
02220
02221
02222 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
02223 _RandomAccessIterator
02224 __unguarded_partition(_RandomAccessIterator __first,
02225 _RandomAccessIterator __last,
02226 _Tp __pivot, _Compare __comp)
02227 {
02228 while (true)
02229 {
02230 while (__comp(*__first, __pivot))
02231 ++__first;
02232 --__last;
02233 while (__comp(__pivot, *__last))
02234 --__last;
02235 if (!(__first < __last))
02236 return __first;
02237 std::iter_swap(__first, __last);
02238 ++__first;
02239 }
02240 }
02241
02242
02243 template<typename _RandomAccessIterator, typename _Size>
02244 void
02245 __introsort_loop(_RandomAccessIterator __first,
02246 _RandomAccessIterator __last,
02247 _Size __depth_limit)
02248 {
02249 typedef typename iterator_traits<_RandomAccessIterator>::value_type
02250 _ValueType;
02251
02252 while (__last - __first > int(_S_threshold))
02253 {
02254 if (__depth_limit == 0)
02255 {
02256 _GLIBCXX_STD_P::partial_sort(__first, __last, __last);
02257 return;
02258 }
02259 --__depth_limit;
02260 _RandomAccessIterator __cut =
02261 std::__unguarded_partition(__first, __last,
02262 _ValueType(std::__median(*__first,
02263 *(__first
02264 + (__last
02265 - __first)
02266 / 2),
02267 *(__last
02268 - 1))));
02269 std::__introsort_loop(__cut, __last, __depth_limit);
02270 __last = __cut;
02271 }
02272 }
02273
02274
02275 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
02276 void
02277 __introsort_loop(_RandomAccessIterator __first,
02278 _RandomAccessIterator __last,
02279 _Size __depth_limit, _Compare __comp)
02280 {
02281 typedef typename iterator_traits<_RandomAccessIterator>::value_type
02282 _ValueType;
02283
02284 while (__last - __first > int(_S_threshold))
02285 {
02286 if (__depth_limit == 0)
02287 {
02288 _GLIBCXX_STD_P::partial_sort(__first, __last, __last, __comp);
02289 return;
02290 }
02291 --__depth_limit;
02292 _RandomAccessIterator __cut =
02293 std::__unguarded_partition(__first, __last,
02294 _ValueType(std::__median(*__first,
02295 *(__first
02296 + (__last
02297 - __first)
02298 / 2),
02299 *(__last - 1),
02300 __comp)),
02301 __comp);
02302 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
02303 __last = __cut;
02304 }
02305 }
02306
02307
02308 template<typename _Size>
02309 inline _Size
02310 __lg(_Size __n)
02311 {
02312 _Size __k;
02313 for (__k = 0; __n != 0; __n >>= 1)
02314 ++__k;
02315 return __k - 1;
02316 }
02317
02318 inline int
02319 __lg(int __n)
02320 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
02321
02322 inline long
02323 __lg(long __n)
02324 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
02325
02326 inline long long
02327 __lg(long long __n)
02328 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
02329
02330
02331
02332 template<typename _RandomAccessIterator, typename _Size>
02333 void
02334 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
02335 _RandomAccessIterator __last, _Size __depth_limit)
02336 {
02337 typedef typename iterator_traits<_RandomAccessIterator>::value_type
02338 _ValueType;
02339
02340 while (__last - __first > 3)
02341 {
02342 if (__depth_limit == 0)
02343 {
02344 std::__heap_select(__first, __nth + 1, __last);
02345
02346
02347 std::iter_swap(__first, __nth);
02348 return;
02349 }
02350 --__depth_limit;
02351 _RandomAccessIterator __cut =
02352 std::__unguarded_partition(__first, __last,
02353 _ValueType(std::__median(*__first,
02354 *(__first
02355 + (__last
02356 - __first)
02357 / 2),
02358 *(__last
02359 - 1))));
02360 if (__cut <= __nth)
02361 __first = __cut;
02362 else
02363 __last = __cut;
02364 }
02365 std::__insertion_sort(__first, __last);
02366 }
02367
02368 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
02369 void
02370 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
02371 _RandomAccessIterator __last, _Size __depth_limit,
02372 _Compare __comp)
02373 {
02374 typedef typename iterator_traits<_RandomAccessIterator>::value_type
02375 _ValueType;
02376
02377 while (__last - __first > 3)
02378 {
02379 if (__depth_limit == 0)
02380 {
02381 std::__heap_select(__first, __nth + 1, __last, __comp);
02382
02383 std::iter_swap(__first, __nth);
02384 return;
02385 }
02386 --__depth_limit;
02387 _RandomAccessIterator __cut =
02388 std::__unguarded_partition(__first, __last,
02389 _ValueType(std::__median(*__first,
02390 *(__first
02391 + (__last
02392 - __first)
02393 / 2),
02394 *(__last - 1),
02395 __comp)),
02396 __comp);
02397 if (__cut <= __nth)
02398 __first = __cut;
02399 else
02400 __last = __cut;
02401 }
02402 std::__insertion_sort(__first, __last, __comp);
02403 }
02404
02405
02406
02407
02408
02409
02410
02411
02412
02413
02414
02415
02416
02417
02418 template<typename _ForwardIterator, typename _Tp>
02419 _ForwardIterator
02420 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
02421 const _Tp& __val)
02422 {
02423 typedef typename iterator_traits<_ForwardIterator>::value_type
02424 _ValueType;
02425 typedef typename iterator_traits<_ForwardIterator>::difference_type
02426 _DistanceType;
02427
02428
02429 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02430 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
02431 __glibcxx_requires_partitioned_lower(__first, __last, __val);
02432
02433 _DistanceType __len = std::distance(__first, __last);
02434 _DistanceType __half;
02435 _ForwardIterator __middle;
02436
02437 while (__len > 0)
02438 {
02439 __half = __len >> 1;
02440 __middle = __first;
02441 std::advance(__middle, __half);
02442 if (*__middle < __val)
02443 {
02444 __first = __middle;
02445 ++__first;
02446 __len = __len - __half - 1;
02447 }
02448 else
02449 __len = __half;
02450 }
02451 return __first;
02452 }
02453
02454
02455
02456
02457
02458
02459
02460
02461
02462
02463
02464
02465
02466
02467
02468
02469 template<typename _ForwardIterator, typename _Tp, typename _Compare>
02470 _ForwardIterator
02471 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
02472 const _Tp& __val, _Compare __comp)
02473 {
02474 typedef typename iterator_traits<_ForwardIterator>::value_type
02475 _ValueType;
02476 typedef typename iterator_traits<_ForwardIterator>::difference_type
02477 _DistanceType;
02478
02479
02480 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02481 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02482 _ValueType, _Tp>)
02483 __glibcxx_requires_partitioned_lower_pred(__first, __last,
02484 __val, __comp);
02485
02486 _DistanceType __len = std::distance(__first, __last);
02487 _DistanceType __half;
02488 _ForwardIterator __middle;
02489
02490 while (__len > 0)
02491 {
02492 __half = __len >> 1;
02493 __middle = __first;
02494 std::advance(__middle, __half);
02495 if (__comp(*__middle, __val))
02496 {
02497 __first = __middle;
02498 ++__first;
02499 __len = __len - __half - 1;
02500 }
02501 else
02502 __len = __half;
02503 }
02504 return __first;
02505 }
02506
02507
02508
02509
02510
02511
02512
02513
02514
02515
02516
02517
02518 template<typename _ForwardIterator, typename _Tp>
02519 _ForwardIterator
02520 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
02521 const _Tp& __val)
02522 {
02523 typedef typename iterator_traits<_ForwardIterator>::value_type
02524 _ValueType;
02525 typedef typename iterator_traits<_ForwardIterator>::difference_type
02526 _DistanceType;
02527
02528
02529 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02530 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
02531 __glibcxx_requires_partitioned_upper(__first, __last, __val);
02532
02533 _DistanceType __len = std::distance(__first, __last);
02534 _DistanceType __half;
02535 _ForwardIterator __middle;
02536
02537 while (__len > 0)
02538 {
02539 __half = __len >> 1;
02540 __middle = __first;
02541 std::advance(__middle, __half);
02542 if (__val < *__middle)
02543 __len = __half;
02544 else
02545 {
02546 __first = __middle;
02547 ++__first;
02548 __len = __len - __half - 1;
02549 }
02550 }
02551 return __first;
02552 }
02553
02554
02555
02556
02557
02558
02559
02560
02561
02562
02563
02564
02565
02566
02567
02568
02569 template<typename _ForwardIterator, typename _Tp, typename _Compare>
02570 _ForwardIterator
02571 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
02572 const _Tp& __val, _Compare __comp)
02573 {
02574 typedef typename iterator_traits<_ForwardIterator>::value_type
02575 _ValueType;
02576 typedef typename iterator_traits<_ForwardIterator>::difference_type
02577 _DistanceType;
02578
02579
02580 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02581 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02582 _Tp, _ValueType>)
02583 __glibcxx_requires_partitioned_upper_pred(__first, __last,
02584 __val, __comp);
02585
02586 _DistanceType __len = std::distance(__first, __last);
02587 _DistanceType __half;
02588 _ForwardIterator __middle;
02589
02590 while (__len > 0)
02591 {
02592 __half = __len >> 1;
02593 __middle = __first;
02594 std::advance(__middle, __half);
02595 if (__comp(__val, *__middle))
02596 __len = __half;
02597 else
02598 {
02599 __first = __middle;
02600 ++__first;
02601 __len = __len - __half - 1;
02602 }
02603 }
02604 return __first;
02605 }
02606
02607
02608
02609
02610
02611
02612
02613
02614
02615
02616
02617
02618
02619
02620
02621
02622
02623
02624 template<typename _ForwardIterator, typename _Tp>
02625 pair<_ForwardIterator, _ForwardIterator>
02626 equal_range(_ForwardIterator __first, _ForwardIterator __last,
02627 const _Tp& __val)
02628 {
02629 typedef typename iterator_traits<_ForwardIterator>::value_type
02630 _ValueType;
02631 typedef typename iterator_traits<_ForwardIterator>::difference_type
02632 _DistanceType;
02633
02634
02635 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02636 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
02637 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
02638 __glibcxx_requires_partitioned_lower(__first, __last, __val);
02639 __glibcxx_requires_partitioned_upper(__first, __last, __val);
02640
02641 _DistanceType __len = std::distance(__first, __last);
02642 _DistanceType __half;
02643 _ForwardIterator __middle, __left, __right;
02644
02645 while (__len > 0)
02646 {
02647 __half = __len >> 1;
02648 __middle = __first;
02649 std::advance(__middle, __half);
02650 if (*__middle < __val)
02651 {
02652 __first = __middle;
02653 ++__first;
02654 __len = __len - __half - 1;
02655 }
02656 else if (__val < *__middle)
02657 __len = __half;
02658 else
02659 {
02660 __left = std::lower_bound(__first, __middle, __val);
02661 std::advance(__first, __len);
02662 __right = std::upper_bound(++__middle, __first, __val);
02663 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
02664 }
02665 }
02666 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
02667 }
02668
02669
02670
02671
02672
02673
02674
02675
02676
02677
02678
02679
02680
02681
02682
02683
02684
02685
02686 template<typename _ForwardIterator, typename _Tp, typename _Compare>
02687 pair<_ForwardIterator, _ForwardIterator>
02688 equal_range(_ForwardIterator __first, _ForwardIterator __last,
02689 const _Tp& __val,
02690 _Compare __comp)
02691 {
02692 typedef typename iterator_traits<_ForwardIterator>::value_type
02693 _ValueType;
02694 typedef typename iterator_traits<_ForwardIterator>::difference_type
02695 _DistanceType;
02696
02697
02698 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02699 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02700 _ValueType, _Tp>)
02701 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02702 _Tp, _ValueType>)
02703 __glibcxx_requires_partitioned_lower_pred(__first, __last,
02704 __val, __comp);
02705 __glibcxx_requires_partitioned_upper_pred(__first, __last,
02706 __val, __comp);
02707
02708 _DistanceType __len = std::distance(__first, __last);
02709 _DistanceType __half;
02710 _ForwardIterator __middle, __left, __right;
02711
02712 while (__len > 0)
02713 {
02714 __half = __len >> 1;
02715 __middle = __first;
02716 std::advance(__middle, __half);
02717 if (__comp(*__middle, __val))
02718 {
02719 __first = __middle;
02720 ++__first;
02721 __len = __len - __half - 1;
02722 }
02723 else if (__comp(__val, *__middle))
02724 __len = __half;
02725 else
02726 {
02727 __left = std::lower_bound(__first, __middle, __val, __comp);
02728 std::advance(__first, __len);
02729 __right = std::upper_bound(++__middle, __first, __val, __comp);
02730 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
02731 }
02732 }
02733 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
02734 }
02735
02736
02737
02738
02739
02740
02741
02742
02743
02744
02745
02746
02747 template<typename _ForwardIterator, typename _Tp>
02748 bool
02749 binary_search(_ForwardIterator __first, _ForwardIterator __last,
02750 const _Tp& __val)
02751 {
02752 typedef typename iterator_traits<_ForwardIterator>::value_type
02753 _ValueType;
02754
02755
02756 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02757 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
02758 __glibcxx_requires_partitioned_lower(__first, __last, __val);
02759 __glibcxx_requires_partitioned_upper(__first, __last, __val);
02760
02761 _ForwardIterator __i = std::lower_bound(__first, __last, __val);
02762 return __i != __last && !(__val < *__i);
02763 }
02764
02765
02766
02767
02768
02769
02770
02771
02772
02773
02774
02775
02776
02777
02778
02779
02780 template<typename _ForwardIterator, typename _Tp, typename _Compare>
02781 bool
02782 binary_search(_ForwardIterator __first, _ForwardIterator __last,
02783 const _Tp& __val, _Compare __comp)
02784 {
02785 typedef typename iterator_traits<_ForwardIterator>::value_type
02786 _ValueType;
02787
02788
02789 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
02790 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
02791 _Tp, _ValueType>)
02792 __glibcxx_requires_partitioned_lower_pred(__first, __last,
02793 __val, __comp);
02794 __glibcxx_requires_partitioned_upper_pred(__first, __last,
02795 __val, __comp);
02796
02797 _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
02798 return __i != __last && !bool(__comp(__val, *__i));
02799 }
02800
02801
02802
02803
02804 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
02805 typename _BidirectionalIterator3>
02806 _BidirectionalIterator3
02807 __merge_backward(_BidirectionalIterator1 __first1,
02808 _BidirectionalIterator1 __last1,
02809 _BidirectionalIterator2 __first2,
02810 _BidirectionalIterator2 __last2,
02811 _BidirectionalIterator3 __result)
02812 {
02813 if (__first1 == __last1)
02814 return std::copy_backward(__first2, __last2, __result);
02815 if (__first2 == __last2)
02816 return std::copy_backward(__first1, __last1, __result);
02817 --__last1;
02818 --__last2;
02819 while (true)
02820 {
02821 if (*__last2 < *__last1)
02822 {
02823 *--__result = *__last1;
02824 if (__first1 == __last1)
02825 return std::copy_backward(__first2, ++__last2, __result);
02826 --__last1;
02827 }
02828 else
02829 {
02830 *--__result = *__last2;
02831 if (__first2 == __last2)
02832 return std::copy_backward(__first1, ++__last1, __result);
02833 --__last2;
02834 }
02835 }
02836 }
02837
02838
02839 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
02840 typename _BidirectionalIterator3, typename _Compare>
02841 _BidirectionalIterator3
02842 __merge_backward(_BidirectionalIterator1 __first1,
02843 _BidirectionalIterator1 __last1,
02844 _BidirectionalIterator2 __first2,
02845 _BidirectionalIterator2 __last2,
02846 _BidirectionalIterator3 __result,
02847 _Compare __comp)
02848 {
02849 if (__first1 == __last1)
02850 return std::copy_backward(__first2, __last2, __result);
02851 if (__first2 == __last2)
02852 return std::copy_backward(__first1, __last1, __result);
02853 --__last1;
02854 --__last2;
02855 while (true)
02856 {
02857 if (__comp(*__last2, *__last1))
02858 {
02859 *--__result = *__last1;
02860 if (__first1 == __last1)
02861 return std::copy_backward(__first2, ++__last2, __result);
02862 --__last1;
02863 }
02864 else
02865 {
02866 *--__result = *__last2;
02867 if (__first2 == __last2)
02868 return std::copy_backward(__first1, ++__last1, __result);
02869 --__last2;
02870 }
02871 }
02872 }
02873
02874
02875 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
02876 typename _Distance>
02877 _BidirectionalIterator1
02878 __rotate_adaptive(_BidirectionalIterator1 __first,
02879 _BidirectionalIterator1 __middle,
02880 _BidirectionalIterator1 __last,
02881 _Distance __len1, _Distance __len2,
02882 _BidirectionalIterator2 __buffer,
02883 _Distance __buffer_size)
02884 {
02885 _BidirectionalIterator2 __buffer_end;
02886 if (__len1 > __len2 && __len2 <= __buffer_size)
02887 {
02888 __buffer_end = std::copy(__middle, __last, __buffer);
02889 std::copy_backward(__first, __middle, __last);
02890 return std::copy(__buffer, __buffer_end, __first);
02891 }
02892 else if (__len1 <= __buffer_size)
02893 {
02894 __buffer_end = std::copy(__first, __middle, __buffer);
02895 std::copy(__middle, __last, __first);
02896 return std::copy_backward(__buffer, __buffer_end, __last);
02897 }
02898 else
02899 {
02900 std::rotate(__first, __middle, __last);
02901 std::advance(__first, std::distance(__middle, __last));
02902 return __first;
02903 }
02904 }
02905
02906
02907 template<typename _BidirectionalIterator, typename _Distance,
02908 typename _Pointer>
02909 void
02910 __merge_adaptive(_BidirectionalIterator __first,
02911 _BidirectionalIterator __middle,
02912 _BidirectionalIterator __last,
02913 _Distance __len1, _Distance __len2,
02914 _Pointer __buffer, _Distance __buffer_size)
02915 {
02916 if (__len1 <= __len2 && __len1 <= __buffer_size)
02917 {
02918 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
02919 _GLIBCXX_STD_P::merge(__buffer, __buffer_end, __middle, __last,
02920 __first);
02921 }
02922 else if (__len2 <= __buffer_size)
02923 {
02924 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
02925 std::__merge_backward(__first, __middle, __buffer,
02926 __buffer_end, __last);
02927 }
02928 else
02929 {
02930 _BidirectionalIterator __first_cut = __first;
02931 _BidirectionalIterator __second_cut = __middle;
02932 _Distance __len11 = 0;
02933 _Distance __len22 = 0;
02934 if (__len1 > __len2)
02935 {
02936 __len11 = __len1 / 2;
02937 std::advance(__first_cut, __len11);
02938 __second_cut = std::lower_bound(__middle, __last,
02939 *__first_cut);
02940 __len22 = std::distance(__middle, __second_cut);
02941 }
02942 else
02943 {
02944 __len22 = __len2 / 2;
02945 std::advance(__second_cut, __len22);
02946 __first_cut = std::upper_bound(__first, __middle,
02947 *__second_cut);
02948 __len11 = std::distance(__first, __first_cut);
02949 }
02950 _BidirectionalIterator __new_middle =
02951 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
02952 __len1 - __len11, __len22, __buffer,
02953 __buffer_size);
02954 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
02955 __len22, __buffer, __buffer_size);
02956 std::__merge_adaptive(__new_middle, __second_cut, __last,
02957 __len1 - __len11,
02958 __len2 - __len22, __buffer, __buffer_size);
02959 }
02960 }
02961
02962
02963 template<typename _BidirectionalIterator, typename _Distance,
02964 typename _Pointer, typename _Compare>
02965 void
02966 __merge_adaptive(_BidirectionalIterator __first,
02967 _BidirectionalIterator __middle,
02968 _BidirectionalIterator __last,
02969 _Distance __len1, _Distance __len2,
02970 _Pointer __buffer, _Distance __buffer_size,
02971 _Compare __comp)
02972 {
02973 if (__len1 <= __len2 && __len1 <= __buffer_size)
02974 {
02975 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
02976 _GLIBCXX_STD_P::merge(__buffer, __buffer_end, __middle, __last,
02977 __first, __comp);
02978 }
02979 else if (__len2 <= __buffer_size)
02980 {
02981 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
02982 std::__merge_backward(__first, __middle, __buffer, __buffer_end,
02983 __last, __comp);
02984 }
02985 else
02986 {
02987 _BidirectionalIterator __first_cut = __first;
02988 _BidirectionalIterator __second_cut = __middle;
02989 _Distance __len11 = 0;
02990 _Distance __len22 = 0;
02991 if (__len1 > __len2)
02992 {
02993 __len11 = __len1 / 2;
02994 std::advance(__first_cut, __len11);
02995 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
02996 __comp);
02997 __len22 = std::distance(__middle, __second_cut);
02998 }
02999 else
03000 {
03001 __len22 = __len2 / 2;
03002 std::advance(__second_cut, __len22);
03003 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
03004 __comp);
03005 __len11 = std::distance(__first, __first_cut);
03006 }
03007 _BidirectionalIterator __new_middle =
03008 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
03009 __len1 - __len11, __len22, __buffer,
03010 __buffer_size);
03011 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
03012 __len22, __buffer, __buffer_size, __comp);
03013 std::__merge_adaptive(__new_middle, __second_cut, __last,
03014 __len1 - __len11,
03015 __len2 - __len22, __buffer,
03016 __buffer_size, __comp);
03017 }
03018 }
03019
03020
03021 template<typename _BidirectionalIterator, typename _Distance>
03022 void
03023 __merge_without_buffer(_BidirectionalIterator __first,
03024 _BidirectionalIterator __middle,
03025 _BidirectionalIterator __last,
03026 _Distance __len1, _Distance __len2)
03027 {
03028 if (__len1 == 0 || __len2 == 0)
03029 return;
03030 if (__len1 + __len2 == 2)
03031 {
03032 if (*__middle < *__first)
03033 std::iter_swap(__first, __middle);
03034 return;
03035 }
03036 _BidirectionalIterator __first_cut = __first;
03037 _BidirectionalIterator __second_cut = __middle;
03038 _Distance __len11 = 0;
03039 _Distance __len22 = 0;
03040 if (__len1 > __len2)
03041 {
03042 __len11 = __len1 / 2;
03043 std::advance(__first_cut, __len11);
03044 __second_cut = std::lower_bound(__middle, __last, *__first_cut);
03045 __len22 = std::distance(__middle, __second_cut);
03046 }
03047 else
03048 {
03049 __len22 = __len2 / 2;
03050 std::advance(__second_cut, __len22);
03051 __first_cut = std::upper_bound(__first, __middle, *__second_cut);
03052 __len11 = std::distance(__first, __first_cut);
03053 }
03054 std::rotate(__first_cut, __middle, __second_cut);
03055 _BidirectionalIterator __new_middle = __first_cut;
03056 std::advance(__new_middle, std::distance(__middle, __second_cut));
03057 std::__merge_without_buffer(__first, __first_cut, __new_middle,
03058 __len11, __len22);
03059 std::__merge_without_buffer(__new_middle, __second_cut, __last,
03060 __len1 - __len11, __len2 - __len22);
03061 }
03062
03063
03064 template<typename _BidirectionalIterator, typename _Distance,
03065 typename _Compare>
03066 void
03067 __merge_without_buffer(_BidirectionalIterator __first,
03068 _BidirectionalIterator __middle,
03069 _BidirectionalIterator __last,
03070 _Distance __len1, _Distance __len2,
03071 _Compare __comp)
03072 {
03073 if (__len1 == 0 || __len2 == 0)
03074 return;
03075 if (__len1 + __len2 == 2)
03076 {
03077 if (__comp(*__middle, *__first))
03078 std::iter_swap(__first, __middle);
03079 return;
03080 }
03081 _BidirectionalIterator __first_cut = __first;
03082 _BidirectionalIterator __second_cut = __middle;
03083 _Distance __len11 = 0;
03084 _Distance __len22 = 0;
03085 if (__len1 > __len2)
03086 {
03087 __len11 = __len1 / 2;
03088 std::advance(__first_cut, __len11);
03089 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
03090 __comp);
03091 __len22 = std::distance(__middle, __second_cut);
03092 }
03093 else
03094 {
03095 __len22 = __len2 / 2;
03096 std::advance(__second_cut, __len22);
03097 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
03098 __comp);
03099 __len11 = std::distance(__first, __first_cut);
03100 }
03101 std::rotate(__first_cut, __middle, __second_cut);
03102 _BidirectionalIterator __new_middle = __first_cut;
03103 std::advance(__new_middle, std::distance(__middle, __second_cut));
03104 std::__merge_without_buffer(__first, __first_cut, __new_middle,
03105 __len11, __len22, __comp);
03106 std::__merge_without_buffer(__new_middle, __second_cut, __last,
03107 __len1 - __len11, __len2 - __len22, __comp);
03108 }
03109
03110
03111
03112
03113
03114
03115
03116
03117
03118
03119
03120
03121
03122
03123
03124
03125
03126
03127
03128 template<typename _BidirectionalIterator>
03129 void
03130 inplace_merge(_BidirectionalIterator __first,
03131 _BidirectionalIterator __middle,
03132 _BidirectionalIterator __last)
03133 {
03134 typedef typename iterator_traits<_BidirectionalIterator>::value_type
03135 _ValueType;
03136 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
03137 _DistanceType;
03138
03139
03140 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
03141 _BidirectionalIterator>)
03142 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
03143 __glibcxx_requires_sorted(__first, __middle);
03144 __glibcxx_requires_sorted(__middle, __last);
03145
03146 if (__first == __middle || __middle == __last)
03147 return;
03148
03149 _DistanceType __len1 = std::distance(__first, __middle);
03150 _DistanceType __len2 = std::distance(__middle, __last);
03151
03152 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
03153 __last);
03154 if (__buf.begin() == 0)
03155 std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
03156 else
03157 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
03158 __buf.begin(), _DistanceType(__buf.size()));
03159 }
03160
03161
03162
03163
03164
03165
03166
03167
03168
03169
03170
03171
03172
03173
03174
03175
03176
03177
03178
03179
03180
03181
03182
03183 template<typename _BidirectionalIterator, typename _Compare>
03184 void
03185 inplace_merge(_BidirectionalIterator __first,
03186 _BidirectionalIterator __middle,
03187 _BidirectionalIterator __last,
03188 _Compare __comp)
03189 {
03190 typedef typename iterator_traits<_BidirectionalIterator>::value_type
03191 _ValueType;
03192 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
03193 _DistanceType;
03194
03195
03196 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
03197 _BidirectionalIterator>)
03198 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03199 _ValueType, _ValueType>)
03200 __glibcxx_requires_sorted_pred(__first, __middle, __comp);
03201 __glibcxx_requires_sorted_pred(__middle, __last, __comp);
03202
03203 if (__first == __middle || __middle == __last)
03204 return;
03205
03206 const _DistanceType __len1 = std::distance(__first, __middle);
03207 const _DistanceType __len2 = std::distance(__middle, __last);
03208
03209 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
03210 __last);
03211 if (__buf.begin() == 0)
03212 std::__merge_without_buffer(__first, __middle, __last, __len1,
03213 __len2, __comp);
03214 else
03215 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
03216 __buf.begin(), _DistanceType(__buf.size()),
03217 __comp);
03218 }
03219
03220 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
03221 typename _Distance>
03222 void
03223 __merge_sort_loop(_RandomAccessIterator1 __first,
03224 _RandomAccessIterator1 __last,
03225 _RandomAccessIterator2 __result,
03226 _Distance __step_size)
03227 {
03228 const _Distance __two_step = 2 * __step_size;
03229
03230 while (__last - __first >= __two_step)
03231 {
03232 __result = _GLIBCXX_STD_P::merge(__first, __first + __step_size,
03233 __first + __step_size,
03234 __first + __two_step,
03235 __result);
03236 __first += __two_step;
03237 }
03238
03239 __step_size = std::min(_Distance(__last - __first), __step_size);
03240 _GLIBCXX_STD_P::merge(__first, __first + __step_size,
03241 __first + __step_size, __last,
03242 __result);
03243 }
03244
03245 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
03246 typename _Distance, typename _Compare>
03247 void
03248 __merge_sort_loop(_RandomAccessIterator1 __first,
03249 _RandomAccessIterator1 __last,
03250 _RandomAccessIterator2 __result, _Distance __step_size,
03251 _Compare __comp)
03252 {
03253 const _Distance __two_step = 2 * __step_size;
03254
03255 while (__last - __first >= __two_step)
03256 {
03257 __result = _GLIBCXX_STD_P::merge(__first, __first + __step_size,
03258 __first + __step_size, __first + __two_step,
03259 __result,
03260 __comp);
03261 __first += __two_step;
03262 }
03263 __step_size = std::min(_Distance(__last - __first), __step_size);
03264
03265 _GLIBCXX_STD_P::merge(__first, __first + __step_size,
03266 __first + __step_size, __last, __result, __comp);
03267 }
03268
03269 template<typename _RandomAccessIterator, typename _Distance>
03270 void
03271 __chunk_insertion_sort(_RandomAccessIterator __first,
03272 _RandomAccessIterator __last,
03273 _Distance __chunk_size)
03274 {
03275 while (__last - __first >= __chunk_size)
03276 {
03277 std::__insertion_sort(__first, __first + __chunk_size);
03278 __first += __chunk_size;
03279 }
03280 std::__insertion_sort(__first, __last);
03281 }
03282
03283 template<typename _RandomAccessIterator, typename _Distance,
03284 typename _Compare>
03285 void
03286 __chunk_insertion_sort(_RandomAccessIterator __first,
03287 _RandomAccessIterator __last,
03288 _Distance __chunk_size, _Compare __comp)
03289 {
03290 while (__last - __first >= __chunk_size)
03291 {
03292 std::__insertion_sort(__first, __first + __chunk_size, __comp);
03293 __first += __chunk_size;
03294 }
03295 std::__insertion_sort(__first, __last, __comp);
03296 }
03297
03298 enum { _S_chunk_size = 7 };
03299
03300 template<typename _RandomAccessIterator, typename _Pointer>
03301 void
03302 __merge_sort_with_buffer(_RandomAccessIterator __first,
03303 _RandomAccessIterator __last,
03304 _Pointer __buffer)
03305 {
03306 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
03307 _Distance;
03308
03309 const _Distance __len = __last - __first;
03310 const _Pointer __buffer_last = __buffer + __len;
03311
03312 _Distance __step_size = _S_chunk_size;
03313 std::__chunk_insertion_sort(__first, __last, __step_size);
03314
03315 while (__step_size < __len)
03316 {
03317 std::__merge_sort_loop(__first, __last, __buffer, __step_size);
03318 __step_size *= 2;
03319 std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
03320 __step_size *= 2;
03321 }
03322 }
03323
03324 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
03325 void
03326 __merge_sort_with_buffer(_RandomAccessIterator __first,
03327 _RandomAccessIterator __last,
03328 _Pointer __buffer, _Compare __comp)
03329 {
03330 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
03331 _Distance;
03332
03333 const _Distance __len = __last - __first;
03334 const _Pointer __buffer_last = __buffer + __len;
03335
03336 _Distance __step_size = _S_chunk_size;
03337 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
03338
03339 while (__step_size < __len)
03340 {
03341 std::__merge_sort_loop(__first, __last, __buffer,
03342 __step_size, __comp);
03343 __step_size *= 2;
03344 std::__merge_sort_loop(__buffer, __buffer_last, __first,
03345 __step_size, __comp);
03346 __step_size *= 2;
03347 }
03348 }
03349
03350 template<typename _RandomAccessIterator, typename _Pointer,
03351 typename _Distance>
03352 void
03353 __stable_sort_adaptive(_RandomAccessIterator __first,
03354 _RandomAccessIterator __last,
03355 _Pointer __buffer, _Distance __buffer_size)
03356 {
03357 const _Distance __len = (__last - __first + 1) / 2;
03358 const _RandomAccessIterator __middle = __first + __len;
03359 if (__len > __buffer_size)
03360 {
03361 std::__stable_sort_adaptive(__first, __middle,
03362 __buffer, __buffer_size);
03363 std::__stable_sort_adaptive(__middle, __last,
03364 __buffer, __buffer_size);
03365 }
03366 else
03367 {
03368 std::__merge_sort_with_buffer(__first, __middle, __buffer);
03369 std::__merge_sort_with_buffer(__middle, __last, __buffer);
03370 }
03371 std::__merge_adaptive(__first, __middle, __last,
03372 _Distance(__middle - __first),
03373 _Distance(__last - __middle),
03374 __buffer, __buffer_size);
03375 }
03376
03377 template<typename _RandomAccessIterator, typename _Pointer,
03378 typename _Distance, typename _Compare>
03379 void
03380 __stable_sort_adaptive(_RandomAccessIterator __first,
03381 _RandomAccessIterator __last,
03382 _Pointer __buffer, _Distance __buffer_size,
03383 _Compare __comp)
03384 {
03385 const _Distance __len = (__last - __first + 1) / 2;
03386 const _RandomAccessIterator __middle = __first + __len;
03387 if (__len > __buffer_size)
03388 {
03389 std::__stable_sort_adaptive(__first, __middle, __buffer,
03390 __buffer_size, __comp);
03391 std::__stable_sort_adaptive(__middle, __last, __buffer,
03392 __buffer_size, __comp);
03393 }
03394 else
03395 {
03396 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
03397 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
03398 }
03399 std::__merge_adaptive(__first, __middle, __last,
03400 _Distance(__middle - __first),
03401 _Distance(__last - __middle),
03402 __buffer, __buffer_size,
03403 __comp);
03404 }
03405
03406
03407 template<typename _RandomAccessIterator>
03408 void
03409 __inplace_stable_sort(_RandomAccessIterator __first,
03410 _RandomAccessIterator __last)
03411 {
03412 if (__last - __first < 15)
03413 {
03414 std::__insertion_sort(__first, __last);
03415 return;
03416 }
03417 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
03418 std::__inplace_stable_sort(__first, __middle);
03419 std::__inplace_stable_sort(__middle, __last);
03420 std::__merge_without_buffer(__first, __middle, __last,
03421 __middle - __first,
03422 __last - __middle);
03423 }
03424
03425
03426 template<typename _RandomAccessIterator, typename _Compare>
03427 void
03428 __inplace_stable_sort(_RandomAccessIterator __first,
03429 _RandomAccessIterator __last, _Compare __comp)
03430 {
03431 if (__last - __first < 15)
03432 {
03433 std::__insertion_sort(__first, __last, __comp);
03434 return;
03435 }
03436 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
03437 std::__inplace_stable_sort(__first, __middle, __comp);
03438 std::__inplace_stable_sort(__middle, __last, __comp);
03439 std::__merge_without_buffer(__first, __middle, __last,
03440 __middle - __first,
03441 __last - __middle,
03442 __comp);
03443 }
03444
03445
03446
03447
03448
03449
03450
03451
03452
03453
03454
03455
03456
03457
03458
03459
03460
03461
03462
03463
03464
03465
03466
03467
03468 template<typename _InputIterator1, typename _InputIterator2>
03469 bool
03470 includes(_InputIterator1 __first1, _InputIterator1 __last1,
03471 _InputIterator2 __first2, _InputIterator2 __last2)
03472 {
03473 typedef typename iterator_traits<_InputIterator1>::value_type
03474 _ValueType1;
03475 typedef typename iterator_traits<_InputIterator2>::value_type
03476 _ValueType2;
03477
03478
03479 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
03480 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
03481 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
03482 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
03483 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
03484 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
03485
03486 while (__first1 != __last1 && __first2 != __last2)
03487 if (*__first2 < *__first1)
03488 return false;
03489 else if(*__first1 < *__first2)
03490 ++__first1;
03491 else
03492 ++__first1, ++__first2;
03493
03494 return __first2 == __last2;
03495 }
03496
03497
03498
03499
03500
03501
03502
03503
03504
03505
03506
03507
03508
03509
03510
03511
03512
03513
03514
03515
03516
03517 template<typename _InputIterator1, typename _InputIterator2,
03518 typename _Compare>
03519 bool
03520 includes(_InputIterator1 __first1, _InputIterator1 __last1,
03521 _InputIterator2 __first2, _InputIterator2 __last2,
03522 _Compare __comp)
03523 {
03524 typedef typename iterator_traits<_InputIterator1>::value_type
03525 _ValueType1;
03526 typedef typename iterator_traits<_InputIterator2>::value_type
03527 _ValueType2;
03528
03529
03530 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
03531 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
03532 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03533 _ValueType1, _ValueType2>)
03534 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03535 _ValueType2, _ValueType1>)
03536 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
03537 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
03538
03539 while (__first1 != __last1 && __first2 != __last2)
03540 if (__comp(*__first2, *__first1))
03541 return false;
03542 else if(__comp(*__first1, *__first2))
03543 ++__first1;
03544 else
03545 ++__first1, ++__first2;
03546
03547 return __first2 == __last2;
03548 }
03549
03550
03551
03552
03553
03554
03555
03556
03557
03558
03559
03560
03561
03562
03563
03564
03565
03566
03567
03568
03569
03570
03571
03572 template<typename _BidirectionalIterator>
03573 bool
03574 next_permutation(_BidirectionalIterator __first,
03575 _BidirectionalIterator __last)
03576 {
03577
03578 __glibcxx_function_requires(_BidirectionalIteratorConcept<
03579 _BidirectionalIterator>)
03580 __glibcxx_function_requires(_LessThanComparableConcept<
03581 typename iterator_traits<_BidirectionalIterator>::value_type>)
03582 __glibcxx_requires_valid_range(__first, __last);
03583
03584 if (__first == __last)
03585 return false;
03586 _BidirectionalIterator __i = __first;
03587 ++__i;
03588 if (__i == __last)
03589 return false;
03590 __i = __last;
03591 --__i;
03592
03593 for(;;)
03594 {
03595 _BidirectionalIterator __ii = __i;
03596 --__i;
03597 if (*__i < *__ii)
03598 {
03599 _BidirectionalIterator __j = __last;
03600 while (!(*__i < *--__j))
03601 {}
03602 std::iter_swap(__i, __j);
03603 std::reverse(__ii, __last);
03604 return true;
03605 }
03606 if (__i == __first)
03607 {
03608 std::reverse(__first, __last);
03609 return false;
03610 }
03611 }
03612 }
03613
03614
03615
03616
03617
03618
03619
03620
03621
03622
03623
03624
03625
03626
03627
03628
03629 template<typename _BidirectionalIterator, typename _Compare>
03630 bool
03631 next_permutation(_BidirectionalIterator __first,
03632 _BidirectionalIterator __last, _Compare __comp)
03633 {
03634
03635 __glibcxx_function_requires(_BidirectionalIteratorConcept<
03636 _BidirectionalIterator>)
03637 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03638 typename iterator_traits<_BidirectionalIterator>::value_type,
03639 typename iterator_traits<_BidirectionalIterator>::value_type>)
03640 __glibcxx_requires_valid_range(__first, __last);
03641
03642 if (__first == __last)
03643 return false;
03644 _BidirectionalIterator __i = __first;
03645 ++__i;
03646 if (__i == __last)
03647 return false;
03648 __i = __last;
03649 --__i;
03650
03651 for(;;)
03652 {
03653 _BidirectionalIterator __ii = __i;
03654 --__i;
03655 if (__comp(*__i, *__ii))
03656 {
03657 _BidirectionalIterator __j = __last;
03658 while (!bool(__comp(*__i, *--__j)))
03659 {}
03660 std::iter_swap(__i, __j);
03661 std::reverse(__ii, __last);
03662 return true;
03663 }
03664 if (__i == __first)
03665 {
03666 std::reverse(__first, __last);
03667 return false;
03668 }
03669 }
03670 }
03671
03672
03673
03674
03675
03676
03677
03678
03679
03680
03681
03682
03683
03684
03685 template<typename _BidirectionalIterator>
03686 bool
03687 prev_permutation(_BidirectionalIterator __first,
03688 _BidirectionalIterator __last)
03689 {
03690
03691 __glibcxx_function_requires(_BidirectionalIteratorConcept<
03692 _BidirectionalIterator>)
03693 __glibcxx_function_requires(_LessThanComparableConcept<
03694 typename iterator_traits<_BidirectionalIterator>::value_type>)
03695 __glibcxx_requires_valid_range(__first, __last);
03696
03697 if (__first == __last)
03698 return false;
03699 _BidirectionalIterator __i = __first;
03700 ++__i;
03701 if (__i == __last)
03702 return false;
03703 __i = __last;
03704 --__i;
03705
03706 for(;;)
03707 {
03708 _BidirectionalIterator __ii = __i;
03709 --__i;
03710 if (*__ii < *__i)
03711 {
03712 _BidirectionalIterator __j = __last;
03713 while (!(*--__j < *__i))
03714 {}
03715 std::iter_swap(__i, __j);
03716 std::reverse(__ii, __last);
03717 return true;
03718 }
03719 if (__i == __first)
03720 {
03721 std::reverse(__first, __last);
03722 return false;
03723 }
03724 }
03725 }
03726
03727
03728
03729
03730
03731
03732
03733
03734
03735
03736
03737
03738
03739
03740
03741
03742 template<typename _BidirectionalIterator, typename _Compare>
03743 bool
03744 prev_permutation(_BidirectionalIterator __first,
03745 _BidirectionalIterator __last, _Compare __comp)
03746 {
03747
03748 __glibcxx_function_requires(_BidirectionalIteratorConcept<
03749 _BidirectionalIterator>)
03750 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03751 typename iterator_traits<_BidirectionalIterator>::value_type,
03752 typename iterator_traits<_BidirectionalIterator>::value_type>)
03753 __glibcxx_requires_valid_range(__first, __last);
03754
03755 if (__first == __last)
03756 return false;
03757 _BidirectionalIterator __i = __first;
03758 ++__i;
03759 if (__i == __last)
03760 return false;
03761 __i = __last;
03762 --__i;
03763
03764 for(;;)
03765 {
03766 _BidirectionalIterator __ii = __i;
03767 --__i;
03768 if (__comp(*__ii, *__i))
03769 {
03770 _BidirectionalIterator __j = __last;
03771 while (!bool(__comp(*--__j, *__i)))
03772 {}
03773 std::iter_swap(__i, __j);
03774 std::reverse(__ii, __last);
03775 return true;
03776 }
03777 if (__i == __first)
03778 {
03779 std::reverse(__first, __last);
03780 return false;
03781 }
03782 }
03783 }
03784
03785
03786
03787
03788
03789
03790
03791
03792
03793
03794
03795
03796
03797
03798
03799
03800
03801
03802 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
03803 _OutputIterator
03804 replace_copy(_InputIterator __first, _InputIterator __last,
03805 _OutputIterator __result,
03806 const _Tp& __old_value, const _Tp& __new_value)
03807 {
03808
03809 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03810 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
03811 typename iterator_traits<_InputIterator>::value_type>)
03812 __glibcxx_function_requires(_EqualOpConcept<
03813 typename iterator_traits<_InputIterator>::value_type, _Tp>)
03814 __glibcxx_requires_valid_range(__first, __last);
03815
03816 for (; __first != __last; ++__first, ++__result)
03817 if (*__first == __old_value)
03818 *__result = __new_value;
03819 else
03820 *__result = *__first;
03821 return __result;
03822 }
03823
03824
03825
03826
03827
03828
03829
03830
03831
03832
03833
03834
03835
03836
03837
03838
03839 template<typename _InputIterator, typename _OutputIterator,
03840 typename _Predicate, typename _Tp>
03841 _OutputIterator
03842 replace_copy_if(_InputIterator __first, _InputIterator __last,
03843 _OutputIterator __result,
03844 _Predicate __pred, const _Tp& __new_value)
03845 {
03846
03847 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
03848 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
03849 typename iterator_traits<_InputIterator>::value_type>)
03850 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
03851 typename iterator_traits<_InputIterator>::value_type>)
03852 __glibcxx_requires_valid_range(__first, __last);
03853
03854 for (; __first != __last; ++__first, ++__result)
03855 if (__pred(*__first))
03856 *__result = __new_value;
03857 else
03858 *__result = *__first;
03859 return __result;
03860 }
03861
03862 #ifdef __GXX_EXPERIMENTAL_CXX0X__
03863
03864
03865
03866
03867
03868
03869
03870 template<typename _ForwardIterator>
03871 inline bool
03872 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
03873 { return std::is_sorted_until(__first, __last) == __last; }
03874
03875
03876
03877
03878
03879
03880
03881
03882
03883
03884 template<typename _ForwardIterator, typename _Compare>
03885 inline bool
03886 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
03887 _Compare __comp)
03888 { return std::is_sorted_until(__first, __last, __comp) == __last; }
03889
03890
03891
03892
03893
03894
03895
03896
03897
03898 template<typename _ForwardIterator>
03899 _ForwardIterator
03900 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
03901 {
03902
03903 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03904 __glibcxx_function_requires(_LessThanComparableConcept<
03905 typename iterator_traits<_ForwardIterator>::value_type>)
03906 __glibcxx_requires_valid_range(__first, __last);
03907
03908 if (__first == __last)
03909 return __last;
03910
03911 _ForwardIterator __next = __first;
03912 for (++__next; __next != __last; __first = __next, ++__next)
03913 if (*__next < *__first)
03914 return __next;
03915 return __next;
03916 }
03917
03918
03919
03920
03921
03922
03923
03924
03925
03926
03927 template<typename _ForwardIterator, typename _Compare>
03928 _ForwardIterator
03929 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
03930 _Compare __comp)
03931 {
03932
03933 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
03934 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
03935 typename iterator_traits<_ForwardIterator>::value_type,
03936 typename iterator_traits<_ForwardIterator>::value_type>)
03937 __glibcxx_requires_valid_range(__first, __last);
03938
03939 if (__first == __last)
03940 return __last;
03941
03942 _ForwardIterator __next = __first;
03943 for (++__next; __next != __last; __first = __next, ++__next)
03944 if (__comp(*__next, *__first))
03945 return __next;
03946 return __next;
03947 }
03948
03949
03950
03951
03952
03953
03954
03955
03956 template<typename _Tp>
03957 inline pair<const _Tp&, const _Tp&>
03958 minmax(const _Tp& __a, const _Tp& __b)
03959 {
03960
03961 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
03962
03963 return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
03964 : pair<const _Tp&, const _Tp&>(__a, __b);
03965 }
03966
03967
03968
03969
03970
03971
03972
03973
03974
03975 template<typename _Tp, typename _Compare>
03976 inline pair<const _Tp&, const _Tp&>
03977 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
03978 {
03979 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
03980 : pair<const _Tp&, const _Tp&>(__a, __b);
03981 }
03982
03983
03984
03985
03986
03987
03988
03989
03990
03991
03992
03993
03994 template<typename _ForwardIterator>
03995 pair<_ForwardIterator, _ForwardIterator>
03996 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
03997 {
03998
03999 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04000 __glibcxx_function_requires(_LessThanComparableConcept<
04001 typename iterator_traits<_ForwardIterator>::value_type>)
04002 __glibcxx_requires_valid_range(__first, __last);
04003
04004 _ForwardIterator __next = __first;
04005 if (__first == __last
04006 || ++__next == __last)
04007 return std::make_pair(__first, __first);
04008
04009 _ForwardIterator __min, __max;
04010 if (*__next < *__first)
04011 {
04012 __min = __next;
04013 __max = __first;
04014 }
04015 else
04016 {
04017 __min = __first;
04018 __max = __next;
04019 }
04020
04021 __first = __next;
04022 ++__first;
04023
04024 while (__first != __last)
04025 {
04026 __next = __first;
04027 if (++__next == __last)
04028 {
04029 if (*__first < *__min)
04030 __min = __first;
04031 else if (!(*__first < *__max))
04032 __max = __first;
04033 break;
04034 }
04035
04036 if (*__next < *__first)
04037 {
04038 if (*__next < *__min)
04039 __min = __next;
04040 if (!(*__first < *__max))
04041 __max = __first;
04042 }
04043 else
04044 {
04045 if (*__first < *__min)
04046 __min = __first;
04047 if (!(*__next < *__max))
04048 __max = __next;
04049 }
04050
04051 __first = __next;
04052 ++__first;
04053 }
04054
04055 return std::make_pair(__min, __max);
04056 }
04057
04058
04059
04060
04061
04062
04063
04064
04065
04066
04067
04068
04069
04070 template<typename _ForwardIterator, typename _Compare>
04071 pair<_ForwardIterator, _ForwardIterator>
04072 minmax_element(_ForwardIterator __first, _ForwardIterator __last,
04073 _Compare __comp)
04074 {
04075
04076 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04077 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
04078 typename iterator_traits<_ForwardIterator>::value_type,
04079 typename iterator_traits<_ForwardIterator>::value_type>)
04080 __glibcxx_requires_valid_range(__first, __last);
04081
04082 _ForwardIterator __next = __first;
04083 if (__first == __last
04084 || ++__next == __last)
04085 return std::make_pair(__first, __first);
04086
04087 _ForwardIterator __min, __max;
04088 if (__comp(*__next, *__first))
04089 {
04090 __min = __next;
04091 __max = __first;
04092 }
04093 else
04094 {
04095 __min = __first;
04096 __max = __next;
04097 }
04098
04099 __first = __next;
04100 ++__first;
04101
04102 while (__first != __last)
04103 {
04104 __next = __first;
04105 if (++__next == __last)
04106 {
04107 if (__comp(*__first, *__min))
04108 __min = __first;
04109 else if (!__comp(*__first, *__max))
04110 __max = __first;
04111 break;
04112 }
04113
04114 if (__comp(*__next, *__first))
04115 {
04116 if (__comp(*__next, *__min))
04117 __min = __next;
04118 if (!__comp(*__first, *__max))
04119 __max = __first;
04120 }
04121 else
04122 {
04123 if (__comp(*__first, *__min))
04124 __min = __first;
04125 if (!__comp(*__next, *__max))
04126 __max = __next;
04127 }
04128
04129 __first = __next;
04130 ++__first;
04131 }
04132
04133 return std::make_pair(__min, __max);
04134 }
04135
04136
04137 template<typename _Tp>
04138 inline _Tp
04139 min(initializer_list<_Tp> __l)
04140 { return *std::min_element(__l.begin(), __l.end()); }
04141
04142 template<typename _Tp, typename _Compare>
04143 inline _Tp
04144 min(initializer_list<_Tp> __l, _Compare __comp)
04145 { return *std::min_element(__l.begin(), __l.end(), __comp); }
04146
04147 template<typename _Tp>
04148 inline _Tp
04149 max(initializer_list<_Tp> __l)
04150 { return *std::max_element(__l.begin(), __l.end()); }
04151
04152 template<typename _Tp, typename _Compare>
04153 inline _Tp
04154 max(initializer_list<_Tp> __l, _Compare __comp)
04155 { return *std::max_element(__l.begin(), __l.end(), __comp); }
04156
04157 template<typename _Tp>
04158 inline pair<_Tp, _Tp>
04159 minmax(initializer_list<_Tp> __l)
04160 {
04161 pair<const _Tp*, const _Tp*> __p =
04162 std::minmax_element(__l.begin(), __l.end());
04163 return std::make_pair(*__p.first, *__p.second);
04164 }
04165
04166 template<typename _Tp, typename _Compare>
04167 inline pair<_Tp, _Tp>
04168 minmax(initializer_list<_Tp> __l, _Compare __comp)
04169 {
04170 pair<const _Tp*, const _Tp*> __p =
04171 std::minmax_element(__l.begin(), __l.end(), __comp);
04172 return std::make_pair(*__p.first, *__p.second);
04173 }
04174 #endif // __GXX_EXPERIMENTAL_CXX0X__
04175
04176 _GLIBCXX_END_NAMESPACE
04177
04178 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
04179
04180
04181
04182
04183
04184
04185
04186
04187
04188
04189
04190
04191
04192 template<typename _InputIterator, typename _Function>
04193 _Function
04194 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
04195 {
04196
04197 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04198 __glibcxx_requires_valid_range(__first, __last);
04199 for (; __first != __last; ++__first)
04200 __f(*__first);
04201 return __f;
04202 }
04203
04204
04205
04206
04207
04208
04209
04210
04211
04212
04213 template<typename _InputIterator, typename _Tp>
04214 inline _InputIterator
04215 find(_InputIterator __first, _InputIterator __last,
04216 const _Tp& __val)
04217 {
04218
04219 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04220 __glibcxx_function_requires(_EqualOpConcept<
04221 typename iterator_traits<_InputIterator>::value_type, _Tp>)
04222 __glibcxx_requires_valid_range(__first, __last);
04223 return std::__find(__first, __last, __val,
04224 std::__iterator_category(__first));
04225 }
04226
04227
04228
04229
04230
04231
04232
04233
04234
04235
04236
04237 template<typename _InputIterator, typename _Predicate>
04238 inline _InputIterator
04239 find_if(_InputIterator __first, _InputIterator __last,
04240 _Predicate __pred)
04241 {
04242
04243 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04244 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
04245 typename iterator_traits<_InputIterator>::value_type>)
04246 __glibcxx_requires_valid_range(__first, __last);
04247 return std::__find_if(__first, __last, __pred,
04248 std::__iterator_category(__first));
04249 }
04250
04251
04252
04253
04254
04255
04256
04257
04258
04259
04260
04261
04262
04263
04264
04265
04266 template<typename _InputIterator, typename _ForwardIterator>
04267 _InputIterator
04268 find_first_of(_InputIterator __first1, _InputIterator __last1,
04269 _ForwardIterator __first2, _ForwardIterator __last2)
04270 {
04271
04272 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04273 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04274 __glibcxx_function_requires(_EqualOpConcept<
04275 typename iterator_traits<_InputIterator>::value_type,
04276 typename iterator_traits<_ForwardIterator>::value_type>)
04277 __glibcxx_requires_valid_range(__first1, __last1);
04278 __glibcxx_requires_valid_range(__first2, __last2);
04279
04280 for (; __first1 != __last1; ++__first1)
04281 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
04282 if (*__first1 == *__iter)
04283 return __first1;
04284 return __last1;
04285 }
04286
04287
04288
04289
04290
04291
04292
04293
04294
04295
04296
04297
04298
04299
04300
04301
04302
04303
04304
04305 template<typename _InputIterator, typename _ForwardIterator,
04306 typename _BinaryPredicate>
04307 _InputIterator
04308 find_first_of(_InputIterator __first1, _InputIterator __last1,
04309 _ForwardIterator __first2, _ForwardIterator __last2,
04310 _BinaryPredicate __comp)
04311 {
04312
04313 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04314 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04315 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04316 typename iterator_traits<_InputIterator>::value_type,
04317 typename iterator_traits<_ForwardIterator>::value_type>)
04318 __glibcxx_requires_valid_range(__first1, __last1);
04319 __glibcxx_requires_valid_range(__first2, __last2);
04320
04321 for (; __first1 != __last1; ++__first1)
04322 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
04323 if (__comp(*__first1, *__iter))
04324 return __first1;
04325 return __last1;
04326 }
04327
04328
04329
04330
04331
04332
04333
04334
04335
04336
04337 template<typename _ForwardIterator>
04338 _ForwardIterator
04339 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
04340 {
04341
04342 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04343 __glibcxx_function_requires(_EqualityComparableConcept<
04344 typename iterator_traits<_ForwardIterator>::value_type>)
04345 __glibcxx_requires_valid_range(__first, __last);
04346 if (__first == __last)
04347 return __last;
04348 _ForwardIterator __next = __first;
04349 while(++__next != __last)
04350 {
04351 if (*__first == *__next)
04352 return __first;
04353 __first = __next;
04354 }
04355 return __last;
04356 }
04357
04358
04359
04360
04361
04362
04363
04364
04365
04366
04367
04368
04369 template<typename _ForwardIterator, typename _BinaryPredicate>
04370 _ForwardIterator
04371 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
04372 _BinaryPredicate __binary_pred)
04373 {
04374
04375 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04376 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04377 typename iterator_traits<_ForwardIterator>::value_type,
04378 typename iterator_traits<_ForwardIterator>::value_type>)
04379 __glibcxx_requires_valid_range(__first, __last);
04380 if (__first == __last)
04381 return __last;
04382 _ForwardIterator __next = __first;
04383 while(++__next != __last)
04384 {
04385 if (__binary_pred(*__first, *__next))
04386 return __first;
04387 __first = __next;
04388 }
04389 return __last;
04390 }
04391
04392
04393
04394
04395
04396
04397
04398
04399
04400
04401 template<typename _InputIterator, typename _Tp>
04402 typename iterator_traits<_InputIterator>::difference_type
04403 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
04404 {
04405
04406 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04407 __glibcxx_function_requires(_EqualOpConcept<
04408 typename iterator_traits<_InputIterator>::value_type, _Tp>)
04409 __glibcxx_requires_valid_range(__first, __last);
04410 typename iterator_traits<_InputIterator>::difference_type __n = 0;
04411 for (; __first != __last; ++__first)
04412 if (*__first == __value)
04413 ++__n;
04414 return __n;
04415 }
04416
04417
04418
04419
04420
04421
04422
04423
04424
04425
04426 template<typename _InputIterator, typename _Predicate>
04427 typename iterator_traits<_InputIterator>::difference_type
04428 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
04429 {
04430
04431 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04432 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
04433 typename iterator_traits<_InputIterator>::value_type>)
04434 __glibcxx_requires_valid_range(__first, __last);
04435 typename iterator_traits<_InputIterator>::difference_type __n = 0;
04436 for (; __first != __last; ++__first)
04437 if (__pred(*__first))
04438 ++__n;
04439 return __n;
04440 }
04441
04442
04443
04444
04445
04446
04447
04448
04449
04450
04451
04452
04453
04454
04455
04456
04457
04458
04459
04460
04461
04462
04463
04464
04465
04466 template<typename _ForwardIterator1, typename _ForwardIterator2>
04467 _ForwardIterator1
04468 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
04469 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
04470 {
04471
04472 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
04473 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
04474 __glibcxx_function_requires(_EqualOpConcept<
04475 typename iterator_traits<_ForwardIterator1>::value_type,
04476 typename iterator_traits<_ForwardIterator2>::value_type>)
04477 __glibcxx_requires_valid_range(__first1, __last1);
04478 __glibcxx_requires_valid_range(__first2, __last2);
04479
04480
04481 if (__first1 == __last1 || __first2 == __last2)
04482 return __first1;
04483
04484
04485 _ForwardIterator2 __p1(__first2);
04486 if (++__p1 == __last2)
04487 return _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
04488
04489
04490 _ForwardIterator2 __p;
04491 _ForwardIterator1 __current = __first1;
04492
04493 for (;;)
04494 {
04495 __first1 = _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
04496 if (__first1 == __last1)
04497 return __last1;
04498
04499 __p = __p1;
04500 __current = __first1;
04501 if (++__current == __last1)
04502 return __last1;
04503
04504 while (*__current == *__p)
04505 {
04506 if (++__p == __last2)
04507 return __first1;
04508 if (++__current == __last1)
04509 return __last1;
04510 }
04511 ++__first1;
04512 }
04513 return __first1;
04514 }
04515
04516
04517
04518
04519
04520
04521
04522
04523
04524
04525
04526
04527
04528
04529
04530
04531
04532
04533
04534
04535
04536
04537 template<typename _ForwardIterator1, typename _ForwardIterator2,
04538 typename _BinaryPredicate>
04539 _ForwardIterator1
04540 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
04541 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
04542 _BinaryPredicate __predicate)
04543 {
04544
04545 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
04546 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
04547 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04548 typename iterator_traits<_ForwardIterator1>::value_type,
04549 typename iterator_traits<_ForwardIterator2>::value_type>)
04550 __glibcxx_requires_valid_range(__first1, __last1);
04551 __glibcxx_requires_valid_range(__first2, __last2);
04552
04553
04554 if (__first1 == __last1 || __first2 == __last2)
04555 return __first1;
04556
04557
04558 _ForwardIterator2 __p1(__first2);
04559 if (++__p1 == __last2)
04560 {
04561 while (__first1 != __last1
04562 && !bool(__predicate(*__first1, *__first2)))
04563 ++__first1;
04564 return __first1;
04565 }
04566
04567
04568 _ForwardIterator2 __p;
04569 _ForwardIterator1 __current = __first1;
04570
04571 for (;;)
04572 {
04573 while (__first1 != __last1
04574 && !bool(__predicate(*__first1, *__first2)))
04575 ++__first1;
04576 if (__first1 == __last1)
04577 return __last1;
04578
04579 __p = __p1;
04580 __current = __first1;
04581 if (++__current == __last1)
04582 return __last1;
04583
04584 while (__predicate(*__current, *__p))
04585 {
04586 if (++__p == __last2)
04587 return __first1;
04588 if (++__current == __last1)
04589 return __last1;
04590 }
04591 ++__first1;
04592 }
04593 return __first1;
04594 }
04595
04596
04597
04598
04599
04600
04601
04602
04603
04604
04605
04606
04607
04608
04609
04610
04611 template<typename _ForwardIterator, typename _Integer, typename _Tp>
04612 _ForwardIterator
04613 search_n(_ForwardIterator __first, _ForwardIterator __last,
04614 _Integer __count, const _Tp& __val)
04615 {
04616
04617 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04618 __glibcxx_function_requires(_EqualOpConcept<
04619 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
04620 __glibcxx_requires_valid_range(__first, __last);
04621
04622 if (__count <= 0)
04623 return __first;
04624 if (__count == 1)
04625 return _GLIBCXX_STD_P::find(__first, __last, __val);
04626 return std::__search_n(__first, __last, __count, __val,
04627 std::__iterator_category(__first));
04628 }
04629
04630
04631
04632
04633
04634
04635
04636
04637
04638
04639
04640
04641
04642
04643
04644
04645
04646
04647 template<typename _ForwardIterator, typename _Integer, typename _Tp,
04648 typename _BinaryPredicate>
04649 _ForwardIterator
04650 search_n(_ForwardIterator __first, _ForwardIterator __last,
04651 _Integer __count, const _Tp& __val,
04652 _BinaryPredicate __binary_pred)
04653 {
04654
04655 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04656 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
04657 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
04658 __glibcxx_requires_valid_range(__first, __last);
04659
04660 if (__count <= 0)
04661 return __first;
04662 if (__count == 1)
04663 {
04664 while (__first != __last && !bool(__binary_pred(*__first, __val)))
04665 ++__first;
04666 return __first;
04667 }
04668 return std::__search_n(__first, __last, __count, __val, __binary_pred,
04669 std::__iterator_category(__first));
04670 }
04671
04672
04673
04674
04675
04676
04677
04678
04679
04680
04681
04682
04683
04684
04685
04686
04687
04688
04689 template<typename _InputIterator, typename _OutputIterator,
04690 typename _UnaryOperation>
04691 _OutputIterator
04692 transform(_InputIterator __first, _InputIterator __last,
04693 _OutputIterator __result, _UnaryOperation __unary_op)
04694 {
04695
04696 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04697 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04698
04699 __typeof__(__unary_op(*__first))>)
04700 __glibcxx_requires_valid_range(__first, __last);
04701
04702 for (; __first != __last; ++__first, ++__result)
04703 *__result = __unary_op(*__first);
04704 return __result;
04705 }
04706
04707
04708
04709
04710
04711
04712
04713
04714
04715
04716
04717
04718
04719
04720
04721
04722
04723
04724
04725 template<typename _InputIterator1, typename _InputIterator2,
04726 typename _OutputIterator, typename _BinaryOperation>
04727 _OutputIterator
04728 transform(_InputIterator1 __first1, _InputIterator1 __last1,
04729 _InputIterator2 __first2, _OutputIterator __result,
04730 _BinaryOperation __binary_op)
04731 {
04732
04733 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
04734 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
04735 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04736
04737 __typeof__(__binary_op(*__first1,*__first2))>)
04738 __glibcxx_requires_valid_range(__first1, __last1);
04739
04740 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
04741 *__result = __binary_op(*__first1, *__first2);
04742 return __result;
04743 }
04744
04745
04746
04747
04748
04749
04750
04751
04752
04753
04754
04755
04756
04757
04758 template<typename _ForwardIterator, typename _Tp>
04759 void
04760 replace(_ForwardIterator __first, _ForwardIterator __last,
04761 const _Tp& __old_value, const _Tp& __new_value)
04762 {
04763
04764 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
04765 _ForwardIterator>)
04766 __glibcxx_function_requires(_EqualOpConcept<
04767 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
04768 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
04769 typename iterator_traits<_ForwardIterator>::value_type>)
04770 __glibcxx_requires_valid_range(__first, __last);
04771
04772 for (; __first != __last; ++__first)
04773 if (*__first == __old_value)
04774 *__first = __new_value;
04775 }
04776
04777
04778
04779
04780
04781
04782
04783
04784
04785
04786
04787
04788
04789
04790 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
04791 void
04792 replace_if(_ForwardIterator __first, _ForwardIterator __last,
04793 _Predicate __pred, const _Tp& __new_value)
04794 {
04795
04796 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
04797 _ForwardIterator>)
04798 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
04799 typename iterator_traits<_ForwardIterator>::value_type>)
04800 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
04801 typename iterator_traits<_ForwardIterator>::value_type>)
04802 __glibcxx_requires_valid_range(__first, __last);
04803
04804 for (; __first != __last; ++__first)
04805 if (__pred(*__first))
04806 *__first = __new_value;
04807 }
04808
04809
04810
04811
04812
04813
04814
04815
04816
04817
04818
04819
04820
04821
04822 template<typename _ForwardIterator, typename _Generator>
04823 void
04824 generate(_ForwardIterator __first, _ForwardIterator __last,
04825 _Generator __gen)
04826 {
04827
04828 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
04829 __glibcxx_function_requires(_GeneratorConcept<_Generator,
04830 typename iterator_traits<_ForwardIterator>::value_type>)
04831 __glibcxx_requires_valid_range(__first, __last);
04832
04833 for (; __first != __last; ++__first)
04834 *__first = __gen();
04835 }
04836
04837
04838
04839
04840
04841
04842
04843
04844
04845
04846
04847
04848
04849
04850 template<typename _OutputIterator, typename _Size, typename _Generator>
04851 _OutputIterator
04852 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
04853 {
04854
04855 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04856
04857 __typeof__(__gen())>)
04858
04859 for (; __n > 0; --__n, ++__first)
04860 *__first = __gen();
04861 return __first;
04862 }
04863
04864
04865
04866
04867
04868
04869
04870
04871
04872
04873
04874
04875
04876
04877
04878
04879
04880
04881
04882
04883
04884
04885
04886 template<typename _InputIterator, typename _OutputIterator>
04887 inline _OutputIterator
04888 unique_copy(_InputIterator __first, _InputIterator __last,
04889 _OutputIterator __result)
04890 {
04891
04892 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04893 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04894 typename iterator_traits<_InputIterator>::value_type>)
04895 __glibcxx_function_requires(_EqualityComparableConcept<
04896 typename iterator_traits<_InputIterator>::value_type>)
04897 __glibcxx_requires_valid_range(__first, __last);
04898
04899 if (__first == __last)
04900 return __result;
04901 return std::__unique_copy(__first, __last, __result,
04902 std::__iterator_category(__first),
04903 std::__iterator_category(__result));
04904 }
04905
04906
04907
04908
04909
04910
04911
04912
04913
04914
04915
04916
04917
04918
04919
04920
04921
04922
04923
04924
04925 template<typename _InputIterator, typename _OutputIterator,
04926 typename _BinaryPredicate>
04927 inline _OutputIterator
04928 unique_copy(_InputIterator __first, _InputIterator __last,
04929 _OutputIterator __result,
04930 _BinaryPredicate __binary_pred)
04931 {
04932
04933 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
04934 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
04935 typename iterator_traits<_InputIterator>::value_type>)
04936 __glibcxx_requires_valid_range(__first, __last);
04937
04938 if (__first == __last)
04939 return __result;
04940 return std::__unique_copy(__first, __last, __result, __binary_pred,
04941 std::__iterator_category(__first),
04942 std::__iterator_category(__result));
04943 }
04944
04945
04946
04947
04948
04949
04950
04951
04952
04953
04954
04955
04956
04957 template<typename _RandomAccessIterator>
04958 inline void
04959 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
04960 {
04961
04962 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04963 _RandomAccessIterator>)
04964 __glibcxx_requires_valid_range(__first, __last);
04965
04966 if (__first != __last)
04967 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
04968 std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
04969 }
04970
04971
04972
04973
04974
04975
04976
04977
04978
04979
04980
04981
04982
04983
04984
04985 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
04986 void
04987 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
04988 _RandomNumberGenerator& __rand)
04989 {
04990
04991 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
04992 _RandomAccessIterator>)
04993 __glibcxx_requires_valid_range(__first, __last);
04994
04995 if (__first == __last)
04996 return;
04997 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
04998 std::iter_swap(__i, __first + __rand((__i - __first) + 1));
04999 }
05000
05001
05002
05003
05004
05005
05006
05007
05008
05009
05010
05011
05012
05013
05014
05015
05016
05017 template<typename _ForwardIterator, typename _Predicate>
05018 inline _ForwardIterator
05019 partition(_ForwardIterator __first, _ForwardIterator __last,
05020 _Predicate __pred)
05021 {
05022
05023 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
05024 _ForwardIterator>)
05025 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
05026 typename iterator_traits<_ForwardIterator>::value_type>)
05027 __glibcxx_requires_valid_range(__first, __last);
05028
05029 return std::__partition(__first, __last, __pred,
05030 std::__iterator_category(__first));
05031 }
05032
05033
05034
05035
05036
05037
05038
05039
05040
05041
05042
05043
05044
05045
05046
05047
05048
05049
05050
05051 template<typename _RandomAccessIterator>
05052 inline void
05053 partial_sort(_RandomAccessIterator __first,
05054 _RandomAccessIterator __middle,
05055 _RandomAccessIterator __last)
05056 {
05057 typedef typename iterator_traits<_RandomAccessIterator>::value_type
05058 _ValueType;
05059
05060
05061 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05062 _RandomAccessIterator>)
05063 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
05064 __glibcxx_requires_valid_range(__first, __middle);
05065 __glibcxx_requires_valid_range(__middle, __last);
05066
05067 std::__heap_select(__first, __middle, __last);
05068 std::sort_heap(__first, __middle);
05069 }
05070
05071
05072
05073
05074
05075
05076
05077
05078
05079
05080
05081
05082
05083
05084
05085
05086
05087
05088
05089
05090 template<typename _RandomAccessIterator, typename _Compare>
05091 inline void
05092 partial_sort(_RandomAccessIterator __first,
05093 _RandomAccessIterator __middle,
05094 _RandomAccessIterator __last,
05095 _Compare __comp)
05096 {
05097 typedef typename iterator_traits<_RandomAccessIterator>::value_type
05098 _ValueType;
05099
05100
05101 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05102 _RandomAccessIterator>)
05103 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05104 _ValueType, _ValueType>)
05105 __glibcxx_requires_valid_range(__first, __middle);
05106 __glibcxx_requires_valid_range(__middle, __last);
05107
05108 std::__heap_select(__first, __middle, __last, __comp);
05109 std::sort_heap(__first, __middle, __comp);
05110 }
05111
05112
05113
05114
05115
05116
05117
05118
05119
05120
05121
05122
05123
05124
05125
05126
05127
05128 template<typename _RandomAccessIterator>
05129 inline void
05130 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
05131 _RandomAccessIterator __last)
05132 {
05133 typedef typename iterator_traits<_RandomAccessIterator>::value_type
05134 _ValueType;
05135
05136
05137 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05138 _RandomAccessIterator>)
05139 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
05140 __glibcxx_requires_valid_range(__first, __nth);
05141 __glibcxx_requires_valid_range(__nth, __last);
05142
05143 if (__first == __last || __nth == __last)
05144 return;
05145
05146 std::__introselect(__first, __nth, __last,
05147 std::__lg(__last - __first) * 2);
05148 }
05149
05150
05151
05152
05153
05154
05155
05156
05157
05158
05159
05160
05161
05162
05163
05164
05165
05166
05167 template<typename _RandomAccessIterator, typename _Compare>
05168 inline void
05169 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
05170 _RandomAccessIterator __last, _Compare __comp)
05171 {
05172 typedef typename iterator_traits<_RandomAccessIterator>::value_type
05173 _ValueType;
05174
05175
05176 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05177 _RandomAccessIterator>)
05178 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05179 _ValueType, _ValueType>)
05180 __glibcxx_requires_valid_range(__first, __nth);
05181 __glibcxx_requires_valid_range(__nth, __last);
05182
05183 if (__first == __last || __nth == __last)
05184 return;
05185
05186 std::__introselect(__first, __nth, __last,
05187 std::__lg(__last - __first) * 2, __comp);
05188 }
05189
05190
05191
05192
05193
05194
05195
05196
05197
05198
05199
05200
05201
05202
05203
05204
05205 template<typename _RandomAccessIterator>
05206 inline void
05207 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
05208 {
05209 typedef typename iterator_traits<_RandomAccessIterator>::value_type
05210 _ValueType;
05211
05212
05213 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05214 _RandomAccessIterator>)
05215 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
05216 __glibcxx_requires_valid_range(__first, __last);
05217
05218 if (__first != __last)
05219 {
05220 std::__introsort_loop(__first, __last,
05221 std::__lg(__last - __first) * 2);
05222 std::__final_insertion_sort(__first, __last);
05223 }
05224 }
05225
05226
05227
05228
05229
05230
05231
05232
05233
05234
05235
05236
05237
05238
05239
05240
05241 template<typename _RandomAccessIterator, typename _Compare>
05242 inline void
05243 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
05244 _Compare __comp)
05245 {
05246 typedef typename iterator_traits<_RandomAccessIterator>::value_type
05247 _ValueType;
05248
05249
05250 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05251 _RandomAccessIterator>)
05252 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
05253 _ValueType>)
05254 __glibcxx_requires_valid_range(__first, __last);
05255
05256 if (__first != __last)
05257 {
05258 std::__introsort_loop(__first, __last,
05259 std::__lg(__last - __first) * 2, __comp);
05260 std::__final_insertion_sort(__first, __last, __comp);
05261 }
05262 }
05263
05264
05265
05266
05267
05268
05269
05270
05271
05272
05273
05274
05275
05276
05277
05278
05279
05280
05281
05282 template<typename _InputIterator1, typename _InputIterator2,
05283 typename _OutputIterator>
05284 _OutputIterator
05285 merge(_InputIterator1 __first1, _InputIterator1 __last1,
05286 _InputIterator2 __first2, _InputIterator2 __last2,
05287 _OutputIterator __result)
05288 {
05289 typedef typename iterator_traits<_InputIterator1>::value_type
05290 _ValueType1;
05291 typedef typename iterator_traits<_InputIterator2>::value_type
05292 _ValueType2;
05293
05294
05295 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05296 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05297 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05298 _ValueType1>)
05299 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05300 _ValueType2>)
05301 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
05302 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05303 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05304
05305 while (__first1 != __last1 && __first2 != __last2)
05306 {
05307 if (*__first2 < *__first1)
05308 {
05309 *__result = *__first2;
05310 ++__first2;
05311 }
05312 else
05313 {
05314 *__result = *__first1;
05315 ++__first1;
05316 }
05317 ++__result;
05318 }
05319 return std::copy(__first2, __last2, std::copy(__first1, __last1,
05320 __result));
05321 }
05322
05323
05324
05325
05326
05327
05328
05329
05330
05331
05332
05333
05334
05335
05336
05337
05338
05339
05340
05341
05342
05343
05344
05345 template<typename _InputIterator1, typename _InputIterator2,
05346 typename _OutputIterator, typename _Compare>
05347 _OutputIterator
05348 merge(_InputIterator1 __first1, _InputIterator1 __last1,
05349 _InputIterator2 __first2, _InputIterator2 __last2,
05350 _OutputIterator __result, _Compare __comp)
05351 {
05352 typedef typename iterator_traits<_InputIterator1>::value_type
05353 _ValueType1;
05354 typedef typename iterator_traits<_InputIterator2>::value_type
05355 _ValueType2;
05356
05357
05358 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05359 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05360 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05361 _ValueType1>)
05362 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05363 _ValueType2>)
05364 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05365 _ValueType2, _ValueType1>)
05366 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05367 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05368
05369 while (__first1 != __last1 && __first2 != __last2)
05370 {
05371 if (__comp(*__first2, *__first1))
05372 {
05373 *__result = *__first2;
05374 ++__first2;
05375 }
05376 else
05377 {
05378 *__result = *__first1;
05379 ++__first1;
05380 }
05381 ++__result;
05382 }
05383 return std::copy(__first2, __last2, std::copy(__first1, __last1,
05384 __result));
05385 }
05386
05387
05388
05389
05390
05391
05392
05393
05394
05395
05396
05397
05398
05399
05400
05401
05402
05403
05404
05405 template<typename _RandomAccessIterator>
05406 inline void
05407 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
05408 {
05409 typedef typename iterator_traits<_RandomAccessIterator>::value_type
05410 _ValueType;
05411 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
05412 _DistanceType;
05413
05414
05415 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05416 _RandomAccessIterator>)
05417 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
05418 __glibcxx_requires_valid_range(__first, __last);
05419
05420 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
05421 __last);
05422 if (__buf.begin() == 0)
05423 std::__inplace_stable_sort(__first, __last);
05424 else
05425 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
05426 _DistanceType(__buf.size()));
05427 }
05428
05429
05430
05431
05432
05433
05434
05435
05436
05437
05438
05439
05440
05441
05442
05443
05444
05445
05446
05447 template<typename _RandomAccessIterator, typename _Compare>
05448 inline void
05449 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
05450 _Compare __comp)
05451 {
05452 typedef typename iterator_traits<_RandomAccessIterator>::value_type
05453 _ValueType;
05454 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
05455 _DistanceType;
05456
05457
05458 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
05459 _RandomAccessIterator>)
05460 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05461 _ValueType,
05462 _ValueType>)
05463 __glibcxx_requires_valid_range(__first, __last);
05464
05465 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
05466 __last);
05467 if (__buf.begin() == 0)
05468 std::__inplace_stable_sort(__first, __last, __comp);
05469 else
05470 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
05471 _DistanceType(__buf.size()), __comp);
05472 }
05473
05474
05475
05476
05477
05478
05479
05480
05481
05482
05483
05484
05485
05486
05487
05488
05489
05490
05491
05492
05493 template<typename _InputIterator1, typename _InputIterator2,
05494 typename _OutputIterator>
05495 _OutputIterator
05496 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
05497 _InputIterator2 __first2, _InputIterator2 __last2,
05498 _OutputIterator __result)
05499 {
05500 typedef typename iterator_traits<_InputIterator1>::value_type
05501 _ValueType1;
05502 typedef typename iterator_traits<_InputIterator2>::value_type
05503 _ValueType2;
05504
05505
05506 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05507 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05508 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05509 _ValueType1>)
05510 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05511 _ValueType2>)
05512 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05513 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
05514 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05515 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05516
05517 while (__first1 != __last1 && __first2 != __last2)
05518 {
05519 if (*__first1 < *__first2)
05520 {
05521 *__result = *__first1;
05522 ++__first1;
05523 }
05524 else if (*__first2 < *__first1)
05525 {
05526 *__result = *__first2;
05527 ++__first2;
05528 }
05529 else
05530 {
05531 *__result = *__first1;
05532 ++__first1;
05533 ++__first2;
05534 }
05535 ++__result;
05536 }
05537 return std::copy(__first2, __last2, std::copy(__first1, __last1,
05538 __result));
05539 }
05540
05541
05542
05543
05544
05545
05546
05547
05548
05549
05550
05551
05552
05553
05554
05555
05556
05557
05558
05559
05560 template<typename _InputIterator1, typename _InputIterator2,
05561 typename _OutputIterator, typename _Compare>
05562 _OutputIterator
05563 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
05564 _InputIterator2 __first2, _InputIterator2 __last2,
05565 _OutputIterator __result, _Compare __comp)
05566 {
05567 typedef typename iterator_traits<_InputIterator1>::value_type
05568 _ValueType1;
05569 typedef typename iterator_traits<_InputIterator2>::value_type
05570 _ValueType2;
05571
05572
05573 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05574 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05575 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05576 _ValueType1>)
05577 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05578 _ValueType2>)
05579 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05580 _ValueType1, _ValueType2>)
05581 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05582 _ValueType2, _ValueType1>)
05583 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05584 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05585
05586 while (__first1 != __last1 && __first2 != __last2)
05587 {
05588 if (__comp(*__first1, *__first2))
05589 {
05590 *__result = *__first1;
05591 ++__first1;
05592 }
05593 else if (__comp(*__first2, *__first1))
05594 {
05595 *__result = *__first2;
05596 ++__first2;
05597 }
05598 else
05599 {
05600 *__result = *__first1;
05601 ++__first1;
05602 ++__first2;
05603 }
05604 ++__result;
05605 }
05606 return std::copy(__first2, __last2, std::copy(__first1, __last1,
05607 __result));
05608 }
05609
05610
05611
05612
05613
05614
05615
05616
05617
05618
05619
05620
05621
05622
05623
05624
05625
05626
05627 template<typename _InputIterator1, typename _InputIterator2,
05628 typename _OutputIterator>
05629 _OutputIterator
05630 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
05631 _InputIterator2 __first2, _InputIterator2 __last2,
05632 _OutputIterator __result)
05633 {
05634 typedef typename iterator_traits<_InputIterator1>::value_type
05635 _ValueType1;
05636 typedef typename iterator_traits<_InputIterator2>::value_type
05637 _ValueType2;
05638
05639
05640 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05641 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05642 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05643 _ValueType1>)
05644 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05645 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
05646 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05647 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05648
05649 while (__first1 != __last1 && __first2 != __last2)
05650 if (*__first1 < *__first2)
05651 ++__first1;
05652 else if (*__first2 < *__first1)
05653 ++__first2;
05654 else
05655 {
05656 *__result = *__first1;
05657 ++__first1;
05658 ++__first2;
05659 ++__result;
05660 }
05661 return __result;
05662 }
05663
05664
05665
05666
05667
05668
05669
05670
05671
05672
05673
05674
05675
05676
05677
05678
05679
05680
05681
05682
05683
05684 template<typename _InputIterator1, typename _InputIterator2,
05685 typename _OutputIterator, typename _Compare>
05686 _OutputIterator
05687 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
05688 _InputIterator2 __first2, _InputIterator2 __last2,
05689 _OutputIterator __result, _Compare __comp)
05690 {
05691 typedef typename iterator_traits<_InputIterator1>::value_type
05692 _ValueType1;
05693 typedef typename iterator_traits<_InputIterator2>::value_type
05694 _ValueType2;
05695
05696
05697 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05698 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05699 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05700 _ValueType1>)
05701 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05702 _ValueType1, _ValueType2>)
05703 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05704 _ValueType2, _ValueType1>)
05705 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05706 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05707
05708 while (__first1 != __last1 && __first2 != __last2)
05709 if (__comp(*__first1, *__first2))
05710 ++__first1;
05711 else if (__comp(*__first2, *__first1))
05712 ++__first2;
05713 else
05714 {
05715 *__result = *__first1;
05716 ++__first1;
05717 ++__first2;
05718 ++__result;
05719 }
05720 return __result;
05721 }
05722
05723
05724
05725
05726
05727
05728
05729
05730
05731
05732
05733
05734
05735
05736
05737
05738
05739
05740
05741
05742 template<typename _InputIterator1, typename _InputIterator2,
05743 typename _OutputIterator>
05744 _OutputIterator
05745 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05746 _InputIterator2 __first2, _InputIterator2 __last2,
05747 _OutputIterator __result)
05748 {
05749 typedef typename iterator_traits<_InputIterator1>::value_type
05750 _ValueType1;
05751 typedef typename iterator_traits<_InputIterator2>::value_type
05752 _ValueType2;
05753
05754
05755 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05756 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05757 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05758 _ValueType1>)
05759 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05760 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
05761 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05762 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05763
05764 while (__first1 != __last1 && __first2 != __last2)
05765 if (*__first1 < *__first2)
05766 {
05767 *__result = *__first1;
05768 ++__first1;
05769 ++__result;
05770 }
05771 else if (*__first2 < *__first1)
05772 ++__first2;
05773 else
05774 {
05775 ++__first1;
05776 ++__first2;
05777 }
05778 return std::copy(__first1, __last1, __result);
05779 }
05780
05781
05782
05783
05784
05785
05786
05787
05788
05789
05790
05791
05792
05793
05794
05795
05796
05797
05798
05799
05800
05801
05802
05803 template<typename _InputIterator1, typename _InputIterator2,
05804 typename _OutputIterator, typename _Compare>
05805 _OutputIterator
05806 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05807 _InputIterator2 __first2, _InputIterator2 __last2,
05808 _OutputIterator __result, _Compare __comp)
05809 {
05810 typedef typename iterator_traits<_InputIterator1>::value_type
05811 _ValueType1;
05812 typedef typename iterator_traits<_InputIterator2>::value_type
05813 _ValueType2;
05814
05815
05816 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05817 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05818 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05819 _ValueType1>)
05820 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05821 _ValueType1, _ValueType2>)
05822 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05823 _ValueType2, _ValueType1>)
05824 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05825 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05826
05827 while (__first1 != __last1 && __first2 != __last2)
05828 if (__comp(*__first1, *__first2))
05829 {
05830 *__result = *__first1;
05831 ++__first1;
05832 ++__result;
05833 }
05834 else if (__comp(*__first2, *__first1))
05835 ++__first2;
05836 else
05837 {
05838 ++__first1;
05839 ++__first2;
05840 }
05841 return std::copy(__first1, __last1, __result);
05842 }
05843
05844
05845
05846
05847
05848
05849
05850
05851
05852
05853
05854
05855
05856
05857
05858
05859
05860
05861 template<typename _InputIterator1, typename _InputIterator2,
05862 typename _OutputIterator>
05863 _OutputIterator
05864 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05865 _InputIterator2 __first2, _InputIterator2 __last2,
05866 _OutputIterator __result)
05867 {
05868 typedef typename iterator_traits<_InputIterator1>::value_type
05869 _ValueType1;
05870 typedef typename iterator_traits<_InputIterator2>::value_type
05871 _ValueType2;
05872
05873
05874 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05875 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05876 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05877 _ValueType1>)
05878 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05879 _ValueType2>)
05880 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
05881 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
05882 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
05883 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
05884
05885 while (__first1 != __last1 && __first2 != __last2)
05886 if (*__first1 < *__first2)
05887 {
05888 *__result = *__first1;
05889 ++__first1;
05890 ++__result;
05891 }
05892 else if (*__first2 < *__first1)
05893 {
05894 *__result = *__first2;
05895 ++__first2;
05896 ++__result;
05897 }
05898 else
05899 {
05900 ++__first1;
05901 ++__first2;
05902 }
05903 return std::copy(__first2, __last2, std::copy(__first1,
05904 __last1, __result));
05905 }
05906
05907
05908
05909
05910
05911
05912
05913
05914
05915
05916
05917
05918
05919
05920
05921
05922
05923
05924
05925
05926
05927 template<typename _InputIterator1, typename _InputIterator2,
05928 typename _OutputIterator, typename _Compare>
05929 _OutputIterator
05930 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
05931 _InputIterator2 __first2, _InputIterator2 __last2,
05932 _OutputIterator __result,
05933 _Compare __comp)
05934 {
05935 typedef typename iterator_traits<_InputIterator1>::value_type
05936 _ValueType1;
05937 typedef typename iterator_traits<_InputIterator2>::value_type
05938 _ValueType2;
05939
05940
05941 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
05942 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
05943 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05944 _ValueType1>)
05945 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
05946 _ValueType2>)
05947 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05948 _ValueType1, _ValueType2>)
05949 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
05950 _ValueType2, _ValueType1>)
05951 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
05952 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
05953
05954 while (__first1 != __last1 && __first2 != __last2)
05955 if (__comp(*__first1, *__first2))
05956 {
05957 *__result = *__first1;
05958 ++__first1;
05959 ++__result;
05960 }
05961 else if (__comp(*__first2, *__first1))
05962 {
05963 *__result = *__first2;
05964 ++__first2;
05965 ++__result;
05966 }
05967 else
05968 {
05969 ++__first1;
05970 ++__first2;
05971 }
05972 return std::copy(__first2, __last2,
05973 std::copy(__first1, __last1, __result));
05974 }
05975
05976
05977
05978
05979
05980
05981
05982
05983
05984 template<typename _ForwardIterator>
05985 _ForwardIterator
05986 min_element(_ForwardIterator __first, _ForwardIterator __last)
05987 {
05988
05989 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
05990 __glibcxx_function_requires(_LessThanComparableConcept<
05991 typename iterator_traits<_ForwardIterator>::value_type>)
05992 __glibcxx_requires_valid_range(__first, __last);
05993
05994 if (__first == __last)
05995 return __first;
05996 _ForwardIterator __result = __first;
05997 while (++__first != __last)
05998 if (*__first < *__result)
05999 __result = __first;
06000 return __result;
06001 }
06002
06003
06004
06005
06006
06007
06008
06009
06010
06011
06012 template<typename _ForwardIterator, typename _Compare>
06013 _ForwardIterator
06014 min_element(_ForwardIterator __first, _ForwardIterator __last,
06015 _Compare __comp)
06016 {
06017
06018 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
06019 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
06020 typename iterator_traits<_ForwardIterator>::value_type,
06021 typename iterator_traits<_ForwardIterator>::value_type>)
06022 __glibcxx_requires_valid_range(__first, __last);
06023
06024 if (__first == __last)
06025 return __first;
06026 _ForwardIterator __result = __first;
06027 while (++__first != __last)
06028 if (__comp(*__first, *__result))
06029 __result = __first;
06030 return __result;
06031 }
06032
06033
06034
06035
06036
06037
06038
06039
06040 template<typename _ForwardIterator>
06041 _ForwardIterator
06042 max_element(_ForwardIterator __first, _ForwardIterator __last)
06043 {
06044
06045 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
06046 __glibcxx_function_requires(_LessThanComparableConcept<
06047 typename iterator_traits<_ForwardIterator>::value_type>)
06048 __glibcxx_requires_valid_range(__first, __last);
06049
06050 if (__first == __last)
06051 return __first;
06052 _ForwardIterator __result = __first;
06053 while (++__first != __last)
06054 if (*__result < *__first)
06055 __result = __first;
06056 return __result;
06057 }
06058
06059
06060
06061
06062
06063
06064
06065
06066
06067
06068 template<typename _ForwardIterator, typename _Compare>
06069 _ForwardIterator
06070 max_element(_ForwardIterator __first, _ForwardIterator __last,
06071 _Compare __comp)
06072 {
06073
06074 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
06075 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
06076 typename iterator_traits<_ForwardIterator>::value_type,
06077 typename iterator_traits<_ForwardIterator>::value_type>)
06078 __glibcxx_requires_valid_range(__first, __last);
06079
06080 if (__first == __last) return __first;
06081 _ForwardIterator __result = __first;
06082 while (++__first != __last)
06083 if (__comp(*__result, *__first))
06084 __result = __first;
06085 return __result;
06086 }
06087
06088 _GLIBCXX_END_NESTED_NAMESPACE
06089
06090 #endif