From 075653a815d6c3c52958e7907ffb1d28879a7a90 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 5 Dec 2015 00:52:45 +0100 Subject: [PATCH] define the expected behaviour for an extensible filter iterator --- src/lib/itertools.hpp | 7 +++++ tests/library/itertools-test.cpp | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/lib/itertools.hpp b/src/lib/itertools.hpp index 92ed94a61..967d77f87 100644 --- a/src/lib/itertools.hpp +++ b/src/lib/itertools.hpp @@ -367,6 +367,13 @@ namespace lib { typedef typename _Filter::Val Val; public: + ExtensibleFilterIter() { } + + template + ExtensibleFilterIter (IT const& src, PRED initialFilterPredicate) + : FilterIter(src, initialFilterPredicate) + { } + template ExtensibleFilterIter& andFilter (COND conjunctiveClause) diff --git a/tests/library/itertools-test.cpp b/tests/library/itertools-test.cpp index 02c66270f..e05684f3a 100644 --- a/tests/library/itertools-test.cpp +++ b/tests/library/itertools-test.cpp @@ -110,6 +110,7 @@ namespace test{ Iter ii (source.begin()); ++++++ii; buildFilterIterator (ii); + verify_filterExtension(); verify_filterRepetitions(); buildTransformingIterator (source.begin()); @@ -133,6 +134,7 @@ namespace test{ static bool takeAll (int) { return true; } static bool takeOdd (int i) { return 0 != i % 2; } static bool takeEve (int i) { return 0 == i % 2; } + static bool takeTrd (int i) { return 0 == i % 3; } void buildFilterIterator (Iter const& ii) @@ -155,6 +157,49 @@ namespace test{ } + /** @test verify the ability to extend a filter condition + * while in the middle of an ongoing iteration. + * Typically this means sharpening the filter condition + * and thus making the filter more restrictive, filtering + * away more elements of the source stream. + */ + void + verify_filterExtension () + { + typedef vector Src; + typedef Src::iterator SrcIter; + typedef RangeIter SeqIter; + typedef ExtensibleFilterIter FilteredSeq; + + Src src; + for (uint i=0; i < 3*NUM_ELMS; ++i) + src.push_back(i); + + SeqIter completeSequence (src.begin(), src.end()); + FilteredSeq filterIter (completeSequence, takeAll); + + CHECK (!isnil (filterIter)); + CHECK (0 == *filterIter); + ++filterIter; + CHECK (1 == *filterIter); + + filterIter.andFilter(takeEve); + CHECK (!isnil (filterIter)); + CHECK (2 == *filterIter); + ++filterIter; + CHECK (4 == *filterIter); + + filterIter.andFilter(takeTrd); + CHECK (!isnil (filterIter)); + CHECK (6 == *filterIter); + ++filterIter; + CHECK (12 == *filterIter); + + verifyComparisons (filterIter); + pullOut (filterIter); + } + + /** @test verify the helper to filter duplicate elements * emitted by an source iterator. This test creates * a sequence of numbers with random repetitions.