Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed May 31, 2024
1 parent cbdb29b commit 7295433
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
41 changes: 26 additions & 15 deletions src/tools/std23/views/test/zip.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) {

Expand All @@ -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 > );
Expand All @@ -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
Expand Down
27 changes: 19 additions & 8 deletions src/tools/std23/views/zip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,29 @@ 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 ) )... );
},
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<void>(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,
Expand Down Expand Up @@ -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 >
Expand Down Expand Up @@ -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_ ) );
Expand Down

0 comments on commit 7295433

Please sign in to comment.