This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] Add is_sorted and is_sorted_until


Hi,

tested x86_64-linux, committed to mainline.

Paolo.

//////////////////
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.
Index: include/ext/algorithm
===================================================================
--- include/ext/algorithm	(revision 129275)
+++ include/ext/algorithm	(working copy)
@@ -428,6 +428,10 @@
 			     __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 @@
 	  return false;
       return true;
     }
+#endif
 
 _GLIBCXX_END_NAMESPACE
 
Index: include/bits/stl_heap.h
===================================================================
--- include/bits/stl_heap.h	(revision 129275)
+++ include/bits/stl_heap.h	(working copy)
@@ -488,9 +488,10 @@
 
 #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 @@
     { 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 @@
    *  @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 @@
     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 @@
    *  @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 @@
     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);
Index: include/bits/stl_algo.h
===================================================================
--- include/bits/stl_algo.h	(revision 129275)
+++ include/bits/stl_algo.h	(working copy)
@@ -3409,10 +3409,10 @@
 
   /**
    *  @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 @@
 
   /**
    *  @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 @@
       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)
Index: include/bits/algorithmfwd.h
===================================================================
--- include/bits/algorithmfwd.h	(revision 129275)
+++ include/bits/algorithmfwd.h	(working copy)
@@ -197,6 +197,22 @@
   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>
Index: testsuite/25_algorithms/is_sorted_until/requirements/explicit_instantiation/2.cc
===================================================================
--- testsuite/25_algorithms/is_sorted_until/requirements/explicit_instantiation/2.cc	(revision 0)
+++ testsuite/25_algorithms/is_sorted_until/requirements/explicit_instantiation/2.cc	(revision 0)
@@ -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);
+} 
Index: testsuite/25_algorithms/is_sorted_until/requirements/explicit_instantiation/pod.cc
===================================================================
--- testsuite/25_algorithms/is_sorted_until/requirements/explicit_instantiation/pod.cc	(revision 0)
+++ testsuite/25_algorithms/is_sorted_until/requirements/explicit_instantiation/pod.cc	(revision 0)
@@ -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);
+} 
Index: testsuite/25_algorithms/is_sorted_until/1.cc
===================================================================
--- testsuite/25_algorithms/is_sorted_until/1.cc	(revision 0)
+++ testsuite/25_algorithms/is_sorted_until/1.cc	(revision 0)
@@ -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;
+}
Index: testsuite/25_algorithms/is_sorted/requirements/explicit_instantiation/2.cc
===================================================================
--- testsuite/25_algorithms/is_sorted/requirements/explicit_instantiation/2.cc	(revision 0)
+++ testsuite/25_algorithms/is_sorted/requirements/explicit_instantiation/2.cc	(revision 0)
@@ -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);
+} 
Index: testsuite/25_algorithms/is_sorted/requirements/explicit_instantiation/pod.cc
===================================================================
--- testsuite/25_algorithms/is_sorted/requirements/explicit_instantiation/pod.cc	(revision 0)
+++ testsuite/25_algorithms/is_sorted/requirements/explicit_instantiation/pod.cc	(revision 0)
@@ -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);
+} 
Index: testsuite/25_algorithms/is_sorted/1.cc
===================================================================
--- testsuite/25_algorithms/is_sorted/1.cc	(revision 0)
+++ testsuite/25_algorithms/is_sorted/1.cc	(revision 0)
@@ -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;
+}
Index: testsuite/25_algorithms/headers/algorithm/synopsis.cc
===================================================================
--- testsuite/25_algorithms/headers/algorithm/synopsis.cc	(revision 129275)
+++ testsuite/25_algorithms/headers/algorithm/synopsis.cc	(working copy)
@@ -417,6 +417,22 @@
   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:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]