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