diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -48,6 +48,7 @@ __ranges/semiregular_box.h __ranges/size.h __ranges/subrange.h + __ranges/transform_view.h __ranges/ref_view.h __ranges/view_interface.h __split_buffer diff --git a/libcxx/include/__ranges/concepts.h b/libcxx/include/__ranges/concepts.h --- a/libcxx/include/__ranges/concepts.h +++ b/libcxx/include/__ranges/concepts.h @@ -72,6 +72,9 @@ range<_Range> && (is_lvalue_reference_v<_Range> || enable_borrowed_range>); + template + using __maybe_const = conditional_t<_Const, const _Tp, _Tp>; + } // namespace ranges #endif // !defined(_LIBCPP_HAS_NO_RANGES) diff --git a/libcxx/include/__ranges/subrange.h b/libcxx/include/__ranges/subrange.h --- a/libcxx/include/__ranges/subrange.h +++ b/libcxx/include/__ranges/subrange.h @@ -65,6 +65,7 @@ _Iter __begin = _Iter(); _Sent __end = _Sent(); + __subrange_base() = default; constexpr __subrange_base(_Iter __iter, _Sent __sent) : __begin(_VSTD::move(__iter)), __end(__sent) { } }; @@ -76,6 +77,7 @@ _Sent __end = _Sent(); make_unsigned_t> __size = 0; + __subrange_base() = default; constexpr __subrange_base(_Iter __iter, _Sent __sent, decltype(__size) __size) : __begin(_VSTD::move(__iter)), __end(__sent), __size(__size) { } }; diff --git a/libcxx/include/__ranges/transform_view.h b/libcxx/include/__ranges/transform_view.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__ranges/transform_view.h @@ -0,0 +1,353 @@ +// -*- 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_TRANSFORM_VIEW_H +#define _LIBCPP___RANGES_TRANSFORM_VIEW_H + +#include <__config> +#include <__iterator/iterator_traits.h> +#include <__iterator/concepts.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__ranges/empty.h> +#include <__ranges/semiregular_box.h> +#include <__ranges/view.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) + +// REVIEW-NOTE: This is *not* part of the transform_view patch and will be added by a *separate* PR. +template +concept indirectly_swappable = + indirectly_readable<_I1> && + indirectly_readable<_I2> && + requires(const _I1 i1, const _I2 i2) { + ranges::iter_swap(i1, i1); + ranges::iter_swap(i1, i2); + ranges::iter_swap(i2, i1); + ranges::iter_swap(i2, i2); + }; +// END-REVIEW-NOTE. + +namespace ranges { + +template + requires view<_View> && is_object_v<_Fn> && + regular_invocable<_Fn&, range_reference_t<_View>> && + __referenceable>> +class transform_view : public view_interface> { + template class __iterator; + template class __sentinel; + + _View __base = _View(); + __semiregular_box<_Fn> __func; + +public: + transform_view() = default; + constexpr transform_view(_View __base, _Fn __func) + : __base(_VSTD::move(__base)), __func(_VSTD::move(__func)) {} + + constexpr _View base() const& requires copy_constructible<_View> { return __base; } + constexpr _View base() && { return _VSTD::move(__base); } + + constexpr __iterator begin() { return __iterator{*this, ranges::begin(__base)}; } + constexpr __iterator begin() const + requires range && + regular_invocable> + { + return __iterator{*this, ranges::begin(__base)}; + } + + constexpr __sentinel end() { return __sentinel{ranges::end(__base)}; } + constexpr __iterator end() + requires common_range<_View> + { + return __iterator{*this, ranges::end(__base)}; + } + constexpr __sentinel end() const + requires range && + regular_invocable> + { + return __sentinel{ranges::end(__base)}; + } + constexpr __iterator end() const + requires common_range && + regular_invocable> + { + return __iterator{*this, ranges::end(__base)}; + } + + constexpr auto size() requires sized_range<_View> { return ranges::size(__base); } + constexpr auto size() const requires sized_range { return ranges::size(__base); } +}; + +template +transform_view(_Range&&, _Fn) + -> transform_view())), _Fn>; + +template +struct __iterator_concept { + using type = conditional_t< + random_access_range<_View>, + random_access_iterator_tag, + conditional_t< + bidirectional_range<_View>, + bidirectional_iterator_tag, + conditional_t< + forward_range<_View>, + forward_iterator_tag, + input_iterator_tag + > + > + >; +}; + +template +struct __iterator_category_base {}; + +template +struct __iterator_category_base<_View, _Fn> { + using _Cat = typename iterator_traits>::iterator_category; + + using iterator_category = conditional_t< + is_lvalue_reference_v>>, + conditional_t< + derived_from<_Cat, contiguous_iterator_tag>, + random_access_iterator_tag, + _Cat + >, + input_iterator_tag + >; +}; + +template + requires view<_View> && is_object_v<_Fn> && + regular_invocable<_Fn&, range_reference_t<_View>> && + __referenceable>> +template +class transform_view<_View, _Fn>::__iterator + : public __iterator_category_base<_View, _Fn> { + + using _Parent = __maybe_const<_Const, transform_view>; + using _Base = __maybe_const<_Const, _View>; + + iterator_t<_Base> __current = iterator_t<_Base>(); + _Parent *__parent = nullptr; + + template + friend class transform_view<_View, _Fn>::__sentinel; + +public: + using iterator_concept = typename __iterator_concept<_View>::type; + using value_type = remove_cvref_t>>; + using difference_type = range_difference_t<_Base>; + + __iterator() = default; + + constexpr __iterator(_Parent& __parent, iterator_t<_Base> __current) + : __current(_VSTD::move(__current)), __parent(_VSTD::addressof(__parent)) {} + constexpr __iterator(__iterator __i) + requires _Const && convertible_to, iterator_t<_Base>> + : __current(_VSTD::move(__i.__current)), __parent(__i.__parent) {} + + constexpr iterator_t<_Base> base() const& + requires copyable> + { + return __current; + } + constexpr iterator_t<_Base> base() && { + return _VSTD::move(__current); + } + + constexpr decltype(auto) operator*() const { return _VSTD::invoke(*__parent->__func, *__current); } + + constexpr __iterator& operator++() { + ++__current; + return *this; + } + constexpr void operator++(int) { ++__current; } + constexpr __iterator operator++(int) + requires forward_range<_Base> + { + auto __tmp = *this; + ++*this; + return __tmp; + } + + constexpr __iterator& operator--() + requires bidirectional_range<_Base> + { + --__current; + return *this; + } + constexpr __iterator operator--(int) + requires bidirectional_range<_Base> + { + auto __tmp = *this; + --*this; + return __tmp; + } + + constexpr __iterator& operator+=(difference_type __n) + requires random_access_range<_Base> + { + __current += __n; + return *this; + } + constexpr __iterator& operator-=(difference_type __n) + requires random_access_range<_Base> + { + __current -= __n; + return *this; + } + constexpr decltype(auto) operator[](difference_type __n) const + requires random_access_range<_Base> + { + return _VSTD::invoke(*__parent->__func, __current[__n]); + } + + friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) + requires equality_comparable> + { + return __x.__current == __y.__current; + } + + friend constexpr bool operator<(const __iterator& __x, const __iterator& __y) + requires random_access_range<_Base> + { + return __x.__current < __y.__current; + } + friend constexpr bool operator>(const __iterator& __x, const __iterator& __y) + requires random_access_range<_Base> + { + return __x.__current > __y.__current; + } + friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y) + requires random_access_range<_Base> + { + return __x.__current <= __y.__current; + } + friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y) + requires random_access_range<_Base> + { + return __x.__current >= __y.__current; + } +// friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y) +// requires random_access_range<_Base> && three_way_comparable> +// { +// return __x.__current <=> __y.__current; +// } + + friend constexpr __iterator operator+(__iterator __i, difference_type __n) + requires random_access_range<_Base> + { + return __iterator{*__i.__parent, __i.__current + __n}; + } + friend constexpr __iterator operator+(difference_type __n, __iterator __i) + requires random_access_range<_Base> + { + return __iterator{*__i.__parent, __i.__current + __n}; + } + + friend constexpr __iterator operator-(__iterator __i, difference_type __n) + requires random_access_range<_Base> + { + return __iterator{*__i.__parent, __i.__current - __n}; + } + friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y) + requires sized_sentinel_for, iterator_t<_Base>> + { + return __x.__current - __y.__current; + } + + friend constexpr decltype(auto) iter_move(const __iterator& __i) + // TODO: this noexcept looks wrong: why do we care about func's noexceptness? + noexcept(noexcept(_VSTD::invoke(*__i.__parent->__func, *__i.__current))) + { + // TODO: this looks wrong: shouldn't this be the oposite. + if constexpr (is_lvalue_reference_v) + return _VSTD::move(*__i); + else + return *__i; + } + + friend constexpr void iter_swap(const __iterator& __x, const __iterator& __y) + noexcept(noexcept(ranges::iter_swap(__x.__current, __y.__current))) + requires indirectly_swappable> + { + ranges::iter_swap(__x.__current, __y.__current); + } +}; + +template + requires view<_View> && is_object_v<_Fn> && + regular_invocable<_Fn&, range_reference_t<_View>> && + __referenceable>> +template +class transform_view<_View, _Fn>::__sentinel { + using _Parent = __maybe_const<_Const, transform_view>; + using _Base = __maybe_const<_Const, _View>; + + sentinel_t<_Base> __end = sentinel_t<_Base>(); + + template + friend class transform_view<_View, _Fn>::__iterator; + + template + friend class transform_view<_View, _Fn>::__sentinel; + +public: + __sentinel() = default; + + constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end(__end) {} + constexpr __sentinel(__sentinel __i) + requires _Const && convertible_to, sentinel_t<_Base>> + : __end(_VSTD::move(__i.__end)) {} + + constexpr sentinel_t<_Base> base() const { return __end; } + + template + requires sentinel_for, iterator_t<__maybe_const<_OtherConst, _View>>> + friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel __y) { + return __x.__current == __y.__end; + } + + template + requires sized_sentinel_for, iterator_t<__maybe_const<_OtherConst, _View>>> + friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>> + operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) { + return __x.__current - __y.__end; + } + + template + requires sized_sentinel_for, iterator_t<__maybe_const<_OtherConst, _View>>> + friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>> + operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) { + return __x.__end - __y.__current; + } +}; + +} // namespace ranges + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___RANGES_TRANSFORM_VIEW_H diff --git a/libcxx/include/ranges b/libcxx/include/ranges --- a/libcxx/include/ranges +++ b/libcxx/include/ranges @@ -85,6 +85,7 @@ #include <__ranges/view.h> #include <__ranges/size.h> #include <__ranges/subrange.h> +#include <__ranges/transform_view.h> #include <__ranges/ref_view.h> #include <__ranges/view_interface.h> #include // Required by the standard. diff --git a/libcxx/test/std/ranges/range.adaptors/range.transform/range.transform.view.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.transform/range.transform.view.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.transform/range.transform.view.pass.cpp @@ -0,0 +1,609 @@ +//===----------------------------------------------------------------------===// +// +// 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::transform_view; + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include "test_macros.h" +#include "test_iterators.h" + +namespace ranges = std::ranges; + +int globalBuff[8] = {0,1,2,3,4,5,6,7}; + +template +concept ValidDropView = requires { typename ranges::transform_view; }; + +struct View : std::ranges::view_base { + int start; + int *ptr; + constexpr View(int* ptr = globalBuff, int start = 0) : start(start), ptr(ptr) {} + constexpr View(View&&) = default; + constexpr View& operator=(View&&) = default; + constexpr friend int* begin(View& view) { return view.ptr + view.start; } + constexpr friend int* begin(View const& view) { return view.ptr + view.start; } + constexpr friend int* end(View& view) { return view.ptr + 8; } + constexpr friend int* end(View const& view) { return view.ptr + 8; } +}; + +struct CopyableView : std::ranges::view_base { + int start; + constexpr CopyableView(int start = 0) : start(start) {} + constexpr CopyableView(CopyableView const&) = default; + constexpr CopyableView& operator=(CopyableView const&) = default; + constexpr friend int* begin(CopyableView& view) { return globalBuff + view.start; } + constexpr friend int* begin(CopyableView const& view) { return globalBuff + view.start; } + constexpr friend int* end(CopyableView&) { return globalBuff + 8; } + constexpr friend int* end(CopyableView const&) { return globalBuff + 8; } +}; + +using ForwardIter = forward_iterator; +struct ForwardView : std::ranges::view_base { + int *ptr; + constexpr ForwardView(int* ptr = globalBuff) : ptr(ptr) {} + constexpr ForwardView(ForwardView&&) = default; + constexpr ForwardView& operator=(ForwardView&&) = default; + constexpr friend ForwardIter begin(ForwardView& view) { return ForwardIter(view.ptr); } + constexpr friend ForwardIter begin(ForwardView const& view) { return ForwardIter(view.ptr); } + constexpr friend ForwardIter end(ForwardView& view) { return ForwardIter(view.ptr + 8); } + constexpr friend ForwardIter end(ForwardView const& view) { return ForwardIter(view.ptr + 8); } +}; + +struct ForwardRange { + ForwardIter begin() const; + ForwardIter end() const; + ForwardIter begin(); + ForwardIter end(); +}; + +using RandomAccessIter = random_access_iterator; +struct RandomAccessView : std::ranges::view_base { + RandomAccessIter begin() const; + RandomAccessIter end() const; + RandomAccessIter begin(); + RandomAccessIter end(); +}; + +using BidirectionalIter = bidirectional_iterator; +struct BidirectionalView : std::ranges::view_base { + BidirectionalIter begin() const; + BidirectionalIter end() const; + BidirectionalIter begin(); + BidirectionalIter end(); +}; + +struct BorrowableRange { + friend int* begin(BorrowableRange const& range); + friend int* end(BorrowableRange const&); + friend int* begin(BorrowableRange& range); + friend int* end(BorrowableRange&); +}; + +template<> +inline constexpr bool ranges::enable_borrowed_range = true; + +struct InputView : std::ranges::view_base { + int *ptr; + constexpr InputView(int* ptr = globalBuff) : ptr(ptr) {} + constexpr cpp20_input_iterator begin() const { return cpp20_input_iterator(ptr); } + constexpr int* end() const { return ptr + 8; } + constexpr cpp20_input_iterator begin() { return cpp20_input_iterator(ptr); } + constexpr int* end() { return ptr + 8; } +}; + +constexpr bool operator==(const cpp20_input_iterator &lhs, int* rhs) { return lhs.base() == rhs; } +constexpr bool operator==(int* lhs, const cpp20_input_iterator &rhs) { return rhs.base() == lhs; } + +struct SentinelView : std::ranges::view_base { + int count; + constexpr SentinelView(int count = 8) : count(count) {} + constexpr RandomAccessIter begin() const { return RandomAccessIter(globalBuff); } + constexpr int* end() const { return globalBuff + count; } + constexpr RandomAccessIter begin() { return RandomAccessIter(globalBuff); } + constexpr int* end() { return globalBuff + count; } +}; + +constexpr long operator- (const RandomAccessIter &lhs, int* rhs) { return lhs.base() - rhs; } +constexpr long operator- (int* lhs, const RandomAccessIter &rhs) { return lhs - rhs.base(); } +constexpr bool operator==(const RandomAccessIter &lhs, int* rhs) { return lhs.base() == rhs; } +constexpr bool operator==(int* lhs, const RandomAccessIter &rhs) { return rhs.base() == lhs; } + +struct SizedSentinelNotConstView : std::ranges::view_base { + ForwardIter begin() const; + int *end() const; + ForwardIter begin(); + int *end(); + size_t size(); +}; +bool operator==(const ForwardIter &lhs, int* rhs); +bool operator==(int* lhs, const ForwardIter &rhs); + +struct Range { + friend int* begin(Range const&); + friend int* end(Range const&); + friend int* begin(Range&); + friend int* end(Range&); +}; + +using CountedIter = stride_counting_iterator>; +struct CountedView : std::ranges::view_base { + constexpr CountedIter begin() { return CountedIter(ForwardIter(globalBuff)); } + constexpr CountedIter begin() const { return CountedIter(ForwardIter(globalBuff)); } + constexpr CountedIter end() { return CountedIter(ForwardIter(globalBuff + 8)); } + constexpr CountedIter end() const { return CountedIter(ForwardIter(globalBuff + 8)); } +}; + +struct PlusPlus { + constexpr int operator()(int x) { return ++x; } +}; + +struct PlusPlusConst { + constexpr int operator()(int x) const { return ++x; } +}; + +struct PlusPlusRef { + constexpr int& operator()(int x) { return ++x; } +}; + +template +concept BeginInvocable = requires(T t) { t.begin(); }; + +template +concept EndInvocable = requires(T t) { t.end(); }; + +template +concept EndIsIter = requires(T t) { ++t.end(); }; + +template +concept SizeInvocable = requires(T t) { t.size(); }; + +constexpr void testViewMembers() { + { + // transform_view::base + std::ranges::transform_view transformView1; + auto base1 = std::move(transformView1).base(); + assert(ranges::begin(base1) == globalBuff); + + std::ranges::transform_view transformView2; + auto base2 = transformView2.base(); + assert(ranges::begin(base2) == globalBuff); + auto base3 = std::move(transformView2).base(); + assert(ranges::begin(base3) == globalBuff); + + const std::ranges::transform_view transformView3; + const auto base4 = transformView3.base(); + assert(ranges::begin(base4) == globalBuff); + const auto base5 = std::move(transformView3).base(); + assert(ranges::begin(base5) == globalBuff); + } + + { + // transform_view::begin + int buff[8] = {0, 1, 2, 3, 4, 5, 6, 7}; + std::ranges::transform_view transformView1(View{buff}, PlusPlus{}); + assert(transformView1.begin().base() == buff); + assert(*transformView1.begin() == 1); + + std::ranges::transform_view transformView2(ForwardView{buff}, PlusPlus{}); + assert(transformView2.begin().base().base() == buff); + assert(*transformView2.begin() == 1); + + std::ranges::transform_view transformView3(InputView{buff}, PlusPlus{}); + assert(transformView3.begin().base().base() == buff); + assert(*transformView3.begin() == 1); + + const std::ranges::transform_view transformView4(View{buff}, PlusPlusConst{}); + assert(*transformView4.begin() == 1); + + static_assert(!BeginInvocable>); + } + + { + // transform_view::end + std::ranges::transform_view transformView1(View{}, PlusPlus{}); + assert(transformView1.end().base() == globalBuff + 8); + + std::ranges::transform_view transformView2(ForwardView{}, PlusPlus{}); + assert(transformView2.end().base().base() == globalBuff + 8); + + std::ranges::transform_view transformView3(InputView{}, PlusPlus{}); + assert(transformView3.end().base() == globalBuff + 8); + + const std::ranges::transform_view transformView4(View{}, PlusPlusConst{}); + assert(transformView4.end().base() == globalBuff + 8); + + static_assert(!EndInvocable>); + static_assert( EndInvocable< std::ranges::transform_view>); + static_assert( EndInvocable>); + static_assert(!EndInvocable>); + static_assert( EndInvocable< std::ranges::transform_view>); + static_assert( EndInvocable>); + + static_assert(!EndIsIter>); + static_assert(!EndIsIter< std::ranges::transform_view>); + static_assert( EndIsIter>); + static_assert( EndIsIter< std::ranges::transform_view>); + } + + { + // transform_view::size + std::ranges::transform_view transformView1(View{}, PlusPlus{}); + assert(transformView1.size() == 8); + + const std::ranges::transform_view transformView2(View{globalBuff, 4}, PlusPlus{}); + assert(transformView2.size() == 4); + + static_assert(!SizeInvocable>); + + static_assert(SizeInvocable>); + static_assert(!SizeInvocable>); + } +} + +template +concept HasIterConcept = requires { typename std::ranges::transform_view::iterator_category; }; + +template +concept BaseInvocable = requires(std::ranges::iterator_t> iter) { + iter.base(); +}; + +constexpr void testIterator() { + // Member typedefs. + { + // Member typedefs for contiguous iterator. + static_assert(std::same_as::iterator_concept, std::contiguous_iterator_tag>); + static_assert(std::same_as::iterator_category, std::random_access_iterator_tag>); + + using TView = std::ranges::transform_view; + using TIter = std::ranges::iterator_t; + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + } + { + // Member typedefs for random access iterator. + using TView = std::ranges::transform_view; + using TIter = std::ranges::iterator_t; + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + } + { + // Member typedefs for random access iterator/not-lvalue-ref. + using TView = std::ranges::transform_view; + using TIter = std::ranges::iterator_t; + static_assert(std::same_as); + static_assert(std::same_as); // Note: this is now input_iterator_tag. + static_assert(std::same_as); + static_assert(std::same_as); + } + { + // Member typedefs for bidirectional iterator. + using TView = std::ranges::transform_view; + using TIter = std::ranges::iterator_t; + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + } + { + // Member typedefs for forward iterator. + using TView = std::ranges::transform_view; + using TIter = std::ranges::iterator_t; + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + } + { + // Member typedefs for input iterator. + using TView = std::ranges::transform_view; + using TIter = std::ranges::iterator_t; + static_assert(std::same_as); + static_assert(!HasIterConcept); + static_assert(std::same_as); + static_assert(std::same_as); + } + + // Members + { + // __iterator::base + std::ranges::transform_view transformView1; + auto iter1 = std::move(transformView1).begin(); + assert(iter1.base() == globalBuff); + assert(std::move(iter1).base() == globalBuff); + + std::ranges::transform_view transformView2; + auto iter2 = transformView2.begin(); + assert(std::move(iter2).base() == globalBuff); + static_assert(!BaseInvocable); + } + // Note: __iterator::operator* tested elsewhere. + // Note: member operator requirements tested at the end of the file. + { + // __iterator::operator{++,--,+=,-=} + std::ranges::transform_view transformView1; + auto iter1 = std::move(transformView1).begin(); + assert((++iter1).base() == globalBuff + 1); + + assert((iter1++).base() == globalBuff + 1); + assert(iter1.base() == globalBuff + 2); + + assert((--iter1).base() == globalBuff + 1); + assert((iter1--).base() == globalBuff + 1); + assert(iter1.base() == globalBuff); + + // Check that decltype(InputIter++) == void. + ASSERT_SAME_TYPE(decltype( + std::declval>>()++), + void); + + assert((iter1 += 4).base() == globalBuff + 4); + assert((iter1 -= 3).base() == globalBuff + 1); + } + { + // __iterator::operator[] + int buff[8] = {0, 1, 2, 3, 4, 5, 6, 7}; + std::ranges::transform_view transformView1(View{buff}, PlusPlus{}); + auto iter1 = std::move(transformView1).begin() + 1; + assert(iter1[0] == 2); + assert(iter1[4] == 6); + } + { + // __iterator::operator{<,>,<=,>=} + std::ranges::transform_view transformView1; + auto iter1 = std::move(transformView1).begin(); + std::ranges::transform_view transformView2; + auto iter2 = std::move(transformView2).begin(); + assert(iter1 == iter2); + assert(iter1 + 1 != iter2); + assert(iter1 + 1 == iter2 + 1); + + assert(iter1 < iter1 + 1); + assert(iter1 + 1 > iter1); + assert(iter1 <= iter1 + 1); + assert(iter1 <= iter2); + assert(iter1 + 1 >= iter2); + assert(iter1 >= iter2); + // TODO: operator<=> when three_way_comparable is implemented. + } + { + // __iterator::operator{+,-} + std::ranges::transform_view transformView1; + auto iter1 = std::move(transformView1).begin(); + std::ranges::transform_view transformView2; + auto iter2 = std::move(transformView2).begin(); + iter1 += 4; + assert((iter1 + 1).base() == globalBuff + 5); + assert((1 + iter1).base() == globalBuff + 5); + assert((iter1 - 1).base() == globalBuff + 3); + assert(iter1 - iter2 == 4); + } + // TODO: I'm not going to test iter_move until the TODOs in transform_view.h are addressed. + { + // __iterator::iter_swap + int buff[8] = {0, 1, 2, 3, 4, 5, 6, 7}; + std::ranges::transform_view transformView1(View{buff}, PlusPlus{}); + auto iter1 = std::move(transformView1).begin(); + std::ranges::transform_view transformView2(View{buff}, PlusPlus{}); + auto iter2 = std::move(transformView2).begin(); + iter1 += 4; + std::ranges::iter_swap(iter1, iter2); + assert(*iter1 == 1); + assert(*(buff + 4) == 0); + assert(*iter2 == 5); + assert(*buff == 4); + assert(*transformView1.begin() == 5); + } +} + +constexpr void testSentinel() { + std::ranges::transform_view transformView1; + // Going to const and back. + auto sent1 = transformView1.end(); + std::ranges::sentinel_t> sent2{sent1}; + std::ranges::sentinel_t> sent3{sent2}; + (void)sent3; + + std::ranges::transform_view transformView2(SentinelView{4}, PlusPlusConst()); + auto sent4 = transformView2.end(); + + static_assert(!EndIsIter); + static_assert(!EndIsIter); + assert(sent1.base() == globalBuff + 8); + + auto iter1 = transformView1.begin(); + assert(iter1 != sent1); + assert(iter1 != sent2); + assert(iter1 != sent4); + assert(iter1 + 8 == sent1); + assert(iter1 + 8 == sent2); + assert(iter1 + 4 == sent4); + + assert(sent1 - iter1 == 8); + assert(sent4 - iter1 == 4); + assert(iter1 - sent1 == -8); + assert(iter1 - sent4 == -4); +} + +// CTAD +static_assert(std::same_as>); +static_assert(std::same_as(), PlusPlus())), + std::ranges::transform_view, PlusPlus>>); +static_assert(std::same_as, PlusPlus>>); + +constexpr bool test() { + testViewMembers(); + testIterator(); + testSentinel(); + + return true; +} + +template +auto toUpper(R range) { + return std::ranges::transform_view(range, [](char c) { return std::toupper(c); }); +} + +template>> +auto withRandom(R range, Fn func = Fn()) { + return std::ranges::transform_view(range, std::bind_front(func, std::rand())); +} + +template> +auto joinArrays(E1 (&a)[N], E2 (&b)[N], Join join = Join()) { + return std::ranges::transform_view(a, [&a, &b, join](auto& x) { + auto idx = (&x) - a; + return join(x, b[idx]); + }); +} + +template<> +constexpr bool ranges::enable_view> = true; + +template<> +constexpr bool ranges::enable_view = true; + +int main(int, char**) { + test(); + static_assert(test()); + + { + std::vector vec = {1,2,3,4}; + auto sortOfRandom = withRandom(vec); + std::vector check = {16808, 16809, 16810, 16811}; + assert(std::equal(sortOfRandom.begin(), sortOfRandom.end(), check.begin(), check.end())); + } + + { + int a[4] = {1, 2, 3, 4}; + int b[4] = {4, 3, 2, 1}; + auto out = joinArrays(a, b); + int check[4] = {5, 5, 5, 5}; + assert(std::equal(out.begin(), out.end(), check)); + } + + { + std::string_view str = "Hello, World."; + auto upp = toUpper(str); + std::string_view check = "HELLO, WORLD."; + assert(std::equal(upp.begin(), upp.end(), check.begin(), check.end())); + } + + return 0; +} + +// The requirements for __iterator: +template +concept IterMinusMinus = requires(std::ranges::iterator_t> iter) { + iter--; +}; +static_assert(IterMinusMinus); +static_assert(!IterMinusMinus); + +template +concept MinusMinusIter = requires(std::ranges::iterator_t> iter) { + --iter; +}; +static_assert(MinusMinusIter); +static_assert(!MinusMinusIter); + +template +concept IterPlusEquals = requires(std::ranges::iterator_t> iter) { + iter += 1; +}; +static_assert(IterPlusEquals); +static_assert(!IterPlusEquals); + +template +concept IterMinusEquals = requires(std::ranges::iterator_t> iter) { + iter -= 1; +}; +static_assert(IterMinusEquals); +static_assert(!IterMinusEquals); + +template +concept IterSubscript = requires(std::ranges::iterator_t> iter) { + iter[0]; +}; +static_assert(IterSubscript); +static_assert(!IterSubscript); + +template +concept IterLess = requires(std::ranges::iterator_t> iter) { + iter < iter; +}; +static_assert(IterLess); +static_assert(!IterLess); + +template +concept IterGreater = requires(std::ranges::iterator_t> iter) { + iter > iter; +}; +static_assert(IterGreater); +static_assert(!IterGreater); + +template +concept IterLessEq = requires(std::ranges::iterator_t> iter) { + iter <= iter; +}; +static_assert(IterLessEq); +static_assert(!IterLessEq); + +template +concept IterGreaterEq = requires(std::ranges::iterator_t> iter) { + iter >= iter; +}; +static_assert(IterGreaterEq); +static_assert(!IterGreaterEq); + +template +concept IterPlusN = requires(std::ranges::iterator_t> iter) { + iter + 1; +}; +static_assert(IterPlusN); +static_assert(!IterPlusN); + +template +concept NPlusIter = requires(std::ranges::iterator_t> iter) { + std::declval() + iter; +}; +static_assert(NPlusIter); +static_assert(!NPlusIter); + +template +concept IterMinusN = requires(std::ranges::iterator_t> iter) { + iter - 1; +}; +static_assert(IterMinusN); +static_assert(!IterMinusN); + +template +concept IterMinusIter = requires(std::ranges::iterator_t> iter) { + iter - iter; +}; +static_assert(IterMinusIter); +static_assert(!IterMinusIter);