Skip to content

Commit

Permalink
Merge pull request #45 from open-space-collective/dev@lucas
Browse files Browse the repository at this point in the history
Dev@lucas
  • Loading branch information
lucas-bremond authored Aug 29, 2018
2 parents 315f7b8 + 57fdd77 commit 9f98d67
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 9 deletions.
6 changes: 3 additions & 3 deletions include/Library/Core/Containers/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef __Library_Core_Containers_Array__
#define __Library_Core_Containers_Array__

#include <Library/Core/Containers/Iterators/Zip.hpp>
#include <Library/Core/Types/Index.hpp>
#include <Library/Core/Types/Size.hpp>
#include <Library/Core/Types/String.hpp>
Expand Down Expand Up @@ -52,8 +53,7 @@ class Array : public std::vector<T>

/// @brief Default constructor (disabled)

// Array ( ) = delete ;
Array ( ) = default ;
Array ( ) = delete ;

/// @brief Constructs an array from a C++ Standard Library vector
///
Expand Down Expand Up @@ -147,7 +147,7 @@ class Array : public std::vector<T>
///
/// @param [in] anOutputStream An output stream
/// @param [in] anArray An array
/// @return An output stream
/// @return A reference to output stream

template <class U>
friend std::ostream& operator << ( std::ostream& anOutputStream,
Expand Down
2 changes: 1 addition & 1 deletion include/Library/Core/Containers/Dictionary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Dictionary
///
/// @param [in] anOutputStream An output stream
/// @param [in] aDictionary An dictionary
/// @return An output stream
/// @return A reference to output stream

friend std::ostream& operator << ( std::ostream& anOutputStream,
const Dictionary& aDictionary ) ;
Expand Down
148 changes: 148 additions & 0 deletions include/Library/Core/Containers/Iterators/Zip.hpp
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

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 changes: 1 addition & 1 deletion include/Library/Core/Containers/Table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class Table
///
/// @param [in] anOutputStream An output stream
/// @param [in] aTable A table
/// @return An output stream
/// @return A reference to output stream

friend std::ostream& operator << ( std::ostream& anOutputStream,
const Table& aTable ) ;
Expand Down
2 changes: 1 addition & 1 deletion include/Library/Core/FileSystem/Directory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Directory
///
/// @param [in] anOutputStream An output stream
/// @param [in] aDirectory A directory
/// @return An output stream
/// @return A reference to output stream

friend std::ostream& operator << ( std::ostream& anOutputStream,
const Directory& aDirectory ) ;
Expand Down
2 changes: 1 addition & 1 deletion include/Library/Core/FileSystem/File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class File
///
/// @param [in] anOutputStream An output stream
/// @param [in] aFile A file
/// @return An output stream
/// @return A reference to output stream

friend std::ostream& operator << ( std::ostream& anOutputStream,
const File& aFile ) ;
Expand Down
2 changes: 1 addition & 1 deletion include/Library/Core/FileSystem/Path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Path
///
/// @param [in] anOutputStream An output stream
/// @param [in] aPath A path
/// @return An output stream
/// @return A reference to output stream

friend std::ostream& operator << ( std::ostream& anOutputStream,
const Path& aPath ) ;
Expand Down
2 changes: 1 addition & 1 deletion include/Library/Core/FileSystem/PermissionSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class PermissionSet
///
/// @param [in] anOutputStream An output stream
/// @param [in] aPermissionSet A permission set
/// @return An output stream
/// @return A reference to output stream

friend std::ostream& operator << ( std::ostream& anOutputStream,
const PermissionSet& aPermissionSet ) ;
Expand Down
29 changes: 29 additions & 0 deletions test/Library/Core/Containers/Array.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,4 +690,33 @@ TEST (Library_Core_Containers_Array, Empty)

}

TEST (Library_Core_Containers_Array, Zip)
{

using library::core::types::Index ;
using library::core::types::Integer ;
using library::core::ctnr::Array ;

{

const Array<Integer> firstArray = { {-4, -3, -2, -1, +0, +1, +2, +3, +4} } ;
const Array<Integer> secondArray = { {-8, -6, -4, -2, +0, +2, +4, +6, +8} } ;

Index index = 0 ;

for (const auto tuple : library::core::ctnr::iterators::Zip(firstArray, secondArray))
{

EXPECT_EQ(std::get<0>(tuple) * 2, std::get<1>(tuple)) ;

index++ ;

}

EXPECT_EQ(9, index) ;

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 comments on commit 9f98d67

Please sign in to comment.