Library: complement the pseudo-iterator by a IterSource front-end

This commit is contained in:
Fischlurch 2017-10-29 15:31:34 +01:00
parent 0682e449a3
commit 750b124f88
3 changed files with 87 additions and 26 deletions

View file

@ -364,6 +364,22 @@ namespace lib {
}
/** an IterSource frontend to return just a single value once.
* @warning behind the scenes, a heap allocation is managed by shared_ptr,
* to maintain a copy of the wrapped element. When passing a reference,
* only a reference will be wrapped, but a heap allocation happens nontheless
*/
template<typename VAL>
auto
singleVal (VAL&& something)
{
using Src = decltype(singleValIterator (forward<VAL>(something)));
using Val = typename _IterT<Src>::Val;
return IterSource<Val>::build (new WrappedLumieraIter<Src>{singleValIterator (forward<VAL>(something))});
}
/** pipes a given Lumiera Forward Iterator through
* a transformation function and wraps the resulting
* transforming Iterator, exposing just an IterSource.
@ -461,11 +477,11 @@ namespace lib {
using Range = RangeIter<typename CON::iterator>;
Range contents (container.begin(), container.end());
return IterSource<ValType>::build (new WrappedLumieraIter<Range>(contents));
return IterSource<ValType>::build (new WrappedLumieraIter<Range>(contents));
}
/** @return a Lumiera Forward Iterator yielding all values
/** @return a Lumiera Forward Iterator to yield all values
* defined by a classical Iterator range.
*/
template<class IT>
@ -478,9 +494,9 @@ namespace lib {
Range contents (begin, end);
return IterSource<ValType>::build (new WrappedLumieraIter<Range>(contents));
}
}
using iter_source::wrapIter;
using iter_source::singleVal;
using iter_source::transform;
using iter_source::eachMapKey;
using iter_source::eachDistinctKey;

View file

@ -59,6 +59,8 @@ namespace test{
using std::list;
using std::rand;
using lumiera::error::LUMIERA_ERROR_ITER_EXHAUST;
namespace { // Subject of test
@ -132,6 +134,16 @@ namespace test{
};
/** diagnostics helper */
template<class IT>
inline void
pullOut (IT& iter)
{
for ( ; iter; ++iter )
cout << "::" << *iter;
cout << endl;
}
} // (END) impl test dummy containers
@ -207,11 +219,25 @@ namespace test{
}
/** @test verify transforming an embedded iterator
* This test not only wraps a source iterator and packages it behind the
* abstracting interface IterSource, but in addition also applies a function
* to each element yielded by the source iterator. As demo transformation
* we use the values from our custom container (\ref WrappedList) to build
* a time value in quarter seconds
*
*/
void
verify_transformIter()
{
WrappedList customList(NUM_ELMS);
WrappedList::iterator sourceValues = customList.begin();
WrappedList::iterator sourceValues = customList.begin();
// transformation function
auto makeTime = [](int input_sec) -> TimeVar
{
return time::Time (time::FSecs (input_sec, 4));
};
TimeIter tIt (transform (sourceValues, makeTime));
CHECK (!isnil (tIt));
@ -219,27 +245,39 @@ namespace test{
CHECK (!tIt);
}
/** transformation function, to be applied for each element:
* just build a time value, using the input as 1/4 seconds
*/
static TimeVar
makeTime (int input_sec)
{
return time::Time (time::FSecs (input_sec, 4));
}
template<class IT>
/** @test an IterSouce which returns just a single value once */
void
pullOut (IT& iter)
verify_singleValIter()
{
for ( ; iter; ++iter )
cout << "::" << *iter;
cout << endl;
int i{-9};
IntIter ii = singleVal(12);
CHECK (not isnil(ii));
CHECK (12 == *ii);
++ii;
CHECK (isnil (ii));
VERIFY_ERROR (ITER_EXHAUST, *ii );
// IterSource is an abstracting interface,
// thus we're able to reassign an embedded iterator
// with a different value type (int& instead of int)
ii = singleVal(i);
CHECK (not isnil(ii));
CHECK (-9 == *ii);
// NOTE: since we passed a reference, a reference got wrapped
i = 23;
CHECK (23 == *ii);
++ii;
CHECK (isnil (ii));
}
template<class MAP>
void
verify_MapWrappers()

View file

@ -4685,11 +4685,15 @@
</node>
<node CREATED="1509151268071" ID="ID_38582779" MODIFIED="1509151422017" TEXT="getChildren aufbohren">
<icon BUILTIN="pencil"/>
<node CREATED="1509151285749" ID="ID_1826645914" MODIFIED="1509151289049" TEXT="eigentlich einfach"/>
<node BACKGROUND_COLOR="#fdfdcf" COLOR="#ff0000" CREATED="1509151289637" ID="ID_892381116" MODIFIED="1509151414927" TEXT="brauche singletonIter">
<icon BUILTIN="pencil"/>
<node CREATED="1509151285749" ID="ID_1826645914" MODIFIED="1509287428715" TEXT="eigentlich einfach">
<icon BUILTIN="idea"/>
</node>
<node COLOR="#338800" CREATED="1509151289637" FOLDED="true" ID="ID_892381116" MODIFIED="1509287431815" TEXT="brauche singletonIter">
<icon BUILTIN="button_ok"/>
<node CREATED="1509151296292" ID="ID_1050274308" MODIFIED="1509151411655" TEXT="liefert ein einziges Element"/>
<node CREATED="1509151305602" ID="ID_452317335" MODIFIED="1509151411655" TEXT="als IterSource verpackt"/>
<node CREATED="1509151305602" ID="ID_452317335" MODIFIED="1509287419861" TEXT="als IterSource verpackt">
<icon BUILTIN="idea"/>
</node>
<node CREATED="1509151309866" ID="ID_1109789284" MODIFIED="1509151411655" TEXT="sollte es als als Basis iter implementieren">
<richcontent TYPE="NOTE"><html>
<head>
@ -4707,8 +4711,11 @@
</richcontent>
</node>
<node CREATED="1509151381113" ID="ID_779657015" MODIFIED="1509151411655" TEXT="brauche ItemWrapper f&#xfc;r beliebiges Element"/>
<node CREATED="1509151390495" ID="ID_947844248" MODIFIED="1509151411655" TEXT="Idee: TransformIter aufbohren">
<icon BUILTIN="help"/>
<node CREATED="1509151390495" ID="ID_947844248" MODIFIED="1509285129251" TEXT="Idee: TransformIter aufbohren">
<icon BUILTIN="button_cancel"/>
</node>
<node CREATED="1509285130478" ID="ID_1391664155" MODIFIED="1509285142156" TEXT="direkt coden ist am klarsten">
<icon BUILTIN="button_ok"/>
</node>
</node>
</node>