[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::mismatch
Patrick Palka
ppalka@gcc.gnu.org
Wed Jan 15 15:40:00 GMT 2020
https://gcc.gnu.org/g:46abdc23d1a9a7443779e9499fb18d688a23fa2f
commit 46abdc23d1a9a7443779e9499fb18d688a23fa2f
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date: Mon Jan 13 15:19:23 2020 -0500
Add ranges::mismatch
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 58 ++++++++++++++++++
.../25_algorithms/mismatch/constrained.cc | 71 ++++++++++++++++++++++
2 files changed, 129 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 0a78565..2f25355 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -268,6 +268,64 @@ namespace ranges
std::move(__pred), std::move(__proj));
}
+ template<typename _Iter1, typename _Iter2>
+ struct mismatch_result
+ {
+ [[no_unique_address]] _Iter1 in1;
+ [[no_unique_address]] _Iter2 in2;
+
+ template<typename _IIter1, typename _IIter2>
+ requires convertible_to<const _Iter1&, _IIter1>
+ && convertible_to<const _Iter2&, _IIter2>
+ operator mismatch_result<_IIter1, _IIter2>() const &
+ {
+ return {in1, in2};
+ }
+
+ template<typename _IIter1, typename _IIter2>
+ requires convertible_to<_Iter1, _IIter1>
+ && convertible_to<_Iter2, _IIter2>
+ operator mismatch_result<_IIter1, _IIter2>() &&
+ {
+ return {std::move(in1), std::move(in2)};
+ }
+ };
+
+ template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
+ input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
+ typename _Pred = ranges::equal_to,
+ typename _Proj1 = identity, typename _Proj2 = identity>
+ requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
+ constexpr mismatch_result<_Iter1, _Iter2>
+ mismatch(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
+ _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+ {
+ while (__first1 != __last1 && __first2 != __last2
+ && (bool)std::__invoke(__pred,
+ std::__invoke(__proj1, *__first1),
+ std::__invoke(__proj2, *__first2)))
+ {
+ ++__first1;
+ ++__first2;
+ }
+ return { __first1, __first2 };
+ }
+
+ template<input_range _Range1, input_range _Range2,
+ typename _Pred = ranges::equal_to,
+ typename _Proj1 = identity, typename _Proj2 = identity>
+ requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
+ _Pred, _Proj1, _Proj2>
+ constexpr mismatch_result<iterator_t<_Range1>, iterator_t<_Range2>>
+ mismatch(_Range1&& __r1, _Range2&& __r2,
+ _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+ {
+ return ranges::mismatch(ranges::begin(__r1), ranges::end(__r1),
+ ranges::begin(__r2), ranges::end(__r2),
+ std::move(__pred),
+ std::move(__proj1), std::move(__proj2));
+ }
+
} // namespace ranges
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
diff --git a/libstdc++-v3/testsuite/25_algorithms/mismatch/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/mismatch/constrained.cc
new file mode 100644
index 0000000..06a5a13
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/mismatch/constrained.cc
@@ -0,0 +1,71 @@
+// 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 <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::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X { int i; };
+
+void
+test01()
+{
+ X xa[] = { {1}, {2}, {3}, {4}, {5}, {6} };
+ X xb[] = { {1}, {2}, {3}, {3}, {5}, {6} };
+ auto res = ranges::mismatch(xa, xa+6, xb, xb+6, {}, &X::i, &X::i);
+ VERIFY( res.in1 == xa+3 && res.in2 == xb+3 );
+
+ test_container<X, forward_iterator_wrapper> ca(xa);
+ test_container<X, forward_iterator_wrapper> cb(xb);
+ auto res2 = ranges::mismatch(ca, cb, {}, &X::i, &X::i);
+ VERIFY( res2.in1->i == 4 && res2.in2->i == 3 );
+ res2 = ranges::mismatch(ca, ca, {}, &X::i, &X::i);
+ VERIFY( res2.in1 == ranges::end(ca) && res2.in2 == ranges::end(ca) );
+
+ test_range<X, input_iterator_wrapper> ra(xa);
+ test_range<X, input_iterator_wrapper> rb(xb);
+ auto res3 = ranges::mismatch(ra, rb, {}, &X::i, &X::i);
+ VERIFY( res3.in1->i == 4 && res3.in2->i == 3 );
+}
+
+struct Y { int i; int j; };
+
+void
+test02()
+{
+ static constexpr Y ya[] = { {1,2}, {2,4}, {3,6}, {1,6} };
+ static constexpr Y yb[] = { {2,1}, {4,2}, {4,2}, {7,1} };
+ static_assert(ranges::mismatch(ya, yb, {}, &Y::i, &Y::j).in1 == ya+2);
+ static_assert(ranges::mismatch(ya, yb, {}, &Y::i, &Y::j).in2 == yb+2);
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
More information about the Libstdc++-cvs
mailing list