stl_algo.h (is_sorted, [...]): Add.

2007-10-14  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/stl_algo.h (is_sorted, is_sorted_until): Add.
	* include/bits/algorithmfwd.h: Add.
	* include/ext/algorithm: Adjust.
	* testsuite/25_algorithms/is_sorted/requirements/
	explicit_instantiation/2.cc: New.
	* testsuite/25_algorithms/is_sorted/requirements/
	explicit_instantiation/pod.cc: Likewise.
	* testsuite/25_algorithms/is_sorted/1.cc: Likewise.
	* testsuite/25_algorithms/is_sorted_until/requirements/
	explicit_instantiation/2.cc: Likewise.
	* testsuite/25_algorithms/is_sorted_until/requirements/
	explicit_instantiation/pod.cc: Likewise.
	* testsuite/25_algorithms/is_sorted_until/1.cc: Likewise.
	* testsuite/25_algorithms/headers/algorithm/synopsis.cc:
	Add is_sorted and is_sorted_until.

	* include/bits/stl_heap.h (is_heap_until): Add concept and
	debug-mode checks.

From-SVN: r129303
This commit is contained in:
Paolo Carlini 2007-10-14 21:17:23 +00:00 committed by Paolo Carlini
parent 1954a27b0c
commit 4b7ed13a8f
12 changed files with 456 additions and 6 deletions

View File

@ -1,3 +1,24 @@
2007-10-14 Paolo Carlini <pcarlini@suse.de>
* include/bits/stl_algo.h (is_sorted, is_sorted_until): Add.
* include/bits/algorithmfwd.h: Add.
* include/ext/algorithm: Adjust.
* testsuite/25_algorithms/is_sorted/requirements/
explicit_instantiation/2.cc: New.
* testsuite/25_algorithms/is_sorted/requirements/
explicit_instantiation/pod.cc: Likewise.
* testsuite/25_algorithms/is_sorted/1.cc: Likewise.
* testsuite/25_algorithms/is_sorted_until/requirements/
explicit_instantiation/2.cc: Likewise.
* testsuite/25_algorithms/is_sorted_until/requirements/
explicit_instantiation/pod.cc: Likewise.
* testsuite/25_algorithms/is_sorted_until/1.cc: Likewise.
* testsuite/25_algorithms/headers/algorithm/synopsis.cc:
Add is_sorted and is_sorted_until.
* include/bits/stl_heap.h (is_heap_until): Add concept and
debug-mode checks.
2007-10-12 Paolo Carlini <pcarlini@suse.de>
* include/bits/stl_heap.h (__is_heap_until): Add.

View File

@ -197,6 +197,22 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
template<typename _RAIter, typename _Compare>
_RAIter
is_heap_until(_RAIter, _RAIter, _Compare);
template<typename _FIter>
bool
is_sorted(_FIter, _FIter);
template<typename _FIter, typename _Compare>
bool
is_sorted(_FIter, _FIter, _Compare);
template<typename _FIter>
_FIter
is_sorted_until(_FIter, _FIter);
template<typename _FIter, typename _Compare>
_FIter
is_sorted_until(_FIter, _FIter, _Compare);
#endif
template<typename _FIter1, typename _FIter2>

View File

