head	1.1;
branch	1.1.1;
access;
symbols
	RELENG_8_4:1.1.1.4.0.30
	RELENG_9_1_0_RELEASE:1.1.1.4
	RELENG_9_1:1.1.1.4.0.28
	RELENG_9_1_BP:1.1.1.4
	RELENG_8_3_0_RELEASE:1.1.1.4
	RELENG_8_3:1.1.1.4.0.26
	RELENG_8_3_BP:1.1.1.4
	RELENG_9_0_0_RELEASE:1.1.1.4
	RELENG_9_0:1.1.1.4.0.24
	RELENG_9_0_BP:1.1.1.4
	RELENG_9:1.1.1.4.0.22
	RELENG_9_BP:1.1.1.4
	RELENG_7_4_0_RELEASE:1.1.1.4
	RELENG_8_2_0_RELEASE:1.1.1.4
	RELENG_7_4:1.1.1.4.0.20
	RELENG_7_4_BP:1.1.1.4
	RELENG_8_2:1.1.1.4.0.18
	RELENG_8_2_BP:1.1.1.4
	RELENG_8_1_0_RELEASE:1.1.1.4
	RELENG_8_1:1.1.1.4.0.16
	RELENG_8_1_BP:1.1.1.4
	RELENG_7_3_0_RELEASE:1.1.1.4
	RELENG_7_3:1.1.1.4.0.14
	RELENG_7_3_BP:1.1.1.4
	RELENG_8_0_0_RELEASE:1.1.1.4
	RELENG_8_0:1.1.1.4.0.12
	RELENG_8_0_BP:1.1.1.4
	RELENG_8:1.1.1.4.0.10
	RELENG_8_BP:1.1.1.4
	RELENG_7_2_0_RELEASE:1.1.1.4
	RELENG_7_2:1.1.1.4.0.8
	RELENG_7_2_BP:1.1.1.4
	RELENG_7_1_0_RELEASE:1.1.1.4
	RELENG_6_4_0_RELEASE:1.1.1.3
	RELENG_7_1:1.1.1.4.0.6
	RELENG_7_1_BP:1.1.1.4
	RELENG_6_4:1.1.1.3.0.20
	RELENG_6_4_BP:1.1.1.3
	RELENG_7_0_0_RELEASE:1.1.1.4
	RELENG_6_3_0_RELEASE:1.1.1.3
	RELENG_7_0:1.1.1.4.0.4
	RELENG_7_0_BP:1.1.1.4
	RELENG_6_3:1.1.1.3.0.18
	RELENG_6_3_BP:1.1.1.3
	RELENG_7:1.1.1.4.0.2
	RELENG_7_BP:1.1.1.4
	gcc_4_2_1_20070718_SVN126787:1.1.1.4
	gcc_4_2_0_20070514_SVN124707:1.1.1.4
	RELENG_6_2_0_RELEASE:1.1.1.3
	RELENG_6_2:1.1.1.3.0.16
	RELENG_6_2_BP:1.1.1.3
	gcc_3_4_6_20060825_SVN116475:1.1.1.3
	RELENG_5_5_0_RELEASE:1.1.1.3
	RELENG_5_5:1.1.1.3.0.14
	RELENG_5_5_BP:1.1.1.3
	RELENG_6_1_0_RELEASE:1.1.1.3
	RELENG_6_1:1.1.1.3.0.12
	RELENG_6_1_BP:1.1.1.3
	RELENG_6_0_0_RELEASE:1.1.1.3
	RELENG_6_0:1.1.1.3.0.10
	RELENG_6_0_BP:1.1.1.3
	RELENG_6:1.1.1.3.0.8
	RELENG_6_BP:1.1.1.3
	gcc_3_4_4_20050518:1.1.1.3
	RELENG_5_4_0_RELEASE:1.1.1.3
	RELENG_5_4:1.1.1.3.0.6
	RELENG_5_4_BP:1.1.1.3
	RELENG_5_3_0_RELEASE:1.1.1.3
	RELENG_5_3:1.1.1.3.0.4
	RELENG_5_3_BP:1.1.1.3
	RELENG_5:1.1.1.3.0.2
	RELENG_5_BP:1.1.1.3
	gcc_3_4_2_20040728:1.1.1.3
	RELENG_5_2_1_RELEASE:1.1.1.2
	RELENG_5_2_0_RELEASE:1.1.1.2
	RELENG_5_2:1.1.1.2.0.6
	RELENG_5_2_BP:1.1.1.2
	gcc_3_3_3_20031106:1.1.1.2
	gcc_3_3_1:1.1.1.2
	gcc_3_3_1_20030711:1.1.1.2
	RELENG_5_1_0_RELEASE:1.1.1.2
	RELENG_5_1:1.1.1.2.0.4
	RELENG_5_1_BP:1.1.1.2
	gcc_3_2_2_20030205:1.1.1.2
	RELENG_5_0_0_RELEASE:1.1.1.2
	RELENG_5_0:1.1.1.2.0.2
	RELENG_5_0_BP:1.1.1.2
	gcc_3_2_1:1.1.1.2
	BEFORE_GCC_3_2_1:1.1.1.2
	gcc_3_2_anoncvs_20021009:1.1.1.2
	gcc_3_2_anoncvs_20020916:1.1.1.2
	gcc_3_2_anoncvs_20020901:1.1.1.2
	gcc_3_1_anoncvs_20020509:1.1.1.1
	FSF:1.1.1;
