Skip to content

Commit

Permalink
Adding the make_transform_view functions
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Oct 12, 2023
1 parent 02c6248 commit 5f2850f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmake/unit_testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ add_subdirectory( src/tools/Log/test )
add_subdirectory( src/tools/ranges/IteratorView/test )
add_subdirectory( src/tools/ranges/TransformIterator/test )
add_subdirectory( src/tools/ranges/make_view/test )
add_subdirectory( src/tools/ranges/make_transform_view/test )
1 change: 1 addition & 0 deletions src/tools/ranges.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
#include "tools/ranges/TransformIterator.hpp"

#include "tools/ranges/make_view.hpp"
#include "tools/ranges/make_transform_view.hpp"
48 changes: 48 additions & 0 deletions src/tools/ranges/make_transform_view.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef NJOY_TOOLS_RANGES_MAKE_TRANSFORM_VIEW
#define NJOY_TOOLS_RANGES_MAKE_TRANSFORM_VIEW

// system includes

// other includes
#include "tools/ranges/IteratorView.hpp"
#include "tools/ranges/TransformIterator.hpp"

namespace njoy {
namespace tools {
namespace ranges {

/**
* @brief Make an IteratorView based on two iterators and a transformation
*
* @param[in] begin the iterator to the beginning of the view
* @param[in] end the iterator to the end of the view
* @param[in] transformation the transformation to be applied
*/
template < typename Iterator, typename Transformation >
constexpr auto make_transform_view( Iterator begin, Iterator end,
Transformation transformation ) {

using Iter = TransformIterator< Iterator, Transformation >;
return IteratorView< Iter >{ Iter( std::move( begin ), transformation ),
Iter( std::move( end ), transformation ) };
}

/**
* @brief Make an IteratorView based on a container and a transformation
*
* @param[in] container the container
* @param[in] transformation the transformation to be applied
*/
template < typename Container, typename Transformation >
constexpr auto make_transform_view( Container&& container,
Transformation transformation ) {

return make_transform_view( container.begin(), container.end(),
std::move( transformation ) );
}

} // ranges namespace
} // tools namespace
} // njoy namespace

#endif
1 change: 1 addition & 0 deletions src/tools/ranges/make_transform_view/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_cpp_test( ranges.make_transform_view make_transform_view.test.cpp )
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// include Catch2
#include <catch2/catch_test_macros.hpp>

// what we are testing
#include "tools/ranges/make_transform_view.hpp"

// other includes

// convenience typedefs
using namespace njoy::tools::ranges;

SCENARIO( "make_view" ) {

GIVEN( "a container with values and a transformation" ) {

std::vector< int > values = { -2, -1, 0, 1, 2 };
auto transform = [] ( auto&& value ) { return 2 * value; };

WHEN( "when using iterators and the transformation" ) {

auto chunk = make_transform_view( values.begin(), values.end(), transform );

THEN( "an IteratorView can be constructed and members can be tested" ) {

CHECK( 5 == chunk.size() );
CHECK( false == chunk.empty() );

CHECK( -4 == chunk[0] );
CHECK( -2 == chunk[1] );
CHECK( 0 == chunk[2] );
CHECK( 2 == chunk[3] );
CHECK( 4 == chunk[4] );

CHECK( -4 == chunk.at( 0 ) );
CHECK( -2 == chunk.at( 1 ) );
CHECK( 0 == chunk.at( 2 ) );
CHECK( 2 == chunk.at( 3 ) );
CHECK( 4 == chunk.at( 4 ) );

CHECK( -4 == chunk.front() );
CHECK( 4 == chunk.back() );
} // THEN
} // WHEN

WHEN( "when using the container and the transformation" ) {

auto chunk = make_transform_view( values, transform );

THEN( "an IteratorView can be constructed and members can be tested" ) {

CHECK( 5 == chunk.size() );
CHECK( false == chunk.empty() );

CHECK( -4 == chunk[0] );
CHECK( -2 == chunk[1] );
CHECK( 0 == chunk[2] );
CHECK( 2 == chunk[3] );
CHECK( 4 == chunk[4] );

CHECK( -4 == chunk.at( 0 ) );
CHECK( -2 == chunk.at( 1 ) );
CHECK( 0 == chunk.at( 2 ) );
CHECK( 2 == chunk.at( 3 ) );
CHECK( 4 == chunk.at( 4 ) );

CHECK( -4 == chunk.front() );
CHECK( 4 == chunk.back() );
} // THEN
} // WHEN
} // GIVEN
} // SCENARIO

0 comments on commit 5f2850f

Please sign in to comment.