@ -3409,10 +3409,10 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
/**
* @brief Permute range into the next "dictionary" ordering using
* comparison functor.
* comparison functor.
* @param first Start of range.
* @param last End of range.
* @param comp
* @param comp A comparison functor.
* @return False if wrapped to first permutation, true otherwise.
*
* Treats all permutations of the range [first,last) as a set of
@ -3520,10 +3520,10 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
/**
* @brief Permute range into the previous "dictionary" ordering using
* comparison functor.
* comparison functor.
* @param first Start of range.
* @param last End of range.
* @param comp
* @param comp A comparison functor.
* @return False if wrapped to last permutation, true otherwise.
*
* Treats all permutations of the range [first,last) as a set of
@ -3651,6 +3651,90 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
return __result;
}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
/**
* @brief Determines whether the elements of a sequence are sorted.
* @param first An iterator.
* @param last Another iterator.
* @return True if the elements are sorted, false otherwise.
*/
template<typename _ForwardIterator>
inline bool
is_sorted(_ForwardIterator __first, _ForwardIterator __last)
{ return std::is_sorted_until(__first, __last) == __last; }
/**
* @brief Determines whether the elements of a sequence are sorted
* according to a comparison functor.
* @param first An iterator.
* @param last Another iterator.
* @param comp A comparison functor.
* @return True if the elements are sorted, false otherwise.
*/
template<typename _ForwardIterator, typename _Compare>
inline bool
is_sorted(_ForwardIterator __first, _ForwardIterator __last,
_Compare __comp)
{ return std::is_sorted_until(__first, __last, __comp) == __last; }
/**
* @brief Determines the end of a sorted sequence.
* @param first An iterator.
* @param last Another iterator.
* @return An iterator pointing to the last iterator i in [first, last)
* for which the range [first, i) is sorted.
*/
template<typename _ForwardIterator>
_ForwardIterator
is_sorted_until(_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 __last;
_ForwardIterator __next = __first;
for (++__next; __next != __last; __first = __next, ++__next)
if (*__next < *__first)
return __next;
return __next;
}
/**
* @brief Determines the end of a sorted sequence using comparison functor.
* @param first An iterator.
* @param last Another iterator.
* @param comp A comparison functor.
* @return An iterator pointing to the last iterator i in [first, last)
* for which the range [first, i) is sorted.
*/
template<typename _ForwardIterator, typename _Compare>
_ForwardIterator
is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
_Compare __comp)
{
// concept requirements
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
typename iterator_traits<_ForwardIterator>::value_type,
typename iterator_traits<_ForwardIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
if (__first == __last)
return __last;
_ForwardIterator __next = __first;
for (++__next; __next != __last; __first = __next, ++__next)
if (__comp(*__next, *__first))
return __next;
return __next;
}
#endif // __GXX_EXPERIMENTAL_CXX0X__
_GLIBCXX_END_NAMESPACE
_GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)

View File

