[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::is_permutation
Patrick Palka
ppalka@gcc.gnu.org
Fri Jan 17 21:44:00 GMT 2020
https://gcc.gnu.org/g:2d063a6c086cb11971aeefdff81dcf94a4a84dff
commit 2d063a6c086cb11971aeefdff81dcf94a4a84dff
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date: Fri Jan 17 14:57:38 2020 -0500
Add ranges::is_permutation
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 82 +++++++++++++++++++++
.../25_algorithms/is_permutation/constrained.cc | 85 ++++++++++++++++++++++
2 files changed, 167 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 8bb9bfa..b41f7d7 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -535,6 +535,88 @@ namespace ranges
std::move(__pred), std::move(__proj));
}
+ template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
+ forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
+ class _Proj1 = identity, class _Proj2 = identity,
+ indirect_equivalence_relation<projected<_Iter1, _Proj1>,
+ projected<_Iter2, _Proj2>> _Pred
+ = ranges::equal_to>
+ constexpr bool
+ is_permutation(_Iter1 __first1, _Sent1 __last1,
+ _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
+ _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+ {
+ constexpr bool __sized_iters
+ = (std::sized_sentinel_for<_Sent1, _Iter1>
+ && std::sized_sentinel_for<_Sent2, _Iter2>);
+ if constexpr (__sized_iters)
+ {
+ auto __d1 = std::distance(__first1, __last1);
+ auto __d2 = std::distance(__first2, __last2);
+ if (__d1 != __d2)
+ return false;
+ }
+
+ // Efficiently compare identical prefixes: O(N) if sequences
+ // have the same elements in the same order.
+ for (; __first1 != __last1 && __first2 != __last2;
+ ++__first1, (void)++__first2)
+ if (!std::__invoke(__pred,
+ std::__invoke(__proj1, *__first1),
+ std::__invoke(__proj2, *__first2)))
+ break;
+
+ if constexpr (__sized_iters)
+ {
+ if (__first1 == __last1)
+ return true;
+ }
+ else
+ {
+ auto __d1 = std::distance(__first1, __last1);
+ auto __d2 = std::distance(__first2, __last2);
+ if (__d1 == 0 && __d2 == 0)
+ return true;
+ if (__d1 != __d2)
+ return false;
+ }
+
+ for (auto __scan = __first1; __scan != __last1; ++__scan)
+ {
+ auto __proj_scan = std::__invoke(__proj1, *__scan);
+ auto __comp_scan = [&] <typename _Tp> (_Tp&& __arg) {
+ return std::__invoke(__pred, __proj_scan,
+ std::forward<_Tp>(__arg));
+ };
+ if (__scan != ranges::find_if(__first1, __scan,
+ __comp_scan, __proj1))
+ continue; // We've seen this one before.
+
+ auto __matches = ranges::count_if(__first2, __last2,
+ __comp_scan, __proj2);
+ if (__matches == 0
+ || ranges::count_if(__scan, __last1,
+ __comp_scan, __proj1) != __matches)
+ return false;
+ }
+ return true;
+ }
+
+ template<forward_range _Range1, forward_range _Range2,
+ class _Proj1 = identity, class _Proj2 = identity,
+ indirect_equivalence_relation<
+ projected<iterator_t<_Range1>, _Proj1>,
+ projected<iterator_t<_Range2>, _Proj2>> _Pred = ranges::equal_to>
+ constexpr bool
+ is_permutation(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
+ _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+ {
+ return ranges::is_permutation(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/is_permutation/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/is_permutation/constrained.cc
new file mode 100644
index 0000000..c5393be
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/is_permutation/constrained.cc
@@ -0,0 +1,85 @@
+// 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;
+using __gnu_test::bidirectional_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X { int i; };
+
+void
+test01()
+{
+ int x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
+ int y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
+ int z[] = { {2}, {6}, {8}, {10}, {2}, {2} };
+
+ VERIFY( ranges::is_permutation(x, x+6, y, y+6) );
+ VERIFY( !ranges::is_permutation(x, x+6, y, y+5) );
+
+ test_container<int, forward_iterator_wrapper> cx(x), cy(y), cz(z);
+ test_range<int, forward_iterator_wrapper> rx(x), ry(y), rz(z);
+ VERIFY( ranges::is_permutation(cx, ry) );
+ VERIFY( !ranges::is_permutation(rx, cz) );
+ VERIFY( ranges::is_permutation(rx, cy) );
+ VERIFY( !ranges::is_permutation(cx, rz) );
+}
+
+void
+test02()
+{
+ static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
+ static constexpr X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
+ static constexpr int z[] = { {2}, {6}, {8}, {10}, {2}, {2} };
+ static_assert(ranges::is_permutation(x, y, {}, &X::i, &X::i));
+ static_assert(!ranges::is_permutation(x, z, {}, &X::i));
+ static_assert(!ranges::is_permutation(z, y, {}, {}, &X::i));
+}
+
+void
+test03()
+{
+ int x[] = { 1, 2, 3, 4 };
+ int y[] = { 1, 2, 3, 3 };
+ test_container<int, bidirectional_iterator_wrapper> cx(x);
+ do
+ do
+ {
+ VERIFY( ranges::is_permutation(cx, x) );
+ VERIFY( !ranges::is_permutation(y, cx) );
+ } while (std::next_permutation(y, y+4));
+ while (std::next_permutation(std::begin(cx), std::end(cx)));
+}
+
+int
+main()
+{
+ test01();
+ test02();
+ test03();
+}
More information about the Libstdc++-cvs
mailing list