From 73a2aea50cfe5586774b7aa962b85f5a2d70a0c7 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Thu, 1 Apr 2010 18:03:34 +0200 Subject: [PATCH] draft a low-level adapter to get all keys/vals of a stl::map intention is just to use a thin wrapper, without abstracting the implementation type (as IterSource does) --- src/lib/iter-adapter-stl.hpp | 102 ++++++++++++++++++++++++ src/lib/iter-adapter.hpp | 6 +- src/lib/iter-source.hpp | 14 ++-- src/lib/test/test-coll.hpp | 115 +++++++++++++++++++++++++++ src/lib/test/test-helper.hpp | 4 +- tests/40components.tests | 4 + tests/lib/iter-adapter-stl-test.cpp | 118 ++++++++++++++++++++++++++++ 7 files changed, 351 insertions(+), 12 deletions(-) create mode 100644 src/lib/iter-adapter-stl.hpp create mode 100644 src/lib/test/test-coll.hpp create mode 100644 tests/lib/iter-adapter-stl-test.cpp diff --git a/src/lib/iter-adapter-stl.hpp b/src/lib/iter-adapter-stl.hpp new file mode 100644 index 000000000..58e19455d --- /dev/null +++ b/src/lib/iter-adapter-stl.hpp @@ -0,0 +1,102 @@ +/* + ITER-ADAPTER-STL.hpp - helpers for building simple forward iterators + + Copyright (C) Lumiera.org + 2010, Hermann Vosseler + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +/** @file iter-adapter-stl.hpp + ** Preconfigured adapters for some STL container standard usage situations. + ** Especially, definitions for accessing views on common STL containers + ** repackaged as lumiera forward iterators. Mostly the purpose + ** is ease of use, we don't create an abstraction barrier or try to + ** hide implementation details. (see iter-source.hpp for such an + ** abstraction facility). As a benefit, these adapters can be + ** considered low overhead. + ** + ** @see iter-adapter-stl-test.cpp + ** @see iter-adapter.hpp + ** @see iter-source.happ + ** @see intertools.hpp + ** + */ + + +#ifndef LIB_ITER_ADAPTER_STL_H +#define LIB_ITER_ADAPTER_STL_H + + +#include "lib/iter-adapter.hpp" + +/////////////////////////TODO remove +#include "lib/itertools.hpp" +/////////////////////////TODO remove + + + +namespace lib { +namespace iter_stl { + + + namespace { // traits and helpers... + + template + struct _MapT + { + typedef typename MAP::key_type Key; + typedef typename MAP::value_type::second_type Val; + typedef typename MAP::iterator MIter; ///////////////////////////TODO + typedef RangeIter SrcRange; + typedef TransformIter KeyIter; ///////////////////////////TODO + typedef TransformIter ValIter; ///////////////////////////TODO + }; + } + + + /** + */ + template + inline typename _MapT::KeyIter + eachKey (MAP) + { + UNIMPLEMENTED ("each key of a map as Lumiera Forward Iterator"); + } + + + /** + */ + template + inline typename _MapT::ValIter + eachVal (MAP) + { + UNIMPLEMENTED ("each value of a map as Lumiera Forward Iterator"); + } + + + /** + */ + template + inline typename _MapT::KeyIter + eachDistinctKey (MAP) + { + UNIMPLEMENTED ("each distinct key a multimap as Lumiera Forward Iterator"); + } + + +}} // namespace lib::iter_stl +#endif diff --git a/src/lib/iter-adapter.hpp b/src/lib/iter-adapter.hpp index 8e085811a..26062f458 100644 --- a/src/lib/iter-adapter.hpp +++ b/src/lib/iter-adapter.hpp @@ -1,5 +1,5 @@ /* - ITER-ADAPTOR.hpp - helpers for building simple forward iterators + ITER-ADAPTER.hpp - helpers for building simple forward iterators Copyright (C) Lumiera.org 2009, Hermann Vosseler @@ -71,8 +71,8 @@ */ -#ifndef LIB_ITER_ADAPTOR_H -#define LIB_ITER_ADAPTOR_H +#ifndef LIB_ITER_ADAPTER_H +#define LIB_ITER_ADAPTER_H #include "lib/error.hpp" diff --git a/src/lib/iter-source.hpp b/src/lib/iter-source.hpp index df924c0e2..57936d481 100644 --- a/src/lib/iter-source.hpp +++ b/src/lib/iter-source.hpp @@ -245,7 +245,7 @@ namespace lib { /* === pre-defined Adapters for frequently used Containers === */ - namespace iter_impl { + namespace iter_source { namespace { // traits and helpers... template @@ -401,12 +401,12 @@ namespace lib { } } - using iter_impl::wrapIter; - using iter_impl::eachMapKey; - using iter_impl::eachDistinctKey; - using iter_impl::eachValForKey; - using iter_impl::eachMapVal; - using iter_impl::eachEntry; + using iter_source::wrapIter; + using iter_source::eachMapKey; + using iter_source::eachDistinctKey; + using iter_source::eachValForKey; + using iter_source::eachMapVal; + using iter_source::eachEntry; } // namespace lib diff --git a/src/lib/test/test-coll.hpp b/src/lib/test/test-coll.hpp new file mode 100644 index 000000000..8053fce37 --- /dev/null +++ b/src/lib/test/test-coll.hpp @@ -0,0 +1,115 @@ +/* + TEST-COLL.hpp - containers and collections with test data + + Copyright (C) Lumiera.org + 2010, Hermann Vosseler + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + + +#ifndef LIB_TEST_TEST_COLL_H +#define LIB_TEST_TEST_COLL_H + + +//#include "lib/symbol.hpp" +//#include "lib/lumitime.hpp" + +//#include +//#include +#include +#include +#include +#include +//#include + + +namespace lib { +namespace test{ + +// using lib::Literal; +// using lumiera::Time; +// using std::string; +// using std::rand; + + typedef std::vector VecI; + + template + inline VEC + getTestSeq_int(const uint NUM_ELMS) + { + VEC vec; + for (uint i=0; i MapII; + typedef std::multimap MMapII; + + typedef std::tr1::unordered_map HMap; + typedef std::tr1::unordered_multimap HMMap; + + + template + inline MAP + getTestMap_int(const uint NUM_ELMS) + { + MAP map; + for (uint i=0; i + inline MUMAP + getTestMultiMap_int(const uint NUM_ELMS) + { + MUMAP map; + for (uint i=0; i + inline void + pullOut (ITER const& i) + { + for (ITER ii(i); ii ; ++ii ) + std::cout << "::" << *ii; + } + + + +}} // namespace lib::test + + + +/* === test helper macros === */ + +/** + * Macro to transmogrify... + */ +//#define MAGIC + +#endif diff --git a/src/lib/test/test-helper.hpp b/src/lib/test/test-helper.hpp index 5f1cc511c..a73a47e50 100644 --- a/src/lib/test/test-helper.hpp +++ b/src/lib/test/test-helper.hpp @@ -21,8 +21,8 @@ */ -#ifndef LIB_TEST_TESTHELPER_H -#define LIB_TEST_TESTHELPER_H +#ifndef LIB_TEST_TEST_HELPER_H +#define LIB_TEST_TEST_HELPER_H #include "lib/symbol.hpp" diff --git a/tests/40components.tests b/tests/40components.tests index ad30413e3..5911f945c 100644 --- a/tests/40components.tests +++ b/tests/40components.tests @@ -345,6 +345,10 @@ out: ::0::1::2::3::4::5::6::7::8::9::10::11::12::13::14::15::16::17::18::19 END +PLANNED "Lumiera Iterator Adapter for STL" IterAdapterSTL_test < + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +* *****************************************************/ + + + +#include "lib/test/run.hpp" +#include "lib/test/test-helper.hpp" +#include "lib/test/test-coll.hpp" +#include "lib/util.hpp" +//#include "lib/util-foreach.hpp" + +#include "lib/iter-adapter-stl.hpp" + +#include +#include +#include + + + +namespace lib { +namespace test{ + + using ::Test; + using boost::lexical_cast; +// using util::for_each; +// using util::isnil; +// using std::vector; + using std::cout; + using std::endl; + + namespace iter = lib::iter_stl; + + + namespace { // provide STL containers with test data + + uint NUM_ELMS = 10; + + } // (END) test data + + + /** test an iterator: create it by calling a constructor function + * and then pull out all contents and print them to STDOUT */ +#define TEST_ITER(_CTOR_, _ARG_) \ + cout << STRINGIFY(_CTOR_) ;\ + pullOut (_CTOR_ _ARG_) ;\ + cout << endl; + + /** print descriptive separator to STDOUT */ +#define PRINT_FUNC(_F_NAME_, _F_TYPE_) \ + cout << "-----"<() << endl; + + + + + + + + + /************************************************************************ + * @test provide test STL containers to verify some of the adapters + * to expose typical container usage patterns as lumiera iterators. + * - keys and values of a map + * - multimap values associated with a given key + * + * @see RangeIter + * @see iter-adapter.hpp + * @see iter-adapter-stl.hpp + */ + class IterAdapterSTL_test : public Test + { + + virtual void + run (Arg arg) + { + if (0 < arg.size()) NUM_ELMS = lexical_cast (arg[0]); + + iterateMapKeyVal (getTestMap_int (NUM_ELMS)); + } + + + template + void + iterateMapKeyVal(MAP const& map) + { + PRINT_FUNC (iterateMapKeyVal, MAP); + + TEST_ITER (iter::eachKey, (map)); + TEST_ITER (iter::eachVal, (map)); + TEST_ITER (iter::eachDistinctKey, (map)); + } + + + }; + + LAUNCHER (IterAdapterSTL_test, "unit common"); + + +}} // namespace lib::test +