From 04706892ca0aa387d846b869949580776c8e8fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Br=C3=A9mond?= Date: Tue, 28 Aug 2018 21:33:00 -0700 Subject: [PATCH 1/2] [misc] Improve documentation --- include/Library/Core/Containers/Array.hpp | 2 +- include/Library/Core/Containers/Dictionary.hpp | 2 +- include/Library/Core/Containers/Table.hpp | 2 +- include/Library/Core/FileSystem/Directory.hpp | 2 +- include/Library/Core/FileSystem/File.hpp | 2 +- include/Library/Core/FileSystem/Path.hpp | 2 +- include/Library/Core/FileSystem/PermissionSet.hpp | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/Library/Core/Containers/Array.hpp b/include/Library/Core/Containers/Array.hpp index 5fab7c39..cd080eba 100755 --- a/include/Library/Core/Containers/Array.hpp +++ b/include/Library/Core/Containers/Array.hpp @@ -147,7 +147,7 @@ class Array : public std::vector /// /// @param [in] anOutputStream An output stream /// @param [in] anArray An array - /// @return An output stream + /// @return A reference to output stream template friend std::ostream& operator << ( std::ostream& anOutputStream, diff --git a/include/Library/Core/Containers/Dictionary.hpp b/include/Library/Core/Containers/Dictionary.hpp index 57bf28d5..4ac60a27 100755 --- a/include/Library/Core/Containers/Dictionary.hpp +++ b/include/Library/Core/Containers/Dictionary.hpp @@ -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 ) ; diff --git a/include/Library/Core/Containers/Table.hpp b/include/Library/Core/Containers/Table.hpp index e3288bbc..d3397781 100755 --- a/include/Library/Core/Containers/Table.hpp +++ b/include/Library/Core/Containers/Table.hpp @@ -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 ) ; diff --git a/include/Library/Core/FileSystem/Directory.hpp b/include/Library/Core/FileSystem/Directory.hpp index f000544a..4901d838 100755 --- a/include/Library/Core/FileSystem/Directory.hpp +++ b/include/Library/Core/FileSystem/Directory.hpp @@ -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 ) ; diff --git a/include/Library/Core/FileSystem/File.hpp b/include/Library/Core/FileSystem/File.hpp index 3673ea5e..f678c0fb 100755 --- a/include/Library/Core/FileSystem/File.hpp +++ b/include/Library/Core/FileSystem/File.hpp @@ -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 ) ; diff --git a/include/Library/Core/FileSystem/Path.hpp b/include/Library/Core/FileSystem/Path.hpp index 950e8120..e12d4eb6 100755 --- a/include/Library/Core/FileSystem/Path.hpp +++ b/include/Library/Core/FileSystem/Path.hpp @@ -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 ) ; diff --git a/include/Library/Core/FileSystem/PermissionSet.hpp b/include/Library/Core/FileSystem/PermissionSet.hpp index be0c3631..5c923efc 100755 --- a/include/Library/Core/FileSystem/PermissionSet.hpp +++ b/include/Library/Core/FileSystem/PermissionSet.hpp @@ -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 ) ; From 57fdd772cf968bdb1597a1c2174914ba259e017d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Br=C3=A9mond?= Date: Wed, 29 Aug 2018 14:12:00 -0700 Subject: [PATCH 2/2] [feature] Add Zip iterator --- include/Library/Core/Containers/Array.hpp | 4 +- .../Library/Core/Containers/Iterators/Zip.hpp | 148 ++++++++++++++++++ test/Library/Core/Containers/Array.test.cpp | 29 ++++ 3 files changed, 179 insertions(+), 2 deletions(-) create mode 100755 include/Library/Core/Containers/Iterators/Zip.hpp diff --git a/include/Library/Core/Containers/Array.hpp b/include/Library/Core/Containers/Array.hpp index cd080eba..0303e67b 100755 --- a/include/Library/Core/Containers/Array.hpp +++ b/include/Library/Core/Containers/Array.hpp @@ -10,6 +10,7 @@ #ifndef __Library_Core_Containers_Array__ #define __Library_Core_Containers_Array__ +#include #include #include #include @@ -52,8 +53,7 @@ class Array : public std::vector /// @brief Default constructor (disabled) - // Array ( ) = delete ; - Array ( ) = default ; + Array ( ) = delete ; /// @brief Constructs an array from a C++ Standard Library vector /// diff --git a/include/Library/Core/Containers/Iterators/Zip.hpp b/include/Library/Core/Containers/Iterators/Zip.hpp new file mode 100755 index 00000000..e8520708 --- /dev/null +++ b/include/Library/Core/Containers/Iterators/Zip.hpp @@ -0,0 +1,148 @@ +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +/// @project Library/Core +/// @file Library/Core/Containers/Iterators/Zip.hpp +/// @author Lucas Brémond +/// @license TBD + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#ifndef __Library_Core_Containers_Iterators_Zip__ +#define __Library_Core_Containers_Iterators_Zip__ + +#include +#include +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace library +{ +namespace core +{ +namespace ctnr +{ +namespace iterators +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +/// @brief Zip iterator +/// +/// @ref https://gist.github.com/mortehu/373069390c75b02f98b655e3f7dbef9a + +template +class ZipIterator +{ + + public: + + class Iterator : std::iterator().begin())...>> + { + + public: + + std::tuple().begin())...> iterators_ ; + + explicit Iterator ( decltype(iterators_) anIteratorList ) + : iterators_{ std::move(anIteratorList) } + { + + } + + Iterator& operator ++ ( ) + { + + increment(std::index_sequence_for{}) ; + + return *this ; + + } + + Iterator operator ++ ( int ) + { + + auto saved{*this} ; + + increment(std::index_sequence_for{}) ; + + return saved ; + + } + + bool operator != ( const Iterator& anIterator ) const + { + return iterators_ != anIterator.iterators_ ; + } + + auto operator * ( ) const + { + return deref(std::index_sequence_for{}) ; + } + + private: + + template + auto deref ( std::index_sequence ) const + { + return typename Iterator::value_type{*std::get(iterators_)...} ; + } + + template + void increment ( std::index_sequence ) + { + + auto l = {(++std::get(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 +auto Zip ( T&&... seqs ) +{ + return ZipIterator{seqs...} ; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} +} +} + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/test/Library/Core/Containers/Array.test.cpp b/test/Library/Core/Containers/Array.test.cpp index e4c8e2d9..25f86515 100755 --- a/test/Library/Core/Containers/Array.test.cpp +++ b/test/Library/Core/Containers/Array.test.cpp @@ -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 firstArray = { {-4, -3, -2, -1, +0, +1, +2, +3, +4} } ; + const Array 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) ; + + } + +} + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// \ No newline at end of file