From b97582180cbe1f48d80f9216ee5f723e34fad846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Can=20B=C3=B6l=C3=BCk?= Date: Mon, 31 Aug 2020 22:42:04 +0200 Subject: [PATCH] Fixed backwards(...) breaking ::zip(). --- VTIL-Common/util/zip.hpp | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/VTIL-Common/util/zip.hpp b/VTIL-Common/util/zip.hpp index af10d85..bc33eba 100644 --- a/VTIL-Common/util/zip.hpp +++ b/VTIL-Common/util/zip.hpp @@ -36,10 +36,8 @@ namespace vtil template struct joint_container { - // Iterator types. - // - using iterator_array = std::tuple() ) )...>; - using reference_array = std::tuple() ) )...>; + using iterator_array = std::tuple() ) )...>; + using reference_array = std::tuple() ) )...>; // Declare the iterator type. // @@ -57,9 +55,6 @@ namespace vtil // iterator_array iterators; - template - constexpr base_iterator( T&&... its ) : iterators( std::make_tuple( std::forward( its )... ) ) {} - // Support bidirectional iteration. // constexpr base_iterator& operator++() { std::apply( [ ] ( auto&... it ) { ( ( ++it ), ... ); }, iterators ); return *this; } @@ -79,18 +74,28 @@ namespace vtil using iterator = base_iterator; using const_iterator = base_iterator; - // Tuple containing data sources. + // Tuple containing data sources, length of iteration range, pre-computed begin and end. // - std::tuple sources; + const std::tuple sources; + const size_t length; + const iterator begin_p; + const iterator end_p; - // Generic container helpers. + template + constexpr joint_container( Tv&&... sc ) + : sources( std::forward( sc )... ), + length( std::size( std::get<0>( sources ) ) ), + begin_p( std::apply( [ & ] ( auto&... src ) { return iterator{ { std::begin( src )... } }; }, sources ) ), + end_p( std::apply( [ & ] ( auto&... src ) { return iterator{ { std::next( std::begin( src ), length )... } }; }, sources ) ) {} + + // Generic container interface. // - constexpr size_t size() const { return std::size( std::get<0>( sources ) ); } - constexpr iterator begin() const { return std::apply( [ ] ( auto&... src ) { return iterator( std::begin( src )... ); }, sources ); } - constexpr iterator end() const { return std::apply( [ ] ( auto&... src ) { return iterator( std::end( src )... ); }, sources ); } + constexpr size_t size() const { return length; } + constexpr iterator begin() const { return begin_p; } + constexpr iterator end() const { return end_p; } constexpr decltype( auto ) operator[]( size_t n ) const { return *std::next( begin(), n ); } }; template - static constexpr auto zip( Tx&&... args ) { return joint_container{ .sources = { std::forward( args )... } }; } + static constexpr joint_container zip( Tx&&... args ) { return { std::forward( args )... }; } }; \ No newline at end of file