helper to pull all elements from an iterator, yielding the last one

This commit is contained in:
Fischlurch 2012-04-24 06:00:55 +02:00
parent 5222384f8b
commit 0ab773ab7c
2 changed files with 42 additions and 1 deletions

View file

@ -289,7 +289,7 @@ namespace lib {
{
return (cached_ && isOK_)
|| (cached_ = true
&&(isOK_ = predicate_(*Raw::pipe())));
&&(isOK_ = predicate_(*Raw::pipe())));
}
void
@ -540,6 +540,31 @@ namespace lib {
}
template<class IT>
inline typename IT::value_type
pull_last (IT iter)
{
typedef typename IT::value_type Val;
typedef wrapper::ItemWrapper<Val> Item;
Item lastElm;
while (iter)
{
lastElm = *iter;
++iter;
}
if (lastElm)
return *lastElm;
else
throw lumiera::error::State ("attempt to retrieve the last element "
"of an exhausted or empty iterator"
,lumiera::error::LUMIERA_ERROR_ITER_EXHAUST);
}
/** filters away repeated values
* emitted by source iterator */
template<class IT>

View file

@ -23,6 +23,7 @@
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/util.hpp"
#include "lib/util-foreach.hpp"
@ -47,6 +48,8 @@ namespace test{
using std::endl;
using std::rand;
using lumiera::error::LUMIERA_ERROR_ITER_EXHAUST;
namespace { // Test data
@ -110,6 +113,8 @@ namespace test{
verify_filterRepetitions();
buildTransformingIterator (source.begin());
verifyPullLast(source.begin());
}
@ -253,6 +258,17 @@ namespace test{
}
void
verifyPullLast(Iter const& ii)
{
Iter::value_type lastElm = pull_last (ii);
CHECK (1 == lastElm); // TestSource holds a decreasing sequence of numbers ending with 1
Iter emptyIterator;
CHECK (isnil (emptyIterator));
VERIFY_ERROR (ITER_EXHAUST, pull_last(emptyIterator) );
}
};
LAUNCHER (IterTools_test, "unit common");