-
Notifications
You must be signed in to change notification settings - Fork 6
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 #45 from open-space-collective/dev@lucas
Dev@lucas
- Loading branch information
Showing
9 changed files
with
186 additions
and
9 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,148 @@ | ||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
/// @project Library/Core | ||
/// @file Library/Core/Containers/Iterators/Zip.hpp | ||
/// @author Lucas Brémond <[email protected]> | ||
/// @license TBD | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
#ifndef __Library_Core_Containers_Iterators_Zip__ | ||
#define __Library_Core_Containers_Iterators_Zip__ | ||
|
||
#include <iterator> | ||
#include <tuple> | ||
#include <utility> | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
namespace library | ||
{ | ||
namespace core | ||
{ | ||
namespace ctnr | ||
{ | ||
namespace iterators | ||
{ | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
/// @brief Zip iterator | ||
/// | ||
/// @ref https://gist.github.com/mortehu/373069390c75b02f98b655e3f7dbef9a | ||
|
||
template <typename... T> | ||
class ZipIterator | ||
{ | ||
|
||
public: | ||
|
||
class Iterator : std::iterator<std::forward_iterator_tag, std::tuple<decltype(*std::declval<T>().begin())...>> | ||
{ | ||
|
||
public: | ||
|
||
std::tuple<decltype(std::declval<T>().begin())...> iterators_ ; | ||
|
||
explicit Iterator ( decltype(iterators_) anIteratorList ) | ||
: iterators_{ std::move(anIteratorList) } | ||
{ | ||
|
||
} | ||
|
||
Iterator& operator ++ ( ) | ||
{ | ||
|
||
increment(std::index_sequence_for<T...>{}) ; | ||
|
||
return *this ; | ||
|
||
} | ||
|
||
Iterator operator ++ ( int ) | ||
{ | ||
|
||
auto saved{*this} ; | ||
|
||
increment(std::index_sequence_for<T...>{}) ; | ||
|
||
return saved ; | ||
|
||
} | ||
|
||
bool operator != ( const Iterator& anIterator ) const | ||
{ | ||
return iterators_ != anIterator.iterators_ ; | ||
} | ||
|
||
auto operator * ( ) const | ||
{ | ||
return deref(std::index_sequence_for<T...>{}) ; | ||
} | ||
|
||
private: | ||
|
||
template <std::size_t... I> | ||
auto deref ( std::index_sequence<I...> ) const | ||
{ | ||
return typename Iterator::value_type{*std::get<I>(iterators_)...} ; | ||
} | ||
|
||
template <std::size_t... I> | ||
void increment ( std::index_sequence<I...> ) | ||
{ | ||
|
||
auto l = {(++std::get<I>(iterators_), 0)...} ; | ||
|
||
(void) l ; | ||
|
||
} | ||
|
||
} ; | ||
|
||
ZipIterator ( T&... aSequence ) | ||
: begin_{ std::make_tuple(aSequence.begin()...) }, | ||
end_{ std::make_tuple(aSequence.end()...) } | ||
{ | ||
|
||
} | ||
|
||
ZipIterator::Iterator begin ( ) const | ||
{ | ||
return begin_ ; | ||
} | ||
|
||
ZipIterator::Iterator end ( ) const | ||
{ | ||
return end_ ; | ||
} | ||
|
||
private: | ||
|
||
ZipIterator::Iterator begin_ ; | ||
ZipIterator::Iterator end_ ; | ||
|
||
} ; | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
/// @note Sequences must be the same length | ||
|
||
template <typename... T> | ||
auto Zip ( T&&... seqs ) | ||
{ | ||
return ZipIterator<T...>{seqs...} ; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
#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
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
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