2010-04-01 18:03:34 +02:00
|
|
|
/*
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
namespace iter_stl {
|
|
|
|
|
|
|
|
|
|
|
2010-04-02 00:07:45 +02:00
|
|
|
/**
|
|
|
|
|
* helper baseclass to simplify
|
|
|
|
|
* defining customised wrapped STL iterators
|
|
|
|
|
*/
|
|
|
|
|
template<typename IT>
|
|
|
|
|
class WrappedStlIter
|
|
|
|
|
{
|
|
|
|
|
IT i_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
typedef typename IT::value_type value_type;
|
|
|
|
|
typedef typename IT::reference reference;
|
|
|
|
|
typedef typename IT::pointer pointer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WrappedStlIter() : i_() { }
|
|
|
|
|
WrappedStlIter(IT const& i) : i_(i) { }
|
|
|
|
|
|
|
|
|
|
IT const& get() const { return i_; }
|
|
|
|
|
pointer operator->() const { return i_; }
|
|
|
|
|
reference operator*() const { return *i_; }
|
|
|
|
|
|
|
|
|
|
WrappedStlIter& operator++() { ++i_; return *this; }
|
|
|
|
|
|
|
|
|
|
friend bool operator== (WrappedStlIter const& i1, WrappedStlIter const& i2) { return i1.i_ == i2.i_; }
|
|
|
|
|
friend bool operator!= (WrappedStlIter const& i1, WrappedStlIter const& i2) { return i1.i_ != i2.i_; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-04-02 01:46:48 +02:00
|
|
|
template<typename IT>
|
|
|
|
|
class DistinctIter
|
|
|
|
|
: public lib::BoolCheckable<DistinctIter<IT> >
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef typename IT::value_type value_type;
|
|
|
|
|
typedef typename IT::reference reference;
|
|
|
|
|
typedef typename IT::pointer pointer;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
IT i_;
|
|
|
|
|
pointer prev_;
|
|
|
|
|
|
|
|
|
|
void memorise() { if (i_) prev_ = &(*i_); }
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
DistinctIter() : i_(), prev_() { }
|
|
|
|
|
DistinctIter(IT const& i) : i_(i),prev_() { memorise(); }
|
|
|
|
|
|
|
|
|
|
pointer operator->() const { return i_; }
|
|
|
|
|
reference operator*() const { return *i_; }
|
|
|
|
|
bool isValid() const { return i_; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DistinctIter&
|
|
|
|
|
operator++()
|
|
|
|
|
{
|
|
|
|
|
do ++i_;
|
|
|
|
|
while (i_ && prev_ && *prev_ == *i_ );
|
|
|
|
|
memorise();
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend bool operator== (DistinctIter const& i1, DistinctIter const& i2) { return i1.i_ == i2.i_; }
|
|
|
|
|
friend bool operator!= (DistinctIter const& i1, DistinctIter const& i2) { return i1.i_ != i2.i_; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-04-01 18:03:34 +02:00
|
|
|
namespace { // traits and helpers...
|
2010-04-02 00:07:45 +02:00
|
|
|
|
2010-04-01 18:03:34 +02:00
|
|
|
template<class MAP>
|
2010-04-02 00:07:45 +02:00
|
|
|
struct _MapTypeSelector
|
2010-04-01 18:03:34 +02:00
|
|
|
{
|
2010-04-02 00:07:45 +02:00
|
|
|
typedef typename MAP::value_type::first_type Key;
|
2010-04-01 18:03:34 +02:00
|
|
|
typedef typename MAP::value_type::second_type Val;
|
2010-04-02 00:07:45 +02:00
|
|
|
typedef typename MAP::iterator Itr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class MAP>
|
|
|
|
|
struct _MapTypeSelector<const MAP>
|
|
|
|
|
{
|
|
|
|
|
typedef typename MAP::value_type::first_type Key;
|
|
|
|
|
typedef typename MAP::value_type::second_type const Val;
|
|
|
|
|
typedef typename MAP::const_iterator Itr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class MAP>
|
|
|
|
|
struct _MapT
|
|
|
|
|
{
|
|
|
|
|
typedef typename _MapTypeSelector<MAP>::Key KeyType;
|
|
|
|
|
typedef typename _MapTypeSelector<MAP>::Val ValType;
|
|
|
|
|
typedef typename _MapTypeSelector<MAP>::Itr EntryIter;
|
|
|
|
|
|
|
|
|
|
typedef WrappedStlIter<EntryIter> WrapI;
|
|
|
|
|
|
|
|
|
|
struct PickKeyIter : WrapI
|
|
|
|
|
{
|
|
|
|
|
typedef KeyType value_type;
|
|
|
|
|
typedef KeyType& reference;
|
|
|
|
|
typedef KeyType* pointer;
|
|
|
|
|
|
2010-04-02 01:46:48 +02:00
|
|
|
PickKeyIter () : WrapI() {}
|
|
|
|
|
PickKeyIter (EntryIter const& iter) : WrapI(iter) {}
|
2010-04-02 00:07:45 +02:00
|
|
|
|
|
|
|
|
pointer operator->() const { return &(WrapI::get()->first); }
|
|
|
|
|
reference operator* () const { return (WrapI::get()->first); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct PickValIter : WrapI
|
|
|
|
|
{
|
|
|
|
|
typedef ValType value_type;
|
|
|
|
|
typedef ValType& reference;
|
|
|
|
|
typedef ValType* pointer;
|
|
|
|
|
|
2010-04-02 01:46:48 +02:00
|
|
|
PickValIter () : WrapI() {}
|
|
|
|
|
PickValIter (EntryIter const& iter) : WrapI(iter) {}
|
2010-04-02 00:07:45 +02:00
|
|
|
|
|
|
|
|
pointer operator->() const { return &(WrapI::get()->second); }
|
|
|
|
|
reference operator* () const { return (WrapI::get()->second); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef RangeIter<PickKeyIter> KeyIter;
|
|
|
|
|
typedef RangeIter<PickValIter> ValIter;
|
2010-04-02 03:36:31 +02:00
|
|
|
|
|
|
|
|
typedef DistinctIter<KeyIter> DistinctKeys;
|
2010-04-01 18:03:34 +02:00
|
|
|
};
|
2010-04-02 01:46:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class SEQ>
|
|
|
|
|
struct _SeqT
|
|
|
|
|
{
|
|
|
|
|
typedef typename SEQ::iterator Iter;
|
|
|
|
|
typedef RangeIter<Iter> Range;
|
|
|
|
|
typedef DistinctIter<Range> DistinctVals;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class SEQ>
|
|
|
|
|
struct _SeqT<const SEQ>
|
|
|
|
|
{
|
|
|
|
|
typedef typename SEQ::const_iterator Iter;
|
|
|
|
|
typedef RangeIter<Iter> Range;
|
|
|
|
|
typedef DistinctIter<Range> DistinctVals;
|
|
|
|
|
};
|
2010-04-01 18:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-02 00:07:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @return Lumiera Forward Iterator to yield
|
|
|
|
|
* each key of a map/multimap
|
2010-04-01 18:03:34 +02:00
|
|
|
*/
|
|
|
|
|
template<class MAP>
|
|
|
|
|
inline typename _MapT<MAP>::KeyIter
|
2010-04-02 00:07:45 +02:00
|
|
|
eachKey (MAP& map)
|
2010-04-01 18:03:34 +02:00
|
|
|
{
|
2010-04-02 00:07:45 +02:00
|
|
|
typedef typename _MapT<MAP>::KeyIter Range;
|
|
|
|
|
typedef typename _MapT<MAP>::PickKeyIter PickKey;
|
|
|
|
|
|
|
|
|
|
return Range (PickKey (map.begin()), PickKey (map.end()));
|
2010-04-01 18:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-02 00:07:45 +02:00
|
|
|
/** @return Lumiera Forward Iterator to yield
|
|
|
|
|
* each value within a map/multimap
|
2010-04-01 18:03:34 +02:00
|
|
|
*/
|
|
|
|
|
template<class MAP>
|
|
|
|
|
inline typename _MapT<MAP>::ValIter
|
2010-04-02 00:07:45 +02:00
|
|
|
eachVal (MAP& map)
|
2010-04-01 18:03:34 +02:00
|
|
|
{
|
2010-04-02 00:07:45 +02:00
|
|
|
typedef typename _MapT<MAP>::ValIter Range;
|
|
|
|
|
typedef typename _MapT<MAP>::PickValIter PickVal;
|
|
|
|
|
|
|
|
|
|
return Range (PickVal (map.begin()), PickVal (map.end()));
|
2010-04-01 18:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-02 01:46:48 +02:00
|
|
|
/** build a Lumiera Forward Iterator to suppress
|
|
|
|
|
* any repetitions in the given sequence.
|
|
|
|
|
*/
|
|
|
|
|
template<class SEQ>
|
|
|
|
|
inline typename _SeqT<SEQ>::DistinctVals
|
|
|
|
|
eachDistinct (SEQ& seq)
|
|
|
|
|
{
|
|
|
|
|
typedef typename _SeqT<SEQ>::Range Range;
|
|
|
|
|
typedef typename _SeqT<SEQ>::DistinctVals DistinctValues;
|
|
|
|
|
|
|
|
|
|
return DistinctValues (Range (seq.begin(), seq.end()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-02 03:36:31 +02:00
|
|
|
/** @return Lumiera Forward Iterator to yield
|
|
|
|
|
* the distinct keys from a multimap
|
|
|
|
|
* @warning full scan of all keys, dropping repetitions
|
2010-04-01 18:03:34 +02:00
|
|
|
*/
|
|
|
|
|
template<class MAP>
|
2010-04-02 03:36:31 +02:00
|
|
|
inline typename _MapT<MAP>::DistinctKeys
|
|
|
|
|
eachDistinctKey (MAP& map)
|
2010-04-01 18:03:34 +02:00
|
|
|
{
|
2010-04-02 03:36:31 +02:00
|
|
|
return typename _MapT<MAP>::DistinctKeys (eachKey (map));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @return Lumiera Forward Iterator to yield
|
|
|
|
|
* the distinct keys from a multimap
|
|
|
|
|
* @warning full scan of all keys, dropping repetitions
|
|
|
|
|
*/
|
|
|
|
|
template<class MMAP, typename KEY>
|
|
|
|
|
inline typename _MapT<MMAP>::ValIter
|
|
|
|
|
eachValForKey (MMAP& multimap, KEY key)
|
|
|
|
|
{
|
|
|
|
|
typedef typename _MapT<MMAP>::EntryIter Pos;
|
|
|
|
|
typedef typename _MapT<MMAP>::ValIter Range;
|
|
|
|
|
typedef typename _MapT<MMAP>::PickValIter PickVal;
|
|
|
|
|
|
|
|
|
|
std::pair<Pos,Pos> valRange = multimap.equal_range (key);
|
|
|
|
|
|
|
|
|
|
return Range (PickVal (valRange.first), PickVal (valRange.second));
|
2010-04-01 18:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::iter_stl
|
|
|
|
|
#endif
|