@ -488,9 +488,10 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
#ifdef __GXX_EXPERIMENTAL_CXX0X__
/**
* @brief Check whether a range is a heap.
* @brief Determines whether a range is a heap.
* @param first Start of range.
* @param last End of range.
* @return True if range is a heap, false otherwise.
* @ingroup heap
*/
template<typename _RandomAccessIterator>
@ -499,10 +500,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
{ return std::is_heap_until(__first, __last) == __last; }
/**
* @brief Check whether a range is a heap using comparison functor.
* @brief Determines whether a range is a heap using comparison functor.
* @param first Start of range.
* @param last End of range.
* @param comp Comparison functor to use.
* @return True if range is a heap, false otherwise.
* @ingroup heap
*/
template<typename _RandomAccessIterator, typename _Compare>
@ -515,6 +517,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
* @brief Search the end of a heap.
* @param first Start of range.
* @param last End of range.
* @return An iterator pointing to the first element not in the heap.
* @ingroup heap
*
* This operation returns the last iterator i in [first, last) for which
@ -524,6 +527,13 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
inline _RandomAccessIterator
is_heap_until(_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 __first + std::__is_heap_until(__first, std::distance(__first,
__last));
}
@ -533,6 +543,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
* @param first Start of range.
* @param last End of range.
* @param comp Comparison functor to use.
* @return An iterator pointing to the first element not in the heap.
* @ingroup heap
*
* This operation returns the last iterator i in [first, last) for which
@ -543,6 +554,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last,
_Compare __comp)
{
// concept requirements
__glibcxx_function_requires(_RandomAccessIteratorConcept<
_RandomAccessIterator>)
__glibcxx_requires_valid_range(__first, __last);
return __first + std::__is_heap_until(__first, std::distance(__first,
__last),
__comp);

View File

@ -428,6 +428,10 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
__out_last - __out_first);
}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
using std::is_heap;
using std::is_sorted;
#else
/**
* This is an SGI extension.
* @ingroup SGIextensions
@ -523,6 +527,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
return false;
return true;
}
#endif
_GLIBCXX_END_NAMESPACE

View File

@ -417,6 +417,22 @@ namespace std
template<typename _RAIter, typename _Compare>
_RAIter
is_heap_until(_RAIter, _RAIter, _Compare);
template<typename _FIter>
bool
is_sorted(_FIter, _FIter);
template<typename _FIter, typename _Compare>
bool
is_sorted(_FIter, _FIter, _Compare);
template<typename _FIter>
_FIter
is_sorted_until(_FIter, _FIter);
template<typename _FIter, typename _Compare>
_FIter
is_sorted_until(_FIter, _FIter, _Compare);
#endif
// 25.3.7, minimum and maximum:

View File

@ -0,0 +1,52 @@
// { dg-options "-std=gnu++0x" }
// 2007-10-14 Paolo Carlini <pcarlini@suse.de>
//
// Copyright (C) 2007 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 Pred 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.
// 25.3.6 Heap operations [lib.alg.heap.operations]
#include <algorithm>
#include <functional>
#include <testsuite_hooks.h>
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int B[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
const int N = sizeof(A) / sizeof(int);
void
test01()
{
bool test __attribute__((unused)) = true;
for (int i = 0; i <= N; ++i)
{
VERIFY( std::is_sorted(A, A + i) );
VERIFY( std::is_sorted(A, A + i, std::less<int>()) );
VERIFY( std::is_sorted(B, B + i, std::greater<int>()) );
VERIFY( (i < 2) || !std::is_sorted(B, B + i) );
}
}
int
main()
{
test01();
return 0;
}

View File

@ -0,0 +1,47 @@
// { dg-do compile }
// { dg-options "-std=gnu++0x" }
// 2007-10-14 Paolo Carlini <pcarlini@suse.de>
// Copyright (C) 2007 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.
#include <algorithm>
#include <functional>
#include <testsuite_api.h>
namespace std
{
using __gnu_test::NonDefaultConstructible;
typedef NonDefaultConstructible value_type;
typedef value_type* iterator_type;
typedef std::less<value_type> compare_type;
template bool is_sorted(iterator_type, iterator_type);
template bool is_sorted(iterator_type, iterator_type, compare_type);
}

View File

@ -0,0 +1,46 @@
// { dg-do compile }
// { dg-options "-std=gnu++0x" }
// 2007-10-14 Paolo Carlini <pcarlini@suse.de>
// Copyright (C) 2007 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.
#include <algorithm>
#include <testsuite_character.h>
namespace std
{
using __gnu_test::pod_int;
typedef pod_int value_type;
typedef value_type* iterator_type;
typedef std::less<value_type> compare_type;
template bool is_sorted(iterator_type, iterator_type);
template bool is_sorted(iterator_type, iterator_type, compare_type);
}

View File

@ -0,0 +1,52 @@
// { dg-options "-std=gnu++0x" }
// 2007-10-14 Paolo Carlini <pcarlini@suse.de>
//
// Copyright (C) 2007 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 Pred 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.
// 25.3.6 Heap operations [lib.alg.heap.operations]
#include <algorithm>
#include <functional>
#include <testsuite_hooks.h>
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int B[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
const int N = sizeof(A) / sizeof(int);
void
test01()
{
bool test __attribute__((unused)) = true;
for (int i = 0; i <= N; ++i)
{
VERIFY( A + i == std::is_sorted_until(A, A + i) );
VERIFY( A + i == std::is_sorted_until(A, A + i, std::less<int>()) );
VERIFY( B + i == std::is_sorted_until(B, B + i, std::greater<int>()) );
VERIFY( B + (i < 2 ? i : 1) == std::is_sorted_until(B, B + i) );
}
}
int
main()
{
test01();
return 0;
}

View File

@ -0,0 +1,48 @@
// { dg-do compile }
// { dg-options "-std=gnu++0x" }
// 2007-10-14 Paolo Carlini <pcarlini@suse.de>
// Copyright (C) 2007 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.
#include <algorithm>
#include <functional>
#include <testsuite_api.h>
namespace std
{
using __gnu_test::NonDefaultConstructible;
typedef NonDefaultConstructible value_type;
typedef value_type* iterator_type;
typedef std::less<value_type> compare_type;
template iterator_type is_sorted_until(iterator_type, iterator_type);
template iterator_type is_sorted_until(iterator_type, iterator_type,
compare_type);
}

View File

@ -0,0 +1,47 @@
// { dg-do compile }
// { dg-options "-std=gnu++0x" }
// 2007-10-14 Paolo Carlini <pcarlini@suse.de>
// Copyright (C) 2007 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.
#include <algorithm>
#include <testsuite_character.h>
namespace std
{
using __gnu_test::pod_int;
typedef pod_int value_type;
typedef value_type* iterator_type;
typedef std::less<value_type> compare_type;
template iterator_type is_sorted_until(iterator_type, iterator_type);
template iterator_type is_sorted_until(iterator_type, iterator_type,
compare_type);
}