locks; strict;
comment	@# @;


1.1
date	2002.05.28.16.15.56;	author obrien;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	2002.05.28.16.15.56;	author obrien;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	2002.09.01.20.39.10;	author kan;	state Exp;
branches;
next	1.1.1.3;

1.1.1.3
date	2004.07.28.03.12.05;	author kan;	state Exp;
branches;
next	1.1.1.4;

1.1.1.4
date	2007.05.19.01.22.05;	author kan;	state Exp;
branches
	1.1.1.4.30.1;
next	;

1.1.1.4.30.1
date	2007.05.19.01.22.05;	author svnexp;	state dead;
branches;
next	1.1.1.4.30.2;

1.1.1.4.30.2
date	2013.03.28.13.01.35;	author svnexp;	state Exp;
branches;
next	;


desc
@@


1.1
log
@Initial revision
@
text
@// Algorithm extensions -*- C++ -*-

// Copyright (C) 2001, 2002 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

/*
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Hewlett-Packard Company makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 *
 * Copyright (c) 1996
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 */

/** @@file ext/algorithm
 *  This file is a GNU extension to the Standard C++ Library (possibly
 *  containing extensions from the HP/SGI STL subset).  You should only
 *  include this header if you are using GCC 3 or later.
 */

#ifndef _EXT_ALGORITHM
#define _EXT_ALGORITHM

#pragma GCC system_header
#include <algorithm>

namespace __gnu_cxx
{
  using std::ptrdiff_t;
  using std::min;
  using std::pair;
  using std::input_iterator_tag;
  using std::random_access_iterator_tag;
  using std::iterator_traits;

  //--------------------------------------------------
  // copy_n (not part of the C++ standard)

  template<typename _InputIter, typename _Size, typename _OutputIter>
    pair<_InputIter, _OutputIter>
    __copy_n(_InputIter __first, _Size __count,
	     _OutputIter __result,
	     input_iterator_tag)
    {
      for ( ; __count > 0; --__count) {
	*__result = *__first;
	++__first;
	++__result;
      }
      return pair<_InputIter, _OutputIter>(__first, __result);
    }

  template<typename _RAIter, typename _Size, typename _OutputIter>
    inline pair<_RAIter, _OutputIter>
    __copy_n(_RAIter __first, _Size __count,
	     _OutputIter __result,
	     random_access_iterator_tag)
    {
      _RAIter __last = __first + __count;
      return pair<_RAIter, _OutputIter>(__last,
					std::copy(__first, __last, __result));
    }

  /**
   *  @@brief Copies the range [first,first+count) into [result,result+count).
   *  @@param  first  An input iterator.
   *  @@param  count  The number of elements to copy.
   *  @@param  result An output iterator.
   *  @@return   A std::pair composed of first+count and result+count.
   *
   *  This is an SGI extension.
   *  This inline function will boil down to a call to @@c memmove whenever
   *  possible.  Failing that, if random access iterators are passed, then the
   *  loop count will be known (and therefore a candidate for compiler
   *  optimizations such as unrolling).
   *  @@ingroup SGIextensions
  */
  template<typename _InputIter, typename _Size, typename _OutputIter>
    inline pair<_InputIter, _OutputIter>
    copy_n(_InputIter __first, _Size __count, _OutputIter __result)
    {
      // concept requirements
      __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
      __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
	    typename iterator_traits<_InputIter>::value_type>)

