[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::{copy_if, copy_n}

Patrick Palka ppalka@gcc.gnu.org
Mon Jan 27 17:34:00 GMT 2020


https://gcc.gnu.org/g:96d6a87d9aa511e8807ad2e814eccdfd6947be72

commit 96d6a87d9aa511e8807ad2e814eccdfd6947be72
Author: Patrick Palka <ppalka@redhat.com>
Date:   Mon Jan 27 12:34:06 2020 -0500

    Add ranges::{copy_if,copy_n}

Diff:
---
 libstdc++-v3/include/bits/ranges_algo.h            | 50 ++++++++++++++
 .../testsuite/25_algorithms/copy_if/constrained.cc | 77 ++++++++++++++++++++++
 .../testsuite/25_algorithms/copy_n/constrained.cc  | 72 ++++++++++++++++++++
 3 files changed, 199 insertions(+)

diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index fea7f66..b2bc2dc 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -1018,6 +1018,56 @@ namespace ranges
 			  std::move(__result));
     }
 
+  template<typename _Iter, typename _Out>
+  using copy_n_result = copy_result<_Iter, _Out>;
+
+  template<input_iterator _Iter, weakly_incrementable _Out>
+    requires indirectly_copyable<_Iter, _Out>
+    constexpr copy_n_result<_Iter, _Out>
+    copy_n(_Iter __first, iter_difference_t<_Iter> __n, _Out __result)
+    {
+      if constexpr (random_access_iterator<_Iter>)
+	return ranges::copy(__first, __first + __n, std::move(__result));
+      else
+	{
+	  for (; __n > 0; --__n, (void)++__result, (void)++__first)
+	    *__result = *__first;
+	  return {std::move(__first), std::move(__result)};
+	}
+    }
+
+  template<typename _Iter, typename _Out>
+  using copy_if_result = copy_result<_Iter, _Out>;
+
+  template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
+	   weakly_incrementable _Out, typename _Proj = identity,
+	   indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+    requires indirectly_copyable<_Iter, _Out>
+    constexpr copy_if_result<_Iter, _Out>
+    copy_if(_Iter __first, _Sent __last, _Out __result,
+	    _Pred __pred, _Proj __proj = {})
+    {
+      for (; __first != __last; ++__first)
+	if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
+	  {
+	    *__result = *__first;
+	    ++__result;
+	  }
+      return {std::move(__first), std::move(__result)};
+    }
+
+  template<input_range _Range, weakly_incrementable _Out,
+	   typename _Proj = identity,
+	   indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+    requires indirectly_copyable<iterator_t<_Range>, _Out>
+    constexpr copy_if_result<safe_iterator_t<_Range>, _Out>
+    copy_if(_Range&& __r, _Out __result, _Pred __pred, _Proj __proj = {})
+    {
+      return ranges::copy_if(ranges::begin(__r), ranges::end(__r),
+			     std::move(__result),
+			     std::move(__pred), std::move(__proj));
+    }
+
   template<bool _IsMove,
 	   bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
 	   bidirectional_iterator _Out>
diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_if/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/copy_if/constrained.cc
new file mode 100644
index 0000000..8a92d227
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/copy_if/constrained.cc
@@ -0,0 +1,77 @@
+// Copyright (C) 2020 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 3, 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 COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <algorithm>
+#include <vector>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_container;
+using __gnu_test::test_range;
+using __gnu_test::input_iterator_wrapper;
+using __gnu_test::output_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+void
+test01()
+{
+  int x[] = {1,2,3,4,5,6,7};
+
+    {
+      const int y[] = {2,4,6};
+      int w[7];
+      test_range<int, input_iterator_wrapper> rx(x);
+      test_range<int, output_iterator_wrapper> rw(w);
+      auto [in,out] = ranges::copy_if(rx, rw.begin(),
+				      [] (int a) { return (a%2)==0; });
+      VERIFY( in == rx.end() && out.ptr == w+3 );
+      VERIFY( ranges::equal(w, w+3, y, y+3) );
+    }
+
+    {
+      const int y[] = {1,3,5,7};
+      int w[7];
+      test_range<int, input_iterator_wrapper> rx(x);
+      test_range<int, output_iterator_wrapper> rw(w);
+      auto [in,out] = ranges::copy_if(rx, rw.begin(),
+				      [] (int a) { return (a%2)==0; },
+				      [] (int a) { return a+1; });
+      VERIFY( in == rx.end() && out.ptr == w+4 );
+      VERIFY( ranges::equal(w, w+4, y, y+4) );
+    }
+}
+
+constexpr bool
+test02()
+{
+  int x[] = {1,2,3};
+  const int y[] = {1,3};
+  int w[3];
+  auto [in,out] = ranges::copy_if(x, w, [] (int a) { return (a%2)==1; });
+  return ranges::equal(w, out, y, y+2);
+}
+
+int
+main()
+{
+  test01();
+  static_assert(test02());
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/copy_n/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/copy_n/constrained.cc
new file mode 100644
index 0000000..78a4539
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/copy_n/constrained.cc
@@ -0,0 +1,72 @@
+// Copyright (C) 2020 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 3, 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 COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <algorithm>
+#include <vector>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_container;
+using __gnu_test::test_range;
+using __gnu_test::input_iterator_wrapper;
+using __gnu_test::output_iterator_wrapper;
+using __gnu_test::random_access_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+template<template<typename> typename in_wrapper,
+	 template<typename> typename out_wrapper>
+void
+test01()
+{
+  int x[] = {1,2,3,4,5,6,7};
+  for (int i = -1; i <= 7; i++)
+    {
+      test_range<int, in_wrapper> rx(x);
+      int w[7];
+      test_range<int, out_wrapper> rw(w);
+      ranges::copy_n(rx.begin(), i, rw.begin());
+      if (i >= 0)
+	VERIFY( ranges::equal(x, x+i, w, w+i) );
+    }
+}
+
+constexpr bool
+test02()
+{
+  int x[] = {1,2,3};
+  int y[2];
+  auto [in,out] = ranges::copy_n(x, 2, y);
+  return (in == x+2
+	  && out == y+2
+	  && ranges::equal(x, x+2, y, y+2));
+}
+
+int
+main()
+{
+  test01<input_iterator_wrapper,
+	 output_iterator_wrapper>();
+  test01<random_access_iterator_wrapper,
+	 output_iterator_wrapper>();
+  test01<random_access_iterator_wrapper,
+	 random_access_iterator_wrapper>();
+  static_assert(test02());
+}



More information about the Libstdc++-cvs mailing list