-
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.
Merge pull request #6 from njoy/feature/TransformIterator
Feature/transform iterator
- Loading branch information
Showing
13 changed files
with
484 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
#include "tools/ranges/IteratorView.hpp" | ||
#include "tools/ranges/TransformIterator.hpp" | ||
|
||
#include "tools/ranges/make_view.hpp" | ||
#include "tools/ranges/make_transform_view.hpp" |
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,157 @@ | ||
#ifndef NJOY_UTILITY_TRANSFORMITERATOR | ||
#define NJOY_UTILITY_TRANSFORMITERATOR | ||
|
||
// system includes | ||
#include <iterator> | ||
|
||
// other includes | ||
|
||
namespace njoy { | ||
namespace tools { | ||
namespace ranges { | ||
|
||
/** | ||
* | ||
* @brief An iterator class that applies a transformation when dereferencing | ||
* the iterator | ||
*/ | ||
template < typename Iterator, typename Transformation > | ||
class TransformIterator { | ||
|
||
/* fields */ | ||
Iterator iter_; | ||
Transformation transform_; | ||
|
||
public: | ||
|
||
using iterator_category = typename Iterator::iterator_category; | ||
using difference_type = typename Iterator::difference_type; | ||
using value_type = decltype( std::declval< Transformation >()( std::declval< typename Iterator::value_type >() ) ); | ||
using pointer = value_type*; | ||
using reference = value_type; | ||
|
||
/* constructor */ | ||
|
||
constexpr TransformIterator() = delete; | ||
|
||
/** | ||
* @brief Constructor | ||
* | ||
* @param[in] iter the iterator | ||
* @param[in] transformation the transformation to be applied | ||
*/ | ||
constexpr TransformIterator( Iterator iter, Transformation transformation ) : | ||
iter_( std::move( iter ) ), transform_( std::move( transformation ) ) {} | ||
|
||
/* methods */ | ||
|
||
constexpr reference operator*() const { | ||
|
||
return this->transform_( *this->iter_ ); | ||
} | ||
|
||
constexpr reference operator[]( difference_type i ) const { | ||
|
||
return this->transform_( this->iter_[i] ); | ||
} | ||
|
||
constexpr TransformIterator& operator+=( difference_type i ) { | ||
|
||
this->iter_ += i; | ||
return *this; | ||
} | ||
|
||
constexpr TransformIterator& operator-=( difference_type i ) { | ||
|
||
this->iter_ -= i; | ||
return *this; | ||
} | ||
|
||
constexpr TransformIterator& operator++( void ) { | ||
|
||
++this->iter_; | ||
return *this; | ||
} | ||
|
||
constexpr TransformIterator operator++( int ) { | ||
|
||
return TransformIterator( this->iter_++, transform_ ); | ||
} | ||
|
||
constexpr TransformIterator& operator--( void ) { | ||
|
||
--this->iter_; | ||
return *this; | ||
} | ||
|
||
constexpr TransformIterator operator--( int ) { | ||
|
||
return TransformIterator( this->iter_--, this->transform_ ); | ||
} | ||
|
||
friend constexpr TransformIterator operator+( difference_type i, | ||
TransformIterator iter ) { | ||
|
||
return TransformIterator( iter.iter_ + i, iter.transform_ ); | ||
} | ||
|
||
friend constexpr TransformIterator operator+( TransformIterator iter, | ||
difference_type i ) { | ||
|
||
return TransformIterator( iter.iter_ + i, iter.transform_ ); | ||
} | ||
|
||
friend constexpr TransformIterator operator-( TransformIterator iter, | ||
difference_type i ) { | ||
|
||
return TransformIterator( iter.iter_ - i, iter.transform_ ); | ||
} | ||
|
||
friend constexpr difference_type operator-( const TransformIterator& left, | ||
const TransformIterator& right ) { | ||
|
||
return left.iter_ - right.iter_; | ||
} | ||
|
||
friend constexpr bool operator==( const TransformIterator& left, | ||
const TransformIterator& right ) { | ||
|
||
return left.iter_ == right.iter_; | ||
}; | ||
|
||
friend constexpr bool operator!=( const TransformIterator& left, | ||
const TransformIterator& right ) { | ||
|
||
return !( left == right ); | ||
}; | ||
|
||
friend constexpr bool operator<( const TransformIterator& left, | ||
const TransformIterator& right) { | ||
|
||
return left.iter_ < right.iter_; | ||
} | ||
|
||
friend constexpr bool operator>( const TransformIterator& left, | ||
const TransformIterator& right ) { | ||
|
||
return right < left; | ||
} | ||
|
||
friend constexpr bool operator<=( const TransformIterator& left, | ||
const TransformIterator& right ) { | ||
|
||
return !( right < left ); | ||
} | ||
|
||
friend constexpr bool operator>=( const TransformIterator& left, | ||
const TransformIterator& right ) { | ||
|
||
return !( left < right ); | ||
} | ||
}; | ||
|
||
} // 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.TransformIterator TransformIterator.test.cpp ) |
85 changes: 85 additions & 0 deletions
85
src/tools/ranges/TransformIterator/test/TransformIterator.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,85 @@ | ||
// include Catch2 | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
// what we are testing | ||
#include "tools/ranges/TransformIterator.hpp" | ||
|
||
// other includes | ||
|
||
// convenience typedefs | ||
using namespace njoy::tools::ranges; | ||
|
||
SCENARIO( "TransformIterator" ) { | ||
|
||
GIVEN( "a container with values and a transformation operation" ) { | ||
|
||
std::vector< int > values = { -2, -1, 0, 1, 2 }; | ||
auto transform = [] ( auto&& value ) { return 2 * value; }; | ||
|
||
WHEN( "when iterators are used" ) { | ||
|
||
TransformIterator iter( values.begin(), transform ); | ||
TransformIterator begin( values.begin(), transform ); | ||
TransformIterator end( values.end(), transform ); | ||
|
||
THEN( "TransformIterator behaves as an iterator" ) { | ||
|
||
CHECK( *iter == -4 ); | ||
|
||
CHECK( iter[0] == -4 ); | ||
CHECK( iter[1] == -2 ); | ||
CHECK( iter[2] == 0 ); | ||
CHECK( iter[3] == 2 ); | ||
CHECK( iter[4] == 4 ); | ||
|
||
CHECK( *( begin + 0 ) == -4 ); | ||
CHECK( *( begin + 1 ) == -2 ); | ||
CHECK( *( begin + 2 ) == 0 ); | ||
CHECK( *( begin + 3 ) == 2 ); | ||
CHECK( *( begin + 4 ) == 4 ); | ||
|
||
CHECK( *( 0 + begin ) == -4 ); | ||
CHECK( *( 1 + begin ) == -2 ); | ||
CHECK( *( 2 + begin ) == 0 ); | ||
CHECK( *( 3 + begin ) == 2 ); | ||
CHECK( *( 4 + begin ) == 4 ); | ||
|
||
CHECK( *( end - 1 ) == 4 ); | ||
CHECK( *( end - 2 ) == 2 ); | ||
CHECK( *( end - 3 ) == 0 ); | ||
CHECK( *( end - 4 ) == -2 ); | ||
CHECK( *( end - 5 ) == -4 ); | ||
|
||
CHECK( end - begin == 5 ); | ||
|
||
iter += 3; | ||
CHECK( *iter == 2 ); | ||
iter -= 3; | ||
CHECK( *iter == -4 ); | ||
|
||
++iter; | ||
CHECK( *iter == -2 ); | ||
--iter; | ||
CHECK( *iter == -4 ); | ||
|
||
CHECK( *( iter++ ) == -4 ); | ||
CHECK( *iter == -2 ); | ||
CHECK( *( iter-- ) == -2 ); | ||
CHECK( *iter == -4 ); | ||
|
||
CHECK( true == ( iter == begin ) ); | ||
CHECK( false == ( iter == end ) ); | ||
CHECK( true == ( iter != end ) ); | ||
CHECK( false == ( iter != begin ) ); | ||
CHECK( true == ( iter < end ) ); | ||
CHECK( false == ( iter < begin ) ); | ||
CHECK( true == ( iter <= begin ) ); | ||
CHECK( false == ( end <= iter ) ); | ||
CHECK( true == ( end > iter ) ); | ||
CHECK( false == ( iter > begin ) ); | ||
CHECK( true == ( end >= iter ) ); | ||
CHECK( false == ( begin >= end ) ); | ||
} // THEN | ||
} // WHEN | ||
} // GIVEN | ||
} // SCENARIO |
Oops, something went wrong.