      return __copy_n(__first, __count, __result,
		      std::__iterator_category(__first));
    }

  template<typename _InputIter1, typename _InputIter2>
    int
    __lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
				   _InputIter2 __first2, _InputIter2 __last2)
    {
      while (__first1 != __last1 && __first2 != __last2) {
	if (*__first1 < *__first2)
	  return -1;
	if (*__first2 < *__first1)
	  return 1;
	++__first1;
	++__first2;
      }
      if (__first2 == __last2) {
	return !(__first1 == __last1);
      }
      else {
	return -1;
      }
    }

  inline int
  __lexicographical_compare_3way(const unsigned char* __first1,
				 const unsigned char* __last1,
				 const unsigned char* __first2,
				 const unsigned char* __last2)
  {
    const ptrdiff_t __len1 = __last1 - __first1;
    const ptrdiff_t __len2 = __last2 - __first2;
    const int __result = std::memcmp(__first1, __first2, min(__len1, __len2));
    return __result != 0 ? __result 
			 : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1));
  }

  inline int 
  __lexicographical_compare_3way(const char* __first1, const char* __last1,
				 const char* __first2, const char* __last2)
  {
#if CHAR_MAX == SCHAR_MAX
    return __lexicographical_compare_3way(
				  (const signed char*) __first1,
				  (const signed char*) __last1,
				  (const signed char*) __first2,
				  (const signed char*) __last2);
#else
    return __lexicographical_compare_3way((const unsigned char*) __first1,
					  (const unsigned char*) __last1,
					  (const unsigned char*) __first2,
					  (const unsigned char*) __last2);
#endif
  }

  /**
   *  @@brief @@c memcmp on steroids.
   *  @@param  first1  An input iterator.
   *  @@param  last1   An input iterator.
   *  @@param  first2  An input iterator.
   *  @@param  last2   An input iterator.
   *  @@return   An int, as with @@c memcmp.
   *
   *  The return value will be less than zero if the first range is
   *  "lexigraphically less than" the second, greater than zero if the second
   *  range is "lexigraphically less than" the first, and zero otherwise.
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
  */
  template<typename _InputIter1, typename _InputIter2>
    int
    lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
				 _InputIter2 __first2, _InputIter2 __last2)
    {
      // concept requirements
      __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>)
      __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>)
      __glibcpp_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_InputIter1>::value_type>)
      __glibcpp_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_InputIter2>::value_type>)

      return __lexicographical_compare_3way(__first1, __last1, __first2, __last2);
    }

  // count and count_if: this version, whose return type is void, was present
  // in the HP STL, and is retained as an extension for backward compatibility.

  template<typename _InputIter, typename _Tp, typename _Size>
    void
    count(_InputIter __first, _InputIter __last,
	  const _Tp& __value,
	  _Size& __n)
    {
      // concept requirements
      __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
      __glibcpp_function_requires(_EqualityComparableConcept<
	    typename iterator_traits<_InputIter>::value_type >)
      __glibcpp_function_requires(_EqualityComparableConcept<_Tp>)
      for ( ; __first != __last; ++__first)
	if (*__first == __value)
	  ++__n;
    }

  template<typename _InputIter, typename _Predicate, typename _Size>
    void
    count_if(_InputIter __first, _InputIter __last,
	     _Predicate __pred,
	     _Size& __n)
    {
      // concept requirements
      __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
      __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate,
	    typename iterator_traits<_InputIter>::value_type>)
      for ( ; __first != __last; ++__first)
	if (__pred(*__first))
	  ++__n;
    }

  // random_sample and random_sample_n (extensions, not part of the standard).

  template<typename _ForwardIter, typename _OutputIter, typename _Distance>
    _OutputIter
    random_sample_n(_ForwardIter __first, _ForwardIter __last,
                    _OutputIter __out, const _Distance __n)
    {
      // concept requirements
      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
      __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
		typename iterator_traits<_ForwardIter>::value_type>)

      _Distance __remaining = std::distance(__first, __last);
      _Distance __m = min(__n, __remaining);

      while (__m > 0) {
	if (std::__random_number(__remaining) < __m) {
	      *__out = *__first;
	      ++__out;
	      --__m;
	}

	--__remaining;
	++__first;
      }
      return __out;
    }

  template<typename _ForwardIter, typename _OutputIter, typename _Distance,
	   typename _RandomNumberGenerator>
    _OutputIter
    random_sample_n(_ForwardIter __first, _ForwardIter __last,
                   _OutputIter __out, const _Distance __n, 
		   _RandomNumberGenerator& __rand)
    {
      // concept requirements
      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
      __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
		typename iterator_traits<_ForwardIter>::value_type>)
      __glibcpp_function_requires(_UnaryFunctionConcept<
		_RandomNumberGenerator, _Distance, _Distance>)

      _Distance __remaining = std::distance(__first, __last);
      _Distance __m = min(__n, __remaining);

      while (__m > 0) {
	if (__rand(__remaining) < __m) {
	      *__out = *__first;
	      ++__out;
	      --__m;
	}

	--__remaining;
	++__first;
      }
      return __out;
    }

  template<typename _InputIter, typename _RandomAccessIter, typename _Distance>
    _RandomAccessIter
    __random_sample(_InputIter __first, _InputIter __last,
		    _RandomAccessIter __out,
		    const _Distance __n)
    {
      _Distance __m = 0;
      _Distance __t = __n;
      for ( ; __first != __last && __m < __n; ++__m, ++__first) 
	__out[__m] = *__first;

      while (__first != __last) {
	++__t;
	_Distance __M = std::__random_number(__t);
	if (__M < __n)
	  __out[__M] = *__first;
	++__first;
      }

      return __out + __m;
    }

  template<typename _InputIter, typename _RandomAccessIter,
	   typename _RandomNumberGenerator, typename _Distance>
    _RandomAccessIter
    __random_sample(_InputIter __first, _InputIter __last,
		    _RandomAccessIter __out,
		    _RandomNumberGenerator& __rand,
		    const _Distance __n)
    {
      // concept requirements
      __glibcpp_function_requires(_UnaryFunctionConcept<
	    _RandomNumberGenerator, _Distance, _Distance>)

      _Distance __m = 0;
      _Distance __t = __n;
      for ( ; __first != __last && __m < __n; ++__m, ++__first)
	__out[__m] = *__first;

      while (__first != __last) {
	++__t;
	_Distance __M = __rand(__t);
	if (__M < __n)
	  __out[__M] = *__first;
	++__first;
      }

      return __out + __m;
    }

  template<typename _InputIter, typename _RandomAccessIter>
    inline _RandomAccessIter
    random_sample(_InputIter __first, _InputIter __last,
		  _RandomAccessIter __out_first, _RandomAccessIter __out_last) 
    {
      // concept requirements
      __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
      __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
	    _RandomAccessIter>)

      return __random_sample(__first, __last,
			     __out_first, __out_last - __out_first);
    }

  template<typename _InputIter, typename _RandomAccessIter, 
	   typename _RandomNumberGenerator>
    inline _RandomAccessIter
    random_sample(_InputIter __first, _InputIter __last,
		  _RandomAccessIter __out_first, _RandomAccessIter __out_last,
		  _RandomNumberGenerator& __rand) 
    {
      // concept requirements
      __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
      __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
	    _RandomAccessIter>)

      return __random_sample(__first, __last,
			     __out_first, __rand,
			     __out_last - __out_first);
    }
  
  // is_heap, a predicate testing whether or not a range is
  // a heap.  This function is an extension, not part of the C++
  // standard.

  template<typename _RandomAccessIter, typename _Distance>
    bool
    __is_heap(_RandomAccessIter __first, _Distance __n)
    {
      _Distance __parent = 0;
      for (_Distance __child = 1; __child < __n; ++__child) {
	if (__first[__parent] < __first[__child]) 
	  return false;
	if ((__child & 1) == 0)
	  ++__parent;
      }
      return true;
    }

  template<typename _RandomAccessIter, typename _Distance,
           typename _StrictWeakOrdering>
    bool
    __is_heap(_RandomAccessIter __first, _StrictWeakOrdering __comp,
	      _Distance __n)
    {
      _Distance __parent = 0;
      for (_Distance __child = 1; __child < __n; ++__child) {
	if (__comp(__first[__parent], __first[__child]))
	  return false;
	if ((__child & 1) == 0)
	  ++__parent;
      }
      return true;
    }

  template<typename _RandomAccessIter>
    inline bool
    is_heap(_RandomAccessIter __first, _RandomAccessIter __last)
    {
      // concept requirements
      __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIter>)
      __glibcpp_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_RandomAccessIter>::value_type>)

      return __is_heap(__first, __last - __first);
    }

  template<typename _RandomAccessIter, typename _StrictWeakOrdering>
    inline bool
    is_heap(_RandomAccessIter __first, _RandomAccessIter __last,
	    _StrictWeakOrdering __comp)
    {
      // concept requirements
      __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIter>)
      __glibcpp_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
	    typename iterator_traits<_RandomAccessIter>::value_type, 
	    typename iterator_traits<_RandomAccessIter>::value_type>)

      return __is_heap(__first, __comp, __last - __first);
    }

  // is_sorted, a predicated testing whether a range is sorted in
  // nondescending order.  This is an extension, not part of the C++
  // standard.

  template<typename _ForwardIter>
    bool
    is_sorted(_ForwardIter __first, _ForwardIter __last)
    {
      // concept requirements
      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
      __glibcpp_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_ForwardIter>::value_type>)

      if (__first == __last)
	return true;

      _ForwardIter __next = __first;
      for (++__next; __next != __last; __first = __next, ++__next) {
	if (*__next < *__first)
	  return false;
      }

      return true;
    }

  template<typename _ForwardIter, typename _StrictWeakOrdering>
    bool
    is_sorted(_ForwardIter __first, _ForwardIter __last, _StrictWeakOrdering __comp)
    {
      // concept requirements
      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
      __glibcpp_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
	    typename iterator_traits<_ForwardIter>::value_type, 
	    typename iterator_traits<_ForwardIter>::value_type>)

      if (__first == __last)
	return true;

      _ForwardIter __next = __first;
      for (++__next; __next != __last; __first = __next, ++__next) {
	if (__comp(*__next, *__first))
	  return false;
      }

      return true;
    }

} // namespace __gnu_cxx

