2001-12-29 02:46:54 +08:00
|
|
|
// Algorithm extensions -*- C++ -*-
|
|
|
|
|
Makefile.am (doxygen, [...]): Tweak targets.
2002-01-28 Phil Edwards <pme@gcc.gnu.org>
* Makefile.am (doxygen, doxygen-maint, doxygen-man): Tweak targets.
* Makefile.in: Regenerate.
* docs/doxygen/run_doxygen: Update, mostly for man pages.
* docs/doxygen/Intro.3: Update.
* docs/doxygen/TODO: Update.
* docs/doxygen/doxygroups.cc: Add namespace hook for __gnu_cxx.
* docs/doxygen/mainpage.doxy: Update.
* docs/doxygen/user.cfg.in: Update for header rename. Also
regenerate comments and variables with 1.2.12.
* docs/doxygen/maint.cfg.in: Remove file.
* include/bits/stl_relops.h: Doxygenate.
* include/bits/stl_tempbuf.h (std::_Temporary_buffer): Likewise.
* include/c_std/std_cassert.h, include/c_std/std_cctype.h,
include/c_std/std_cerrno.h, include/c_std/std_cfloat.h,
include/c_std/std_ciso646.h, include/c_std/std_climits.h,
include/c_std/std_clocale.h, include/c_std/std_cmath.h,
include/c_std/std_csetjmp.h, include/c_std/std_csignal.h,
include/c_std/std_cstdarg.h, include/c_std/std_cstddef.h,
include/c_std/std_cstdio.h, include/c_std/std_cstdlib.h,
include/c_std/std_cstring.h, include/c_std/std_ctime.h,
include/c_std/std_cwchar.h, include/c_std/std_cwctype.h,
include/ext/algorithm, include/ext/functional, include/ext/hash_map,
include/ext/hash_set, include/ext/iterator, include/ext/memory,
include/ext/numeric, include/ext/rb_tree, include/ext/rope,
include/ext/ropeimpl.h, include/ext/slist, include/ext/stl_hash_fun.h,
include/ext/stl_hashtable.h, include/ext/stl_rope.h,
include/std/std_algorithm.h, include/std/std_bitset.h,
include/std/std_complex.h, include/std/std_deque.h,
include/std/std_fstream.h, include/std/std_functional.h,
include/std/std_iomanip.h, include/std/std_ios.h,
include/std/std_iosfwd.h, include/std/std_iostream.h,
include/std/std_istream.h, include/std/std_iterator.h,
include/std/std_limits.h, include/std/std_list.h,
include/std/std_locale.h, include/std/std_map.h,
include/std/std_memory.h, include/std/std_numeric.h,
include/std/std_ostream.h, include/std/std_queue.h,
include/std/std_set.h, include/std/std_sstream.h,
include/std/std_stack.h, include/std/std_stdexcept.h,
include/std/std_streambuf.h, include/std/std_string.h,
include/std/std_utility.h, include/std/std_valarray.h,
include/std/std_vector.h: Add/correct @file doxygen hook.
* include/ext/memory: Doxygenate most of rest of file.
* libsupc++/exception: Doxygen output formatting.
* libsupc++/new: Say which header it is.
* testsuite/lib/libstdc++-v3-dg.exp: Fix spacing.
* docs/html/19_diagnostics/howto.html: Describe concept-checks switch.
* docs/html/23_containers/howto.html: Describe O(n) list::size().
* docs/html/27_io/howto.html: Also link to Langer and Kreft text.
From-SVN: r49300
2002-01-29 06:13:12 +08:00
|
|
|
// Copyright (C) 2001, 2002 Free Software Foundation, Inc.
|
2001-12-29 02:46:54 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
*/
|
|
|
|
|
Makefile.am (doxygen, [...]): Tweak targets.
2002-01-28 Phil Edwards <pme@gcc.gnu.org>
* Makefile.am (doxygen, doxygen-maint, doxygen-man): Tweak targets.
* Makefile.in: Regenerate.
* docs/doxygen/run_doxygen: Update, mostly for man pages.
* docs/doxygen/Intro.3: Update.
* docs/doxygen/TODO: Update.
* docs/doxygen/doxygroups.cc: Add namespace hook for __gnu_cxx.
* docs/doxygen/mainpage.doxy: Update.
* docs/doxygen/user.cfg.in: Update for header rename. Also
regenerate comments and variables with 1.2.12.
* docs/doxygen/maint.cfg.in: Remove file.
* include/bits/stl_relops.h: Doxygenate.
* include/bits/stl_tempbuf.h (std::_Temporary_buffer): Likewise.
* include/c_std/std_cassert.h, include/c_std/std_cctype.h,
include/c_std/std_cerrno.h, include/c_std/std_cfloat.h,
include/c_std/std_ciso646.h, include/c_std/std_climits.h,
include/c_std/std_clocale.h, include/c_std/std_cmath.h,
include/c_std/std_csetjmp.h, include/c_std/std_csignal.h,
include/c_std/std_cstdarg.h, include/c_std/std_cstddef.h,
include/c_std/std_cstdio.h, include/c_std/std_cstdlib.h,
include/c_std/std_cstring.h, include/c_std/std_ctime.h,
include/c_std/std_cwchar.h, include/c_std/std_cwctype.h,
include/ext/algorithm, include/ext/functional, include/ext/hash_map,
include/ext/hash_set, include/ext/iterator, include/ext/memory,
include/ext/numeric, include/ext/rb_tree, include/ext/rope,
include/ext/ropeimpl.h, include/ext/slist, include/ext/stl_hash_fun.h,
include/ext/stl_hashtable.h, include/ext/stl_rope.h,
include/std/std_algorithm.h, include/std/std_bitset.h,
include/std/std_complex.h, include/std/std_deque.h,
include/std/std_fstream.h, include/std/std_functional.h,
include/std/std_iomanip.h, include/std/std_ios.h,
include/std/std_iosfwd.h, include/std/std_iostream.h,
include/std/std_istream.h, include/std/std_iterator.h,
include/std/std_limits.h, include/std/std_list.h,
include/std/std_locale.h, include/std/std_map.h,
include/std/std_memory.h, include/std/std_numeric.h,
include/std/std_ostream.h, include/std/std_queue.h,
include/std/std_set.h, include/std/std_sstream.h,
include/std/std_stack.h, include/std/std_stdexcept.h,
include/std/std_streambuf.h, include/std/std_string.h,
include/std/std_utility.h, include/std/std_valarray.h,
include/std/std_vector.h: Add/correct @file doxygen hook.
* include/ext/memory: Doxygenate most of rest of file.
* libsupc++/exception: Doxygen output formatting.
* libsupc++/new: Say which header it is.
* testsuite/lib/libstdc++-v3-dg.exp: Fix spacing.
* docs/html/19_diagnostics/howto.html: Describe concept-checks switch.
* docs/html/23_containers/howto.html: Describe O(n) list::size().
* docs/html/27_io/howto.html: Also link to Langer and Kreft text.
From-SVN: r49300
2002-01-29 06:13:12 +08:00
|
|
|
/** @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.
|
|
|
|
*/
|
|
|
|
|
2001-12-29 02:46:54 +08:00
|
|
|
#ifndef _EXT_ALGORITHM
|
|
|
|
#define _EXT_ALGORITHM
|
|
|
|
|
2002-01-01 02:03:47 +08:00
|
|
|
#pragma GCC system_header
|
Makefile.am (std_headers): Remove cXXX from list.
2002-01-04 Benjamin Kosnik <bkoz@redhat.com>
* include/Makefile.am (std_headers): Remove cXXX from list.
(c_base_srcdir): No bits subdirectory.
(c_base_builddir): Same.
(c_base_headers): Adjust names, add ciso646.
(bits_headers): Remove std_xxx.h headers.
* include/Makefile.in: Regenerate.
* include/ext/iterator: Adjust includes.
* include/ext/ropeimpl.h: Same.
* include/ext/stl_hash_fun.h: Same.
* include/ext/algorithm: Same.
* include/backward/bvector.h: Adjust includes.
* include/backward/vector.h: Same.
* include/backward/strstream: Same.
* include/backward/streambuf.h: Same.
* include/backward/stack.h: Same.
* include/backward/set.h: Same.
* include/backward/queue.h: Same.
* include/backward/multiset.h: Same.
* include/backward/multimap.h: Same.
* include/backward/map.h: Same.
* include/backward/list.h: Same.
* include/backward/iterator.h: Same.
* include/backward/iostream.h: Same.
* include/backward/iomanip.h: Same.
* include/backward/fstream.h: Same.
* include/backward/deque.h: Same.
* include/backward/complex.h: Same.
* include/bits/std_algorithm.h: Move to...
* include/std/algorithm: ...here.
* include/bits/std_iosfwd.h, std_locale.h, std_stack.h,
std_bitset.h, std_ios.h, std_map.h, std_stdexcept.h,
std_complex.h, std_iostream.h, std_memory.h, std_streambuf.h,
std_deque.h, std_istream.h, std_numeric.h, std_string.h,
std_fstream.h, std_ostream.h, std_utility.h, std_iterator.h,
std_queue.h, std_valarray.h, std_functional.h, std_limits.h,
std_set.h, std_vector.h, std_iomanip.h, std_list.h, std_sstream.h:
Same.
* include/c_std/bits/std_cassert.h: Move to...
* include/c_std/cassert: Here.
* include/c_std/std_cctype.h, std_cerrno.h, std_cfloat.h,
std_climits.h, std_clocale.h, std_cmath.h, std_csetjmp.h,
std_csignal.h, std_cstdarg.h, std_cstddef.h, std_cstdio.h,
std_cstdlib.h, std_cstring.h, std_ctime.h, std_cwchar.h,
std_cwctype.h: Same.
* include/c_std/cmath: Adjust cmath.tcc include.
* include/c_std/cstdlib: Adjust includes.
* include/c_std/cwchar: Same.
* include/c_std/ctime: Same.
* include/c_std/cstring: Same.
* include/c_std/cstdio: Same.
* include/c_std/bits: Remove directory.
* include/c/bits/std_cassert.h: Move to...
* include/c/cassert: Here.
* include/c/std_cctype.h, std_cerrno.h, std_cfloat.h,
std_climits.h, std_clocale.h, std_cmath.h, std_csetjmp.h,
std_csignal.h, std_cstdarg.h, std_cstddef.h, std_cstdio.h,
std_cstdlib.h, std_cstring.h, std_ctime.h, std_cwchar.h,
std_cwctype.h: Same.
* include/c/bits: Remove directory.
* include/std/cwctype: Remove.
* include/std/cwchar: Remove.
* include/std/ctime: Remove.
* include/std/cstring: Remove.
* include/std/cstdlib: Remove.
* include/std/cstdio: Remove.
* include/std/cstddef: Remove.
* include/std/cstdarg: Remove.
* include/std/csignal: Remove.
* include/std/csetjmp: Remove.
* include/std/cmath: Remove.
* include/std/clocale: Remove.
* include/std/climits: Remove.
* include/std/ciso646: Remove.
* include/std/cfloat: Remove.
* include/std/cerrno: Remove.
* include/std/cctype: Remove.
* include/std/cassert: Remove.
* include/bits/fpos.h: Adjust includes, removing <bits/std_xxx.h>
files and subsituting <.xxx>
* include/bits/valarray_array.h: Same.
* include/bits/stl_uninitialized.h: Same.
* include/bits/stl_alloc.h: Same.
* include/bits/stl_algobase.h: Same.
* include/bits/sstream.tcc: Same.
* include/bits/pthread_allocimpl.h: Same.
* include/bits/ostream.tcc: Same.
* include/bits/localefwd.h: Same.
* include/bits/locale_facets.tcc: Same.
* include/bits/locale_facets.h: Same.
* include/bits/istream.tcc: Same.
* include/bits/char_traits.h: Same.
* include/bits/boost_concept_check.h: Same.
* include/bits/basic_file.h: Same.
* include/std/bitset: Same.
* include/std/complex: Same.
* include/std/fstream: Same.
* include/std/functional: Same.
* include/std/ios: Same.
* include/std/iostream: Same.
* include/std/ostream: Same.
* include/std/sstream: Same.
* include/std/streambuf: Same.
* include/std/string: Same.
* include/std/iterator: Same.
* include/std/valarray: Same.
* src/ios.cc: Adjust includes.
* src/valarray-inst.cc: Same.
* src/string-inst.cc: Same.
* src/stl-inst.cc: Same.
* src/stdexcept.cc: Same.
* src/misc-inst.cc: Same.
* src/localename.cc: Same.
* src/locale-inst.cc: Same.
* src/locale.cc: Same.
* src/limits.cc: Same.
* src/concept-inst.cc: Same.
* src/complex_io.cc: Same.
* src/codecvt.cc: Same.
* src/bitset.cc: Same.
* src/cmath.cc: Same.
From-SVN: r48544
2002-01-05 05:27:36 +08:00
|
|
|
#include <algorithm>
|
2001-12-29 02:46:54 +08:00
|
|
|
|
|
|
|
namespace __gnu_cxx
|
|
|
|
{
|
2002-01-02 20:16:56 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2001-12-29 02:46:54 +08:00
|
|
|
// 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<
|
2002-01-02 20:16:56 +08:00
|
|
|
typename iterator_traits<_InputIter>::value_type >)
|
2001-12-29 02:46:54 +08:00
|
|
|
__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,
|
2002-01-02 20:16:56 +08:00
|
|
|
typename iterator_traits<_InputIter>::value_type>)
|
2001-12-29 02:46:54 +08:00
|
|
|
for ( ; __first != __last; ++__first)
|
|
|
|
if (__pred(*__first))
|
|
|
|
++__n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// random_sample and random_sample_n (extensions, not part of the standard).
|
|
|
|
|
2002-07-02 14:17:36 +08:00
|
|
|
/**
|
|
|
|
* This is an SGI extension.
|
|
|
|
* @ingroup SGIextensions
|
|
|
|
* @doctodo
|
|
|
|
*/
|
2001-12-29 02:46:54 +08:00
|
|
|
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,
|
2002-01-02 20:16:56 +08:00
|
|
|
typename iterator_traits<_ForwardIter>::value_type>)
|
2001-12-29 02:46:54 +08:00
|
|
|
|
|
|
|
_Distance __remaining = std::distance(__first, __last);
|
2002-01-02 20:16:56 +08:00
|
|
|
_Distance __m = min(__n, __remaining);
|
2001-12-29 02:46:54 +08:00
|
|
|
|
|
|
|
while (__m > 0) {
|
|
|
|
if (std::__random_number(__remaining) < __m) {
|
|
|
|
*__out = *__first;
|
|
|
|
++__out;
|
|
|
|
--__m;
|
|
|
|
}
|
|
|
|
|
|
|
|
--__remaining;
|
|
|
|
++__first;
|
|
|
|
}
|
|
|
|
return __out;
|
|
|
|
}
|
|
|
|
|
2002-07-02 14:17:36 +08:00
|
|
|
/**
|
|
|
|
* This is an SGI extension.
|
|
|
|
* @ingroup SGIextensions
|
|
|
|
* @doctodo
|
|
|
|
*/
|
2001-12-29 02:46:54 +08:00
|
|
|
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,
|
2002-01-02 20:16:56 +08:00
|
|
|
typename iterator_traits<_ForwardIter>::value_type>)
|
2001-12-29 02:46:54 +08:00
|
|
|
__glibcpp_function_requires(_UnaryFunctionConcept<
|
|
|
|
_RandomNumberGenerator, _Distance, _Distance>)
|
|
|
|
|
|
|
|
_Distance __remaining = std::distance(__first, __last);
|
2002-01-02 20:16:56 +08:00
|
|
|
_Distance __m = min(__n, __remaining);
|
2001-12-29 02:46:54 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-07-02 14:17:36 +08:00
|
|
|
/**
|
|
|
|
* This is an SGI extension.
|
|
|
|
* @ingroup SGIextensions
|
|
|
|
* @doctodo
|
|
|
|
*/
|
2001-12-29 02:46:54 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-07-02 14:17:36 +08:00
|
|
|
/**
|
|
|
|
* This is an SGI extension.
|
|
|
|
* @ingroup SGIextensions
|
|
|
|
* @doctodo
|
|
|
|
*/
|
2001-12-29 02:46:54 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-07-02 14:17:36 +08:00
|
|
|
/**
|
|
|
|
* This is an SGI extension.
|
|
|
|
* @ingroup SGIextensions
|
|
|
|
* @doctodo
|
|
|
|
*/
|
2001-12-29 02:46:54 +08:00
|
|
|
template<typename _RandomAccessIter>
|
|
|
|
inline bool
|
|
|
|
is_heap(_RandomAccessIter __first, _RandomAccessIter __last)
|
|
|
|
{
|
|
|
|
// concept requirements
|
|
|
|
__glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIter>)
|
|
|
|
__glibcpp_function_requires(_LessThanComparableConcept<
|
2002-01-02 20:16:56 +08:00
|
|
|
typename iterator_traits<_RandomAccessIter>::value_type>)
|
2001-12-29 02:46:54 +08:00
|
|
|
|
|
|
|
return __is_heap(__first, __last - __first);
|
|
|
|
}
|
|
|
|
|
2002-07-02 14:17:36 +08:00
|
|
|
/**
|
|
|
|
* This is an SGI extension.
|
|
|
|
* @ingroup SGIextensions
|
|
|
|
* @doctodo
|
|
|
|
*/
|
2001-12-29 02:46:54 +08:00
|
|
|
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,
|
2002-01-02 20:16:56 +08:00
|
|
|
typename iterator_traits<_RandomAccessIter>::value_type,
|
|
|
|
typename iterator_traits<_RandomAccessIter>::value_type>)
|
2001-12-29 02:46:54 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2002-07-02 14:17:36 +08:00
|
|
|
/**
|
|
|
|
* This is an SGI extension.
|
|
|
|
* @ingroup SGIextensions
|
|
|
|
* @doctodo
|
|
|
|
*/
|
2001-12-29 02:46:54 +08:00
|
|
|
template<typename _ForwardIter>
|
|
|
|
bool
|
|
|
|
is_sorted(_ForwardIter __first, _ForwardIter __last)
|
|
|
|
{
|
|
|
|
// concept requirements
|
|
|
|
__glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>)
|
|
|
|
__glibcpp_function_requires(_LessThanComparableConcept<
|
2002-01-02 20:16:56 +08:00
|
|
|
typename iterator_traits<_ForwardIter>::value_type>)
|
2001-12-29 02:46:54 +08:00
|
|
|
|
|
|
|
if (__first == __last)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
_ForwardIter __next = __first;
|
|
|
|
for (++__next; __next != __last; __first = __next, ++__next) {
|
|
|
|
if (*__next < *__first)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-07-02 14:17:36 +08:00
|
|
|
/**
|
|
|
|
* This is an SGI extension.
|
|
|
|
* @ingroup SGIextensions
|
|
|
|
* @doctodo
|
|
|
|
*/
|
2001-12-29 02:46:54 +08:00
|
|
|
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,
|
2002-01-02 20:16:56 +08:00
|
|
|
typename iterator_traits<_ForwardIter>::value_type,
|
|
|
|
typename iterator_traits<_ForwardIter>::value_type>)
|
2001-12-29 02:46:54 +08:00
|
|
|
|
|
|
|
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 */
|