home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 May / PCP163A.iso / Runimage / Cbuilder4 / Include / ALGORITH.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-26  |  79.7 KB  |  2,527 lines

  1. #ifndef __ALGORITH_CC
  2. #define __ALGORITH_CC
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. /***************************************************************************
  5.  *
  6.  * algorithm.cc - Non-inline definitions for the Standard Library algorithms
  7.  *
  8.  ***************************************************************************
  9.  *
  10.  * Copyright (c) 1994
  11.  * Hewlett-Packard Company
  12.  *
  13.  * Permission to use, copy, modify, distribute and sell this software
  14.  * and its documentation for any purpose is hereby granted without fee,
  15.  * provided that the above copyright notice appear in all copies and
  16.  * that both that copyright notice and this permission notice appear
  17.  * in supporting documentation.  Hewlett-Packard Company makes no
  18.  * representations about the suitability of this software for any
  19.  * purpose.  It is provided "as is" without express or implied warranty.
  20.  *
  21.  *
  22.  ***************************************************************************
  23.  *
  24.  * (c) Copyright 1994, 1998 Rogue Wave Software, Inc.
  25.  * ALL RIGHTS RESERVED
  26.  *
  27.  * The software and information contained herein are proprietary to, and
  28.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  29.  * intends to preserve as trade secrets such software and information.
  30.  * This software is furnished pursuant to a written license agreement and
  31.  * may be used, copied, transmitted, and stored only in accordance with
  32.  * the terms of such license and with the inclusion of the above copyright
  33.  * notice.  This software and information or any other copies thereof may
  34.  * not be provided or otherwise made available to any other person.
  35.  *
  36.  * Notwithstanding any other lease or license that may pertain to, or
  37.  * accompany the delivery of, this computer software and information, the
  38.  * rights of the Government regarding its use, reproduction and disclosure
  39.  * are as set forth in Section 52.227-19 of the FARS Computer
  40.  * Software-Restricted Rights clause.
  41.  * 
  42.  * Use, duplication, or disclosure by the Government is subject to
  43.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  44.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  45.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  46.  * P.O. Box 2328, Corvallis, Oregon 97339.
  47.  *
  48.  * This computer software and information is distributed with "restricted
  49.  * rights."  Use, duplication or disclosure is subject to restrictions as
  50.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  51.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  52.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  53.  * then the "Alternate III" clause applies.
  54.  *
  55.  **************************************************************************/
  56.  
  57. #include <stdcomp.h>
  58.  
  59. #ifndef _RWSTD_NO_NAMESPACE
  60. namespace std {
  61. #endif
  62.  
  63. //
  64. // Forward declare raw_storage_iterator 
  65. //   
  66.   template <class OutputIterator, class T>
  67.   class raw_storage_iterator;
  68.  
  69. //
  70. // Non-modifying sequence operations.
  71. //
  72.  
  73.   template <class InputIterator, class Function>
  74.   Function for_each (InputIterator first, InputIterator last, Function f)
  75.   {
  76.     while (first != last) f(*first++);
  77.     return f;
  78.   }
  79.  
  80.   template <class InputIterator, class T>
  81.   InputIterator find (InputIterator first, InputIterator last, const T& value)
  82.   {
  83.     while (first != last && *first != value) 
  84.       ++first;
  85.     return first;
  86.   }
  87.  
  88.   template <class InputIterator, class Predicate>
  89.   InputIterator find_if (InputIterator first, InputIterator last, Predicate pred)
  90.   {
  91.     while (first != last && !pred(*first)) ++first;
  92.     return first;
  93.   }
  94.   template <class ForwardIterator1, class ForwardIterator2, 
  95.   class Distance>
  96.   ForwardIterator1 __find_end (ForwardIterator1 first1,
  97.                                ForwardIterator1 last1,
  98.                                ForwardIterator2 first2,
  99.                                ForwardIterator2 last2,
  100.                                Distance*)
  101.   {
  102.     Distance d, d2;
  103.     __initialize(d,Distance(0));
  104.     __initialize(d2,Distance(0));
  105.     distance(first2,last2,d);
  106.     if (!d)
  107.       return first1;
  108.     distance(first1,last1,d2);
  109.     ForwardIterator1 save = last1;
  110.  
  111.     while (d2 >= d)
  112.     {
  113.       if (equal(first2,last2,first1))
  114.         save  = first1;
  115.       __initialize(d2,Distance(0));
  116.       distance(++first1,last1,d2);
  117.     }
  118.     return save;
  119.   }
  120.   template <class ForwardIterator1, class ForwardIterator2>
  121.   ForwardIterator1 find_end (ForwardIterator1 first1,
  122.                              ForwardIterator1 last1,
  123.                              ForwardIterator2 first2,
  124.                              ForwardIterator2 last2)
  125.   {
  126.     return __find_end(first1,last1,first2,last2,
  127.                       __distance_type(first1));
  128.   }
  129.  
  130.   template <class ForwardIterator1, class ForwardIterator2, 
  131.   class BinaryPredicate, class Distance>
  132.   ForwardIterator1 __find_end (ForwardIterator1 first1,
  133.                                ForwardIterator1 last1,
  134.                                ForwardIterator2 first2,
  135.                                ForwardIterator2 last2,
  136.                                BinaryPredicate pred,
  137.                                Distance*)
  138.   {
  139.     Distance d, d2;
  140.     __initialize(d,Distance(0));
  141.     __initialize(d2,Distance(0));
  142.     distance(first2,last2,d);
  143.     if (!d)
  144.       return first1;
  145.     distance(first1,last1,d2);
  146.     ForwardIterator1 save = last1;
  147.  
  148.     while (d2 >= d)
  149.     {
  150.       if (equal(first2,last2,first1,pred))
  151.         save  = first1;
  152.       __initialize(d2,Distance(0));
  153.       distance(++first1,last1,d2);
  154.     }
  155.     return save;
  156.   }
  157.  
  158.   template <class ForwardIterator1, class ForwardIterator2, 
  159.   class BinaryPredicate>
  160.   ForwardIterator1 find_end (ForwardIterator1 first1,
  161.                              ForwardIterator1 last1,
  162.                              ForwardIterator2 first2,
  163.                              ForwardIterator2 last2,
  164.                              BinaryPredicate pred)
  165.   {
  166.     return __find_end(first1,last1,first2,last2,
  167.                       pred,__distance_type(first1));
  168.   }
  169.   template <class ForwardIterator1, class ForwardIterator2>
  170.   ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
  171.                                   ForwardIterator2 first2, ForwardIterator2 last2)
  172.   {
  173.     if (first2 == last2)
  174.       return first1;
  175.     ForwardIterator1 next = first1;
  176.     while (next != last1)
  177.     {
  178.       if (find(first2,last2,*next) != last2)
  179.         return next;
  180.       next++;
  181.     }
  182.     return last1;
  183.   }
  184.  
  185.   template <class ForwardIterator1, class ForwardIterator2,
  186.   class BinaryPredicate>
  187.   ForwardIterator1 find_first_of (ForwardIterator1 first1,ForwardIterator1 last1,
  188.                                   ForwardIterator2 first2,ForwardIterator2 last2,
  189.                                   BinaryPredicate pred)
  190.   {
  191.     if (first2 == last2)
  192.       return first1;
  193.     ForwardIterator1 next = first1;
  194.     while (next != last1)
  195.     {
  196.       if (find_if(first2,last2,bind2nd(pred,*next)) != last2)
  197.         return next;
  198.       next++;
  199.     }
  200.     return last1;
  201.   }
  202.   template <class ForwardIterator>
  203.   ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last)
  204.   {
  205.     if (first == last) return last;
  206.     ForwardIterator next = first;
  207.     while (++next != last)
  208.     {
  209.       if (*first == *next) return first;
  210.       first = next;
  211.     }
  212.     return last;
  213.   }
  214.  
  215.   template <class ForwardIterator, class BinaryPredicate>
  216.   ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last,
  217.                                  BinaryPredicate binary_pred)
  218.   {
  219.     if (first == last) return last;
  220.     ForwardIterator next = first;
  221.     while (++next != last)
  222.     {
  223.       if (binary_pred(*first, *next)) return first;
  224.       first = next;
  225.     }
  226.     return last;
  227.   }
  228.  
  229. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
  230.   template <class InputIterator, class T>
  231.   _TYPENAME iterator_traits<InputIterator>::difference_type
  232.   count (InputIterator first, InputIterator last, const T& value)
  233.   {
  234.     typename iterator_traits<InputIterator>::difference_type n = 0; //RW_BUG: fix for bts-42842
  235.     while (first != last)
  236.       if (*first++ == value) ++n;
  237.     return n;
  238.   }
  239.  
  240.   template <class InputIterator, class Predicate>
  241.   _TYPENAME iterator_traits<InputIterator>::difference_type
  242.   count_if (InputIterator first, InputIterator last, Predicate pred)
  243.   {
  244.     typename iterator_traits<InputIterator>::difference_type n = 0; //RW_BUG: fix for bts-42842
  245.     while (first != last)
  246.       if (pred(*first++)) ++n;
  247.     return n;
  248.   }
  249. #endif /* _RWSTD_NO_CLASS_PARTIAL_SPEC */
  250.  
  251. #ifndef _RWSTD_NO_OLD_COUNT
  252.   template <class InputIterator, class T, class Size>
  253.   void count (InputIterator first, InputIterator last, const T& value, Size& n)
  254.   {
  255.     while (first != last)
  256.       if (*first++ == value) ++n;
  257.   }
  258.  
  259.   template <class InputIterator, class Predicate, class Size>
  260.   void count_if (InputIterator first, InputIterator last, Predicate pred,
  261.                  Size& n)
  262.   {
  263.     while (first != last)
  264.       if (pred(*first++)) ++n;
  265.   }
  266. #endif /* _RWSTD_NO_OLD_COUNT */
  267.  
  268.   template <class InputIterator1, class InputIterator2>
  269.   pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1,
  270.                                                 InputIterator1 last1,
  271.                                                 InputIterator2 first2)
  272.   {
  273.     while (first1 != last1 && *first1 == *first2)
  274.     {
  275.       ++first1;
  276.       ++first2;
  277.     }
  278.     pair<InputIterator1, InputIterator2> tmp(first1, first2);
  279.     return tmp;
  280.   }
  281.  
  282.   template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  283.   pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1,
  284.                                                  InputIterator1 last1,
  285.                                                  InputIterator2 first2,
  286.                                                  BinaryPredicate binary_pred)
  287.   {
  288.     while (first1 != last1 && binary_pred(*first1, *first2))
  289.     {
  290.       ++first1;
  291.       ++first2;
  292.     }
  293.     pair<InputIterator1, InputIterator2> tmp(first1, first2);
  294.     return tmp;
  295.   }
  296.  
  297.   template <class ForwardIterator1, class ForwardIterator2,
  298.   class Distance1, class Distance2>
  299.   ForwardIterator1 __search (ForwardIterator1 first1, ForwardIterator1 last1,
  300.                              ForwardIterator2 first2, ForwardIterator2 last2,
  301.                              Distance1*, Distance2*)
  302.   {
  303.     Distance1 d1;
  304.     __initialize(d1, Distance1(0));
  305.     distance(first1, last1, d1);
  306.     Distance2 d2;
  307.     __initialize(d2, Distance2(0));
  308.     distance(first2, last2, d2);
  309.  
  310.     if (d1 < d2) return last1;
  311.  
  312.     ForwardIterator1 current1 = first1;
  313.     ForwardIterator2 current2 = first2;
  314.  
  315.     while (current2 != last2)
  316.     {
  317.       if (*current1++ != *current2++)
  318.         if (d1-- == d2)
  319.           return last1;
  320.         else
  321.         {
  322.           current1 = ++first1;
  323.           current2 = first2;
  324.         }
  325.     }
  326.     return (current2 == last2) ? first1 : last1;
  327.   }
  328.  
  329.   template <class ForwardIterator1, class ForwardIterator2,
  330.   class BinaryPredicate, class Distance1, class Distance2>
  331.   ForwardIterator1 __search (ForwardIterator1 first1, ForwardIterator1 last1,
  332.                              ForwardIterator2 first2, ForwardIterator2 last2,
  333.                              BinaryPredicate binary_pred, Distance1*, Distance2*)
  334.   {
  335.     Distance1 d1;
  336.     __initialize(d1, Distance1(0));
  337.     distance(first1, last1, d1);
  338.     Distance2 d2;
  339.     __initialize(d2, Distance2(0));
  340.     distance(first2, last2, d2);
  341.  
  342.     if (d1 < d2) return last1;
  343.  
  344.     ForwardIterator1 current1 = first1;
  345.     ForwardIterator2 current2 = first2;
  346.  
  347.     while (current2 != last2)
  348.     {
  349.       if (!binary_pred(*current1++, *current2++))
  350.         if (d1-- == d2)
  351.           return last1;
  352.         else
  353.         {
  354.           current1 = ++first1;
  355.           current2 = first2;
  356.         }
  357.     }
  358.     return (current2 == last2) ? first1 : last1;
  359.   }
  360.  
  361.   template <class ForwardIterator, class Distance, class Size, class T>
  362.   ForwardIterator __search_n (ForwardIterator first, ForwardIterator last,
  363.                               Distance*, Size count, const T& value)
  364.   {
  365.     Distance d;
  366.     __initialize(d, Distance(0));
  367.     distance(first, last, d);
  368.  
  369.     if (d < count || count <= 0) return last;
  370.  
  371.     Distance        span    = d - count;
  372.     Size            matches = 0;
  373.     ForwardIterator current = first;
  374.  
  375.     while (current != last)
  376.     {
  377.       if (*current++ != value)
  378.       {
  379.         if (span < matches + 1)
  380.           return last;
  381.         span   -= matches + 1;
  382.         matches = 0;
  383.         first   = current;
  384.       }
  385.       else
  386.         if (++matches == count)
  387.           return first;
  388.     }
  389.  
  390.     return last;
  391.   }
  392.  
  393.   template <class ForwardIterator, class Distance, class Size, class T,
  394.   class BinaryPredicate>
  395.   ForwardIterator __search_n (ForwardIterator first, ForwardIterator last,
  396.                               Distance*, Size count, const T& value,
  397.                               BinaryPredicate pred)
  398.   {
  399.     Distance d;
  400.     __initialize(d, Distance(0));
  401.     distance(first, last, d);
  402.  
  403.     if (d < count || count <= 0) return last;
  404.  
  405.     Distance        span    = d - count;
  406.     Size            matches = 0;
  407.     ForwardIterator current = first;
  408.  
  409.     while (current != last)
  410.     {
  411.       if (!pred(*current++, value))
  412.       {
  413.         if (span < matches + 1)
  414.           return last;
  415.         span   -= matches + 1;
  416.         matches = 0;
  417.         first   = current;
  418.       }
  419.       else
  420.         if (++matches == count)
  421.           return first;
  422.     }
  423.  
  424.     return last;
  425.   }
  426.  
  427. //
  428. // Modifying sequence operations.
  429. //
  430.  
  431.   template <class InputIterator, class OutputIterator>
  432.   OutputIterator copy (InputIterator first, InputIterator last,
  433.                        OutputIterator result)
  434.   {
  435.     while (first != last) *result++ = *first++;
  436.     return result;
  437.   }
  438.  
  439.   template <class BidirectionalIterator1, class BidirectionalIterator2>
  440.   BidirectionalIterator2 copy_backward (BidirectionalIterator1 first, 
  441.                                         BidirectionalIterator1 last, 
  442.                                         BidirectionalIterator2 result)
  443.   {
  444.     while (first != last) *--result = *--last;
  445.     return result;
  446.   }
  447.  
  448.   template <class ForwardIterator1, class ForwardIterator2>
  449.   ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,
  450.                                 ForwardIterator2 first2)
  451.   {
  452.     while (first1 != last1) iter_swap(first1++, first2++);
  453.     return first2;
  454.   }
  455.  
  456.   template <class InputIterator, class OutputIterator, class UnaryOperation>
  457.   OutputIterator transform (InputIterator first, InputIterator last,
  458.                             OutputIterator result, UnaryOperation op)
  459.   {
  460.     while (first != last) *result++ = op(*first++);
  461.     return result;
  462.   }
  463.  
  464.   template <class InputIterator1, class InputIterator2, class OutputIterator,
  465.   class BinaryOperation>
  466.   OutputIterator transform (InputIterator1 first1, InputIterator1 last1,
  467.                             InputIterator2 first2, OutputIterator result,
  468.                             BinaryOperation binary_op)
  469.   {
  470.     while (first1 != last1) *result++ = binary_op(*first1++, *first2++);
  471.     return result;
  472.   }
  473.  
  474.   template <class ForwardIterator, class T>
  475.   void replace (ForwardIterator first, ForwardIterator last, const T& old_value,
  476.                 const T& new_value)
  477.   {
  478.     while (first != last)
  479.     {
  480.       if (*first == old_value) *first = new_value;
  481.       ++first;
  482.     }
  483.   }
  484.  
  485.   template <class ForwardIterator, class Predicate, class T>
  486.   void replace_if (ForwardIterator first, ForwardIterator last, Predicate pred,
  487.                    const T& new_value)
  488.   {
  489.     while (first != last)
  490.     {
  491.       if (pred(*first)) *first = new_value;
  492.       ++first;
  493.     }
  494.   }
  495.  
  496.   template <class InputIterator, class OutputIterator, class T>
  497.   OutputIterator replace_copy (InputIterator first, InputIterator last,
  498.                                OutputIterator result, const T& old_value,
  499.                                const T& new_value)
  500.   {
  501.     while (first != last)
  502.     {
  503.       *result++ = *first == old_value ? new_value : *first;
  504.       ++first;
  505.     }
  506.     return result;
  507.   }
  508.  
  509.   template <class Iterator, class OutputIterator, class Predicate, class T>
  510.   OutputIterator replace_copy_if (Iterator first, Iterator last,
  511.                                   OutputIterator result, Predicate pred,
  512.                                   const T& new_value)
  513.   {
  514.     while (first != last)
  515.     {
  516.       if(pred(*first))
  517.         *result++ = new_value;
  518.       else
  519.         *result++ = *first;
  520.       ++first;
  521.     }
  522.     return result;
  523.   }
  524.  
  525.   template <class ForwardIterator, class T>
  526. #ifdef _RWSTD_FILL_NAME_CLASH
  527.   void std_fill (ForwardIterator first, ForwardIterator last, const T& value)
  528. #else
  529.   void fill (ForwardIterator first, ForwardIterator last, const T& value)
  530. #endif
  531.   {
  532.     while (first != last) *first++ = value;
  533.   }
  534.  
  535.   template <class OutputIterator, class Size, class T>
  536.   void fill_n (OutputIterator first, Size n, const T& value)
  537.   {
  538.     while (n-- > 0) *first++ = value;
  539.   }
  540.  
  541.   template <class ForwardIterator, class Generator>
  542.   void generate (ForwardIterator first, ForwardIterator last, Generator gen)
  543.   {
  544.     while (first != last) *first++ = gen();
  545.   }
  546.  
  547.   template <class OutputIterator, class Size, class Generator>
  548.   void generate_n (OutputIterator first, Size n, Generator gen)
  549.   {
  550.     while (n-- > 0) *first++ = gen();
  551.   }
  552.  
  553.   template <class InputIterator, class OutputIterator, class T>
  554.   OutputIterator remove_copy (InputIterator first, InputIterator last,
  555.                               OutputIterator result, const T& value)
  556.   {
  557.     while (first != last)
  558.     {
  559.       if (*first != value) *result++ = *first;
  560.       ++first;
  561.     }
  562.     return result;
  563.   }
  564.  
  565.   template <class InputIterator, class OutputIterator, class Predicate>
  566.   OutputIterator remove_copy_if (InputIterator first, InputIterator last,
  567.                                  OutputIterator result, Predicate pred)
  568.   {
  569.     while (first != last)
  570.     {
  571.       if (!pred(*first)) *result++ = *first;
  572.       ++first;
  573.     }
  574.     return result;
  575.   }
  576.  
  577.   template <class InputIterator, class ForwardIterator>
  578.   ForwardIterator __unique_copy (InputIterator first, InputIterator last,
  579.                                  ForwardIterator result, forward_iterator_tag)
  580.   {
  581.     *result = *first;
  582.     while (++first != last)
  583.       if (*result != *first) *++result = *first;
  584.     return ++result;
  585.   }
  586.  
  587.   template <class InputIterator, class OutputIterator, class T>
  588.   OutputIterator __unique_copy (InputIterator first, InputIterator last,
  589.                                 OutputIterator result, T*)
  590.   {
  591.     T value = *first;
  592.     *result = value;
  593.     while (++first != last)
  594.     {
  595.       if (value != *first)
  596.       {
  597.         value = *first;
  598.         *++result = value;
  599.       }
  600.     }
  601.     return ++result;
  602.   }
  603.  
  604.   template <class InputIterator, class ForwardIterator, class BinaryPredicate>
  605.   ForwardIterator __unique_copy (InputIterator first, InputIterator last,
  606.                                  ForwardIterator result, 
  607.                                  BinaryPredicate binary_pred,
  608.                                  forward_iterator_tag)
  609.   {
  610.     *result = *first;
  611.     while (++first != last)
  612.       if (!binary_pred(*result, *first)) *++result = *first;
  613.     return ++result;
  614.   }
  615.  
  616.   template <class InputIterator, class OutputIterator, class BinaryPredicate,
  617.   class T>
  618.   OutputIterator __unique_copy (InputIterator first, InputIterator last,
  619.                                 OutputIterator result,
  620.                                 BinaryPredicate binary_pred, T*)
  621.   {
  622.     T value = *first;
  623.     *result = value;
  624.     while (++first != last)
  625.     {
  626.       if (!binary_pred(value, *first))
  627.       {
  628.         value = *first;
  629.         *++result = value;
  630.       }
  631.     }
  632.     return ++result;
  633.   }
  634.  
  635.   template <class BidirectionalIterator>
  636.   void __reverse (BidirectionalIterator first, BidirectionalIterator last, 
  637.                   bidirectional_iterator_tag)
  638.   {
  639.     while (true)
  640.       if (first == last || first == --last)
  641.         return;
  642.       else
  643.         iter_swap(first++, last);
  644.   }
  645.  
  646.   template <class RandomAccessIterator>
  647.   void __reverse (RandomAccessIterator first, RandomAccessIterator last,
  648.                   random_access_iterator_tag)
  649.   {
  650.     while (first < last) iter_swap(first++, --last);
  651.   }
  652.  
  653.   template <class BidirectionalIterator, class OutputIterator>
  654.   OutputIterator reverse_copy (BidirectionalIterator first,
  655.                                BidirectionalIterator last,
  656.                                OutputIterator result)
  657.   {
  658.     while (first != last) *result++ = *--last;
  659.     return result;
  660.   }
  661.  
  662.   template <class ForwardIterator, class Distance>
  663.   void __rotate (ForwardIterator first, ForwardIterator middle,
  664.                  ForwardIterator last, Distance*, forward_iterator_tag)
  665.   {
  666.     for (ForwardIterator i = middle; ;)
  667.     {
  668.       iter_swap(first++, i++);
  669.       if (first == middle)
  670.       {
  671.         if (i == last) return;
  672.         middle = i;
  673.       }
  674.       else if (i == last)
  675.         i = middle;
  676.     }
  677.   }
  678.  
  679.   template <class EuclideanRingElement>
  680.   EuclideanRingElement __gcd (EuclideanRingElement m, EuclideanRingElement n)
  681.   {
  682.     while (n != 0)
  683.     {
  684.       EuclideanRingElement t = m % n;
  685.       m = n;
  686.       n = t;
  687.     }
  688.     return m;
  689.   }
  690.  
  691.   template <class RandomAccessIterator, class Distance, class T>
  692.   void __rotate_cycle (RandomAccessIterator first, RandomAccessIterator last,
  693.                        RandomAccessIterator initial, Distance shift, T*)
  694.   {
  695.     T value = *initial;
  696.     RandomAccessIterator ptr1 = initial;
  697.     RandomAccessIterator ptr2 = ptr1 + shift;
  698.     while (ptr2 != initial)
  699.     {
  700.       *ptr1 = *ptr2;
  701.       ptr1 = ptr2;
  702.       if (last - ptr2 > shift)
  703.         ptr2 += shift;
  704.       else
  705.         ptr2 = first + (shift - (last - ptr2));
  706.     }
  707.     *ptr1 = value;
  708.   }
  709.  
  710.   template <class RandomAccessIterator, class Distance>
  711.   void __rotate (RandomAccessIterator first, RandomAccessIterator middle,
  712.                  RandomAccessIterator last, Distance*,
  713.                  random_access_iterator_tag)
  714.   {
  715.     Distance n = __gcd(last - first, middle - first);
  716.     while (n--)
  717.       __rotate_cycle(first, last, first + n, middle - first,
  718.                      _RWSTD_VALUE_TYPE(first));
  719.   }
  720. #ifndef _RWSTD_NO_NAMESPACE
  721. }
  722. namespace __rwstd {
  723. #endif
  724.   extern unsigned _RWSTDExport long __long_random (unsigned long);
  725. #ifndef _RWSTD_NO_NAMESPACE
  726. }
  727. namespace std {
  728. #endif
  729.  
  730.   template <class RandomAccessIterator, class Distance>
  731.   void __random_shuffle (RandomAccessIterator first, RandomAccessIterator last,
  732.                          Distance*)
  733.   {
  734.     if (!(first == last))
  735.       for (RandomAccessIterator i = first + 1; i != last; ++i)
  736.         iter_swap(i, first + Distance(__RWSTD::__long_random((i - first) + 1)));
  737.   }
  738.  
  739.   template <class RandomAccessIterator, class RandomNumberGenerator>
  740.   void random_shuffle (RandomAccessIterator first, RandomAccessIterator last,
  741.                        RandomNumberGenerator& rand)
  742.   {
  743.     if (!(first == last))
  744.       for (RandomAccessIterator i = first + 1; i != last; ++i)
  745.         iter_swap(i, first + rand((i - first) + 1));
  746.   }
  747.  
  748.   template <class BidirectionalIterator, class Predicate>
  749.   BidirectionalIterator partition (BidirectionalIterator first,
  750.                                    BidirectionalIterator last, Predicate pred)
  751.   {
  752.     while (true)
  753.     {
  754.       while (true)
  755.       {
  756.         if (first == last)
  757.           return first;
  758.         else if (pred(*first))
  759.           ++first;
  760.         else
  761.           break;
  762.       }
  763.       --last;
  764.       while (true)
  765.       {
  766.         if (first == last)
  767.           return first;
  768.         else if (!pred(*last))
  769.           --last;
  770.         else
  771.           break;
  772.       }
  773.       iter_swap(first, last);
  774.       ++first;
  775.     }
  776.   }
  777.  
  778.   template <class BidirectionalIterator, class Predicate, class Distance>
  779.   BidirectionalIterator __inplace_stable_partition (BidirectionalIterator first,
  780.                                                     BidirectionalIterator last,
  781.                                                     Predicate pred,
  782.                                                     Distance len)
  783.   {
  784.     if (len == 1) return pred(*first) ? last : first;
  785.     BidirectionalIterator middle = first;
  786.     advance(middle, len / 2);
  787.     BidirectionalIterator 
  788.       first_cut = __inplace_stable_partition(first, middle, pred, len / 2);
  789.     BidirectionalIterator 
  790.       second_cut = __inplace_stable_partition(middle, last, pred, len - len / 2);
  791.     rotate(first_cut, middle, second_cut);
  792.     __initialize(len, Distance(0));
  793.     distance(middle, second_cut, len);
  794.     advance(first_cut, len);
  795.     return first_cut;
  796.   }
  797.  
  798.   template <class BidirectionalIterator, class Pointer, class Predicate,
  799.   class Distance, class T>
  800.   BidirectionalIterator __stable_partition_adaptive (BidirectionalIterator first,
  801.                                                      BidirectionalIterator last,
  802.                                                      Predicate pred, Distance len,
  803.                                                      Pointer buffer,
  804.                                                      Distance buffer_size,
  805.                                                      Distance& fill_pointer, T*)
  806.   {
  807.     if (len <= buffer_size)
  808.     {
  809.       len = 0;
  810.       BidirectionalIterator result1 = first;
  811.       Pointer result2 = buffer;
  812.       while (first != last && len < fill_pointer)
  813.       {
  814.         if (pred(*first))
  815.           *result1++ = *first++;
  816.         else
  817.         {
  818.           *result2++ = *first++;
  819.           ++len;
  820.         }
  821.       }
  822.       if (first != last)
  823.       {
  824.         raw_storage_iterator<Pointer, T> result3(result2);
  825.         while (first != last)
  826.         {
  827.           if (pred(*first))
  828.             *result1++ = *first++;
  829.           else
  830.           {
  831.             *result3++ = *first++;
  832.             ++len;
  833.           }
  834.         }
  835.         fill_pointer = len;
  836.       }
  837.       copy(buffer, buffer + len, result1);
  838.       return result1;
  839.     }
  840.     BidirectionalIterator middle = first;
  841.     advance(middle, len / 2);
  842.     BidirectionalIterator first_cut = __stable_partition_adaptive
  843.       (first, middle, pred, len / 2, buffer, buffer_size, fill_pointer, (T*)0);
  844.     BidirectionalIterator second_cut = __stable_partition_adaptive
  845.       (middle, last, pred, len-len/2, buffer, buffer_size, fill_pointer, (T*)0);
  846.     rotate(first_cut, middle, second_cut);
  847.     __initialize(len, Distance(0));
  848.     distance(middle, second_cut, len);
  849.     advance(first_cut, len);
  850.     return first_cut;
  851.   }
  852.  
  853.   template <class BidirectionalIterator, class Predicate, class Pointer,
  854.             class Distance>
  855.   BidirectionalIterator __stable_partition (BidirectionalIterator first,
  856.                                             BidirectionalIterator last,
  857.                                             Predicate pred, Distance len,
  858.                                             pair<Pointer, Distance> p)
  859.   {
  860.     if (p.first == 0)
  861.       return __inplace_stable_partition(first, last, pred, len);
  862.     Distance fill_pointer = 0;
  863.     BidirectionalIterator result = 
  864.       __stable_partition_adaptive(first, last, pred, len, p.first,
  865.                                   p.second, fill_pointer,
  866.                                   _RWSTD_VALUE_TYPE(first)); 
  867.     __RWSTD::__destroy(p.first, p.first + fill_pointer);
  868.     return_temporary_buffer(p.first);
  869.     return result;
  870.   }
  871.  
  872. //
  873. // Sorting and related operations.
  874. //
  875.  
  876.   template <class RandomAccessIterator, class T>
  877.   RandomAccessIterator __unguarded_partition (RandomAccessIterator first, 
  878.                                               RandomAccessIterator last, 
  879.                                               T pivot)
  880.   {
  881.     while (true)
  882.     {
  883.       while (*first < pivot) ++first;
  884.       --last;
  885.       while (pivot < *last) --last;
  886.       if (!(first < last)) return first;
  887.       iter_swap(first, last);
  888.       ++first;
  889.     }
  890.   }    
  891.  
  892.   template <class RandomAccessIterator, class T, class Compare>
  893.   RandomAccessIterator __unguarded_partition (RandomAccessIterator first, 
  894.                                               RandomAccessIterator last, 
  895.                                               T pivot, Compare _RWSTD_COMP)
  896.   {
  897.     while (true)
  898.     {
  899.       while (_RWSTD_COMP(*first, pivot)) ++first;
  900.       --last;
  901.       while (_RWSTD_COMP(pivot, *last)) --last;
  902.       if (!(first < last)) return first;
  903.       iter_swap(first, last);
  904.       ++first;
  905.     }
  906.   }
  907.  
  908.   const int __stl_threshold = 16;
  909.  
  910.   template <class RandomAccessIterator, class T>
  911.   void __quick_sort_loop_aux (RandomAccessIterator first,
  912.                               RandomAccessIterator last, T*)
  913.   {
  914.     while (last - first > __stl_threshold)
  915.     {
  916.       RandomAccessIterator cut = __unguarded_partition
  917.       (first, last, T(__median(*first, *(first + (last - first)/2),
  918.                                *(last - 1))));
  919.       if (cut - first >= last - cut)
  920.       {
  921.         __quick_sort_loop(cut, last);
  922.         last = cut;
  923.       }
  924.       else
  925.       {
  926.         __quick_sort_loop(first, cut);
  927.         first = cut;
  928.       }
  929.     }
  930.   }
  931.  
  932.   template <class RandomAccessIterator, class T, class Compare>
  933.   void __quick_sort_loop_aux (RandomAccessIterator first, 
  934.                               RandomAccessIterator last, T*, Compare _RWSTD_COMP)
  935.   {
  936.     while (last - first > __stl_threshold)
  937.     {
  938.       RandomAccessIterator cut = __unguarded_partition
  939.       (first, last, T(__median(*first, *(first + (last - first)/2), 
  940.                                *(last - 1), _RWSTD_COMP)), _RWSTD_COMP);
  941.       if (cut - first >= last - cut)
  942.       {
  943.         __quick_sort_loop(cut, last, _RWSTD_COMP);
  944.         last = cut;
  945.       }
  946.       else
  947.       {
  948.         __quick_sort_loop(first, cut, _RWSTD_COMP);
  949.         first = cut;
  950.       }
  951.     }
  952.   }
  953.  
  954.   template <class RandomAccessIterator, class T>
  955.   void __unguarded_linear_insert (RandomAccessIterator last, T value)
  956.   {
  957.     RandomAccessIterator next = last;
  958.     --next;
  959.     while (value < *next)
  960.     {
  961.       *last = *next;
  962.       last = next--;
  963.     }
  964.     *last = value;
  965.   }
  966.  
  967.   template <class RandomAccessIterator, class T, class Compare>
  968.   void __unguarded_linear_insert (RandomAccessIterator last,T value,Compare _RWSTD_COMP)
  969.   {
  970.     RandomAccessIterator next = last;
  971.     --next;  
  972.     while (_RWSTD_COMP(value , *next))
  973.     {
  974.       *last = *next;
  975.       last = next--;
  976.     }
  977.     *last = value;
  978.   }
  979.  
  980.   template <class RandomAccessIterator>
  981.   void __insertion_sort (RandomAccessIterator first, RandomAccessIterator last)
  982.   {
  983.     if (!(first == last))
  984.       for (RandomAccessIterator i = first + 1; i != last; ++i)
  985.         __linear_insert(first, i, _RWSTD_VALUE_TYPE(first));
  986.   }
  987.  
  988.   template <class RandomAccessIterator, class Compare>
  989.   void __insertion_sort (RandomAccessIterator first,
  990.                          RandomAccessIterator last, Compare _RWSTD_COMP)
  991.   {
  992.     if (!(first == last))
  993.       for (RandomAccessIterator i = first + 1; i != last; ++i)
  994.         __linear_insert(first, i, _RWSTD_VALUE_TYPE(first), _RWSTD_COMP);
  995.   }
  996.  
  997.   template <class RandomAccessIterator, class T>
  998.   void __unguarded_insertion_sort_aux (RandomAccessIterator first, 
  999.                                        RandomAccessIterator last, T*)
  1000.   {
  1001.     for (RandomAccessIterator i = first; i != last; ++i)
  1002.       __unguarded_linear_insert(i, T(*i));
  1003.   }
  1004.  
  1005.   template <class RandomAccessIterator, class T, class Compare>
  1006.   void __unguarded_insertion_sort_aux (RandomAccessIterator first, 
  1007.                                        RandomAccessIterator last,
  1008.                                        T*, Compare _RWSTD_COMP)
  1009.   {
  1010.     for (RandomAccessIterator i = first; i != last; ++i)
  1011.       __unguarded_linear_insert(i, T(*i), _RWSTD_COMP);
  1012.   }
  1013.   template <class RandomAccessIterator>
  1014.   void __final_insertion_sort (RandomAccessIterator first, 
  1015.                                RandomAccessIterator last)
  1016.   {
  1017.     if (last - first > __stl_threshold)
  1018.     {
  1019.       __insertion_sort(first, first + __stl_threshold);
  1020.       __unguarded_insertion_sort(first + __stl_threshold, last);
  1021.     }
  1022.     else
  1023.       __insertion_sort(first, last);
  1024.   }
  1025.  
  1026.   template <class RandomAccessIterator, class Compare>
  1027.   void __final_insertion_sort (RandomAccessIterator first, 
  1028.                                RandomAccessIterator last, Compare _RWSTD_COMP)
  1029.   {
  1030.     if (last - first > __stl_threshold)
  1031.     {
  1032.       __insertion_sort(first, first + __stl_threshold, _RWSTD_COMP);
  1033.       __unguarded_insertion_sort(first + __stl_threshold, last, _RWSTD_COMP);
  1034.     }
  1035.     else
  1036.       __insertion_sort(first, last, _RWSTD_COMP);
  1037.   }
  1038.  
  1039.   template <class RandomAccessIterator1, class RandomAccessIterator2,
  1040.   class Distance>
  1041.   void __merge_sort_loop (RandomAccessIterator1 first,
  1042.                           RandomAccessIterator1 last, 
  1043.                           RandomAccessIterator2 result, Distance step_size)
  1044.   {
  1045.     Distance two_step = 2 * step_size;
  1046.  
  1047.     while (last - first >= two_step)
  1048.     {
  1049.       result = merge(first, first + step_size,
  1050.                      first + step_size, first + two_step, result);
  1051.       first += two_step;
  1052.     }
  1053.     step_size = min(Distance(last - first), step_size);
  1054.  
  1055.     merge(first, first + step_size, first + step_size, last, result);
  1056.   }
  1057.  
  1058.   template <class RandomAccessIterator1, class RandomAccessIterator2,
  1059.   class Distance, class Compare>
  1060.   void __merge_sort_loop (RandomAccessIterator1 first,
  1061.                           RandomAccessIterator1 last, 
  1062.                           RandomAccessIterator2 result, Distance step_size,
  1063.                           Compare _RWSTD_COMP)
  1064.   {
  1065.     Distance two_step = 2 * step_size;
  1066.  
  1067.     while (last - first >= two_step)
  1068.     {
  1069.       result = merge(first, first + step_size,
  1070.                      first + step_size, first + two_step, result, _RWSTD_COMP);
  1071.       first += two_step;
  1072.     }
  1073.     step_size = min(Distance(last - first), step_size);
  1074.  
  1075.     merge(first, first + step_size, first + step_size, last, result, _RWSTD_COMP);
  1076.   }
  1077.  
  1078.   const int __stl_chunk_size = 7;
  1079.     
  1080.   template <class RandomAccessIterator, class Distance>
  1081.   void __chunk_insertion_sort (RandomAccessIterator first, 
  1082.                                RandomAccessIterator last, Distance chunk_size)
  1083.   {
  1084.     while (last - first >= chunk_size)
  1085.     {
  1086.       __insertion_sort(first, first + chunk_size);
  1087.       first += chunk_size;
  1088.     }
  1089.     __insertion_sort(first, last);
  1090.   }
  1091.  
  1092.   template <class RandomAccessIterator, class Distance, class Compare>
  1093.   void __chunk_insertion_sort (RandomAccessIterator first, 
  1094.                                RandomAccessIterator last,
  1095.                                Distance chunk_size, Compare _RWSTD_COMP)
  1096.   {
  1097.     while (last - first >= chunk_size)
  1098.     {
  1099.       __insertion_sort(first, first + chunk_size, _RWSTD_COMP);
  1100.       first += chunk_size;
  1101.     }
  1102.     __insertion_sort(first, last, _RWSTD_COMP);
  1103.   }
  1104.  
  1105.   template <class RandomAccessIterator, class Pointer, class Distance, class T>
  1106.   void __merge_sort_with_buffer (RandomAccessIterator first, 
  1107.                                  RandomAccessIterator last,
  1108.                                  Pointer buffer, Distance*, T*)
  1109.   {
  1110.     Distance len = last - first;
  1111.     Pointer buffer_last = buffer + len;
  1112.  
  1113.     Distance step_size = __stl_chunk_size;
  1114.     __chunk_insertion_sort(first, last, step_size);
  1115.  
  1116.     while (step_size < len)
  1117.     {
  1118.       __merge_sort_loop(first, last, buffer, step_size);
  1119.       step_size *= 2;
  1120.       __merge_sort_loop(buffer, buffer_last, first, step_size);
  1121.       step_size *= 2;
  1122.     }
  1123.   }
  1124.  
  1125.   template <class RandomAccessIterator, class Pointer, class Distance, class T,
  1126.   class Compare>
  1127.   void __merge_sort_with_buffer (RandomAccessIterator first, 
  1128.                                  RandomAccessIterator last, Pointer buffer,
  1129.                                  Distance*, T*, Compare _RWSTD_COMP)
  1130.   {
  1131.     Distance len = last - first;
  1132.     Pointer buffer_last = buffer + len;
  1133.  
  1134.     Distance step_size = __stl_chunk_size;
  1135.     __chunk_insertion_sort(first, last, step_size, _RWSTD_COMP);
  1136.  
  1137.     while (step_size < len)
  1138.     {
  1139.       __merge_sort_loop(first, last, buffer, step_size, _RWSTD_COMP);
  1140.       step_size *= 2;
  1141.       __merge_sort_loop(buffer, buffer_last, first, step_size, _RWSTD_COMP);
  1142.       step_size *= 2;
  1143.     }
  1144.   }
  1145.  
  1146.   template <class RandomAccessIterator, class Pointer, class Distance, class T>
  1147.   void __stable_sort_adaptive (RandomAccessIterator first, 
  1148.                                RandomAccessIterator last, Pointer buffer,
  1149.                                Distance buffer_size, T*)
  1150.   {
  1151.     Distance len = (last - first + 1) / 2;
  1152.     RandomAccessIterator middle = first + len;
  1153.     if (len > buffer_size)
  1154.     {
  1155.       __stable_sort_adaptive(first, middle, buffer, buffer_size, 
  1156.                              _RWSTD_STATIC_CAST(T*,0));
  1157.       __stable_sort_adaptive(middle, last, buffer, buffer_size, 
  1158.                              _RWSTD_STATIC_CAST(T*,0));
  1159.     }
  1160.     else
  1161.     {
  1162.       __merge_sort_with_buffer(first, middle, buffer, 
  1163.                                _RWSTD_STATIC_CAST(Distance*,0),
  1164.                                _RWSTD_STATIC_CAST(T*,0));
  1165.       __merge_sort_with_buffer(middle, last, buffer, 
  1166.                                _RWSTD_STATIC_CAST(Distance*,0),
  1167.                                _RWSTD_STATIC_CAST(T*,0));
  1168.     }
  1169.     __merge_adaptive(first, middle, last, Distance(middle - first), 
  1170.                      Distance(last - middle), buffer, buffer_size, _RWSTD_STATIC_CAST(T*,0));
  1171.   }
  1172.  
  1173.   template <class RandomAccessIterator, class Pointer, class Distance, class T,
  1174.   class Compare>
  1175.   void __stable_sort_adaptive (RandomAccessIterator first, 
  1176.                                RandomAccessIterator last, Pointer buffer,
  1177.                                Distance buffer_size, T*, Compare _RWSTD_COMP)
  1178.   {
  1179.     Distance len = (last - first + 1) / 2;
  1180.     RandomAccessIterator middle = first + len;
  1181.     if (len > buffer_size)
  1182.     {
  1183.       __stable_sort_adaptive(first, middle, buffer, buffer_size,_RWSTD_STATIC_CAST(T*,0),_RWSTD_COMP);
  1184.       __stable_sort_adaptive(middle, last, buffer, buffer_size, _RWSTD_STATIC_CAST(T*,0),_RWSTD_COMP);
  1185.     }
  1186.     else
  1187.     {
  1188.       __merge_sort_with_buffer(first, middle, buffer, 
  1189.                                _RWSTD_STATIC_CAST(Distance*,0),
  1190.                                _RWSTD_STATIC_CAST(T*,0), _RWSTD_COMP);
  1191.       __merge_sort_with_buffer(middle, last, buffer, 
  1192.                                _RWSTD_STATIC_CAST(Distance*,0),
  1193.                                _RWSTD_STATIC_CAST(T*,0), _RWSTD_COMP);
  1194.     }
  1195.     __merge_adaptive(first, middle, last, Distance(middle - first), 
  1196.                      Distance(last-middle), buffer, buffer_size, _RWSTD_STATIC_CAST(T*,0),_RWSTD_COMP);
  1197.   }
  1198.  
  1199.   template <class RandomAccessIterator, class T>
  1200.   void __partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
  1201.                        RandomAccessIterator last, T*)
  1202.   {
  1203.     make_heap(first, middle);
  1204.     for (RandomAccessIterator i = middle; i < last; ++i)
  1205.       if (*i < *first) 
  1206.         __pop_heap(first, middle, i, T(*i), __distance_type(first));
  1207.     sort_heap(first, middle);
  1208.   }
  1209.  
  1210.   template <class RandomAccessIterator, class T, class Compare>
  1211.   void __partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
  1212.                        RandomAccessIterator last, T*, Compare _RWSTD_COMP)
  1213.   {
  1214.     make_heap(first, middle, _RWSTD_COMP);
  1215.     for (RandomAccessIterator i = middle; i < last; ++i)
  1216.       if (_RWSTD_COMP(*i,*first)) 
  1217.         __pop_heap(first, middle, i, T(*i), _RWSTD_COMP, __distance_type(first));
  1218.     sort_heap(first, middle, _RWSTD_COMP);
  1219.   }
  1220.  
  1221.   template <class InputIterator, class RandomAccessIterator, class Distance,
  1222.   class T>
  1223.   RandomAccessIterator __partial_sort_copy (InputIterator first,
  1224.                                             InputIterator last,
  1225.                                             RandomAccessIterator result_first,
  1226.                                             RandomAccessIterator result_last, 
  1227.                                             Distance*, T*)
  1228.   {
  1229.     if (result_first == result_last) return result_last;
  1230.     RandomAccessIterator result_real_last = result_first;
  1231.     while(first != last && result_real_last != result_last)
  1232.       *result_real_last++ = *first++;
  1233.     make_heap(result_first, result_real_last);
  1234.     while (first != last)
  1235.     {
  1236.       if (*first < *result_first) 
  1237.         __adjust_heap(result_first, Distance(0),
  1238.                       Distance(result_real_last - result_first),
  1239.                       T(*first));
  1240.       ++first;
  1241.     }
  1242.     sort_heap(result_first, result_real_last);
  1243.     return result_real_last;
  1244.   }
  1245.  
  1246.   template <class InputIterator, class RandomAccessIterator, class Compare,
  1247.   class Distance, class T>
  1248.   RandomAccessIterator __partial_sort_copy (InputIterator first,
  1249.                                             InputIterator last,
  1250.                                             RandomAccessIterator result_first,
  1251.                                             RandomAccessIterator result_last,
  1252.                                             Compare _RWSTD_COMP, Distance*, T*)
  1253.   {
  1254.     if (result_first == result_last) return result_last;
  1255.     RandomAccessIterator result_real_last = result_first;
  1256.     while(first != last && result_real_last != result_last)
  1257.       *result_real_last++ = *first++;
  1258.     make_heap(result_first, result_real_last, _RWSTD_COMP);
  1259.     while (first != last)
  1260.     {
  1261.       if (_RWSTD_COMP(*first,*result_first)) 
  1262.         __adjust_heap(result_first, Distance(0),
  1263.                       Distance(result_real_last - result_first), T(*first),
  1264.                       _RWSTD_COMP);
  1265.       ++first;
  1266.     }
  1267.     sort_heap(result_first, result_real_last, _RWSTD_COMP);
  1268.     return result_real_last;
  1269.   }
  1270.  
  1271.   template <class RandomAccessIterator, class T>
  1272.   void __nth_element (RandomAccessIterator first, RandomAccessIterator nth,
  1273.                       RandomAccessIterator last, T*)
  1274.   {
  1275.     while (last - first > 3)
  1276.     {
  1277.       RandomAccessIterator cut = __unguarded_partition
  1278.       (first, last, T(__median(*first, *(first + (last - first)/2),
  1279.                                *(last - 1))));
  1280.       if (cut <= nth)
  1281.         first = cut;
  1282.       else 
  1283.         last = cut;
  1284.     }
  1285.     __insertion_sort(first, last);
  1286.   }
  1287.  
  1288.   template <class RandomAccessIterator, class T, class Compare>
  1289.   void __nth_element (RandomAccessIterator first, RandomAccessIterator nth,
  1290.                       RandomAccessIterator last, T*, Compare _RWSTD_COMP)
  1291.   {
  1292.     while (last - first > 3)
  1293.     {
  1294.       RandomAccessIterator cut = __unguarded_partition
  1295.       (first, last, T(__median(*first, *(first + (last - first)/2), 
  1296.                                *(last - 1), _RWSTD_COMP)), _RWSTD_COMP);
  1297.       if (cut <= nth)
  1298.         first = cut;
  1299.       else 
  1300.         last = cut;
  1301.     }
  1302.     __insertion_sort(first, last, _RWSTD_COMP);
  1303.   }
  1304.  
  1305. //
  1306. // Binary search.
  1307. //
  1308.  
  1309.   template <class ForwardIterator, class T, class Distance>
  1310.   ForwardIterator __lower_bound (ForwardIterator first, ForwardIterator last,
  1311.                                  const T& value, Distance*,
  1312.                                  forward_iterator_tag)
  1313.   {
  1314.     Distance len;
  1315.     __initialize(len, Distance(0));
  1316.     distance(first, last, len);
  1317.     Distance half;
  1318.     ForwardIterator middle;
  1319.  
  1320.     while (len > 0)
  1321.     {
  1322.       half = len / 2;
  1323.       middle = first;
  1324.       advance(middle, half);
  1325.       if (*middle < value)
  1326.       {
  1327.         first = middle;
  1328.         ++first;
  1329.         len = len - half - 1;
  1330.       }
  1331.       else
  1332.         len = half;
  1333.     }
  1334.     return first;
  1335.   }
  1336.  
  1337.   template <class RandomAccessIterator, class T, class Distance>
  1338.   RandomAccessIterator __lower_bound (RandomAccessIterator first,
  1339.                                       RandomAccessIterator last, const T& value,
  1340.                                       Distance*, random_access_iterator_tag)
  1341.   {
  1342.     Distance len = last - first;
  1343.     Distance half;
  1344.     RandomAccessIterator middle;
  1345.  
  1346.     while (len > 0)
  1347.     {
  1348.       half = len / 2;
  1349.       middle = first + half;
  1350.       if (*middle < value)
  1351.       {
  1352.         first = middle + 1;
  1353.         len = len - half - 1;
  1354.       }
  1355.       else
  1356.         len = half;
  1357.     }
  1358.     return first;
  1359.   }
  1360.  
  1361.   template <class ForwardIterator, class T, class Compare, class Distance>
  1362.   ForwardIterator __lower_bound (ForwardIterator first, ForwardIterator last,
  1363.                                  const T& value, Compare _RWSTD_COMP, Distance*,
  1364.                                  forward_iterator_tag)
  1365.   {
  1366.     Distance len;
  1367.     __initialize(len, Distance(0));
  1368.     distance(first, last, len);
  1369.     Distance half;
  1370.     ForwardIterator middle;
  1371.  
  1372.     while (len > 0)
  1373.     {
  1374.       half = len / 2;
  1375.       middle = first;
  1376.       advance(middle, half);
  1377.       if (_RWSTD_COMP(*middle, value))
  1378.       {
  1379.         first = middle;
  1380.         ++first;
  1381.         len = len - half - 1;
  1382.       }
  1383.       else
  1384.         len = half;
  1385.     }
  1386.     return first;
  1387.   }
  1388.  
  1389.   template <class RandomAccessIterator, class T, class Compare, class Distance>
  1390.   RandomAccessIterator __lower_bound (RandomAccessIterator first,
  1391.                                       RandomAccessIterator last,
  1392.                                       const T& value, 
  1393.                                       Compare _RWSTD_COMP, 
  1394.                                       Distance*,
  1395.                                       random_access_iterator_tag)
  1396.   {
  1397.     Distance len = last - first;
  1398.     Distance half;
  1399.     RandomAccessIterator middle;
  1400.  
  1401.     while (len > 0)
  1402.     {
  1403.       half = len / 2;
  1404.       middle = first + half;
  1405.       if (_RWSTD_COMP(*middle, value))
  1406.       {
  1407.         first = middle + 1;
  1408.         len = len - half - 1;
  1409.       }
  1410.       else
  1411.         len = half;
  1412.     }
  1413.     return first;
  1414.   }
  1415.  
  1416.   template <class ForwardIterator, class T, class Distance>
  1417.   ForwardIterator __upper_bound (ForwardIterator first, ForwardIterator last,
  1418.                                  const T& value, Distance*,
  1419.                                  forward_iterator_tag)
  1420.   {
  1421.     Distance len;
  1422.     __initialize(len, Distance(0));
  1423.     distance(first, last, len);
  1424.     Distance half;
  1425.     ForwardIterator middle;
  1426.  
  1427.     while (len > 0)
  1428.     {
  1429.       half = len / 2;
  1430.       middle = first;
  1431.       advance(middle, half);
  1432.       if (value < *middle)
  1433.         len = half;
  1434.       else
  1435.       {
  1436.         first = middle;
  1437.         ++first;
  1438.         len = len - half - 1;
  1439.       }
  1440.     }
  1441.     return first;
  1442.   }
  1443.  
  1444.   template <class RandomAccessIterator, class T, class Distance>
  1445.   RandomAccessIterator __upper_bound (RandomAccessIterator first,
  1446.                                       RandomAccessIterator last, const T& value,
  1447.                                       Distance*, random_access_iterator_tag)
  1448.   {
  1449.     Distance len = last - first;
  1450.     Distance half;
  1451.     RandomAccessIterator middle;
  1452.  
  1453.     while (len > 0)
  1454.     {
  1455.       half = len / 2;
  1456.       middle = first + half;
  1457.       if (value < *middle)
  1458.         len = half;
  1459.       else
  1460.       {
  1461.         first = middle + 1;
  1462.         len = len - half - 1;
  1463.       }
  1464.     }
  1465.     return first;
  1466.   }
  1467.  
  1468.   template <class ForwardIterator, class T, class Compare, class Distance>
  1469.   ForwardIterator __upper_bound (ForwardIterator first, ForwardIterator last,
  1470.                                  const T& value, Compare _RWSTD_COMP, Distance*,
  1471.                                  forward_iterator_tag)
  1472.   {
  1473.     Distance len;
  1474.     __initialize(len, Distance(0));
  1475.     distance(first, last, len);
  1476.     Distance half;
  1477.     ForwardIterator middle;
  1478.  
  1479.     while (len > 0)
  1480.     {
  1481.       half = len / 2;
  1482.       middle = first;
  1483.       advance(middle, half);
  1484.       if (_RWSTD_COMP(value, *middle))
  1485.         len = half;
  1486.       else {
  1487.         first = middle;
  1488.         ++first;
  1489.         len = len - half - 1;
  1490.       }
  1491.     }
  1492.     return first;
  1493.   }
  1494.  
  1495.   template <class RandomAccessIterator, class T, class Compare, class Distance>
  1496.   RandomAccessIterator __upper_bound (RandomAccessIterator first,
  1497.                                       RandomAccessIterator last,
  1498.                                       const T& value, 
  1499.                                       Compare _RWSTD_COMP, Distance*,
  1500.                                       random_access_iterator_tag)
  1501.   {
  1502.     Distance len = last - first;
  1503.     Distance half;
  1504.     RandomAccessIterator middle;
  1505.  
  1506.     while (len > 0)
  1507.     {
  1508.       half = len / 2;
  1509.       middle = first + half;
  1510.       if (_RWSTD_COMP(value, *middle))
  1511.         len = half;
  1512.       else {
  1513.         first = middle + 1;
  1514.         len = len - half - 1;
  1515.       }
  1516.     }
  1517.     return first;
  1518.   }
  1519.  
  1520.   template <class ForwardIterator, class T, class Distance>
  1521.   pair<ForwardIterator, ForwardIterator>
  1522.   __equal_range (ForwardIterator first, ForwardIterator last, const T& value,
  1523.                  Distance*, forward_iterator_tag)
  1524.   {
  1525.     Distance len;
  1526.     __initialize(len, Distance(0));
  1527.     distance(first, last, len);
  1528.     Distance half;
  1529.     ForwardIterator middle, left, right;
  1530.  
  1531.     while (len > 0)
  1532.     {
  1533.       half = len / 2;
  1534.       middle = first;
  1535.       advance(middle, half);
  1536.       if (*middle < value)
  1537.       {
  1538.         first = middle;
  1539.         ++first;
  1540.         len = len - half - 1;
  1541.       }
  1542.       else if (value < *middle)
  1543.         len = half;
  1544.       else
  1545.       {
  1546.         left = lower_bound(first, middle, value);
  1547.         advance(first, len);
  1548.         right = upper_bound(++middle, first, value);
  1549.         pair<ForwardIterator, ForwardIterator> tmp(left, right);
  1550.         return tmp;
  1551.       }
  1552.     }
  1553.     pair<ForwardIterator, ForwardIterator> tmp(first, first);
  1554.     return tmp;
  1555.   }
  1556.  
  1557.   template <class RandomAccessIterator, class T, class Distance>
  1558.   pair<RandomAccessIterator, RandomAccessIterator>
  1559.   __equal_range (RandomAccessIterator first, RandomAccessIterator last,
  1560.                  const T& value, Distance*, random_access_iterator_tag)
  1561.   {
  1562.     Distance len = last - first;
  1563.     Distance half;
  1564.     RandomAccessIterator middle, left, right;
  1565.  
  1566.     while (len > 0)
  1567.     {
  1568.       half = len / 2;
  1569.       middle = first + half;
  1570.       if (*middle < value)
  1571.       {
  1572.         first = middle + 1;
  1573.         len = len - half - 1;
  1574.       }
  1575.       else if (value < *middle)
  1576.         len = half;
  1577.       else
  1578.       {
  1579.         left = lower_bound(first, middle, value);
  1580.         right = upper_bound(++middle, first + len, value);
  1581.         pair<RandomAccessIterator,RandomAccessIterator> tmp(left,right);
  1582.         return tmp;
  1583.       }
  1584.     }
  1585.     pair<RandomAccessIterator, RandomAccessIterator> tmp(first, first);
  1586.     return tmp;
  1587.   }
  1588.  
  1589.   template <class ForwardIterator, class T, class Compare, class Distance>
  1590.   pair<ForwardIterator, ForwardIterator>
  1591.   __equal_range (ForwardIterator first, ForwardIterator last, const T& value,
  1592.                  Compare _RWSTD_COMP, Distance*, forward_iterator_tag)
  1593.   {
  1594.     Distance len;
  1595.     __initialize(len, Distance(0));
  1596.     distance(first, last, len);
  1597.     Distance half;
  1598.     ForwardIterator middle, left, right;
  1599.  
  1600.     while (len > 0)
  1601.     {
  1602.       half = len / 2;
  1603.       middle = first;
  1604.       advance(middle, half);
  1605.       if (_RWSTD_COMP(*middle, value))
  1606.       {
  1607.         first = middle;
  1608.         ++first;
  1609.         len = len - half - 1;
  1610.       }
  1611.       else if (_RWSTD_COMP(value, *middle))
  1612.         len = half;
  1613.       else
  1614.       {
  1615.         left = lower_bound(first, middle, value, _RWSTD_COMP);
  1616.         advance(first, len);
  1617.         right = upper_bound(++middle, first, value, _RWSTD_COMP);
  1618.         pair<ForwardIterator, ForwardIterator> tmp(left, right);
  1619.         return tmp;
  1620.       }
  1621.     }
  1622.     pair<ForwardIterator, ForwardIterator> tmp(first, first);
  1623.     return tmp;
  1624.   }           
  1625.  
  1626.   template <class RandomAccessIterator, class T, class Compare, class Distance>
  1627.   pair<RandomAccessIterator, RandomAccessIterator>
  1628.   __equal_range (RandomAccessIterator first, RandomAccessIterator last,
  1629.                  const T& value, Compare _RWSTD_COMP, Distance*,
  1630.                  random_access_iterator_tag)
  1631.   {
  1632.     Distance len = last - first;
  1633.     Distance half;
  1634.     RandomAccessIterator middle, left, right;
  1635.  
  1636.     while (len > 0)
  1637.     {
  1638.       half = len / 2;
  1639.       middle = first + half;
  1640.       if (_RWSTD_COMP(*middle, value))
  1641.       {
  1642.         first = middle + 1;
  1643.         len = len - half - 1;
  1644.       }
  1645.       else if (_RWSTD_COMP(value, *middle))
  1646.         len = half;
  1647.       else
  1648.       {
  1649.         left = lower_bound(first, middle, value, _RWSTD_COMP);
  1650.         right = upper_bound(++middle, first + len, value, _RWSTD_COMP);
  1651.         pair<RandomAccessIterator,RandomAccessIterator> tmp(left, right);
  1652.         return tmp;
  1653.       }
  1654.     }
  1655.     pair<RandomAccessIterator, RandomAccessIterator> tmp(first, first);
  1656.     return tmp;
  1657.   }           
  1658.  
  1659. //
  1660. // Merge
  1661. //
  1662.  
  1663.   template <class InputIterator1, class InputIterator2, class OutputIterator>
  1664.   OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
  1665.                         InputIterator2 first2, InputIterator2 last2,
  1666.                         OutputIterator result)
  1667.   {
  1668.     while (first1 != last1 && first2 != last2)
  1669.     {
  1670.       if (*first2 < *first1)
  1671.         *result++ = *first2++;
  1672.       else
  1673.         *result++ = *first1++;
  1674.     }
  1675.     return copy(first2, last2, copy(first1, last1, result));
  1676.   }
  1677.  
  1678.   template <class InputIterator1, class InputIterator2, class OutputIterator,
  1679.   class Compare>
  1680.   OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
  1681.                         InputIterator2 first2, InputIterator2 last2,
  1682.                         OutputIterator result, Compare _RWSTD_COMP)
  1683.   {
  1684.     while (first1 != last1 && first2 != last2)
  1685.     {
  1686.       if (_RWSTD_COMP(*first2, *first1))
  1687.         *result++ = *first2++;
  1688.       else
  1689.         *result++ = *first1++;
  1690.     }
  1691.     return copy(first2, last2, copy(first1, last1, result));
  1692.   }
  1693.  
  1694.   template <class BidirectionalIterator, class Distance>
  1695.   void __merge_without_buffer (BidirectionalIterator first,
  1696.                                BidirectionalIterator middle,
  1697.                                BidirectionalIterator last,
  1698.                                Distance len1, Distance len2)
  1699.   {
  1700.     if (len1 == 0 || len2 == 0) return;
  1701.     if (len1 + len2 == 2)
  1702.     {
  1703.       if (*middle < *first) iter_swap(first, middle);
  1704.       return;
  1705.     }
  1706.     BidirectionalIterator first_cut = first;
  1707.     BidirectionalIterator second_cut = middle;
  1708.     Distance len11;
  1709.     __initialize(len11, Distance(0));
  1710.     Distance len22;
  1711.     __initialize(len22, Distance(0));
  1712.     if (len1 > len2)
  1713.     {
  1714.       len11 = len1 / 2;
  1715.       advance(first_cut, len11);
  1716.       second_cut = lower_bound(middle, last, *first_cut);
  1717.       distance(middle, second_cut, len22);
  1718.     }
  1719.     else
  1720.     {
  1721.       len22 = len2 / 2;
  1722.       advance(second_cut, len22);
  1723.       first_cut = upper_bound(first, middle, *second_cut);
  1724.       distance(first, first_cut, len11);
  1725.     }
  1726.     rotate(first_cut, middle, second_cut);
  1727.     BidirectionalIterator new_middle = first_cut;
  1728.     advance(new_middle, len22);
  1729.     __merge_without_buffer(first, first_cut, new_middle, len11, len22);
  1730.     __merge_without_buffer(new_middle, second_cut, last, len1 - len11,
  1731.                            len2 - len22);
  1732.   }
  1733.  
  1734.   template <class BidirectionalIterator, class Distance, class Compare>
  1735.   void __merge_without_buffer (BidirectionalIterator first,
  1736.                                BidirectionalIterator middle,
  1737.                                BidirectionalIterator last,
  1738.                                Distance len1, Distance len2, Compare _RWSTD_COMP)
  1739.   {
  1740.     if (len1 == 0 || len2 == 0) return;
  1741.     if (len1 + len2 == 2)
  1742.     {
  1743.       if (_RWSTD_COMP(*middle, *first)) iter_swap(first, middle);
  1744.       return;
  1745.     }
  1746.     BidirectionalIterator first_cut = first;
  1747.     BidirectionalIterator second_cut = middle;
  1748.     Distance len11;
  1749.     __initialize(len11, Distance(0));
  1750.     Distance len22;
  1751.     __initialize(len22, Distance(0));
  1752.     if (len1 > len2)
  1753.     {
  1754.       len11 = len1 / 2;
  1755.       advance(first_cut, len11);
  1756.       second_cut = lower_bound(middle, last, *first_cut, _RWSTD_COMP);
  1757.       distance(middle, second_cut, len22);
  1758.     }
  1759.     else
  1760.     {
  1761.       len22 = len2 / 2;
  1762.       advance(second_cut, len22);
  1763.       first_cut = upper_bound(first, middle, *second_cut, _RWSTD_COMP);
  1764.       distance(first, first_cut, len11);
  1765.     }
  1766.     rotate(first_cut, middle, second_cut);
  1767.     BidirectionalIterator new_middle = first_cut;
  1768.     advance(new_middle, len22);
  1769.     __merge_without_buffer(first, first_cut, new_middle, len11, len22, _RWSTD_COMP);
  1770.     __merge_without_buffer(new_middle, second_cut, last, len1 - len11,
  1771.                            len2 - len22, _RWSTD_COMP);
  1772.   }
  1773.  
  1774.   template <class BidirectionalIterator1, class BidirectionalIterator2,
  1775.   class Distance>
  1776.   BidirectionalIterator1 __rotate_adaptive (BidirectionalIterator1 first,
  1777.                                             BidirectionalIterator1 middle,
  1778.                                             BidirectionalIterator1 last,
  1779.                                             Distance len1, Distance len2,
  1780.                                             BidirectionalIterator2 buffer,
  1781.                                             Distance buffer_size)
  1782.   {
  1783.     BidirectionalIterator2 buffer_end;
  1784.     if (len1 > len2 && len2 <= buffer_size)
  1785.     {
  1786.       buffer_end = copy(middle, last, buffer);
  1787.       copy_backward(first, middle, last);
  1788.       return copy(buffer, buffer_end, first);
  1789.     }
  1790.     else if (len1 <= buffer_size)
  1791.     {
  1792.       buffer_end = copy(first, middle, buffer);
  1793.       copy(middle, last, first);
  1794.       return copy_backward(buffer, buffer_end, last);
  1795.     }
  1796.     else
  1797.     {
  1798.       rotate(first, middle, last);
  1799.       advance(first, len2);
  1800.       return first;
  1801.     }
  1802.   }
  1803.  
  1804.   template <class BidirectionalIterator1, class BidirectionalIterator2,
  1805.   class BidirectionalIterator3>
  1806.   BidirectionalIterator3 __merge_backward (BidirectionalIterator1 first1,
  1807.                                            BidirectionalIterator1 last1,
  1808.                                            BidirectionalIterator2 first2,
  1809.                                            BidirectionalIterator2 last2,
  1810.                                            BidirectionalIterator3 result)
  1811.   {
  1812.     if (first1 == last1) return copy_backward(first2, last2, result);
  1813.     if (first2 == last2) return copy_backward(first1, last1, result);
  1814.     --last1;
  1815.     --last2;
  1816.     while (true)
  1817.     {
  1818.       if (*last2 < *last1)
  1819.       {
  1820.         *--result = *last1;
  1821.         if (first1 == last1) return copy_backward(first2, ++last2, result);
  1822.         --last1;
  1823.       }
  1824.       else
  1825.       {
  1826.         *--result = *last2;
  1827.         if (first2 == last2) return copy_backward(first1, ++last1, result);
  1828.         --last2;
  1829.       }
  1830.     }
  1831.   }
  1832.  
  1833.   template <class BidirectionalIterator1, class BidirectionalIterator2,
  1834.   class BidirectionalIterator3, class Compare>
  1835.   BidirectionalIterator3 __merge_backward (BidirectionalIterator1 first1,
  1836.                                            BidirectionalIterator1 last1,
  1837.                                            BidirectionalIterator2 first2,
  1838.                                            BidirectionalIterator2 last2,
  1839.                                            BidirectionalIterator3 result,
  1840.                                            Compare _RWSTD_COMP)
  1841.   {
  1842.     if (first1 == last1) return copy_backward(first2, last2, result);
  1843.     if (first2 == last2) return copy_backward(first1, last1, result);
  1844.     --last1;
  1845.     --last2;
  1846.     while (true)
  1847.     {
  1848.       if (_RWSTD_COMP(*last2, *last1))
  1849.       {
  1850.         *--result = *last1;
  1851.         if (first1 == last1) return copy_backward(first2, ++last2, result);
  1852.         --last1;
  1853.       }
  1854.       else
  1855.       {
  1856.         *--result = *last2;
  1857.         if (first2 == last2) return copy_backward(first1, ++last1, result);
  1858.         --last2;
  1859.       }
  1860.     }
  1861.   }
  1862.  
  1863.   template <class BidirectionalIterator, class Distance, class Pointer, class T>
  1864.   void __merge_adaptive (BidirectionalIterator first,
  1865.                          BidirectionalIterator middle,
  1866.                          BidirectionalIterator last, Distance len1,Distance len2,
  1867.                          Pointer buffer, Distance buffer_size, T*)
  1868.   {
  1869.     if (len1 <= len2 && len1 <= buffer_size)
  1870.     {
  1871.       Pointer end_buffer = copy(first, middle, buffer);
  1872.       merge(buffer, end_buffer, middle, last, first);
  1873.     }
  1874.     else if (len2 <= buffer_size)
  1875.     {
  1876.       Pointer end_buffer = copy(middle, last, buffer);
  1877.       __merge_backward(first, middle, buffer, end_buffer, last);
  1878.     }
  1879.     else
  1880.     {
  1881.       BidirectionalIterator first_cut = first;
  1882.       BidirectionalIterator second_cut = middle;
  1883.       Distance len11;
  1884.       __initialize(len11, Distance(0));
  1885.       Distance len22;
  1886.       __initialize(len22, Distance(0));
  1887.       if (len1 > len2)
  1888.       {
  1889.         len11 = len1 / 2;
  1890.         advance(first_cut, len11);
  1891.         second_cut = lower_bound(middle, last, *first_cut);
  1892.         distance(middle, second_cut, len22);   
  1893.       }
  1894.       else
  1895.       {
  1896.         len22 = len2 / 2;
  1897.         advance(second_cut, len22);
  1898.         first_cut = upper_bound(first, middle, *second_cut);
  1899.         distance(first, first_cut, len11);
  1900.       }
  1901.       BidirectionalIterator new_middle =
  1902.       __rotate_adaptive(first_cut, middle, second_cut, len1 - len11,
  1903.                         len22, buffer, buffer_size);
  1904.       __merge_adaptive(first, first_cut, new_middle, len11, len22, buffer,
  1905.                        buffer_size, _RWSTD_STATIC_CAST(T*,0));
  1906.       __merge_adaptive(new_middle, second_cut, last, len1 - len11,
  1907.                        len2 - len22, buffer, buffer_size, _RWSTD_STATIC_CAST(T*,0));
  1908.     }
  1909.   }
  1910.  
  1911.   template <class BidirectionalIterator, class Distance, class Pointer, class T,
  1912.   class Compare>
  1913.   void __merge_adaptive (BidirectionalIterator first,
  1914.                          BidirectionalIterator middle,
  1915.                          BidirectionalIterator last, Distance len1,Distance len2,
  1916.                          Pointer buffer, Distance buffer_size, T*, Compare _RWSTD_COMP)
  1917.   {
  1918.     if (len1 <= len2 && len1 <= buffer_size)
  1919.     {
  1920.       Pointer end_buffer = copy(first, middle, buffer);
  1921.       merge(buffer, end_buffer, middle, last, first, _RWSTD_COMP);
  1922.     }
  1923.     else if (len2 <= buffer_size)
  1924.     {
  1925.       Pointer end_buffer = copy(middle, last, buffer);
  1926.       __merge_backward(first, middle, buffer, end_buffer, last, _RWSTD_COMP);
  1927.     }
  1928.     else
  1929.     {
  1930.       BidirectionalIterator first_cut = first;
  1931.       BidirectionalIterator second_cut = middle;
  1932.       Distance len11;
  1933.       __initialize(len11, Distance(0));
  1934.       Distance len22;
  1935.       __initialize(len22, Distance(0));
  1936.       if (len1 > len2)
  1937.       {
  1938.         len11 = len1 / 2;
  1939.         advance(first_cut, len11);
  1940.         second_cut = lower_bound(middle, last, *first_cut, _RWSTD_COMP);
  1941.         distance(middle, second_cut, len22);   
  1942.       }
  1943.       else
  1944.       {
  1945.         len22 = len2 / 2;
  1946.         advance(second_cut, len22);
  1947.         first_cut = upper_bound(first, middle, *second_cut, _RWSTD_COMP);
  1948.         distance(first, first_cut, len11);
  1949.       }
  1950.       BidirectionalIterator new_middle =
  1951.         __rotate_adaptive(first_cut, middle, second_cut, len1 - len11,
  1952.                           len22, buffer, buffer_size);
  1953.       __merge_adaptive(first, first_cut, new_middle, len11, len22, buffer,
  1954.                        buffer_size, _RWSTD_STATIC_CAST(T*,0), _RWSTD_COMP);
  1955.       __merge_adaptive(new_middle, second_cut, last, len1 - len11,
  1956.                        len2 - len22, buffer, buffer_size, _RWSTD_STATIC_CAST(T*,0), _RWSTD_COMP);
  1957.     }
  1958.   }
  1959.  
  1960.   template <class BidirectionalIterator, class Distance, class Pointer, class T>
  1961.   void __inplace_merge (BidirectionalIterator first,
  1962.                         BidirectionalIterator middle, 
  1963.                         BidirectionalIterator last, Distance len1,
  1964.                         Distance len2, pair<Pointer, Distance> p, T*)
  1965.   {
  1966.     if (p.first == 0)
  1967.       __merge_without_buffer(first, middle, last, len1, len2);
  1968.     else
  1969.     {
  1970.       Distance len = min(p.second, len1 + len2);
  1971.       fill_n(raw_storage_iterator<Pointer, T>(p.first), len, *first);
  1972.       __merge_adaptive(first, middle, last, len1, len2, p.first,
  1973.                        p.second, _RWSTD_STATIC_CAST(T*,0));
  1974.       __RWSTD::__destroy(p.first, p.first + len);
  1975.       return_temporary_buffer(p.first);
  1976.     }
  1977.   }
  1978.  
  1979.   template <class BidirectionalIterator, class Distance, class Pointer, class T,
  1980.   class Compare>
  1981.   void __inplace_merge (BidirectionalIterator first,
  1982.                         BidirectionalIterator middle,
  1983.                         BidirectionalIterator last, Distance len1,
  1984.                         Distance len2, pair<Pointer, Distance> p, T*,
  1985.                         Compare _RWSTD_COMP)
  1986.   {
  1987.     if (p.first == 0)
  1988.       __merge_without_buffer(first, middle, last, len1, len2, _RWSTD_COMP);
  1989.     else
  1990.     {
  1991.       Distance len = min(p.second, len1 + len2);
  1992.       fill_n(raw_storage_iterator<Pointer, T>(p.first), len, *first);
  1993.       __merge_adaptive(first, middle, last, len1, len2, p.first,
  1994.                        p.second, _RWSTD_STATIC_CAST(T*,0), _RWSTD_COMP); 
  1995.       __RWSTD::__destroy(p.first, p.first + len);
  1996.       return_temporary_buffer(p.first);
  1997.     }
  1998.   }
  1999.  
  2000. //
  2001. // Set operations.
  2002. //
  2003.  
  2004.   template <class InputIterator1, class InputIterator2>
  2005.   bool includes (InputIterator1 first1, InputIterator1 last1,
  2006.                  InputIterator2 first2, InputIterator2 last2)
  2007.   {
  2008.     while (first1 != last1 && first2 != last2)
  2009.     {
  2010.       if (*first2 < *first1)
  2011.         return false;
  2012.       else if(*first1 < *first2) 
  2013.         ++first1;
  2014.       else
  2015.         ++first1, ++first2;
  2016.     }
  2017.     return first2 == last2;
  2018.   }
  2019.  
  2020.   template <class InputIterator1, class InputIterator2, class Compare>
  2021.   bool includes (InputIterator1 first1, InputIterator1 last1,
  2022.                  InputIterator2 first2, InputIterator2 last2, Compare _RWSTD_COMP)
  2023.   {
  2024.     while (first1 != last1 && first2 != last2)
  2025.     {
  2026.       if (_RWSTD_COMP(*first2, *first1))
  2027.         return false;
  2028.       else if(_RWSTD_COMP(*first1, *first2)) 
  2029.         ++first1;
  2030.       else
  2031.         ++first1, ++first2;
  2032.     }
  2033.     return first2 == last2;
  2034.   }
  2035.  
  2036.   template <class InputIterator1, class InputIterator2, class OutputIterator>
  2037.   OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
  2038.                             InputIterator2 first2, InputIterator2 last2,
  2039.                             OutputIterator result)
  2040.   {
  2041.     while (first1 != last1 && first2 != last2)
  2042.     {
  2043.       if (*first1 < *first2)
  2044.         *result++ = *first1++;
  2045.       else if (*first2 < *first1)
  2046.         *result++ = *first2++;
  2047.       else
  2048.       {
  2049.         *result++ = *first1++;
  2050.         first2++;
  2051.       }
  2052.     }
  2053.     return copy(first2, last2, copy(first1, last1, result));
  2054.   }
  2055.  
  2056.   template <class InputIterator1, class InputIterator2, class OutputIterator,
  2057.   class Compare>
  2058.   OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
  2059.                             InputIterator2 first2, InputIterator2 last2,
  2060.                             OutputIterator result, Compare _RWSTD_COMP)
  2061.   {
  2062.     while (first1 != last1 && first2 != last2)
  2063.     {
  2064.       if (_RWSTD_COMP(*first1, *first2))
  2065.         *result++ = *first1++;
  2066.       else if (_RWSTD_COMP(*first2, *first1))
  2067.         *result++ = *first2++;
  2068.       else
  2069.       {
  2070.         *result++ = *first1++;
  2071.         ++first2;
  2072.       }
  2073.     }
  2074.     return copy(first2, last2, copy(first1, last1, result));
  2075.   }
  2076.  
  2077.   template <class InputIterator1, class InputIterator2, class OutputIterator>
  2078.   OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
  2079.                                    InputIterator2 first2, InputIterator2 last2,
  2080.                                    OutputIterator result)
  2081.   {
  2082.     while (first1 != last1 && first2 != last2)
  2083.     {
  2084.       if (*first1 < *first2)
  2085.         ++first1;
  2086.       else if (*first2 < *first1)
  2087.         ++first2;
  2088.       else
  2089.       {
  2090.         *result++ = *first1++;
  2091.         ++first2;
  2092.       }
  2093.     }
  2094.     return result;
  2095.   }
  2096.  
  2097.   template <class InputIterator1, class InputIterator2, class OutputIterator,
  2098.   class Compare>
  2099.   OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
  2100.                                    InputIterator2 first2, InputIterator2 last2,
  2101.                                    OutputIterator result, Compare _RWSTD_COMP)
  2102.   {
  2103.     while (first1 != last1 && first2 != last2)
  2104.     {
  2105.       if (_RWSTD_COMP(*first1, *first2))
  2106.         ++first1;
  2107.       else if (_RWSTD_COMP(*first2, *first1))
  2108.         ++first2;
  2109.       else
  2110.       {
  2111.         *result++ = *first1++;
  2112.         ++first2;
  2113.       }
  2114.     }
  2115.     return result;
  2116.   }
  2117.  
  2118.   template <class InputIterator1, class InputIterator2, class OutputIterator>
  2119.   OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
  2120.                                  InputIterator2 first2, InputIterator2 last2,
  2121.                                  OutputIterator result)
  2122.   {
  2123.     while (first1 != last1 && first2 != last2)
  2124.     {
  2125.       if (*first1 < *first2)
  2126.         *result++ = *first1++;
  2127.       else if (*first2 < *first1)
  2128.         ++first2;
  2129.       else
  2130.       {
  2131.         ++first1;
  2132.         ++first2;
  2133.       }
  2134.     }
  2135.     return copy(first1, last1, result);
  2136.   }
  2137.  
  2138.   template <class InputIterator1, class InputIterator2, class OutputIterator, 
  2139.   class Compare>
  2140.   OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
  2141.                                  InputIterator2 first2, InputIterator2 last2, 
  2142.                                  OutputIterator result, Compare _RWSTD_COMP)
  2143.   {
  2144.     while (first1 != last1 && first2 != last2)
  2145.     {
  2146.       if (_RWSTD_COMP(*first1, *first2))
  2147.         *result++ = *first1++;
  2148.       else if (_RWSTD_COMP(*first2, *first1))
  2149.         ++first2;
  2150.       else
  2151.       {
  2152.         ++first1;
  2153.         ++first2;
  2154.       }
  2155.     }
  2156.     return copy(first1, last1, result);
  2157.   }
  2158.  
  2159.   template <class InputIterator1, class InputIterator2, class OutputIterator>
  2160.   OutputIterator set_symmetric_difference (InputIterator1 first1,
  2161.                                            InputIterator1 last1,
  2162.                                            InputIterator2 first2,
  2163.                                            InputIterator2 last2,
  2164.                                            OutputIterator result)
  2165.   {
  2166.     while (first1 != last1 && first2 != last2)
  2167.     {
  2168.       if (*first1 < *first2)
  2169.         *result++ = *first1++;
  2170.       else if (*first2 < *first1)
  2171.         *result++ = *first2++;
  2172.       else
  2173.       {
  2174.         ++first1;
  2175.         ++first2;
  2176.       }
  2177.     }
  2178.     return copy(first2, last2, copy(first1, last1, result));
  2179.   }
  2180.  
  2181.   template <class InputIterator1, class InputIterator2, class OutputIterator,
  2182.   class Compare>
  2183.   OutputIterator set_symmetric_difference (InputIterator1 first1,
  2184.                                            InputIterator1 last1,
  2185.                                            InputIterator2 first2,
  2186.                                            InputIterator2 last2,
  2187.                                            OutputIterator result, Compare _RWSTD_COMP)
  2188.   {
  2189.     while (first1 != last1 && first2 != last2)
  2190.     {
  2191.       if (_RWSTD_COMP(*first1, *first2))
  2192.         *result++ = *first1++;
  2193.       else if (_RWSTD_COMP(*first2, *first1))
  2194.         *result++ = *first2++;
  2195.       else
  2196.       {
  2197.         ++first1;
  2198.         ++first2;
  2199.       }
  2200.     }
  2201.     return copy(first2, last2, copy(first1, last1, result));
  2202.   }
  2203.  
  2204. //
  2205. // Heap operations.
  2206. //
  2207.  
  2208.   template <class RandomAccessIterator, class Distance, class T>
  2209.   void __push_heap (RandomAccessIterator first, Distance holeIndex,
  2210.                     Distance topIndex, T value)
  2211.   {
  2212.     Distance parent = (holeIndex - 1) / 2;
  2213.     while (holeIndex > topIndex && *(first + parent) < value)
  2214.     {
  2215.       *(first + holeIndex) = *(first + parent);
  2216.       holeIndex = parent;
  2217.       parent = (holeIndex - 1) / 2;
  2218.     }    
  2219.     *(first + holeIndex) = value;
  2220.   }
  2221.  
  2222.   template <class RandomAccessIterator, class Distance, class T, class Compare>
  2223.   void __push_heap (RandomAccessIterator first, Distance holeIndex,
  2224.                     Distance topIndex, T value, Compare _RWSTD_COMP)
  2225.   {
  2226.     Distance parent = (holeIndex - 1) / 2;
  2227.     while (holeIndex > topIndex && _RWSTD_COMP(*(first + parent), value))
  2228.     {
  2229.       *(first + holeIndex) = *(first + parent);
  2230.       holeIndex = parent;
  2231.       parent = (holeIndex - 1) / 2;
  2232.     }
  2233.     *(first + holeIndex) = value;
  2234.   }
  2235.  
  2236.   template <class RandomAccessIterator, class Distance, class T>
  2237.   void __adjust_heap (RandomAccessIterator first, Distance holeIndex,
  2238.                       Distance len, T value)
  2239.   {
  2240.     Distance topIndex = holeIndex;
  2241.     Distance secondChild = 2 * holeIndex + 2;
  2242.     while (secondChild < len)
  2243.     {
  2244.       if (*(first + secondChild) < *(first + (secondChild - 1)))
  2245.         secondChild--;
  2246.       *(first + holeIndex) = *(first + secondChild);
  2247.       holeIndex = secondChild;
  2248.       secondChild = 2 * (secondChild + 1);
  2249.     }
  2250.     if (secondChild == len)
  2251.     {
  2252.       *(first + holeIndex) = *(first + (secondChild - 1));
  2253.       holeIndex = secondChild - 1;
  2254.     }
  2255.     __push_heap(first, holeIndex, topIndex, value);
  2256.   }
  2257.  
  2258.   template <class RandomAccessIterator, class Distance, class T, class Compare>
  2259.   void __adjust_heap (RandomAccessIterator first, Distance holeIndex,
  2260.                       Distance len, T value, Compare _RWSTD_COMP)
  2261.   {
  2262.     Distance topIndex = holeIndex;
  2263.     Distance secondChild = 2 * holeIndex + 2;
  2264.     while (secondChild < len)
  2265.     {
  2266.       if (_RWSTD_COMP(*(first + secondChild), *(first + (secondChild - 1))))
  2267.         secondChild--;
  2268.       *(first + holeIndex) = *(first + secondChild);
  2269.       holeIndex = secondChild;
  2270.       secondChild = 2 * (secondChild + 1);
  2271.     }
  2272.     if (secondChild == len)
  2273.     {
  2274.       *(first + holeIndex) = *(first + (secondChild - 1));
  2275.       holeIndex = secondChild - 1;
  2276.     }
  2277.     __push_heap(first, holeIndex, topIndex, value, _RWSTD_COMP);
  2278.   }
  2279.  
  2280.   template <class RandomAccessIterator, class T, class Distance>
  2281.   void __make_heap (RandomAccessIterator first, RandomAccessIterator last, T*,
  2282.                     Distance*)
  2283.   {
  2284.     Distance len = last - first;
  2285.     Distance parent = (len - 2)/2;
  2286.     while (true)
  2287.     {
  2288.       __adjust_heap(first, parent, len, T(*(first + parent)));
  2289.       if (parent == 0) return;
  2290.       parent--;
  2291.     }
  2292.   }
  2293.  
  2294.   template <class RandomAccessIterator, class Compare, class T, class Distance>
  2295.   void __make_heap (RandomAccessIterator first, RandomAccessIterator last,
  2296.                     Compare _RWSTD_COMP, T*, Distance*)
  2297.   {
  2298.     Distance len = last - first;
  2299.     Distance parent = (len - 2)/2;
  2300.     while (true)
  2301.     {
  2302.       __adjust_heap(first, parent, len, T(*(first + parent)), _RWSTD_COMP);
  2303.       if (parent == 0)
  2304.         return;
  2305.       parent--;
  2306.     }
  2307.   }
  2308.  
  2309.   template <class RandomAccessIterator>
  2310.   void sort_heap (RandomAccessIterator first, RandomAccessIterator last)
  2311.   {
  2312.     while (last - first > 1) pop_heap(first, last--);
  2313.   }
  2314.  
  2315.   template <class RandomAccessIterator, class Compare>
  2316.   void sort_heap (RandomAccessIterator first, RandomAccessIterator last,
  2317.                   Compare _RWSTD_COMP)
  2318.   {
  2319.     while (last - first > 1) pop_heap(first, last--, _RWSTD_COMP);
  2320.   }
  2321.  
  2322. //
  2323. // Minimum and maximum.
  2324. //
  2325.  
  2326.   template <class ForwardIterator>
  2327.   ForwardIterator min_element (ForwardIterator first, ForwardIterator last)
  2328.   {
  2329.     if (first == last) return first;
  2330.     ForwardIterator result = first;
  2331.     while (++first != last) 
  2332.       if (*first < *result) result = first;
  2333.     return result;
  2334.   }
  2335.  
  2336.   template <class ForwardIterator, class Compare>
  2337.   ForwardIterator min_element (ForwardIterator first, ForwardIterator last,
  2338.                                Compare _RWSTD_COMP)
  2339.   {
  2340.     if (first == last) return first;
  2341.     ForwardIterator result = first;
  2342.     while (++first != last) 
  2343.       if (_RWSTD_COMP(*first, *result)) result = first;
  2344.     return result;
  2345.   }
  2346.  
  2347.   template <class ForwardIterator>
  2348.   ForwardIterator max_element (ForwardIterator first, ForwardIterator last)
  2349.   {
  2350.     if (first == last) return first;
  2351.     ForwardIterator result = first;
  2352.     while (++first != last) 
  2353.       if (*result < *first) result = first;
  2354.     return result;
  2355.   }
  2356.  
  2357.   template <class ForwardIterator, class Compare>
  2358.   ForwardIterator max_element (ForwardIterator first, ForwardIterator last,
  2359.                                Compare _RWSTD_COMP)
  2360.   {
  2361.     if (first == last) return first;
  2362.     ForwardIterator result = first;
  2363.     while (++first != last) 
  2364.       if (_RWSTD_COMP(*result, *first)) result = first;
  2365.     return result;
  2366.   }
  2367.  
  2368.   template <class InputIterator1, class InputIterator2>
  2369.   bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,
  2370.                                 InputIterator2 first2, InputIterator2 last2)
  2371.   {
  2372.     while (first1 != last1 && first2 != last2)
  2373.     {
  2374.       if (*first1 < *first2)     return true;
  2375.       if (*first2++ < *first1++) return false;
  2376.     }
  2377.     return first1 == last1 && first2 != last2;
  2378.   }
  2379.  
  2380.   template <class InputIterator1, class InputIterator2, class Compare>
  2381.   bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
  2382.                                InputIterator2 first2, InputIterator2 last2,
  2383.                                Compare _RWSTD_COMP)
  2384.   {
  2385.     while (first1 != last1 && first2 != last2)
  2386.     {
  2387.       if (_RWSTD_COMP(*first1, *first2))     return true;
  2388.       if (_RWSTD_COMP(*first2++, *first1++)) return false;
  2389.     }
  2390.     return first1 == last1 && first2 != last2;
  2391.   }
  2392.  
  2393. //
  2394. // Permutations.
  2395. //
  2396.  
  2397.   template <class BidirectionalIterator>
  2398.   bool next_permutation (BidirectionalIterator first,
  2399.                          BidirectionalIterator last)
  2400.   {
  2401.     if (first == last) return false;
  2402.     BidirectionalIterator i = first;
  2403.     ++i;
  2404.     if (i == last) return false;
  2405.     i = last;
  2406.     --i;
  2407.  
  2408.     for (;;)
  2409.     {
  2410.       BidirectionalIterator ii = i--;
  2411.       if (*i < *ii)
  2412.       {
  2413.         BidirectionalIterator j = last;
  2414.         while (!(*i < *--j))
  2415.           ;
  2416.         iter_swap(i, j);
  2417.         reverse(ii, last);
  2418.         return true;
  2419.       }
  2420.       if (i == first)
  2421.       {
  2422.         reverse(first, last);
  2423.         return false;
  2424.       }
  2425.     }
  2426.   }
  2427.  
  2428.   template <class BidirectionalIterator, class Compare>
  2429.   bool next_permutation (BidirectionalIterator first, BidirectionalIterator last,
  2430.                          Compare _RWSTD_COMP)
  2431.   {
  2432.     if (first == last) return false;
  2433.     BidirectionalIterator i = first;
  2434.     ++i;
  2435.     if (i == last) return false;
  2436.     i = last;
  2437.     --i;
  2438.  
  2439.     for (;;)
  2440.     {
  2441.       BidirectionalIterator ii = i--;
  2442.       if (_RWSTD_COMP(*i, *ii))
  2443.       {
  2444.         BidirectionalIterator j = last;
  2445.         while (!_RWSTD_COMP(*i, *--j))
  2446.           ;
  2447.         iter_swap(i, j);
  2448.         reverse(ii, last);
  2449.         return true;
  2450.       }
  2451.       if (i == first)
  2452.       {
  2453.         reverse(first, last);
  2454.         return false;
  2455.       }
  2456.     }
  2457.   }
  2458.  
  2459.   template <class BidirectionalIterator>
  2460.   bool prev_permutation (BidirectionalIterator first,
  2461.                          BidirectionalIterator last)
  2462.   {
  2463.     if (first == last) return false;
  2464.     BidirectionalIterator i = first;
  2465.     ++i;
  2466.     if (i == last) return false;
  2467.     i = last;
  2468.     --i;
  2469.  
  2470.     for (;;)
  2471.     {
  2472.       BidirectionalIterator ii = i--;
  2473.       if (*ii < *i)
  2474.       {
  2475.         BidirectionalIterator j = last;
  2476.         while (!(*--j < *i))
  2477.           ;
  2478.         iter_swap(i, j);
  2479.         reverse(ii, last);
  2480.         return true;
  2481.       }
  2482.       if (i == first)
  2483.       {
  2484.         reverse(first, last);
  2485.         return false;
  2486.       }
  2487.     }
  2488.   }
  2489.  
  2490.   template <class BidirectionalIterator, class Compare>
  2491.   bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last,
  2492.                          Compare _RWSTD_COMP)
  2493.   {
  2494.     if (first == last) return false;
  2495.     BidirectionalIterator i = first;
  2496.     ++i;
  2497.     if (i == last) return false;
  2498.     i = last;
  2499.     --i;
  2500.  
  2501.     for(;;)
  2502.     {
  2503.       BidirectionalIterator ii = i--;
  2504.       if (_RWSTD_COMP(*ii, *i))
  2505.       {
  2506.         BidirectionalIterator j = last;
  2507.         while (!_RWSTD_COMP(*--j, *i))
  2508.           ;
  2509.         iter_swap(i, j);
  2510.         reverse(ii, last);
  2511.         return true;
  2512.       }
  2513.       if (i == first)
  2514.       {
  2515.         reverse(first, last);
  2516.         return false;
  2517.       }
  2518.     }
  2519.   }
  2520.  
  2521. #ifndef _RWSTD_NO_NAMESPACE
  2522. }
  2523. #endif
  2524.  
  2525. #pragma option pop
  2526. #endif /* __ALGORITH_CC */
  2527.