#endif /* _EXT_ALGORITHM */
@


1.1.1.1
log
@Gcc 3.1.0 pre-release's C++ support bits from the FSF anoncvs repo
on 9-May-2002 15:57:15 EDT.
@
text
@@


1.1.1.2
log
@Gcc 3.2.1-prerelease libf2c bits from the FSF anoncvs repo gcc-3_2-branch on 1-Sep-2002 00:00:01 EDT.
@
text
@a249 5
  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
a275 5
  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
a355 5
  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
a369 5
  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
a420 5
  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
a432 5
  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
a450 5
  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
a471 5
  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
@


1.1.1.3
log
@Gcc 3.4.2 20040728 C++ support bits.
@
text
@d63 1
a63 1
#define _EXT_ALGORITHM 1
a65 1

d80 4
a83 4
  template<typename _InputIterator, typename _Size, typename _OutputIterator>
    pair<_InputIterator, _OutputIterator>
    __copy_n(_InputIterator __first, _Size __count,
	     _OutputIterator __result,
d91 1
a91 1
      return pair<_InputIterator, _OutputIterator>(__first, __result);
d94 4
a97 4
  template<typename _RAIterator, typename _Size, typename _OutputIterator>
    inline pair<_RAIterator, _OutputIterator>
    __copy_n(_RAIterator __first, _Size __count,
	     _OutputIterator __result,
d100 2
a101 2
      _RAIterator __last = __first + __count;
      return pair<_RAIterator, _OutputIterator>(__last,
d119 3
a121 3
  template<typename _InputIterator, typename _Size, typename _OutputIterator>
    inline pair<_InputIterator, _OutputIterator>
    copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
d124 3
a126 3
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
	    typename iterator_traits<_InputIterator>::value_type>)
d132 1
a132 1
  template<typename _InputIterator1, typename _InputIterator2>
d134 2
a135 2
    __lexicographical_compare_3way(_InputIterator1 __first1, _InputIterator1 __last1,
				   _InputIterator2 __first2, _InputIterator2 __last2)
d162 1
a162 1
    return __result != 0 ? __result
d166 1
a166 1
  inline int
d198 1
a198 1
  template<typename _InputIterator1, typename _InputIterator2>
d200 2
a201 2
    lexicographical_compare_3way(_InputIterator1 __first1, _InputIterator1 __last1,
				 _InputIterator2 __first2, _InputIterator2 __last2)
d204 6
a209 8
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
      __glibcxx_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_InputIterator1>::value_type>)
      __glibcxx_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_InputIterator2>::value_type>)
      __glibcxx_requires_valid_range(__first1, __last1);
      __glibcxx_requires_valid_range(__first2, __last2);
