diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index e83b289b13a2..798c4d891503 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -53,6 +53,7 @@ set(files __ranges/enable_view.h __ranges/view_interface.h __ranges/size.h + __ranges/subrange.h __split_buffer __std_stream __string diff --git a/libcxx/include/__ranges/subrange.h b/libcxx/include/__ranges/subrange.h new file mode 100644 index 000000000000..dcdb06033051 --- /dev/null +++ b/libcxx/include/__ranges/subrange.h @@ -0,0 +1,237 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#ifndef _LIBCPP___RANGES_SUBRANGE_H +#define _LIBCPP___RANGES_SUBRANGE_H + +#include <__config> +#include <__iterator/concepts.h> +#include <__iterator/iterator_traits.h> +#include <__iterator/advance.h> +#include <__ranges/access.h> +#include <__ranges/enable_borrowed_range.h> +#include <__ranges/view_interface.h> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if !defined(_LIBCPP_HAS_NO_RANGES) + +// clang-format off +namespace ranges { + template + concept __convertible_to_non_slicing = + convertible_to<_From, _To> && + // If they're both pointers, they must have the same element type. + !(is_pointer_v> && + is_pointer_v> && + __different_from>, remove_pointer_t>>); + + template + concept __pair_like = + !is_reference_v<_Tp> && requires(_Tp __t) { + typename tuple_size<_Tp>::type; // Ensures `tuple_size` is complete. + requires derived_from, integral_constant>; + typename tuple_element_t<0, remove_const_t<_Tp>>; + typename tuple_element_t<1, remove_const_t<_Tp>>; + { _VSTD::get<0>(__t) } -> convertible_to&>; + { _VSTD::get<1>(__t) } -> convertible_to&>; + }; + + template + concept __pair_like_convertible_from = + !range<_Pair> && __pair_like<_Pair> && + constructible_from<_Pair, _Iter, _Sent> && + __convertible_to_non_slicing<_Iter, tuple_element_t<0, _Pair>> && + convertible_to<_Sent, tuple_element_t<1, _Pair>>; + + enum class _LIBCPP_ENUM_VIS subrange_kind : bool { unsized, sized }; + + template + struct __subrange_base { + static constexpr bool __store_size = false; + _Iter __begin_ = _Iter(); + _Sent __end_ = _Sent(); + + constexpr __subrange_base() = default; + constexpr __subrange_base(_Iter __iter, _Sent __sent, make_unsigned_t> = 0) + : __begin_(_VSTD::move(__iter)), __end_(__sent) { } + }; + + template + struct __subrange_base<_Iter, _Sent, true> { + static constexpr bool __store_size = true; + _Iter __begin_ = _Iter(); + _Sent __end_ = _Sent(); + make_unsigned_t> __size_ = 0; + + constexpr __subrange_base() = default; + constexpr __subrange_base(_Iter __iter, _Sent __sent, decltype(__size_) __size) + : __begin_(_VSTD::move(__iter)), __end_(__sent), __size_(__size) { } + }; + + template _Sent = _Iter, + subrange_kind _Kind = sized_sentinel_for<_Sent, _Iter> + ? subrange_kind::sized + : subrange_kind::unsized> + requires (_Kind == subrange_kind::sized || !sized_sentinel_for<_Sent, _Iter>) + struct _LIBCPP_TEMPLATE_VIS subrange + : public view_interface>, + private __subrange_base<_Iter, _Sent, _Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _Iter>> { + + using _Base = __subrange_base<_Iter, _Sent, _Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _Iter>>; + + subrange() requires default_initializable<_Iter> = default; + + constexpr subrange(__convertible_to_non_slicing<_Iter> auto __iter, _Sent __sent) + requires (!_Base::__store_size) + : _Base(_VSTD::move(__iter), __sent) {} + + constexpr subrange(__convertible_to_non_slicing<_Iter> auto __iter, _Sent __sent, + make_unsigned_t> __n) + requires (_Kind == subrange_kind::sized) + : _Base(_VSTD::move(__iter), __sent, __n) { } + + template<__different_from _Range> + requires borrowed_range<_Range> && + __convertible_to_non_slicing, _Iter> && + convertible_to, _Sent> + constexpr subrange(_Range&& __range) + requires (!_Base::__store_size) + : subrange(ranges::begin(__range), ranges::end(__range)) { } + + template<__different_from _Range> + requires borrowed_range<_Range> && + __convertible_to_non_slicing, _Iter> && + convertible_to, _Sent> + constexpr subrange(_Range&& __range) + requires _Base::__store_size && sized_range<_Range> + : subrange(__range, ranges::size(__range)) { } + + + template + requires __convertible_to_non_slicing, _Iter> && + convertible_to, _Sent> + constexpr subrange(_Range&& __range, make_unsigned_t> __n) + requires (_Kind == subrange_kind::sized) + : subrange(ranges::begin(__range), ranges::end(__range), __n) { } + + template<__different_from _Pair> + requires __pair_like_convertible_from<_Pair, const _Iter&, const _Sent&> + constexpr operator _Pair() const { return _Pair(this->__begin_, this->__end_); } + + constexpr _Iter begin() const requires copyable<_Iter> { + return this->__begin_; + } + + [[nodiscard]] constexpr _Iter begin() requires (!copyable<_Iter>) { + return _VSTD::move(this->__begin_); + } + + constexpr _Sent end() const { return this->__end_; } + + [[nodiscard]] constexpr bool empty() const { return this->__begin_ == this->__end_; } + + constexpr make_unsigned_t> size() const + requires (_Kind == subrange_kind::sized) + { + if constexpr (_Base::__store_size) + return this->__size_; + else + return __to_unsigned_like(this->__end_ - this->__begin_); + } + + [[nodiscard]] constexpr subrange next(iter_difference_t<_Iter> __n = 1) const& + requires forward_iterator<_Iter> { + auto __tmp = *this; + __tmp.advance(__n); + return __tmp; + } + + [[nodiscard]] constexpr subrange next(iter_difference_t<_Iter> __n = 1) && { + advance(__n); + return _VSTD::move(*this); + } + + [[nodiscard]] constexpr subrange prev(iter_difference_t<_Iter> __n = 1) const + requires bidirectional_iterator<_Iter> { + auto __tmp = *this; + __tmp.advance(-__n); + return __tmp; + } + + constexpr subrange& advance(iter_difference_t<_Iter> __n) { + if constexpr (bidirectional_iterator<_Iter>) { + if (__n < 0) { + ranges::advance(this->__begin_, __n); + if constexpr (_Base::__store_size) + this->__size_ += _VSTD::__to_unsigned_like(-__n); + return *this; + } + } + + auto __d = __n - ranges::advance(this->__begin_, __n, this->__end_); + if constexpr (_Base::__store_size) + this->__size_ -= _VSTD::__to_unsigned_like(__d); + return *this; + } + }; + + template _Sent> + subrange(_Iter, _Sent) -> subrange<_Iter, _Sent>; + + template _Sent> + subrange(_Iter, _Sent, make_unsigned_t>) + -> subrange<_Iter, _Sent, subrange_kind::sized>; + + template + subrange(_Range&&) -> subrange, sentinel_t<_Range>, + (sized_range<_Range> || sized_sentinel_for, iterator_t<_Range>>) + ? subrange_kind::sized : subrange_kind::unsized>; + + template + subrange(_Range&&, make_unsigned_t>) + -> subrange, sentinel_t<_Range>, subrange_kind::sized>; + + template + requires (_Index < 2) + constexpr auto get(const subrange<_Iter, _Sent, _Kind>& __subrange) { + if constexpr (_Index == 0) + return __subrange.begin(); + else + return __subrange.end(); + } + + template + requires (_Index < 2) + constexpr auto get(subrange<_Iter, _Sent, _Kind>&& __subrange) { + if constexpr (_Index == 0) + return __subrange.begin(); + else + return __subrange.end(); + } +} // namespace ranges + +using ranges::get; + +// clang-format off + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___RANGES_SUBRANGE_H diff --git a/libcxx/include/ranges b/libcxx/include/ranges index 9e5b55e369e2..edf74746c54c 100644 --- a/libcxx/include/ranges +++ b/libcxx/include/ranges @@ -101,6 +101,7 @@ namespace std::ranges { #include <__ranges/enable_borrowed_range.h> #include <__ranges/enable_view.h> #include <__ranges/size.h> +#include <__ranges/subrange.h> #include <__ranges/view_interface.h> #include // Required by the standard. #include // Required by the standard. diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/access/advance.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/access/advance.pass.cpp new file mode 100644 index 000000000000..2d1cd7edd286 --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/access/advance.pass.cpp @@ -0,0 +1,59 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// class std::ranges::subrange; + +#include + +#include +#include "test_macros.h" +#include "test_iterators.h" +#include "../subrange_test_types.h" + +constexpr bool test() { + std::ranges::subrange a(globalBuff, globalBuff + 8, 8); + auto a1 = a.next(); + assert(a1.begin() == globalBuff + 1); + assert(a1.size() == 7); + auto a5 = a.next(5); + assert(a5.begin() == globalBuff + 5); + assert(a5.size() == 3); + auto a4 = a5.prev(); + assert(a4.begin() == globalBuff + 4); + assert(a4.size() == 4); + + std::ranges::subrange b(InputIter(globalBuff), InputIter(globalBuff + 8)); + auto b1 = std::move(b).next(); + assert(b1.begin().base() == globalBuff + 1); + + std::ranges::subrange c(BidirIter(globalBuff + 4), BidirIter(globalBuff + 8)); + auto c1 = c.prev(); + assert(c1.begin().base() == globalBuff + 3); + auto c2 = c.prev(4); + assert(c2.begin().base() == globalBuff); + + std::ranges::subrange d(BidirIter(globalBuff + 4), BidirIter(globalBuff + 8)); + auto d1 = d.advance(4); + assert(d1.begin().base() == globalBuff + 8); + assert(d1.empty()); + auto d2 = d1.advance(-4); + assert(d2.begin().base() == globalBuff + 4); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/access/get.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/access/get.pass.cpp new file mode 100644 index 000000000000..8c1a027096c5 --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/access/get.pass.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// class std::ranges::subrange; + +#include + +#include +#include "test_macros.h" +#include "test_iterators.h" +#include "../subrange_test_types.h" + +template +concept GetInvocable = requires { + std::get(std::declval()); +}; + +static_assert( GetInvocable<0, std::ranges::subrange>); +static_assert( GetInvocable<1, std::ranges::subrange>); +static_assert(!GetInvocable<2, std::ranges::subrange>); +static_assert(!GetInvocable<3, std::ranges::subrange>); + +constexpr bool test() { + std::ranges::subrange a(globalBuff, globalBuff + 8, 8); + assert(std::get<0>(a) == a.begin()); + assert(std::get<1>(a) == a.end()); + + assert(a.begin() == std::get<0>(std::move(a))); + std::ranges::subrange b(globalBuff, globalBuff + 8, 8); + assert(b.end() == std::get<1>(std::move(b))); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/access/primitives.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/access/primitives.pass.cpp new file mode 100644 index 000000000000..fe31471cb06e --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/access/primitives.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// class std::ranges::subrange; + +#include + +#include +#include "test_macros.h" +#include "test_iterators.h" +#include "../subrange_test_types.h" + +// Note: begin and end tested in range.subrange.ctor.pass.cpp. + +constexpr bool test() { + std::ranges::subrange a(MoveOnlyForwardIter(globalBuff), globalBuff + 8, 8); + assert(a.begin().base == globalBuff); + assert(!a.empty()); + assert(a.size() == 8); + + std::ranges::subrange b(ForwardIter(nullptr), ForwardIter(nullptr)); + assert(b.empty()); + + std::ranges::subrange c{ForwardIter(globalBuff), ForwardIter(globalBuff)}; + assert(c.empty()); + + std::ranges::subrange d(ForwardIter(globalBuff), ForwardIter(globalBuff + 1)); + assert(!d.empty()); + + std::ranges::subrange e(SizedSentinelForwardIter(globalBuff), + SizedSentinelForwardIter(globalBuff + 8), 8); + assert(!e.empty()); + assert(e.size() == 8); + + // Make sure that operator- is used to calculate size when possible. + if (!std::is_constant_evaluated()) + assert(SizedSentinelForwardIter::minusCount == 1); + + return true; +} + + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/ctad.compile.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/ctad.compile.pass.cpp new file mode 100644 index 000000000000..e34b519fd932 --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/ctad.compile.pass.cpp @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// class std::ranges::subrange; + +#include + +#include +#include "test_macros.h" +#include "test_iterators.h" + +using FI = forward_iterator; +FI fi{nullptr}; +int *ptr = nullptr; + +static_assert(std::same_as>); +static_assert(std::same_as>); +static_assert(std::same_as>); + +struct ForwardRange { + forward_iterator begin() const; + forward_iterator end() const; +}; +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +struct SizedRange { + int *begin(); + int *end(); +}; +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +static_assert(std::same_as>); +static_assert(std::same_as>); +static_assert(std::same_as>); diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/ctor/begin_end.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/ctor/begin_end.pass.cpp new file mode 100644 index 000000000000..5a6364f5368f --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/ctor/begin_end.pass.cpp @@ -0,0 +1,53 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// class std::ranges::subrange; + +#include + +#include "../subrange_test_types.h" +#include +#include "test_macros.h" +#include "test_iterators.h" + +// convertible-to-non-slicing cases: +// 1. Not convertible (fail) +// 2. Only one is a pointer (succeed) +// 3. Both are not pointers (succeed) +// 4. Pointer elements are different types (fail) +// 5. Pointer elements are same type (succeed) + +// !StoreSize ctor. +static_assert( std::is_constructible_v); // Default case. +static_assert(!std::is_constructible_v); // 1. +static_assert( std::is_constructible_v); // 2. +static_assert( std::is_constructible_v); // 3. (Same as default case.) +// 4. and 5. must be sized. + +constexpr bool test() { + ForwardSubrange a(ForwardIter(globalBuff), ForwardIter(globalBuff + 8)); + assert(a.begin().base() == globalBuff); + assert(a.end().base() == globalBuff + 8); + + ConvertibleForwardSubrange b(ConvertibleForwardIter(globalBuff), globalBuff + 8); + assert(b.begin() == globalBuff); + assert(b.end() == globalBuff + 8); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/ctor/begin_end_size.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/ctor/begin_end_size.pass.cpp new file mode 100644 index 000000000000..e8ae605379f6 --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/ctor/begin_end_size.pass.cpp @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// class std::ranges::subrange; + +#include + +#include "../subrange_test_types.h" +#include +#include "test_macros.h" +#include "test_iterators.h" + +// convertible-to-non-slicing cases: +// 1. Not convertible (fail) +// 2. Only one is a pointer (succeed) +// 3. Both are not pointers (succeed) +// 4. Pointer elements are different types (fail) +// 5. Pointer elements are same type (succeed) + +static_assert( std::is_constructible_v); // Default case. +static_assert(!std::is_constructible_v); // 1. +static_assert( std::is_constructible_v); // 2. +static_assert( std::is_constructible_v); // 3. (Same as default case.) +static_assert(!std::is_constructible_v); // 4. +static_assert( std::is_constructible_v); // 5. + +constexpr bool test() { + SizedSentinelForwardSubrange a(ConditionallyConvertibleIter(globalBuff), ConditionallyConvertibleIter(globalBuff + 8), 8); + assert(a.begin().base() == globalBuff); + assert(a.end().base() == globalBuff + 8); + assert(a.size() == 8); + + ConvertibleSizedSentinelForwardSubrange b(ConvertibleSizedSentinelForwardIter(globalBuff), ConvertibleSizedSentinelForwardIter(globalBuff + 8), 8); + assert(b.begin() == globalBuff); + assert(b.end() == globalBuff + 8); + assert(b.size() == 8); + + SizedIntPtrSubrange c(globalBuff, globalBuff + 8, 8); + assert(c.begin() == globalBuff); + assert(c.end() == globalBuff + 8); + assert(c.size() == 8); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/ctor/pair_like_conv.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/ctor/pair_like_conv.pass.cpp new file mode 100644 index 000000000000..1b749831ec8f --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/ctor/pair_like_conv.pass.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// class std::ranges::subrange; + +#include + +#include "../subrange_test_types.h" +#include +#include "test_macros.h" +#include "test_iterators.h" +#include +#include + +static_assert( std::is_convertible_v>); +static_assert( std::is_convertible_v>); +static_assert(!std::is_convertible_v&>); +static_assert(!std::is_convertible_v>); +static_assert( std::is_convertible_v>); +static_assert(!std::is_convertible_v>); +static_assert( std::is_convertible_v>); + +constexpr bool test() { + ForwardSubrange a(ForwardIter(globalBuff), ForwardIter(globalBuff + 8)); + std::pair aPair = a; + assert(aPair.first.base() == globalBuff); + assert(aPair.second.base() == globalBuff + 8); + std::tuple aTuple = a; + assert(get<0>(aTuple).base() == globalBuff); + assert(get<1>(aTuple).base() == globalBuff + 8); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/ctor/range.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/ctor/range.pass.cpp new file mode 100644 index 000000000000..a6e03001cc86 --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/ctor/range.pass.cpp @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// class std::ranges::subrange; + +#include + +#include "../subrange_test_types.h" +#include +#include "test_macros.h" +#include "test_iterators.h" + +static_assert( std::is_constructible_v); // Default case. +static_assert(!std::is_constructible_v); // Not borrowed. +// Iter convertible to sentinel (pointer) type. +static_assert( std::is_constructible_v); +// Where neither iter or sentinel are pointers, but they are different. +static_assert( std::is_constructible_v); +static_assert( std::is_constructible_v); + +constexpr bool test() { + ForwardSubrange a{ForwardBorrowedRange()}; + assert(a.begin().base() == globalBuff); + assert(a.end().base() == globalBuff + 8); + + ConvertibleForwardSubrange b{ConvertibleForwardBorrowedRange()}; + assert(b.begin() == globalBuff); + assert(b.end() == globalBuff + 8); + + DifferentSentinelSubrange c{ForwardBorrowedRangeDifferentSentinel()}; + assert(c.begin().base() == globalBuff); + assert(c.end().value == globalBuff + 8); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/ctor/range_size.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/ctor/range_size.pass.cpp new file mode 100644 index 000000000000..fe71fee4f7ca --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/ctor/range_size.pass.cpp @@ -0,0 +1,21 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// class std::ranges::subrange; + +#include + +// Tested in pair_like_conv.pass.cpp. + +int main(int, char**) { + return 0; +} diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/general.compile.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/general.compile.pass.cpp new file mode 100644 index 000000000000..a7a944bc3374 --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/general.compile.pass.cpp @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// class std::ranges::subrange; + +#include + +#include +#include "test_macros.h" +#include "test_iterators.h" + +template +concept ValidSubrangeKind = requires { typename std::ranges::subrange; }; + +template +concept ValidSubrange = requires { typename std::ranges::subrange; }; + +static_assert( ValidSubrange>); +static_assert( ValidSubrange, forward_iterator>); +static_assert( ValidSubrangeKind, forward_iterator>); +static_assert( ValidSubrangeKind, forward_iterator>); +// Wrong sentinel type. +static_assert(!ValidSubrange, int*>); +static_assert( ValidSubrange); +static_assert( ValidSubrange); +// Must be sized. +static_assert(!ValidSubrangeKind); +static_assert( ValidSubrangeKind); +// Wrong sentinel type. +static_assert(!ValidSubrange>); +// Not an iterator. +static_assert(!ValidSubrange); diff --git a/libcxx/test/std/ranges/range.utility/range.subrange/subrange_test_types.h b/libcxx/test/std/ranges/range.utility/range.subrange/subrange_test_types.h new file mode 100644 index 000000000000..95326dbaacd1 --- /dev/null +++ b/libcxx/test/std/ranges/range.utility/range.subrange/subrange_test_types.h @@ -0,0 +1,215 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef SUBRANGE_TEST_TYPES_H +#define SUBRANGE_TEST_TYPES_H + +#include "test_macros.h" +#include "test_iterators.h" + +int globalBuff[8]; + +struct Empty {}; + +using InputIter = cpp17_input_iterator; +using ForwardIter = forward_iterator; +using BidirIter = bidirectional_iterator; + +using ForwardSubrange = std::ranges::subrange; +using SizedIntPtrSubrange = std::ranges::subrange; + +struct MoveOnlyForwardIter { + typedef std::forward_iterator_tag iterator_category; + typedef int value_type; + typedef std::ptrdiff_t difference_type; + typedef int* pointer; + typedef int& reference; + typedef MoveOnlyForwardIter self; + + int *base = nullptr; + + MoveOnlyForwardIter() = default; + MoveOnlyForwardIter(MoveOnlyForwardIter &&) = default; + MoveOnlyForwardIter &operator=(MoveOnlyForwardIter&&) = default; + MoveOnlyForwardIter(MoveOnlyForwardIter const&) = delete; + constexpr MoveOnlyForwardIter(int *ptr) : base(ptr) { } + + friend bool operator==(const self&, const self&); + friend constexpr bool operator==(const self& lhs, int* rhs) { return lhs.base == rhs; } + + reference operator*() const; + pointer operator->() const; + self& operator++(); + self operator++(int); + self& operator--(); + self operator--(int); + + constexpr operator pointer() const { return base; } +}; + +struct SizedSentinelForwardIter { + typedef std::forward_iterator_tag iterator_category; + typedef int value_type; + typedef std::ptrdiff_t difference_type; + typedef int* pointer; + typedef int& reference; + typedef std::make_unsigned_t udifference_type; + typedef SizedSentinelForwardIter self; + + int *base; + + SizedSentinelForwardIter() = default; + constexpr SizedSentinelForwardIter(int *ptr) : base(ptr) { } + + friend constexpr bool operator==(const self& lhs, const self& rhs) { return lhs.base == rhs.base; } + + reference operator*() const; + pointer operator->() const; + self& operator++(); + self operator++(int); + self& operator--(); + self operator--(int); + + static int minusCount; + friend constexpr difference_type operator-(SizedSentinelForwardIter const&a, + SizedSentinelForwardIter const&b) { + if (!std::is_constant_evaluated()) + minusCount++; + return a.base - b.base; + } +}; + +int SizedSentinelForwardIter::minusCount = 0; + +struct ConvertibleForwardIter { + typedef std::forward_iterator_tag iterator_category; + typedef int value_type; + typedef std::ptrdiff_t difference_type; + typedef int* pointer; + typedef int& reference; + typedef ConvertibleForwardIter self; + + int *base_ = nullptr; + + constexpr ConvertibleForwardIter() = default; + constexpr explicit ConvertibleForwardIter(int *ptr) : base_(ptr) { } + + friend bool operator==(const self&, const self&); + + reference operator*() const; + pointer operator->() const; + self& operator++(); + self operator++(int); + self& operator--(); + self operator--(int); + + constexpr operator pointer() const { return base_; } + + // Explicitly deleted so this doesn't model sized_sentinel_for. + friend constexpr difference_type operator-(int *, self const&) = delete; + friend constexpr difference_type operator-(self const&, int*) = delete; +}; +using ConvertibleForwardSubrange = std::ranges::subrange; +static_assert(std::is_convertible_v); + +template +struct ConditionallyConvertibleBase { + typedef std::forward_iterator_tag iterator_category; + typedef int value_type; + typedef std::ptrdiff_t difference_type; + typedef int* pointer; + typedef int& reference; + typedef std::make_unsigned_t udifference_type; + typedef ConditionallyConvertibleBase self; + + int *base_ = nullptr; + + constexpr ConditionallyConvertibleBase() = default; + constexpr explicit ConditionallyConvertibleBase(int *ptr) : base_(ptr) {} + + constexpr int *base() const { return base_; } + + friend bool operator==(const self&, const self&); + + reference operator*() const; + pointer operator->() const; + self& operator++(); + self operator++(int); + self& operator--(); + self operator--(int); + + template + requires E + constexpr operator pointer() const { return base_; } +}; +using ConditionallyConvertibleIter = ConditionallyConvertibleBase; +using SizedSentinelForwardSubrange = std::ranges::subrange; +using ConvertibleSizedSentinelForwardIter = ConditionallyConvertibleBase; +using ConvertibleSizedSentinelForwardSubrange = std::ranges::subrange; + +struct ForwardBorrowedRange { + constexpr ForwardIter begin() const { return ForwardIter(globalBuff); } + constexpr ForwardIter end() const { return ForwardIter(globalBuff + 8); } +}; + +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +struct ForwardRange { + ForwardIter begin() const; + ForwardIter end() const; +}; + +struct ConvertibleForwardBorrowedRange { + constexpr ConvertibleForwardIter begin() const { return ConvertibleForwardIter(globalBuff); } + constexpr int *end() const { return globalBuff + 8; } +}; + +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +struct ForwardBorrowedRangeDifferentSentinel { + struct sentinel { + int *value; + friend bool operator==(sentinel s, ForwardIter i) { return s.value == i.base(); } + }; + + constexpr ForwardIter begin() const { return ForwardIter(globalBuff); } + constexpr sentinel end() const { return sentinel{globalBuff + 8}; } +}; + +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +using DifferentSentinelSubrange = std::ranges::subrange; + +struct DifferentSentinelWithSizeMember { + struct sentinel { + int *value; + friend bool operator==(sentinel s, ForwardIter i) { return s.value == i.base(); } + }; + + constexpr ForwardIter begin() const { return ForwardIter(globalBuff); } + constexpr sentinel end() const { return sentinel{globalBuff + 8}; } + constexpr size_t size() const { return 8; } +}; + +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +using DifferentSentinelWithSizeMemberSubrange = std::ranges::subrange; + +#endif // SUBRANGE_TEST_TYPES_H