Iterator adapter to ennumerate all values to a multimap key (closes #492)

This commit is contained in:
Fischlurch 2010-03-31 05:01:03 +02:00
parent c9bf26d8c4
commit 57a9b1a270
3 changed files with 54 additions and 4 deletions

View file

@ -337,7 +337,7 @@ namespace lib {
/** @return a Lumiera Forward Iterator to yield
* all the keys of the given Map or Hashtable
* all the values of the given Map or Hashtable
*/
template<class MAP>
typename _MapT<MAP>::ValIter
@ -366,6 +366,23 @@ namespace lib {
}
/** @return a Lumiera Forward Iterator to yield all values
* associated to the given key within this Map or Multimap
* @note obviously in case of a Map we'll get at most one result.
*/
template<class MAP>
typename _MapT<MAP>::ValIter
eachValForKey (MAP& map, typename _MapT<MAP>::Key key)
{
typedef typename MAP::iterator Pos;
typedef RangeIter<Pos> Range;
std::pair<Pos,Pos> valuesForKey = map.equal_range(key);
Range contents (valuesForKey.first, valuesForKey.second);
return wrapIter (takePairSecond(contents));
}
/** @param container a STL-like container, providing
* - a typedef \c iterator
* - functions \c begin() and \c end()
@ -387,6 +404,7 @@ 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;

View file

@ -366,6 +366,9 @@ out: (::00:0.:....00){13}$
out: (::X...........){13}$
out: (::00:0.:....00){13}$
out: distinct_keys::0::1::2::3::4::5::6::7::8::9::10::11::12
out: values_4_key(::[0-9]{1,2})+
out: distinct_keys::0::1::2::3::4::5::6::7::8::9::10::11::12
out: values_4_key(::[0-9]{1,2})+
END

View file

@ -172,8 +172,8 @@ namespace test{
verify_MapWrappers<TreeMap>();
verify_MapWrappers<HashMap>();
verify_MultimapKeyIter<TreeMultimap>();
verify_MultimapKeyIter<HashMultimap>();
verify_MultimapIters<TreeMultimap>();
verify_MultimapIters<HashMultimap>();
}
@ -225,12 +225,31 @@ namespace test{
pullOut (tIter);
ASSERT (!sIter && !tIter);
// The each-value-for-given-key-Iterator can be used for a map or multimap.
// In case of a map, it should yield exactly one result for existing values
// and zero results for non-existing mappings
StringIter justSomeKey = eachMapKey (testMap);
TimeIter correspondingVal = eachMapVal (testMap);
++justSomeKey;
++correspondingVal;
ASSERT (justSomeKey);
TimeIter value4key = eachValForKey (testMap, "nonexistent key");
ASSERT (!value4key);
value4key = eachValForKey (testMap, *justSomeKey);
ASSERT (value4key);
ASSERT (*value4key == *correspondingVal);
++value4key;
ASSERT (!value4key);
}
template<class MAP>
void
verify_MultimapKeyIter() ///< @see IterTools_test#verify_filterRepetitions
verify_MultimapIters() ///< @see IterTools_test#verify_filterRepetitions
{
MAP testMap;
for (uint i=0; i<NUM_ELMS; ++i)
@ -247,6 +266,16 @@ namespace test{
ASSERT (keys);
pullOut (keys);
ASSERT (!keys);
cout << "values_4_key";
IntIter vals = eachValForKey (testMap, NUM_ELMS); // non-existent key
ASSERT (!vals);
vals = eachValForKey (testMap, 0);
ASSERT (vals);
pullOut (vals); // should produce anything between 1 and 100 entries
ASSERT (!vals);
}
};