d217 1
a217 1
  template<typename _InputIterator, typename _Tp, typename _Size>
d219 1
a219 1
    count(_InputIterator __first, _InputIterator __last,
d224 4
a227 6
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_EqualityComparableConcept<
	    typename iterator_traits<_InputIterator>::value_type >)
      __glibcxx_function_requires(_EqualityComparableConcept<_Tp>)
      __glibcxx_requires_valid_range(__first, __last);

d233 1
a233 1
  template<typename _InputIterator, typename _Predicate, typename _Size>
d235 1
a235 1
    count_if(_InputIterator __first, _InputIterator __last,
d240 3
a242 5
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
	    typename iterator_traits<_InputIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);

d255 4
a258 4
  template<typename _ForwardIterator, typename _OutputIterator, typename _Distance>
    _OutputIterator
    random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
                    _OutputIterator __out, const _Distance __n)
d261 3
a263 4
      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
		typename iterator_traits<_ForwardIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);
d269 1
a269 1
	if ((std::rand() % __remaining) < __m) {
d286 1
a286 1
  template<typename _ForwardIterator, typename _OutputIterator, typename _Distance,
d288 3
a290 3
    _OutputIterator
    random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
                   _OutputIterator __out, const _Distance __n,
d294 4
a297 4
      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
		typename iterator_traits<_ForwardIterator>::value_type>)
      __glibcxx_function_requires(_UnaryFunctionConcept<
a298 1
      __glibcxx_requires_valid_range(__first, __last);
d316 4
a319 4
  template<typename _InputIterator, typename _RandomAccessIterator, typename _Distance>
    _RandomAccessIterator
    __random_sample(_InputIterator __first, _InputIterator __last,
		    _RandomAccessIterator __out,
d324 1
a324 1
      for ( ; __first != __last && __m < __n; ++__m, ++__first)
d329 1
a329 1
	_Distance __M = std::rand() % (__t);
d338 1
a338 1
  template<typename _InputIterator, typename _RandomAccessIterator,
d340 3
a342 3
    _RandomAccessIterator
    __random_sample(_InputIterator __first, _InputIterator __last,
		    _RandomAccessIterator __out,
d347 1
a347 1
      __glibcxx_function_requires(_UnaryFunctionConcept<
d371 4
a374 4
  template<typename _InputIterator, typename _RandomAccessIterator>
    inline _RandomAccessIterator
    random_sample(_InputIterator __first, _InputIterator __last,
		  _RandomAccessIterator __out_first, _RandomAccessIterator __out_last)
d377 3
a379 5
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
	    _RandomAccessIterator>)
      __glibcxx_requires_valid_range(__first, __last);
      __glibcxx_requires_valid_range(__out_first, __out_last);
d390 1
a390 1
  template<typename _InputIterator, typename _RandomAccessIterator,
d392 4
a395 4
    inline _RandomAccessIterator
    random_sample(_InputIterator __first, _InputIterator __last,
		  _RandomAccessIterator __out_first, _RandomAccessIterator __out_last,
		  _RandomNumberGenerator& __rand)
d398 3
a400 5
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
	    _RandomAccessIterator>)
      __glibcxx_requires_valid_range(__first, __last);
      __glibcxx_requires_valid_range(__out_first, __out_last);
d406 34
d446 1
a446 1
  template<typename _RandomAccessIterator>
d448 1
a448 1
    is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
d451 3
a453 4
      __glibcxx_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
      __glibcxx_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_RandomAccessIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);
d455 1
a455 1
      return std::__is_heap(__first, __last - __first);
d463 1
a463 1
  template<typename _RandomAccessIterator, typename _StrictWeakOrdering>
d465 1
a465 1
    is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
d469 4
a472 5
      __glibcxx_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
      __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
	    typename iterator_traits<_RandomAccessIterator>::value_type,
	    typename iterator_traits<_RandomAccessIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);
d474 1
a474 1
      return std::__is_heap(__first, __comp, __last - __first);
d486 1
a486 1
  template<typename _ForwardIterator>
d488 1
a488 1
    is_sorted(_ForwardIterator __first, _ForwardIterator __last)
d491 3
a493 4
      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
      __glibcxx_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_ForwardIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);
d498 1
a498 1
      _ForwardIterator __next = __first;
d512 1
a512 1
  template<typename _ForwardIterator, typename _StrictWeakOrdering>
d514 1
a514 1
    is_sorted(_ForwardIterator __first, _ForwardIterator __last, _StrictWeakOrdering __comp)
d517 4
a520 5
      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
      __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
	    typename iterator_traits<_ForwardIterator>::value_type,
	    typename iterator_traits<_ForwardIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);
d525 1
a525 1
      _ForwardIterator __next = __first;
d533 1
@


1.1.1.4
log
@GCC 4.2.0 release C++ standard library and runtime support code.
@
text
@d3 1
a3 1
// Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
d18 1
a18 1
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
d58 2
a59 1
 *  containing extensions from the HP/SGI STL subset).
d69 2
a70 2
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)

d87 5
a91 6
      for ( ; __count > 0; --__count)
	{
	  *__result = *__first;
	  ++__first;
	  ++__result;
	}
d102 2
a103 3
      return pair<_RAIterator, _OutputIterator>(__last, std::copy(__first,
								  __last,
								  __result));
