From 729543397cefaa1941be060bd60b9409d43239c3 Mon Sep 17 00:00:00 2001 From: Wim Haeck Date: Fri, 31 May 2024 12:01:28 -0600 Subject: [PATCH] Updates --- src/tools/std23/views/test/zip.test.cpp | 41 ++++++++++++++++--------- src/tools/std23/views/zip.hpp | 27 +++++++++++----- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/tools/std23/views/test/zip.test.cpp b/src/tools/std23/views/test/zip.test.cpp index 7e5f7c5..43c87a3 100644 --- a/src/tools/std23/views/test/zip.test.cpp +++ b/src/tools/std23/views/test/zip.test.cpp @@ -17,9 +17,20 @@ SCENARIO( "zip_view" ) { GIVEN( "a container with random access iterators" ) { - std::vector< int > values1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + std::vector< std::tuple< int, char, std::string > > equal = { + + { 1, 'a', "ab" }, + { 2, 'b', "cd" }, + { 3, 'c', "ef" }, + { 4, 'd', "gh" }, + { 5, 'e', "ij" }, + { 6, 'f', "kl" }, + { 7, 'g', "mn" } + }; + + std::vector< int > values1 = { 1, 2, 3, 4, 5, 6, 7 }; std::vector< char > values2 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; - std::vector< std::string > values3 = { "ab", "cd", "ef", "gh", "ij", "kl", "mn", "op", "qr" }; + std::vector< std::string > values3 = { "ab", "cd", "ef", "gh", "ij", "kl", "mn", "op" }; WHEN( "when iterators are used" ) { @@ -29,8 +40,6 @@ SCENARIO( "zip_view" ) { THEN( "the stride_view satisfies the required concepts" ) { - CHECK( std20::ranges::viewable_range< Range > ); - CHECK( std20::ranges::range< Range > ); CHECK( std20::ranges::view< Range > ); CHECK( std20::ranges::sized_range< Range > ); @@ -43,21 +52,23 @@ SCENARIO( "zip_view" ) { THEN( "a stride_view can be constructed and members can be tested" ) { - CHECK( 9 == chunk.size() ); + CHECK( 7 == chunk.size() ); CHECK( false == chunk.empty() ); CHECK( true == bool( chunk ) ); -// CHECK( std20::ranges::equal( equal, chunk ) ); -// -// CHECK( equal[0] == chunk.front() ); -// CHECK( equal[4] == chunk.back() ); -// -// CHECK( 1 == chunk[0] ); -// CHECK( 3 == chunk[1] ); -// CHECK( 5 == chunk[2] ); -// CHECK( 7 == chunk[3] ); -// CHECK( 9 == chunk[4] ); + CHECK( std20::ranges::equal( equal, chunk ) ); + + CHECK( equal[0] == chunk.front() ); + CHECK( equal[6] == chunk.back() ); + + CHECK( std::tuple< int, char, std::string >{ 1, 'a', "ab" } == chunk[0] ); + CHECK( std::tuple< int, char, std::string >{ 2, 'b', "cd" } == chunk[1] ); + CHECK( std::tuple< int, char, std::string >{ 3, 'c', "ef" } == chunk[2] ); + CHECK( std::tuple< int, char, std::string >{ 4, 'd', "gh" } == chunk[3] ); + CHECK( std::tuple< int, char, std::string >{ 5, 'e', "ij" } == chunk[4] ); + CHECK( std::tuple< int, char, std::string >{ 6, 'f', "kl" } == chunk[5] ); + CHECK( std::tuple< int, char, std::string >{ 7, 'g', "mn" } == chunk[6] ); } // THEN } // WHEN } // GIVEN diff --git a/src/tools/std23/views/zip.hpp b/src/tools/std23/views/zip.hpp index 93eabb7..e5fb36e 100644 --- a/src/tools/std23/views/zip.hpp +++ b/src/tools/std23/views/zip.hpp @@ -25,11 +25,11 @@ auto tuple_or_pair_test() -> std::enable_if_t< ( sizeof...( T ) != 2 ), std::tup template < typename... T > using tuple_or_pair = decltype( tuple_or_pair_test< T...>() ); -template < typename Function, typename Tuple, typename... Ts > +template < typename Function, typename Tuple > constexpr auto tuple_transform( Function&& f, Tuple&& tuple ) { return std::apply( - [&] ( Ts&&... elements ) { + [&]< typename... Ts >( Ts&&... elements ) { return tuple_or_pair< std::invoke_result_t< Function&, Ts >... >( std::invoke( f, std::forward< Ts >( elements ) )... ); @@ -37,6 +37,17 @@ constexpr auto tuple_transform( Function&& f, Tuple&& tuple ) { std::forward< Tuple >( tuple ) ); } +template < typename Function, typename Tuple > +constexpr void tuple_for_each( Function&& f, Tuple&& tuple ) { + + std::apply( + [&]< typename... Ts >( Ts&&... elements ) { + + (static_cast(std::invoke( f, std::forward< Ts >(elements))), ...); + }, + std::forward< Tuple >(tuple)); +} + template < typename Function, typename LeftTuple, typename RightTuple, std::size_t... Indices > constexpr tuple_or_pair< std20::invoke_result_t< Function&, typename std::tuple_element< Indices, std20::remove_cvref_t< LeftTuple > >::type, @@ -313,11 +324,11 @@ struct zip_view : std20::ranges::view_interface< zip_view< Rs... > > { sentinel() = default; -// template < typename = std::enable_if_t< -// Const && -// ( std20::convertible_to< -// std20::ranges::sentinel_t< Rs >, -// std20::ranges::sentinel_t< maybe_const< Const, Rs > > > && ... ), int > > + template < typename = std::enable_if_t< + Const && + ( std20::convertible_to< + std20::ranges::sentinel_t< Rs >, + std20::ranges::sentinel_t< maybe_const< Const, Rs > > > && ... ), int > > constexpr sentinel( sentinel< ! Const > iter ) : end_( std::move( iter.end_ ) ) {} template < bool Other > @@ -364,7 +375,7 @@ struct zip_view : std20::ranges::view_interface< zip_view< Rs... > > { constexpr explicit zip_view( Rs... views ) : views_( std::move( views )... ) {} -// template < typename = std::enable_if_t< ! ( std20::ranges::detail::simple_view< RRs > && ... ), int > > +// template < typename = std::enable_if_t< ! ( std20::ranges::detail::simple_view< Rs > && ... ), int > > constexpr auto begin() { return iterator< false >( tuple_transform( std20::ranges::begin, this->views_ ) );