diff --git a/CppLinq/cpplinq.hpp b/CppLinq/cpplinq.hpp index 0aa3e25..25c0797 100644 --- a/CppLinq/cpplinq.hpp +++ b/CppLinq/cpplinq.hpp @@ -544,28 +544,31 @@ namespace cpplinq container_type&& container ) : container (std::move (container)) - , current (container.begin ()) - , upcoming (container.begin ()) - , end (container.end ()) { + upcoming = current = this->container.begin(); + end = this->container.end(); } CPPLINQ_INLINEMETHOD from_copy_range ( container_type const & container ) : container (container) - , current (container.begin ()) - , upcoming (container.begin ()) - , end (container.end ()) { + upcoming = current = this->container.begin(); + end = this->container.end(); } CPPLINQ_INLINEMETHOD from_copy_range (from_copy_range const & v) : container (v.container) - , current (v.current) - , upcoming (v.upcoming) - , end (v.end) { + iterator_type s_b = v.container.begin(); + auto d_c = std::distance(s_b, v.current); + auto d_u = std::distance(v.current, v.upcoming); // upcoming is never before current. + current = container.begin(); + std::advance(current, d_c); + upcoming = current; + std::advance(upcoming, d_u); + end = container.end(); // end must re-create from new container. } CPPLINQ_INLINEMETHOD from_copy_range (from_copy_range && v) CPPLINQ_NOEXCEPT @@ -2113,10 +2116,13 @@ namespace cpplinq return true; } - if (range.next ()) + while(range.next ()) { inner_range = predicate (range.front ()); - return inner_range && inner_range->next (); + if(inner_range && inner_range->next ()) + { + return true; + } } inner_range.clear (); diff --git a/Test/CppLinqTests.hpp b/Test/CppLinqTests.hpp index 532faf2..aca5fec 100644 --- a/Test/CppLinqTests.hpp +++ b/Test/CppLinqTests.hpp @@ -32,6 +32,7 @@ // ---------------------------------------------------------------------------------------------- #include #include +#include // ---------------------------------------------------------------------------------------------- #include "../CppLinq/cpplinq.hpp" // ---------------------------------------------------------------------------------------------- @@ -1775,7 +1776,41 @@ namespace } } } + { + std::vector seed; + seed.push_back(1); + seed.push_back(2); + std::vector select_many_result = + from (seed) + >> select_many ([&seed](int n){ + if(n == 1) + return from(std::vector()); + else + return from(seed); + }) + >> to_vector () + ; + + TEST_ASSERT (2U, select_many_result.size ()); + } + { + std::vector seed; + seed.push_back(1); + std::vector select_many_result = + from (seed) + >> select_many ([](int n){ + std::vector seq; + seq.push_back(n); + seq.push_back(n); + return from_copy(std::move(seq)); + }) + >> to_vector () + ; + TEST_ASSERT (2U, select_many_result.size ()); + TEST_ASSERT (1, select_many_result[0]); + TEST_ASSERT (1, select_many_result[1]); + } } void test_orderby ()