-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the make_transform_view functions
- Loading branch information
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) |
71 changes: 71 additions & 0 deletions
71
src/tools/ranges/make_transform_view/test/make_transform_view.test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |