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)
This commit is contained in:
Fischlurch 2010-04-01 18:03:34 +02:00
parent fc44e522e0
commit 73a2aea50c
7 changed files with 351 additions and 12 deletions

View file

@ -0,0 +1,102 @@
/*
ITER-ADAPTER-STL.hpp - helpers for building simple forward iterators
Copyright (C) Lumiera.org
2010, Hermann Vosseler <Ichthyostega@web.de>
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 <b>lumiera forward iterators</b>. 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<class MAP>
struct _MapT
{
typedef typename MAP::key_type Key;
typedef typename MAP::value_type::second_type Val;
typedef typename MAP::iterator MIter; ///////////////////////////TODO
typedef RangeIter<MIter> SrcRange;
typedef TransformIter<SrcRange,Key> KeyIter; ///////////////////////////TODO
typedef TransformIter<SrcRange,Val> ValIter; ///////////////////////////TODO
};
}
/**
*/
template<class MAP>
inline typename _MapT<MAP>::KeyIter
eachKey (MAP)
{
UNIMPLEMENTED ("each key of a map as Lumiera Forward Iterator");
}
/**
*/
template<class MAP>
inline typename _MapT<MAP>::ValIter
eachVal (MAP)
{
UNIMPLEMENTED ("each value of a map as Lumiera Forward Iterator");
}
/**
*/
template<class MAP>
inline typename _MapT<MAP>::KeyIter
eachDistinctKey (MAP)
{
UNIMPLEMENTED ("each distinct key a multimap as Lumiera Forward Iterator");
}
}} // namespace lib::iter_stl
#endif

View file

@ -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 <Ichthyostega@web.de>
@ -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"

View file

@ -245,7 +245,7 @@ namespace lib {
/* === pre-defined Adapters for frequently used Containers === */
namespace iter_impl {
namespace iter_source {
namespace { // traits and helpers...
template<class CON>
@ -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

115
src/lib/test/test-coll.hpp Normal file
View file

@ -0,0 +1,115 @@
/*
TEST-COLL.hpp - containers and collections with test data
Copyright (C) Lumiera.org
2010, Hermann Vosseler <Ichthyostega@web.de>
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 <typeinfo>
//#include <cstdlib>
#include <tr1/unordered_map>
#include <iostream>
#include <vector>
#include <map>
//#include <string>
namespace lib {
namespace test{
// using lib::Literal;
// using lumiera::Time;
// using std::string;
// using std::rand;
typedef std::vector<int> VecI;
template<class VEC>
inline VEC
getTestSeq_int(const uint NUM_ELMS)
{
VEC vec;
for (uint i=0; i<NUM_ELMS; ++i)
vec.push_back (i);
return vec;
}
typedef std::map<int,int> MapII;
typedef std::multimap<int,int> MMapII;
typedef std::tr1::unordered_map<int,int> HMap;
typedef std::tr1::unordered_multimap<int,int> HMMap;
template<class MAP>
inline MAP
getTestMap_int(const uint NUM_ELMS)
{
MAP map;
for (uint i=0; i<NUM_ELMS; ++i)
map[i] = 2*i;
return map;
}
template<class MUMAP>
inline MUMAP
getTestMultiMap_int(const uint NUM_ELMS)
{
MUMAP map;
for (uint i=0; i<NUM_ELMS; ++i)
for (uint j=NUM_ELMS; j; --j)
map.insert (std::make_pair (i, j));
return map;
}
template<class ITER>
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

View file

@ -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"

View file

@ -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 <<END
END
TEST "Lumiera Iterator Tools" IterTools_test 20 <<END
out: ::20::19::18::17::16::15::14::13::12::11::10::9::8::7::6::5::4::3::2::1
out: ::20::18::16::14::12::10::8::6::4::2

View file

@ -0,0 +1,118 @@
/*
IterAdapterSTL(Test) - building various custom iterators for a given container
Copyright (C) Lumiera.org
2010, Hermann Vosseler <Ichthyostega@web.de>
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 <boost/lexical_cast.hpp>
#include <iostream>
#include <vector>
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 << "-----"<<STRINGIFY(_F_NAME_)<<"---" << showType<_F_TYPE_>() << 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<uint> (arg[0]);
iterateMapKeyVal (getTestMap_int<MapII> (NUM_ELMS));
}
template<class MAP>
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