d135 12
a146 15
    __lexicographical_compare_3way(_InputIterator1 __first1,
				   _InputIterator1 __last1,
				   _InputIterator2 __first2,
				   _InputIterator2 __last2)
    {
      while (__first1 != __last1 && __first2 != __last2)
	{
	  if (*__first1 < *__first2)
	    return -1;
	  if (*__first2 < *__first1)
	    return 1;
	  ++__first1;
	  ++__first2;
	}
      if (__first2 == __last2)
d148 2
a149 1
      else
d151 1
d172 5
a176 4
    return __lexicographical_compare_3way((const signed char*) __first1,
					  (const signed char*) __last1,
					  (const signed char*) __first2,
					  (const signed char*) __last2);
d201 2
a202 4
    lexicographical_compare_3way(_InputIterator1 __first1,
				 _InputIterator1 __last1,
				 _InputIterator2 __first2,
				 _InputIterator2 __last2)
d214 1
a214 2
      return __lexicographical_compare_3way(__first1, __last1, __first2,
					    __last2);
d219 1
d262 1
a262 2
  template<typename _ForwardIterator, typename _OutputIterator,
	   typename _Distance>
d276 2
a277 4
      while (__m > 0)
	{
	  if ((std::rand() % __remaining) < __m)
	    {
a280 3
	    }
	  --__remaining;
	  ++__first;
d282 4
d294 2
a295 2
  template<typename _ForwardIterator, typename _OutputIterator,
	   typename _Distance, typename _RandomNumberGenerator>
d312 2
a313 4
      while (__m > 0)
	{
	  if (__rand(__remaining) < __m)
	    {
a316 3
	    }
	  --__remaining;
	  ++__first;
d318 4
d325 1
a325 2
  template<typename _InputIterator, typename _RandomAccessIterator,
	   typename _Distance>
d336 8
a343 8
      while (__first != __last)
	{
	  ++__t;
	  _Distance __M = std::rand() % (__t);
	  if (__M < __n)
	    __out[__M] = *__first;
	  ++__first;
	}
d364 8
a371 8
      while (__first != __last)
	{
	  ++__t;
	  _Distance __M = __rand(__t);
	  if (__M < __n)
	    __out[__M] = *__first;
	  ++__first;
	}
d383 1
a383 2
		  _RandomAccessIterator __out_first,
		  _RandomAccessIterator __out_last)
d405 1
a405 2
		  _RandomAccessIterator __out_first,
		  _RandomAccessIterator __out_last,
d430 1
a430 2
      __glibcxx_function_requires(_RandomAccessIteratorConcept<
				  _RandomAccessIterator>)
d449 1
a449 2
      __glibcxx_function_requires(_RandomAccessIteratorConcept<
				  _RandomAccessIterator>)
d481 1
a481 1
      for (++__next; __next != __last; __first = __next, ++__next)
d484 2
d496 1
a496 2
    is_sorted(_ForwardIterator __first, _ForwardIterator __last,
	      _StrictWeakOrdering __comp)
d509 1
a509 1
      for (++__next; __next != __last; __first = __next, ++__next)
d512 2
d516 1
a516 2

_GLIBCXX_END_NAMESPACE
@


1.1.1.4.30.1
log
@file algorithm was added on branch RELENG_8_4 on 2013-03-28 13:01:35 +0000
@
text
@d1 527
@


1.1.1.4.30.2
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/248810
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@a0 527
// Algorithm extensions -*- C++ -*-

// Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

/*
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Hewlett-Packard Company makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 *
 * Copyright (c) 1996
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 */

/** @@file ext/algorithm
 *  This file is a GNU extension to the Standard C++ Library (possibly
 *  containing extensions from the HP/SGI STL subset).
 */

#ifndef _EXT_ALGORITHM
#define _EXT_ALGORITHM 1

#pragma GCC system_header

#include <algorithm>

_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)

  using std::ptrdiff_t;
  using std::min;
  using std::pair;
  using std::input_iterator_tag;
  using std::random_access_iterator_tag;
  using std::iterator_traits;

  //--------------------------------------------------
  // copy_n (not part of the C++ standard)

  template<typename _InputIterator, typename _Size, typename _OutputIterator>
    pair<_InputIterator, _OutputIterator>
    __copy_n(_InputIterator __first, _Size __count,
	     _OutputIterator __result,
	     input_iterator_tag)
    {
      for ( ; __count > 0; --__count)
	{
	  *__result = *__first;
	  ++__first;
	  ++__result;
	}
      return pair<_InputIterator, _OutputIterator>(__first, __result);
    }

  template<typename _RAIterator, typename _Size, typename _OutputIterator>
    inline pair<_RAIterator, _OutputIterator>
    __copy_n(_RAIterator __first, _Size __count,
	     _OutputIterator __result,
	     random_access_iterator_tag)
    {
      _RAIterator __last = __first + __count;
      return pair<_RAIterator, _OutputIterator>(__last, std::copy(__first,
								  __last,
								  __result));
    }

  /**
   *  @@brief Copies the range [first,first+count) into [result,result+count).
   *  @@param  first  An input iterator.
   *  @@param  count  The number of elements to copy.
   *  @@param  result An output iterator.
   *  @@return   A std::pair composed of first+count and result+count.
   *
   *  This is an SGI extension.
   *  This inline function will boil down to a call to @@c memmove whenever
   *  possible.  Failing that, if random access iterators are passed, then the
   *  loop count will be known (and therefore a candidate for compiler
   *  optimizations such as unrolling).
   *  @@ingroup SGIextensions
  */
  template<typename _InputIterator, typename _Size, typename _OutputIterator>
    inline pair<_InputIterator, _OutputIterator>
    copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
    {
      // concept requirements
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
	    typename iterator_traits<_InputIterator>::value_type>)

      return __copy_n(__first, __count, __result,
		      std::__iterator_category(__first));
    }

  template<typename _InputIterator1, typename _InputIterator2>
    int
    __lexicographical_compare_3way(_InputIterator1 __first1,
				   _InputIterator1 __last1,
				   _InputIterator2 __first2,
				   _InputIterator2 __last2)
    {
      while (__first1 != __last1 && __first2 != __last2)
	{
	  if (*__first1 < *__first2)
	    return -1;
	  if (*__first2 < *__first1)
	    return 1;
	  ++__first1;
	  ++__first2;
	}
      if (__first2 == __last2)
	return !(__first1 == __last1);
      else
	return -1;
    }

  inline int
  __lexicographical_compare_3way(const unsigned char* __first1,
				 const unsigned char* __last1,
				 const unsigned char* __first2,
				 const unsigned char* __last2)
  {
    const ptrdiff_t __len1 = __last1 - __first1;
    const ptrdiff_t __len2 = __last2 - __first2;
    const int __result = std::memcmp(__first1, __first2, min(__len1, __len2));
    return __result != 0 ? __result
			 : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1));
  }

  inline int
  __lexicographical_compare_3way(const char* __first1, const char* __last1,
				 const char* __first2, const char* __last2)
  {
#if CHAR_MAX == SCHAR_MAX
    return __lexicographical_compare_3way((const signed char*) __first1,
					  (const signed char*) __last1,
					  (const signed char*) __first2,
					  (const signed char*) __last2);
#else
    return __lexicographical_compare_3way((const unsigned char*) __first1,
					  (const unsigned char*) __last1,
					  (const unsigned char*) __first2,
					  (const unsigned char*) __last2);
#endif
  }

  /**
   *  @@brief @@c memcmp on steroids.
   *  @@param  first1  An input iterator.
   *  @@param  last1   An input iterator.
   *  @@param  first2  An input iterator.
   *  @@param  last2   An input iterator.
   *  @@return   An int, as with @@c memcmp.
   *
   *  The return value will be less than zero if the first range is
   *  "lexigraphically less than" the second, greater than zero if the second
   *  range is "lexigraphically less than" the first, and zero otherwise.
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
  */
  template<typename _InputIterator1, typename _InputIterator2>
    int
    lexicographical_compare_3way(_InputIterator1 __first1,
				 _InputIterator1 __last1,
				 _InputIterator2 __first2,
				 _InputIterator2 __last2)
    {
      // concept requirements
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
      __glibcxx_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_InputIterator1>::value_type>)
      __glibcxx_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_InputIterator2>::value_type>)
      __glibcxx_requires_valid_range(__first1, __last1);
      __glibcxx_requires_valid_range(__first2, __last2);

      return __lexicographical_compare_3way(__first1, __last1, __first2,
					    __last2);
    }

  // count and count_if: this version, whose return type is void, was present
  // in the HP STL, and is retained as an extension for backward compatibility.
  template<typename _InputIterator, typename _Tp, typename _Size>
    void
    count(_InputIterator __first, _InputIterator __last,
	  const _Tp& __value,
	  _Size& __n)
    {
      // concept requirements
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_EqualityComparableConcept<
	    typename iterator_traits<_InputIterator>::value_type >)
      __glibcxx_function_requires(_EqualityComparableConcept<_Tp>)
      __glibcxx_requires_valid_range(__first, __last);

      for ( ; __first != __last; ++__first)
	if (*__first == __value)
	  ++__n;
    }

  template<typename _InputIterator, typename _Predicate, typename _Size>
    void
    count_if(_InputIterator __first, _InputIterator __last,
	     _Predicate __pred,
	     _Size& __n)
    {
      // concept requirements
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
	    typename iterator_traits<_InputIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);

      for ( ; __first != __last; ++__first)
	if (__pred(*__first))
	  ++__n;
    }

  // random_sample and random_sample_n (extensions, not part of the standard).

  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
  template<typename _ForwardIterator, typename _OutputIterator,
	   typename _Distance>
    _OutputIterator
    random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
                    _OutputIterator __out, const _Distance __n)
    {
      // concept requirements
      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
		typename iterator_traits<_ForwardIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);

      _Distance __remaining = std::distance(__first, __last);
      _Distance __m = min(__n, __remaining);

      while (__m > 0)
	{
	  if ((std::rand() % __remaining) < __m)
	    {
	      *__out = *__first;
	      ++__out;
	      --__m;
	    }
	  --__remaining;
	  ++__first;
	}
      return __out;
    }

  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
  template<typename _ForwardIterator, typename _OutputIterator,
	   typename _Distance, typename _RandomNumberGenerator>
    _OutputIterator
    random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
                   _OutputIterator __out, const _Distance __n,
		   _RandomNumberGenerator& __rand)
    {
      // concept requirements
      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
		typename iterator_traits<_ForwardIterator>::value_type>)
      __glibcxx_function_requires(_UnaryFunctionConcept<
		_RandomNumberGenerator, _Distance, _Distance>)
      __glibcxx_requires_valid_range(__first, __last);

      _Distance __remaining = std::distance(__first, __last);
      _Distance __m = min(__n, __remaining);

      while (__m > 0)
	{
	  if (__rand(__remaining) < __m)
	    {
	      *__out = *__first;
	      ++__out;
	      --__m;
	    }
	  --__remaining;
	  ++__first;
	}
      return __out;
    }

  template<typename _InputIterator, typename _RandomAccessIterator,
	   typename _Distance>
    _RandomAccessIterator
    __random_sample(_InputIterator __first, _InputIterator __last,
		    _RandomAccessIterator __out,
		    const _Distance __n)
    {
      _Distance __m = 0;
      _Distance __t = __n;
      for ( ; __first != __last && __m < __n; ++__m, ++__first)
	__out[__m] = *__first;

      while (__first != __last)
	{
	  ++__t;
	  _Distance __M = std::rand() % (__t);
	  if (__M < __n)
	    __out[__M] = *__first;
	  ++__first;
	}
      return __out + __m;
    }

  template<typename _InputIterator, typename _RandomAccessIterator,
	   typename _RandomNumberGenerator, typename _Distance>
    _RandomAccessIterator
    __random_sample(_InputIterator __first, _InputIterator __last,
		    _RandomAccessIterator __out,
		    _RandomNumberGenerator& __rand,
		    const _Distance __n)
    {
      // concept requirements
      __glibcxx_function_requires(_UnaryFunctionConcept<
	    _RandomNumberGenerator, _Distance, _Distance>)

      _Distance __m = 0;
      _Distance __t = __n;
      for ( ; __first != __last && __m < __n; ++__m, ++__first)
	__out[__m] = *__first;

      while (__first != __last)
	{
	  ++__t;
	  _Distance __M = __rand(__t);
	  if (__M < __n)
	    __out[__M] = *__first;
	  ++__first;
	}
      return __out + __m;
    }

  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
  template<typename _InputIterator, typename _RandomAccessIterator>
    inline _RandomAccessIterator
    random_sample(_InputIterator __first, _InputIterator __last,
		  _RandomAccessIterator __out_first,
		  _RandomAccessIterator __out_last)
    {
      // concept requirements
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
	    _RandomAccessIterator>)
      __glibcxx_requires_valid_range(__first, __last);
      __glibcxx_requires_valid_range(__out_first, __out_last);

      return __random_sample(__first, __last,
			     __out_first, __out_last - __out_first);
    }

  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
  template<typename _InputIterator, typename _RandomAccessIterator,
	   typename _RandomNumberGenerator>
    inline _RandomAccessIterator
    random_sample(_InputIterator __first, _InputIterator __last,
		  _RandomAccessIterator __out_first,
		  _RandomAccessIterator __out_last,
		  _RandomNumberGenerator& __rand)
    {
      // concept requirements
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
	    _RandomAccessIterator>)
      __glibcxx_requires_valid_range(__first, __last);
      __glibcxx_requires_valid_range(__out_first, __out_last);

      return __random_sample(__first, __last,
			     __out_first, __rand,
			     __out_last - __out_first);
    }

  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
  template<typename _RandomAccessIterator>
    inline bool
    is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
    {
      // concept requirements
      __glibcxx_function_requires(_RandomAccessIteratorConcept<
				  _RandomAccessIterator>)
      __glibcxx_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_RandomAccessIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);

      return std::__is_heap(__first, __last - __first);
    }

  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
  template<typename _RandomAccessIterator, typename _StrictWeakOrdering>
    inline bool
    is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
	    _StrictWeakOrdering __comp)
    {
      // concept requirements
      __glibcxx_function_requires(_RandomAccessIteratorConcept<
				  _RandomAccessIterator>)
      __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
	    typename iterator_traits<_RandomAccessIterator>::value_type,
	    typename iterator_traits<_RandomAccessIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);

      return std::__is_heap(__first, __comp, __last - __first);
    }

  // is_sorted, a predicated testing whether a range is sorted in
  // nondescending order.  This is an extension, not part of the C++
  // standard.

  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
  template<typename _ForwardIterator>
    bool
    is_sorted(_ForwardIterator __first, _ForwardIterator __last)
    {
      // concept requirements
      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
      __glibcxx_function_requires(_LessThanComparableConcept<
	    typename iterator_traits<_ForwardIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);

      if (__first == __last)
	return true;

      _ForwardIterator __next = __first;
      for (++__next; __next != __last; __first = __next, ++__next)
	if (*__next < *__first)
	  return false;
      return true;
    }

  /**
   *  This is an SGI extension.
   *  @@ingroup SGIextensions
   *  @@doctodo
  */
  template<typename _ForwardIterator, typename _StrictWeakOrdering>
    bool
    is_sorted(_ForwardIterator __first, _ForwardIterator __last,
	      _StrictWeakOrdering __comp)
    {
      // concept requirements
      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
      __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
	    typename iterator_traits<_ForwardIterator>::value_type,
	    typename iterator_traits<_ForwardIterator>::value_type>)
      __glibcxx_requires_valid_range(__first, __last);

      if (__first == __last)
	return true;

      _ForwardIterator __next = __first;
      for (++__next; __next != __last; __first = __next, ++__next)
	if (__comp(*__next, *__first))
	  return false;
      return true;
    }

_GLIBCXX_END_NAMESPACE

#endif /* _EXT_ALGORITHM */
@


