[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add ranges::lexicographical_compare
Patrick Palka
ppalka@gcc.gnu.org
Mon Jan 27 16:43:00 GMT 2020
https://gcc.gnu.org/g:0c59f605ac46c96568ba1a9090bebb7e39335b63
commit 0c59f605ac46c96568ba1a9090bebb7e39335b63
Author: Patrick Palka <ppalka@redhat.com>
Date: Mon Jan 27 11:40:10 2020 -0500
Add ranges::lexicographical_compare
Diff:
---
libstdc++-v3/include/bits/ranges_algo.h | 107 ++++++++++++++
.../lexicographical_compare/constrained.cc | 160 +++++++++++++++++++++
2 files changed, 267 insertions(+)
diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 75e6c3e..fea7f66 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -3378,6 +3378,113 @@ namespace ranges
std::move(__comp), std::move(__proj));
}
+ template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
+ input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
+ typename _Proj1, typename _Proj2,
+ indirect_strict_weak_order<projected<_Iter1, _Proj1>,
+ projected<_Iter2, _Proj2>> _Comp>
+ constexpr bool
+ __lexicographical_compare(_Iter1 __first1, _Sent1 __last1,
+ _Iter2 __first2, _Sent2 __last2,
+ _Comp __comp, _Proj1 __proj1, _Proj2 __proj2)
+ {
+ constexpr bool __sized_iters
+ = (sized_sentinel_for<_Sent1, _Iter1>
+ && sized_sentinel_for<_Sent2, _Iter2>);
+ if constexpr (__sized_iters)
+ {
+ auto __d1 = ranges::distance(__first1, __last1);
+ auto __d2 = ranges::distance(__first2, __last2);
+
+ using _ValueType1 = iter_value_t<_Iter1>;
+ using _ValueType2 = iter_value_t<_Iter2>;
+ constexpr bool __use_memcmp
+ = ((is_integral_v<_ValueType1> || is_pointer_v<_ValueType1>)
+ && is_same_v<_ValueType1, _ValueType2>
+ && is_pointer_v<_Iter1>
+ && is_pointer_v<_Iter2>
+ && (is_same_v<_Comp, ranges::less>
+ || is_same_v<_Comp, ranges::greater>)
+ && is_same_v<_Proj1, identity>
+ && is_same_v<_Proj2, identity>);
+ if constexpr (__use_memcmp)
+ {
+ if (const auto __len = std::min(__d1, __d2))
+ {
+ const auto __c = std::__memcmp(__first1, __first2, __len);
+ if constexpr (is_same_v<_Comp, ranges::less>)
+ {
+ if (__c < 0)
+ return true;
+ if (__c > 0)
+ return false;
+ }
+ else if constexpr (is_same_v<_Comp, ranges::greater>)
+ {
+ if (__c > 0)
+ return true;
+ if (__c < 0)
+ return false;
+ }
+ else
+ __builtin_unreachable();
+ }
+ return (__last1 - __first1 < __last2 - __first2);
+ }
+ }
+
+ for (; __first1 != __last1 && __first2 != __last2;
+ ++__first1, (void) ++__first2)
+ {
+ if (std::__invoke(__comp,
+ std::__invoke(__proj1, *__first1),
+ std::__invoke(__proj2, *__first2)))
+ return true;
+ if (std::__invoke(__comp,
+ std::__invoke(__proj2, *__first2),
+ std::__invoke(__proj1, *__first1)))
+ return false;
+ }
+ return __first1 == __last1 && __first2 != __last2;
+ }
+
+ template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
+ input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
+ typename _Proj1 = identity, typename _Proj2 = identity,
+ indirect_strict_weak_order<projected<_Iter1, _Proj1>,
+ projected<_Iter2, _Proj2>>
+ _Comp = ranges::less>
+ constexpr bool
+ lexicographical_compare(_Iter1 __first1, _Sent1 __last1,
+ _Iter2 __first2, _Sent2 __last2,
+ _Comp __comp = {},
+ _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+ {
+ return (ranges::__lexicographical_compare
+ (std::__niter_base(std::move(__first1)),
+ std::__niter_base(std::move(__last1)),
+ std::__niter_base(std::move(__first2)),
+ std::__niter_base(std::move(__last2)),
+ std::move(__comp),
+ std::move(__proj1), std::move(__proj2)));
+ }
+
+ template<input_range _Range1, input_range _Range2, typename _Proj1 = identity,
+ typename _Proj2 = identity,
+ indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
+ projected<iterator_t<_Range2>, _Proj2>>
+ _Comp = ranges::less>
+ constexpr bool
+ lexicographical_compare(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
+ _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
+ {
+ return (ranges::lexicographical_compare
+ (ranges::begin(__r1), ranges::end(__r1),
+ ranges::begin(__r2), ranges::end(__r2),
+ std::move(__comp),
+ std::move(__proj1), std::move(__proj2)));
+ }
+
template<typename _Iter>
struct next_permutation_result {
bool found;
diff --git a/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/constrained.cc
new file mode 100644
index 0000000..b28c736
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/constrained.cc
@@ -0,0 +1,160 @@
+// 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;
+
+void
+test01()
+{
+ int x[] = {1, 2, 3, 4, 5};
+ int y[] = {1, 2, 3, 5};
+ int z[] = {1, 2, 3, 4, 5, 6};
+
+ {
+ test_range<int, input_iterator_wrapper> rx(x), ry(y), rz(z);
+
+ VERIFY( ranges::lexicographical_compare(rx, ry) );
+ rx.bounds.first = x;
+ ry.bounds.first = y;
+ VERIFY( !ranges::lexicographical_compare(ry, rx) );
+ }
+
+ test_range<int, forward_iterator_wrapper> rx(x), ry(y), rz(z);
+
+ VERIFY( ranges::lexicographical_compare(rx, rz) );
+ VERIFY( !ranges::lexicographical_compare(rz, rx) );
+
+ VERIFY( !ranges::lexicographical_compare(rx, rx) );
+ VERIFY( ranges::lexicographical_compare(rx, rx, {}, std::negate<>{}) );
+ VERIFY( ranges::lexicographical_compare(rx, rx, std::greater{},
+ {}, std::negate<>{}) );
+
+ VERIFY( !ranges::lexicographical_compare(rx, ry, {},
+ std::negate<>{},
+ std::negate<>{}) );
+ VERIFY( ranges::lexicographical_compare(ry, rx, {},
+ std::negate<>{},
+ std::negate<>{}) );
+
+ VERIFY( ranges::lexicographical_compare(rx, rz, ranges::greater{}) );
+ VERIFY( !ranges::lexicographical_compare(rz, rx, ranges::greater{}) );
+
+ VERIFY( ranges::lexicographical_compare(rx, ry, ranges::greater{},
+ std::negate<>{},
+ std::negate<>{}) );
+ VERIFY( !ranges::lexicographical_compare(ry, rx, ranges::greater{},
+ std::negate<>{},
+ std::negate<>{}) );
+}
+
+void
+test02()
+{
+ int x[] = {1, 2, 3, 4, 5};
+ int y[] = {1, 2, 3, 5};
+ int z[] = {1, 2, 3, 4, 5, 6};
+
+ VERIFY( ranges::lexicographical_compare(x, y) );
+ VERIFY( !ranges::lexicographical_compare(y, x) );
+
+ VERIFY( ranges::lexicographical_compare(x, z) );
+ VERIFY( !ranges::lexicographical_compare(z, x) );
+
+ VERIFY( !ranges::lexicographical_compare(x, x) );
+
+ VERIFY( !ranges::lexicographical_compare(x, y, {},
+ std::negate<>{},
+ std::negate<>{}) );
+ VERIFY( ranges::lexicographical_compare(y, x, {},
+ std::negate<>{},
+ std::negate<>{}) );
+
+ VERIFY( ranges::lexicographical_compare(x, z, ranges::greater{}) );
+ VERIFY( !ranges::lexicographical_compare(z, x, ranges::greater{}) );
+
+ VERIFY( ranges::lexicographical_compare(x, y, ranges::greater{},
+ std::negate<>{},
+ std::negate<>{}) );
+ VERIFY( !ranges::lexicographical_compare(y, x, ranges::greater{},
+ std::negate<>{},
+ std::negate<>{}) );
+}
+
+void
+test03()
+{
+ int x[] = {1, 2, 3, 4, 5};
+ int y[] = {1, 2, 5, 3};
+ int z[] = {1, 2, 3, 5};
+
+ do
+ {
+ VERIFY( ranges::lexicographical_compare(x, y) );
+ VERIFY( !ranges::lexicographical_compare(x, y, ranges::greater{}) );
+ VERIFY( !ranges::lexicographical_compare(y, x) );
+ VERIFY( ranges::lexicographical_compare(y, x, ranges::greater{}) );
+
+ test_container<int, forward_iterator_wrapper> cy(y);
+ VERIFY( ranges::lexicographical_compare(x, cy) );
+ VERIFY( !ranges::lexicographical_compare(x, cy, ranges::greater{}) );
+ VERIFY( !ranges::lexicographical_compare(cy, x) );
+ VERIFY( ranges::lexicographical_compare(cy, x, ranges::greater{}) );
+
+ test_container<int, forward_iterator_wrapper> cz(z);
+ VERIFY( ranges::lexicographical_compare(cz.begin(), cz.end(),
+ cy.begin(), cy.end()) );
+ VERIFY( !ranges::lexicographical_compare(cy.begin(), cy.end(),
+ cz.begin(), cz.end()) );
+
+ std::vector<int> vx(x, x+5), vy(y, y+5);
+ VERIFY( ranges::lexicographical_compare(vx, vy) );
+ VERIFY( !ranges::lexicographical_compare(vx, vy, ranges::greater{}) );
+ VERIFY( !ranges::lexicographical_compare(vy, vx) );
+ VERIFY( ranges::lexicographical_compare(vy, vx, ranges::greater{}) );
+ } while (ranges::next_permutation(y).found);
+}
+
+constexpr bool
+test04()
+{
+ int x[] = {1};
+ int y[] = {1};
+ return (ranges::lexicographical_compare((int[]){1,2,3,5},
+ (int[]){1,2,4})
+ && !ranges::lexicographical_compare(x, x, y, y));
+}
+
+int
+main()
+{
+ test01();
+ test02();
+ test03();
+ static_assert(test04());
+}
More information about the Libstdc++-cvs
mailing list