Wiselib
wiselib.testing/util/pstl/algorithm.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  **                                                                       **
00019  ** Author: Juan Farré, UPC                                               **
00020  **                                                                       **
00021  ***************************************************************************/
00022 #ifndef __WISELIB_INTERNAL_INTERFACE_STL_ALGORITHM_H
00023 #define __WISELIB_INTERNAL_INTERFACE_STL_ALGORITHM_H
00024 
00025 #include <util/pstl/iterator.h>
00026 #include <util/pstl/utility.h>
00027 
00028 namespace wiselib {
00029 
00030 template<class InputIterator, class Function>
00031 Function for_each(InputIterator first, InputIterator last, Function f) {
00032    for (; first != last; ++first)
00033       f(*first);
00034    return f;
00035 }
00036 
00037 template<class InputIterator, class T>
00038 InputIterator find(InputIterator first, InputIterator last, T const &value) {
00039    for (; first != last && *first != value; ++first)
00040       ;
00041    return first;
00042 }
00043 
00044 template<class InputIterator, class Predicate>
00045 InputIterator find_if(InputIterator first, InputIterator last, Predicate pred) {
00046    for (; first != last && !pred(*first); ++first)
00047       ;
00048    return first;
00049 }
00050 
00051 template<class ForwardIterator>
00052 ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last) {
00053    if (first == last)
00054       return;
00055    ForwardIterator next = first;
00056    ++next;
00057    while (next != last) {
00058       if (*first == *next)
00059          return first;
00060       ++first;
00061       ++next;
00062    }
00063    return last;
00064 }
00065 
00066 template<class ForwardIterator, class BinaryPredicate>
00067 ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last,
00068       BinaryPredicate pred) {
00069    if (first == last)
00070       return;
00071    ForwardIterator next = first;
00072    ++next;
00073    while (next != last) {
00074       if (pred(*first, *next))
00075          return first;
00076       ++first;
00077       ++next;
00078    }
00079    return last;
00080 }
00081 
00082 template<class InputIterator, class T>
00083 typename iterator_traits<InputIterator>::difference_type count(
00084       InputIterator first, InputIterator last, T const &value) {
00085    typename iterator_traits<InputIterator>::difference_type c = 0;
00086    while (first != last)
00087       if (*first++ == value)
00088          ++c;
00089    return c;
00090 }
00091 
00092 template<class InputIterator, class Predicate>
00093 typename iterator_traits<InputIterator>::difference_type count_if(
00094       InputIterator first, InputIterator last, Predicate pred) {
00095    typename iterator_traits<InputIterator>::difference_type c = 0;
00096    while (first != last)
00097       if (pred(*first++))
00098          ++c;
00099    return c;
00100 }
00101 
00102 template<class InputIterator1, class InputIterator2>
00103 bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) {
00104    while (first1 != last1)
00105       if (*first1++ != *first2++)
00106          return false;
00107    return true;
00108 }
00109 
00110 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
00111 bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
00112       BinaryPredicate pred) {
00113    while (first1 != last1)
00114       if (!pred(*first1++, *first2++))
00115          return false;
00116    return true;
00117 }
00118 
00119 template<class InputIterator1, class InputIterator2>
00120 pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1,
00121       InputIterator1 last1, InputIterator2 first2) {
00122    while (first1 != last1) {
00123       if (*first1 != *first2)
00124          break;
00125       ++first1;
00126       ++first2;
00127    }
00128    return make_pair(first1, first2);
00129 }
00130 
00131 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
00132 pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1,
00133       InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred) {
00134    while (first1 != last1) {
00135       if (!pred(*first1, *first2))
00136          break;
00137       ++first1;
00138       ++first2;
00139    }
00140    return make_pair(first1, first2);
00141 }
00142 
00143 template<class ForwardIterator1, class ForwardIterator2>
00144 ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
00145       ForwardIterator2 first2, ForwardIterator2 last2) {
00146    if (first2 == last2)
00147       return first1;
00148    for (; first1 != last1; ++first1) {
00149       ForwardIterator1 it1 = first1;
00150       ForwardIterator2 it2 = first2;
00151       while (*it1 == *it2) {
00152          ++it1;
00153          ++it2;
00154          if (it2 == last2)
00155             return first1;
00156          if (it1 == last1)
00157             return last1;
00158       }
00159    }
00160    return last1;
00161 }
00162 
00163 template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
00164 ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
00165       ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred) {
00166    if (first2 == last2)
00167       return first1;
00168    for (; first1 != last1; ++first1) {
00169       ForwardIterator1 it1 = first1;
00170       ForwardIterator2 it2 = first2;
00171       while (pred(*it1, *it2)) {
00172          ++it1;
00173          ++it2;
00174          if (it2 == last2)
00175             return first1;
00176          if (it1 == last1)
00177             return last1;
00178       }
00179    }
00180    return last1;
00181 }
00182 
00183 template<class ForwardIterator1, class ForwardIterator2>
00184 ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
00185       ForwardIterator2 first2, ForwardIterator2 last2) {
00186    if (first2 == last2)
00187       return last1;
00188    ForwardIterator1 ret = last1;
00189    for (; first1 != last1; ++first1) {
00190       ForwardIterator1 it1 = first1;
00191       ForwardIterator2 it2 = first2;
00192       while (*it1 == *it2) {
00193          ++it1;
00194          ++it2;
00195          if (it2 == last2) {
00196             ret = first1;
00197             if (it1 == last1)
00198                return ret;
00199             else
00200                break;
00201          }
00202          if (it1 == last1)
00203             return ret;
00204       }
00205    }
00206    return ret;
00207 }
00208 
00209 template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
00210 ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
00211       ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred) {
00212    if (first2 == last2)
00213       return last1;
00214    ForwardIterator1 ret = last1;
00215    for (; first1 != last1; ++first1) {
00216       ForwardIterator1 it1 = first1;
00217       ForwardIterator2 it2 = first2;
00218       while (pred(*it1, *it2)) {
00219          ++it1;
00220          ++it2;
00221          if (it2 == last2) {
00222             ret = first1;
00223             if (it1 == last1)
00224                return ret;
00225             else
00226                break;
00227          }
00228          if (it1 == last1)
00229             return ret;
00230       }
00231    }
00232    return ret;
00233 }
00234 
00235 template<class ForwardIterator, class Size, class T>
00236 ForwardIterator search_n(ForwardIterator first, ForwardIterator last,
00237       Size count, T const &value) {
00238    Size n = 0;
00239    ForwardIterator ret = first;
00240    for (; first != last && n != count; ++first) {
00241       if (*first == value)
00242          ++n;
00243       else {
00244          n = 0;
00245          ret = first;
00246          ++ret;
00247       }
00248    }
00249    return (n == count) ? ret : last;
00250 }
00251 
00252 template<class ForwardIterator, class Size, class T, class BinaryPredicate>
00253 ForwardIterator search_n(ForwardIterator first, ForwardIterator last,
00254       Size count, T const &value, BinaryPredicate pred) {
00255    Size n = 0;
00256    ForwardIterator ret = first;
00257    for (; first != last && n != count; ++first) {
00258       if (pred(*first, value))
00259          ++n;
00260       else {
00261          n = 0;
00262          ret = first;
00263          ++ret;
00264       }
00265    }
00266    return (n == count) ? ret : last;
00267 }
00268 
00269 template<class ForwardIterator1, class ForwardIterator2>
00270 ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
00271       ForwardIterator2 first2, ForwardIterator2 last2) {
00272    for (; first1 != last1; ++first1)
00273       for (ForwardIterator2 it = first2; it != last2; ++it)
00274          if (*it == *first1)
00275             return first1;
00276    return last1;
00277 }
00278 
00279 template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
00280 ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
00281       ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred) {
00282    for (; first1 != last1; ++first1)
00283       for (ForwardIterator2 it = first2; it != last2; ++it)
00284          if (pred(*it, *first1))
00285             return first1;
00286    return last1;
00287 }
00288 
00289 template<class T>
00290 T const &min(T const &a, T const &b) {
00291    return (a < b) ? a : b;
00292 }
00293 
00294 template<class T, class Compare>
00295 T const &min(T const &a, T const &b, Compare comp) {
00296    return comp(a, b) ? a : b;
00297 }
00298 
00299 template<class T>
00300 T const &max(T const &a, T const &b) {
00301    return (b < a) ? a : b;
00302 }
00303 
00304 template<class T, class Compare>
00305 T const &max(T const &a, T const &b, Compare comp) {
00306    return comp(b, a) ? a : b;
00307 }
00308 
00309 template<class ForwardIterator>
00310 ForwardIterator min_element(ForwardIterator first, ForwardIterator last) {
00311    if (first == last)
00312       return last;
00313    ForwardIterator lowest = first;
00314    while (++first != last)
00315       if (*first < *lowest)
00316          lowest = first;
00317    return lowest;
00318 }
00319 
00320 template<class ForwardIterator, class Compare>
00321 ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
00322       Compare comp) {
00323    if (first == last)
00324       return last;
00325    ForwardIterator lowest = first;
00326    while (++first != last)
00327       if (comp(*first, *lowest))
00328          lowest = first;
00329    return lowest;
00330 }
00331 
00332 template<class ForwardIterator>
00333 ForwardIterator max_element(ForwardIterator first, ForwardIterator last) {
00334    if (first == last)
00335       return last;
00336    ForwardIterator largest = first;
00337    while (++first != last)
00338       if (*largest < *first)
00339          largest = first;
00340    return largest;
00341 }
00342 
00343 template<class ForwardIterator, class Compare>
00344 ForwardIterator max_element(ForwardIterator first, ForwardIterator last,
00345       Compare comp) {
00346    if (first == last)
00347       return last;
00348    ForwardIterator largest = first;
00349    while (++first != last)
00350       if (comp(*largest, *first))
00351          largest = first;
00352    return largest;
00353 }
00354 
00355 template<class InputIterator1, class InputIterator2>
00356 bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
00357       InputIterator2 first2, InputIterator2 last2) {
00358    while (first1 != last1) {
00359       if (first2 == last2 || *first2 < *first1)
00360          return false;
00361       else if (*first1 < *first2)
00362          return true;
00363       ++first1;
00364       ++first2;
00365    }
00366    return (first2 != last2);
00367 }
00368 
00369 template<class InputIterator1, class InputIterator2, class Compare>
00370 bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
00371       InputIterator2 first2, InputIterator2 last2, Compare comp) {
00372    while (first1 != last1) {
00373       if (first2 == last2 || comp(*first2, *first1))
00374          return false;
00375       else if (comp(*first1, *first2))
00376          return true;
00377       ++first1;
00378       ++first2;
00379    }
00380    return (first2 != last2);
00381 }
00382 
00383 template<class ForwardIterator, class T>
00384 ForwardIterator sequential_lower_bound(ForwardIterator first,
00385       ForwardIterator last, T const &value) {
00386    for (; first != last && *first < value; ++first)
00387       ;
00388    return first;
00389 }
00390 
00391 template<class ForwardIterator, class T, class Compare>
00392 ForwardIterator sequential_lower_bound(ForwardIterator first,
00393       ForwardIterator last, T const &value, Compare comp) {
00394    for (; first != last && comp(*first, value); ++first)
00395       ;
00396    return first;
00397 }
00398 
00399 template<class ForwardIterator, class T>
00400 ForwardIterator sequential_upper_bound(ForwardIterator first,
00401       ForwardIterator last, T const &value) {
00402    for (; first != last && !(value < *first); ++first)
00403       ;
00404    return first;
00405 }
00406 
00407 template<class ForwardIterator, class T, class Compare>
00408 ForwardIterator sequential_upper_bound(ForwardIterator first,
00409       ForwardIterator last, T const &value, Compare comp) {
00410    for (; first != last && !comp(value, *first); ++first)
00411       ;
00412    return first;
00413 }
00414 
00415 template<class ForwardIterator, class T>
00416 pair<ForwardIterator, ForwardIterator> sequential_equal_range(
00417       ForwardIterator first, ForwardIterator last, T const &value) {
00418    first = sequential_lower_bound(first, last, value);
00419    return make_pair(first, sequential_upper_bound(first, last, value));
00420 }
00421 
00422 template<class ForwardIterator, class T, class Compare>
00423 pair<ForwardIterator, ForwardIterator> sequential_equal_range(
00424       ForwardIterator first, ForwardIterator last, T const &value,
00425       Compare comp) {
00426    first = sequential_lower_bound(first, last, value, comp);
00427    return make_pair(first, sequential_upper_bound(first, last, value, comp));
00428 }
00429 
00430 template<class ForwardIterator, class T>
00431 bool sequential_search(ForwardIterator first, ForwardIterator last,
00432       T const &value) {
00433    first = sequential_lower_bound(first, last, value);
00434    return (first != last && !(value < *first));
00435 }
00436 
00437 template<class ForwardIterator, class T, class Compare>
00438 bool sequential_search(ForwardIterator first, ForwardIterator last,
00439       T const &value, Compare comp) {
00440    first = sequential_lower_bound(first, last, value);
00441    return (first != last && !comp(value, *first));
00442 }
00443 
00444 template<class ForwardIterator, class T>
00445 ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
00446       T const &value) {
00447    ForwardIterator it;
00448    typename iterator_traits<ForwardIterator>::distance_type count, step;
00449    count = distance(first, last);
00450    while (count > 0) {
00451       it = first;
00452       step = count >> 1;
00453       advance(it, step);
00454       if (*it < value) {
00455          first = it + 1;
00456          count -= step + 1;
00457       } else
00458          count = step;
00459    }
00460    return first;
00461 }
00462 
00463 template<class ForwardIterator, class T, class Compare>
00464 ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
00465       T const &value, Compare comp) {
00466    ForwardIterator it;
00467    typename iterator_traits<ForwardIterator>::distance_type count, step;
00468    count = distance(first, last);
00469    while (count > 0) {
00470       it = first;
00471       step = count >> 1;
00472       advance(it, step);
00473       if (comp(*it, value)) {
00474          first = it + 1;
00475          count -= step + 1;
00476       } else
00477          count = step;
00478    }
00479    return first;
00480 }
00481 
00482 template<class ForwardIterator, class T>
00483 ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last,
00484       T const &value) {
00485    ForwardIterator it;
00486    typename iterator_traits<ForwardIterator>::distance_type count, step;
00487    count = distance(first, last);
00488    while (count > 0) {
00489       it = first;
00490       step = count >> 1;
00491       advance(it, step);
00492       if (!(value < *it)) {
00493          first = it + 1;
00494          count -= step + 1;
00495       } else
00496          count = step;
00497    }
00498    return first;
00499 }
00500 
00501 template<class ForwardIterator, class T, class Compare>
00502 ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last,
00503       T const &value, Compare comp) {
00504    ForwardIterator it;
00505    typename iterator_traits<ForwardIterator>::distance_type count, step;
00506    count = distance(first, last);
00507    while (count > 0) {
00508       it = first;
00509       step = count >> 1;
00510       advance(it, step);
00511       if (!comp(value, *it)) {
00512          first = it + 1;
00513          count -= step + 1;
00514       } else
00515          count = step;
00516    }
00517    return first;
00518 }
00519 
00520 template<class ForwardIterator, class T>
00521 pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first,
00522       ForwardIterator last, T const &value) {
00523    first = lower_bound(first, last, value);
00524    return make_pair(first, upper_bound(first, last, value));
00525 }
00526 
00527 template<class ForwardIterator, class T, class Compare>
00528 pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first,
00529       ForwardIterator last, T const &value, Compare comp) {
00530    first = lower_bound(first, last, value, comp);
00531    return make_pair(first, upper_bound(first, last, value, comp));
00532 }
00533 
00534 template<class ForwardIterator, class T>
00535 bool binary_search(ForwardIterator first, ForwardIterator last, T const &value) {
00536    first = lower_bound(first, last, value);
00537    return (first != last && !(value < *first));
00538 }
00539 
00540 template<class ForwardIterator, class T, class Compare>
00541 bool binary_search(ForwardIterator first, ForwardIterator last, T const &value,
00542       Compare comp) {
00543    first = lower_bound(first, last, value);
00544    return (first != last && !comp(value, *first));
00545 }
00546 
00547 template<class T>
00548 void swap(T &a, T &b) {
00549    T const c(a);
00550    a = b;
00551    b = c;
00552 }
00553 
00554 template<class ForwardIterator1, class ForwardIterator2>
00555 void iter_swap(ForwardIterator1 a, ForwardIterator2 b) {
00556    swap(*a, *b);
00557 }
00558 
00559 template<class ForwardIterator1, class ForwardIterator2>
00560 ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1,
00561       ForwardIterator2 first2) {
00562    while (first1 != last1)
00563       iter_swap(first1++, first2++);
00564    return first2;
00565 }
00566 
00567 template<class InputIterator, class OutputIterator>
00568 OutputIterator copy(InputIterator first, InputIterator last,
00569       OutputIterator dest) {
00570    while (first != last)
00571       *dest++ = *first++;
00572    return dest;
00573 }
00574 
00575 template<class BIter1, class BIter2>
00576 BIter2 copy_backward(BIter1 first, BIter1 last, BIter2 dest_end) {
00577    while (last != first)
00578       *--dest_end = *--last;
00579    return dest_end;
00580 }
00581 
00582 template<class InputIterator, class OutputIterator, class UnaryOperator>
00583 OutputIterator transform(InputIterator first, InputIterator last,
00584       OutputIterator result, UnaryOperator op) {
00585    while (first != last)
00586       *result++ = op(*first++);
00587    return result;
00588 }
00589 
00590 template<class InputIterator1, class InputIterator2, class OutputIterator,
00591       class BinaryOperator>
00592 OutputIterator transform(InputIterator1 first1, InputIterator1 last1,
00593       InputIterator2 first2, OutputIterator result, BinaryOperator binary_op) {
00594    while (first1 != last1)
00595       *result++ = binary_op(*first1++, *first2++);
00596    return result;
00597 }
00598 
00599 template<class ForwardIterator, class T>
00600 void replace(ForwardIterator first, ForwardIterator last, T const &old_value,
00601       T const &new_value) {
00602    for (; first != last; ++first)
00603       if (*first == old_value)
00604          *first = new_value;
00605 }
00606 
00607 template<class ForwardIterator, class Predicate, class T>
00608 void replace_if(ForwardIterator first, ForwardIterator last, Predicate pred,
00609       T const &new_value) {
00610    for (; first != last; ++first)
00611       if (pred(*first))
00612          *first = new_value;
00613 }
00614 
00615 template<class InputIterator, class OutputIterator, class T>
00616 OutputIterator replace_copy(InputIterator first, InputIterator last,
00617       OutputIterator result, T const &old_value, T const &new_value) {
00618    for (; first != last; ++first)
00619       *result++ = (*first == old_value) ? new_value : *first;
00620    return result;
00621 }
00622 
00623 template<class InputIterator, class OutputIterator, class Predicate, class T>
00624 OutputIterator replace_copy_if(InputIterator first, InputIterator last,
00625       OutputIterator result, Predicate pred, T const &new_value) {
00626    for (; first != last; ++first)
00627       *result++ = (pred(*first)) ? new_value : *first;
00628    return result;
00629 }
00630 
00631 template<class ForwardIterator, class T>
00632 void fill(ForwardIterator first, ForwardIterator last, T const &value) {
00633    while (first != last)
00634       *first++ = value;
00635 }
00636 
00637 template<class OutputIterator, class Size, class T>
00638 void fill_n(OutputIterator first, Size n, T const &value) {
00639    for (; n > 0; --n)
00640       *first++ = value;
00641 }
00642 
00643 template<class ForwardIterator, class Generator>
00644 void generate(ForwardIterator first, ForwardIterator last, Generator gen) {
00645    while (first != last)
00646       *first++ = gen();
00647 }
00648 
00649 template<class OutputIterator, class Size, class Generator>
00650 void generate(OutputIterator first, Size n, Generator gen) {
00651    for (; n > 0; --n)
00652       *first++ = gen();
00653 }
00654 
00655 template<class ForwardIterator, class T>
00656 ForwardIterator remove(ForwardIterator first, ForwardIterator last,
00657       T const &value) {
00658    remove_copy(first, last, first, value);
00659 }
00660 
00661 template<class ForwardIterator, class Predicate>
00662 ForwardIterator remove_if(ForwardIterator first, ForwardIterator last,
00663       Predicate pred) {
00664    remove_copy_if(first, last, first, pred);
00665 }
00666 
00667 template<class InputIterator, class OutputIterator, class T>
00668 OutputIterator remove_copy(InputIterator first, InputIterator last,
00669       OutputIterator result, T const &value) {
00670    for (; first != last; ++first)
00671       if (!(*first == value))
00672          *result++ = *first;
00673    return result;
00674 }
00675 
00676 template<class InputIterator, class OutputIterator, class Predicate>
00677 OutputIterator remove_copy_if(InputIterator first, InputIterator last,
00678       OutputIterator result, Predicate pred) {
00679    for (; first != last; ++first)
00680       if (!pred(*first))
00681          *result++ = *first;
00682    return result;
00683 }
00684 
00685 template<class ForwardIterator>
00686 ForwardIterator unique(ForwardIterator first, ForwardIterator last) {
00687    ForwardIterator result = first;
00688    while (++first != last) {
00689       if (!(*result == *first))
00690          *++result = *first;
00691    }
00692    return ++result;
00693 }
00694 
00695 template<class ForwardIterator, class BinaryPredicate>
00696 ForwardIterator unique(ForwardIterator first, ForwardIterator last,
00697       BinaryPredicate pred) {
00698    ForwardIterator result = first;
00699    while (++first != last) {
00700       if (!pred(*result, *first))
00701          *++result = *first;
00702    }
00703    return ++result;
00704 }
00705 
00706 template<class InputIterator, class OutputIterator>
00707 OutputIterator unique_copy(InputIterator first, InputIterator last,
00708       OutputIterator result) {
00709    *result = *first;
00710    while (++first != last) {
00711       if (!(*result == *first))
00712          *++result = *first;
00713    }
00714    return ++result;
00715 }
00716 
00717 template<class InputIterator, class OutputIterator, class BinaryPredicate>
00718 OutputIterator unique_copy(InputIterator first, InputIterator last,
00719       OutputIterator result, BinaryPredicate pred) {
00720    *result = *first;
00721    while (++first != last) {
00722       if (!pred(*result, *first))
00723          *++result = *first;
00724    }
00725    return ++result;
00726 }
00727 
00728 template<class BidirectionalIterator>
00729 void reverse(BidirectionalIterator first, BidirectionalIterator last) {
00730    while ((first != last) && (first != --last))
00731       iter_swap(first++, last);
00732 }
00733 
00734 template<class BidirectionalIterator, class OutputIterator>
00735 OutputIterator reverse_copy(BidirectionalIterator first,
00736       BidirectionalIterator last, OutputIterator result) {
00737    while (first != last)
00738       *result++ = *--last;
00739    return result;
00740 }
00741 
00742 template<class ForwardIterator>
00743 void rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last) {
00744    ForwardIterator next = middle;
00745    while (first != next) {
00746       swap(*first++, *next++);
00747       if (next == last)
00748          next = middle;
00749       else if (first == middle)
00750          middle = next;
00751    }
00752 }
00753 
00754 template<class ForwardIterator, class OutputIterator>
00755 OutputIterator rotate_copy(ForwardIterator first, ForwardIterator middle,
00756       ForwardIterator last, OutputIterator result) {
00757    result = copy(middle, last, result);
00758    return copy(first, middle, result);
00759 }
00760 
00761 template<class RandomAccessIterator, class RandomNumberGenerator>
00762 void random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
00763       RandomNumberGenerator& rand) {
00764    for (typename iterator_traits<RandomAccessIterator>::difference_type i = 2; i
00765          < last - first; ++i)
00766       swap(first[i], first[rand(i)]);
00767 }
00768 
00769 template<class BidirectionalIterator, class Predicate>
00770 BidirectionalIterator partition(BidirectionalIterator first,
00771       BidirectionalIterator last, Predicate pred) {
00772    for (;; ++first) {
00773       for (; first != last && pred(*first); ++first)
00774          ;
00775       if (first == last)
00776          break;
00777       while (first != --last && !pred(*last))
00778          ;
00779       if (first == last)
00780          break;
00781       iter_swap(first, last);
00782    }
00783    return first;
00784 }
00785 
00786 template<class BidirectionalIterator>
00787 BidirectionalIterator __partition(BidirectionalIterator first,
00788       BidirectionalIterator pivot, BidirectionalIterator last) {
00789    typedef typename iterator_traits<BidirectionalIterator>::value_type
00790          value_type;
00791 
00792    class Pred {
00793    public:
00794       explicit Pred(value_type const value) :
00795          pivot_value(value) {
00796       }
00797 
00798       bool operator()(value_type const value) const {
00799          return value < pivot_value;
00800       }
00801 
00802    private:
00803       value_type const pivot_value;
00804    };
00805 
00806    iter_swap(--last, pivot);
00807    pivot = partition(first, last, Pred(*last));
00808    iter_swap(pivot, last);
00809    return pivot;
00810 }
00811 
00812 template<class RandomAccessIterator>
00813 RandomAccessIterator __medianof3(RandomAccessIterator first,
00814       RandomAccessIterator last) {
00815    RandomAccessIterator mid = first + (last - first) >> 1;
00816    if (*mid < *first)
00817       swap(first, mid);
00818    if (*--last < *first)
00819       swap(first, last);
00820    if (*last < *mid)
00821       swap(mid, last);
00822    return mid;
00823 }
00824 
00825 template<class RandomAccessIterator>
00826 void quickselect(RandomAccessIterator first, RandomAccessIterator nth,
00827       RandomAccessIterator last) {
00828    for (;;) {
00829       RandomAccessIterator const pos = __partition(first, __medianof3(first,
00830             last), last);
00831       if (pos == nth)
00832          return;
00833       if (pos < nth)
00834          first = pos + 1;
00835       else
00836          last = pos;
00837    }
00838 }
00839 
00840 template<class BidirectionalIterator, class Compare>
00841 BidirectionalIterator __partition(BidirectionalIterator first,
00842       BidirectionalIterator pivot, BidirectionalIterator last, Compare comp) {
00843    typedef typename iterator_traits<BidirectionalIterator>::value_type
00844          value_type;
00845 
00846    class Pred {
00847    public:
00848       Pred(value_type const value, Compare cmp) :
00849          pivot_value(value), comp(cmp) {
00850       }
00851 
00852       bool operator()(value_type const value) const {
00853          return comp(value, pivot_value);
00854       }
00855 
00856    private:
00857       value_type const pivot_value;
00858       Compare const comp;
00859    };
00860 
00861    iter_swap(--last, pivot);
00862    pivot = partition(first, last, Pred(*last, comp));
00863    iter_swap(pivot, last);
00864    return pivot;
00865 }
00866 
00867 template<class RandomAccessIterator, class Compare>
00868 RandomAccessIterator __medianof3(RandomAccessIterator first,
00869       RandomAccessIterator last, Compare comp) {
00870    RandomAccessIterator mid = first + (last - first) >> 1;
00871    if (comp(*mid, *first))
00872       swap(first, mid);
00873    if (comp(*--last, *first))
00874       swap(first, last);
00875    if (comp(*last, *mid))
00876       swap(mid, last);
00877    return mid;
00878 }
00879 
00880 template<class RandomAccessIterator, class Compare>
00881 void quickselect(RandomAccessIterator first, RandomAccessIterator nth,
00882       RandomAccessIterator last, Compare comp) {
00883    for (;;) {
00884       RandomAccessIterator const pos = __partition(first, __medianof3(first,
00885             last, comp), last, comp);
00886       if (pos == nth)
00887          return;
00888       if (pos < nth)
00889          first = pos + 1;
00890       else
00891          last = pos;
00892    }
00893 }
00894 
00895 template<class RandomAccessIterator>
00896 void __push_heap(RandomAccessIterator const base, typename iterator_traits<
00897       RandomAccessIterator>::difference_type n) {
00898    while (n > 1) {
00899       typename iterator_traits<RandomAccessIterator>::difference_type const
00900             parent = n >> 1;
00901       if (!(base[parent] < base[n]))
00902          return;
00903       swap(base[parent], base[n]);
00904       n = parent;
00905    }
00906 }
00907 
00908 template<class RandomAccessIterator, class Compare>
00909 void __push_heap(RandomAccessIterator const base, typename iterator_traits<
00910       RandomAccessIterator>::difference_type n, Compare const comp) {
00911    while (n > 1) {
00912       typename iterator_traits<RandomAccessIterator>::difference_type const
00913             parent = n >> 1;
00914       if (!comp(base[parent], base[n]))
00915          return;
00916       swap(base[parent], base[n]);
00917       n = parent;
00918    }
00919 }
00920 
00921 template<class RandomAccessIterator>
00922 void push_heap(RandomAccessIterator first, RandomAccessIterator last) {
00923    __push_heap(--first, last - first);
00924 }
00925 
00926 template<class RandomAccessIterator, class Compare>
00927 void push_heap(RandomAccessIterator first, RandomAccessIterator last,
00928       Compare comp) {
00929    __push_heap(--first, last - first, comp);
00930 }
00931 
00932 template<class RandomAccessIterator>
00933 void __sift_down(RandomAccessIterator const base, typename iterator_traits<
00934       RandomAccessIterator>::difference_type start, typename iterator_traits<
00935       RandomAccessIterator>::difference_type const end) {
00936    while (start <= end >> 1) {
00937       typename iterator_traits<RandomAccessIterator>::difference_type child =
00938             start << 1;
00939       if (child < end && base[child] < base[child | 1])
00940          child |= 1;
00941       if (!(base[start] < base[child]))
00942          return;
00943       swap(base[start], base[child]);
00944       start = child;
00945    }
00946 }
00947 
00948 template<class RandomAccessIterator, class Compare>
00949 void __sift_down(RandomAccessIterator const base, typename iterator_traits<
00950       RandomAccessIterator>::difference_type start, typename iterator_traits<
00951       RandomAccessIterator>::difference_type const end, Compare const comp) {
00952    while (start <= end >> 1) {
00953       typename iterator_traits<RandomAccessIterator>::difference_type child =
00954             start << 1;
00955       if (child < end && comp(base[child], base[child | 1]))
00956          child |= 1;
00957       if (!comp(base[start], base[child]))
00958          return;
00959       swap(base[start], base[child]);
00960       start = child;
00961    }
00962 }
00963 
00964 template<class RandomAccessIterator>
00965 void __pop_heap(RandomAccessIterator const base, typename iterator_traits<
00966       RandomAccessIterator>::difference_type const n) {
00967    swap(base[n], base[1]);
00968    __sift_down(base, 1, n - 1);
00969 }
00970 
00971 template<class RandomAccessIterator, class Compare>
00972 void __pop_heap(RandomAccessIterator const base, typename iterator_traits<
00973       RandomAccessIterator>::difference_type const n, Compare const comp) {
00974    swap(base[n], base[1]);
00975    __sift_down(base, 1, n - 1, comp);
00976 }
00977 
00978 template<class RandomAccessIterator>
00979 void pop_heap(RandomAccessIterator first, RandomAccessIterator last) {
00980    __pop_heap(--first, last - first);
00981 }
00982 
00983 template<class RandomAccessIterator, class Compare>
00984 void pop_heap(RandomAccessIterator first, RandomAccessIterator last,
00985       Compare comp) {
00986    __pop_heap(--first, last - first, comp);
00987 }
00988 
00989 template<class RandomAccessIterator>
00990 void make_heap(RandomAccessIterator first, RandomAccessIterator last) {
00991    typedef typename iterator_traits<RandomAccessIterator>::difference_type
00992          index_type;
00993    index_type const n = last - first;
00994    --first;
00995    for (index_type start = n >> 1; start; --start)
00996       __sift_down(first, start, n);
00997 }
00998 
00999 template<class RandomAccessIterator, class Compare>
01000 void make_heap(RandomAccessIterator first, RandomAccessIterator last,
01001       Compare comp) {
01002    typedef typename iterator_traits<RandomAccessIterator>::difference_type
01003          index_type;
01004    index_type const n = last - first;
01005    --first;
01006    for (index_type start = n >> 1; start; --start)
01007       __sift_down(first, start, n, comp);
01008 }
01009 
01010 template<class RandomAccessIterator>
01011 void sort_heap(RandomAccessIterator first, RandomAccessIterator last) {
01012    for (typename iterator_traits<RandomAccessIterator>::difference_type n =
01013          last - first--; n > 1; --n)
01014       __pop_heap(first, n);
01015 }
01016 
01017 template<class RandomAccessIterator, class Compare>
01018 void sort_heap(RandomAccessIterator first, RandomAccessIterator last,
01019       Compare comp) {
01020    for (typename iterator_traits<RandomAccessIterator>::difference_type n =
01021          last - first--; n > 1; --n)
01022       __pop_heap(first, n, comp);
01023 }
01024 
01025 template<class RandomAccessIterator>
01026 void heap_sort(RandomAccessIterator first, RandomAccessIterator last) {
01027    make_heap(first, last);
01028    sort_heap(first, last);
01029 }
01030 
01031 template<class RandomAccessIterator, class Compare>
01032 void heap_sort(RandomAccessIterator first, RandomAccessIterator last,
01033       Compare comp) {
01034    make_heap(first, last, comp);
01035    sort_heap(first, last, comp);
01036 }
01037 
01038 template<class InputIterator1, class InputIterator2, class OutputIterator>
01039 OutputIterator merge(InputIterator1 first1, InputIterator1 last1,
01040       InputIterator2 first2, InputIterator2 last2, OutputIterator result) {
01041    for (;;)
01042       if (*first1 < *first2) {
01043          *result++ = *first1++;
01044          if (first1 == last1)
01045             return copy(first1, last1, result);
01046       } else {
01047          *result++ = *first2++;
01048          if (first2 == last2)
01049             return copy(first2, last2, result);
01050       }
01051 }
01052 
01053 template<class InputIterator1, class InputIterator2, class OutputIterator,
01054       class Compare>
01055 OutputIterator merge(InputIterator1 first1, InputIterator1 last1,
01056       InputIterator2 first2, InputIterator2 last2, OutputIterator result,
01057       Compare comp) {
01058    for (;;)
01059       if (comp(*first1, *first2)) {
01060          *result++ = *first1++;
01061          if (first1 == last1)
01062             return copy(first1, last1, result);
01063       } else {
01064          *result++ = *first2++;
01065          if (first2 == last2)
01066             return copy(first2, last2, result);
01067       }
01068 }
01069 
01070 template<class BidirectionalIterator>
01071 void inplace_merge(BidirectionalIterator first, BidirectionalIterator middle,
01072       BidirectionalIterator last) {
01073    heap_sort(first, last);
01074 }
01075 
01076 template<class BidirectionalIterator, class Compare>
01077 void inplace_merge(BidirectionalIterator first, BidirectionalIterator middle,
01078       BidirectionalIterator last, Compare comp) {
01079    heap_sort(first, last, comp);
01080 }
01081 
01082 template<class InputIterator1, class InputIterator2>
01083 bool includes(InputIterator1 first1, InputIterator1 last1,
01084       InputIterator2 first2, InputIterator2 last2) {
01085    while (first1 != last1) {
01086       if (first2 == last2)
01087          return true;
01088       if (*first2 < *first1)
01089          break;
01090       ++first1;
01091       if (!(*first1 < *first2))
01092          ++first2;
01093    }
01094    return false;
01095 }
01096 
01097 template<class InputIterator1, class InputIterator2, class Compare>
01098 bool includes(InputIterator1 first1, InputIterator1 last1,
01099       InputIterator2 first2, InputIterator2 last2, Compare comp) {
01100    while (first1 != last1) {
01101       if (first2 == last2)
01102          return true;
01103       if (comp(*first2, *first1))
01104          break;
01105       ++first1;
01106       if (!comp(*first1, *first2))
01107          ++first2;
01108    }
01109    return false;
01110 }
01111 
01112 template<class InputIterator1, class InputIterator2, class OutputIterator>
01113 OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,
01114       InputIterator2 first2, InputIterator2 last2, OutputIterator result) {
01115    for (;;) {
01116       if (first1 == last1)
01117          return copy(first2, last2, result);
01118       if (first2 == last2)
01119          return copy(first1, last1, result);
01120       if (*first1 < *first2)
01121          *result++ = *first1++;
01122       else {
01123          *result++ = *first2++;
01124          if (!(*first2 < *first1))
01125             ++first1;
01126       }
01127    }
01128 }
01129 
01130 template<class InputIterator1, class InputIterator2, class OutputIterator,
01131       class Compare>
01132 OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,
01133       InputIterator2 first2, InputIterator2 last2, OutputIterator result,
01134       Compare comp) {
01135    for (;;) {
01136       if (first1 == last1)
01137          return copy(first2, last2, result);
01138       if (first2 == last2)
01139          return copy(first1, last1, result);
01140       if (comp(*first1, *first2))
01141          *result++ = *first1++;
01142       else {
01143          *result++ = *first2++;
01144          if (!comp(*first2, *first1))
01145             ++first1;
01146       }
01147    }
01148 }
01149 
01150 template<class InputIterator1, class InputIterator2, class OutputIterator>
01151 OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1,
01152       InputIterator2 first2, InputIterator2 last2, OutputIterator result) {
01153    while (first1 != last1 && first2 != last2) {
01154       if (*first1 < *first2)
01155          ++first1;
01156       else if (*first2 < *first1)
01157          ++first2;
01158       else {
01159          *result++ = *first1++;
01160          ++first2;
01161       }
01162    }
01163    return result;
01164 }
01165 
01166 template<class InputIterator1, class InputIterator2, class OutputIterator,
01167       class Compare>
01168 OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1,
01169       InputIterator2 first2, InputIterator2 last2, OutputIterator result,
01170       Compare comp) {
01171    while (first1 != last1 && first2 != last2) {
01172       if (comp(*first1, *first2))
01173          ++first1;
01174       else if (comp(*first2, *first1))
01175          ++first2;
01176       else {
01177          *result++ = *first1++;
01178          ++first2;
01179       }
01180    }
01181    return result;
01182 }
01183 
01184 template<class InputIterator1, class InputIterator2, class OutputIterator>
01185 OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1,
01186       InputIterator2 first2, InputIterator2 last2, OutputIterator result) {
01187    while (first1 != last1 && first2 != last2) {
01188       if (*first1 < *first2)
01189          *result++ = *first1++;
01190       else {
01191          if (!(*first2 < *first1))
01192             ++first1;
01193          first2++;
01194       }
01195 
01196    }
01197    return copy(first1, last1, result);
01198 }
01199 
01200 template<class InputIterator1, class InputIterator2, class OutputIterator,
01201       class Compare>
01202 OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1,
01203       InputIterator2 first2, InputIterator2 last2, OutputIterator result,
01204       Compare comp) {
01205    while (first1 != last1 && first2 != last2) {
01206       if (comp(*first1, *first2))
01207          *result++ = *first1++;
01208       else {
01209          if (!comp(*first2, *first1))
01210             ++first1;
01211          first2++;
01212       }
01213 
01214    }
01215    return copy(first1, last1, result);
01216 }
01217 
01218 template<class InputIterator1, class InputIterator2, class OutputIterator>
01219 OutputIterator set_symmetric_difference(InputIterator1 first1,
01220       InputIterator1 last1, InputIterator2 first2, InputIterator2 last2,
01221       OutputIterator result) {
01222    for (;;) {
01223       if (first1 == last1)
01224          return copy(first2, last2, result);
01225       if (first2 == last2)
01226          return copy(first1, last1, result);
01227       if (*first1 < *first2)
01228          *result++ = *first1++;
01229       else if (*first2 < *first1)
01230          *result++ = *first2++;
01231       else {
01232          ++first1;
01233          ++first2;
01234       }
01235    }
01236 }
01237 
01238 template<class InputIterator1, class InputIterator2, class OutputIterator,
01239       class Compare>
01240 OutputIterator set_symmetric_difference(InputIterator1 first1,
01241       InputIterator1 last1, InputIterator2 first2, InputIterator2 last2,
01242       OutputIterator result, Compare comp) {
01243    for (;;) {
01244       if (first1 == last1)
01245          return copy(first2, last2, result);
01246       if (first2 == last2)
01247          return copy(first1, last1, result);
01248       if (comp(*first1, *first2))
01249          *result++ = *first1++;
01250       else if (comp(*first2, *first1))
01251          *result++ = *first2++;
01252       else {
01253          ++first1;
01254          ++first2;
01255       }
01256    }
01257 }
01258 
01259 template<class RandomAccessIterator>
01260 void __heap_select(RandomAccessIterator first, RandomAccessIterator middle,
01261       RandomAccessIterator last) {
01262    typedef typename iterator_traits<RandomAccessIterator>::difference_type
01263          index_type;
01264    make_heap(first, middle);
01265    index_type const k = middle - first--;
01266    for (index_type i = k; ++i < last - first;)
01267       if (*i < *first) {
01268          iter_swap(first, i);
01269          __sift_down(first, 1, k);
01270       }
01271 }
01272 
01273 template<class RandomAccessIterator, class Compare>
01274 void __heap_select(RandomAccessIterator first, RandomAccessIterator middle,
01275       RandomAccessIterator last, Compare comp) {
01276    typedef typename iterator_traits<RandomAccessIterator>::difference_type
01277          index_type;
01278    make_heap(first, middle, comp);
01279    index_type const k = middle - first--;
01280    for (index_type i = k; ++i < last - first;)
01281       if (comp(*i, *first)) {
01282          iter_swap(first, i);
01283          __sift_down(first, 1, k, comp);
01284       }
01285 }
01286 
01287 template<class RandomAccessIterator>
01288 void heap_select(RandomAccessIterator first, RandomAccessIterator nth,
01289       RandomAccessIterator last) {
01290    __heap_select(first, nth, last);
01291    iter_swap(first, nth);
01292 }
01293 
01294 template<class RandomAccessIterator, class Compare>
01295 void heap_select(RandomAccessIterator first, RandomAccessIterator nth,
01296       RandomAccessIterator last, Compare comp) {
01297    __heap_select(first, nth, last, comp);
01298    iter_swap(first, nth);
01299 }
01300 
01301 template<class RandomAccessIterator>
01302 void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
01303       RandomAccessIterator last) {
01304    quickselect(first, nth, last);
01305 }
01306 
01307 template<class RandomAccessIterator, class Compare>
01308 void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
01309       RandomAccessIterator last, Compare comp) {
01310    quickselect(first, nth, last, comp);
01311 }
01312 
01313 template<class BidirectionalIterator, class T>
01314 void linear_insert(BidirectionalIterator first, BidirectionalIterator last,
01315       T val) {
01316    for (BidirectionalIterator next = last; last != first && val < *--next; last
01317          = next)
01318       *last = *next;
01319    *last = val;
01320 }
01321 
01322 template<class BidirectionalIterator, class T, class Compare>
01323 void linear_insert(BidirectionalIterator first, BidirectionalIterator last,
01324       T val, Compare comp) {
01325    for (BidirectionalIterator next = last; last != first && comp(val, *--next); last
01326          = next)
01327       *last = *next;
01328    *last = val;
01329 }
01330 
01331 template<class BidirectionalIterator>
01332 void insertion_sort(BidirectionalIterator first, BidirectionalIterator last) {
01333    for (BidirectionalIterator i = first; i != last; ++i)
01334       linear_insert(first, i, *i);
01335 }
01336 
01337 template<class BidirectionalIterator, class Compare>
01338 void insertion_sort(BidirectionalIterator first, BidirectionalIterator last,
01339       Compare comp) {
01340    for (BidirectionalIterator i = first; i != last; ++i)
01341       linear_insert(first, i, *i, comp);
01342 }
01343 
01344 template<class RandomAccessIterator>
01345 void selection_partial_sort(RandomAccessIterator first,
01346       RandomAccessIterator middle, RandomAccessIterator last) {
01347    for (; first != middle; ++first)
01348       iter_swap(first, min_element(first, last));
01349 }
01350 
01351 template<class RandomAccessIterator, class Compare>
01352 void selection_partial_sort(RandomAccessIterator first,
01353       RandomAccessIterator middle, RandomAccessIterator last, Compare comp) {
01354    for (; first != middle; ++first)
01355       iter_swap(first, min_element(first, last, comp));
01356 }
01357 
01358 template<class RandomAccessIterator>
01359 void stable_sort(RandomAccessIterator first, RandomAccessIterator last) {
01360    insertion_sort(first, last);
01361 }
01362 
01363 template<class RandomAccessIterator, class Compare>
01364 void stable_sort(RandomAccessIterator first, RandomAccessIterator last,
01365       Compare comp) {
01366    insertion_sort(first, last, comp);
01367 }
01368 
01369 template<class RandomAccessIterator>
01370 void sort(RandomAccessIterator first, RandomAccessIterator last) {
01371    heap_sort(first, last);
01372 }
01373 
01374 template<class RandomAccessIterator, class Compare>
01375 void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp) {
01376    heap_sort(first, last, comp);
01377 }
01378 
01379 template<class RandomAccessIterator>
01380 void partial_sort(RandomAccessIterator first, RandomAccessIterator middle,
01381       RandomAccessIterator last) {
01382    __heap_select(first, middle, last);
01383    sort(++first, middle);
01384 }
01385 
01386 template<class RandomAccessIterator, class Compare>
01387 void partial_sort(RandomAccessIterator first, RandomAccessIterator middle,
01388       RandomAccessIterator last, Compare comp) {
01389    __heap_select(first, middle, last, comp);
01390    sort(++first, middle, comp);
01391 }
01392 
01393 template<class InputIterator, class RandomAccessIterator>
01394 RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last,
01395       RandomAccessIterator result_first, RandomAccessIterator result_last) {
01396    if (result_first == result_last)
01397       return result_last;
01398    RandomAccessIterator result_real_last = result_first;
01399    while (first != last && result_real_last != result_last)
01400       *result_real_last++ = *first++;
01401    make_heap(result_first, result_real_last);
01402    for (; first != last; ++first)
01403       if (*first < *result_first) {
01404          *result_first = *first;
01405          __sift_down(result_first, 1, result_real_last - result_first);
01406       }
01407    sort_heap(result_first, result_real_last);
01408    return result_real_last;
01409 }
01410 
01411 template<class InputIterator, class RandomAccessIterator, class Compare>
01412 RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last,
01413       RandomAccessIterator result_first, RandomAccessIterator result_last,
01414       Compare comp) {
01415    if (result_first == result_last)
01416       return result_last;
01417    RandomAccessIterator result_real_last = result_first;
01418    while (first != last && result_real_last != result_last)
01419       *result_real_last++ = *first++;
01420    make_heap(result_first, result_real_last, comp);
01421    for (; first != last; ++first)
01422       if (comp(*first, *result_first)) {
01423          *result_first = *first;
01424          __sift_down(result_first, 1, result_real_last - result_first, comp);
01425       }
01426    sort_heap(result_first, result_real_last, comp);
01427    return result_real_last;
01428 }
01429 
01430 }
01431 
01432 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines