Re: problem storing different iterators in a container
On 3=D4 16=C8=D5, =CF =CE=E79=CA=B125=B7=D6, Lutz Altmann <lutz.altm...@=
mastersong.de> wrote:
Hi there :)
I'm trying to write a functor-class, which supports the de-
multiplexing of data from one
input-container to two or multiple output containers (de-
interleaving).
For example:
vec1 = [1 5 2 6 3 7]
#demultiplex vec1 -> result1,result2
result1 = [1 2 3]
result2 = [5 6 7]
The idea is, to provide a functor-class , which makes the de-
multiplexing very easy to use:
It should be possible to do soemthing like this:
snip<
DemuxFunctor func;
func.addChannel(output_iter);
func.addChannel(output_iter2);
..
..
for_each(input_vec.begin(),input_vec.end(),func);
snip<
It should be possible to "register" iterators to which the functor
writes the output-data.
here is my draft :
#include <vector>
#include <iterator>
using namespace std;
template<typename T>
class DemuxFunctor : public unary_function<void,T>
{
public:
DemuxFunctor():m_current(0){}
void operator()(const T& arg)
{
// add arg to one of the iterators
*(m_outchannels(m_current)) = arg;
// step ahead
(m_outchannels(m_current))++;
if (m_current >= m_outchannels.size())
{
m_current = 0;
}
else
{
m_current++;
}
}
template<typename X>
void addChannel(insert_iterator<X>& iter)
{
m_outchannels.push_back(iter);
}
private:
// PROBLEM : how to declare this vector !!!
vector<insert_iterator&> m_outchannels;..
unsigned int m_current;
};
The Problem is, that i have to save the (registered) iterators in a
data-structure - but because
different kinds of iterators (vectors,lists..) should be possible i
dont know how to declare the
iterator-vector .. is there a solution?!
Maybe you also have some princible comments to the design.
Regards,
Lutz
#include <vector>
#include <iterator>
#include <functional>
#include <iostream>
using namespace std;
template<typename T>
class DemuxFunctor : public unary_function<void,T>
{
public:
DemuxFunctor():m_current(0){}
private:
class AbstractWrapper
{
public:
virtual void operator = (const T & arg) = 0;
virtual void operator ++ (int) = 0;
};
public:
template<typename IterType>
class WrapperIterator : public AbstractWrapper
{
public:
WrapperIterator(IterType & it) : _iter(it)
{
}
void operator = (const T & arg)
{
*_iter = arg;
}
void operator ++ (int)
{
_iter ++;
}
private:
IterType & _iter;
};
void operator()(const T& arg)
{
// add arg to one of the iterators
*m_outchannels[m_current] = arg;
// step ahead
(*m_outchannels[m_current ++]) ++;
if (m_current >= m_outchannels.size())
{
m_current = 0;
}
}
void addChannel(AbstractWrapper *iter)
{
m_outchannels.push_back(iter);
}
private:
// PROBLEM : how to declare this vector !!!
vector<AbstractWrapper *> m_outchannels;
unsigned int m_current;
};
int main()
{
int buffer[] = {1, 2};
vector<int> a;
vector<int> b;
insert_iterator<vector<int> > ita(a, a.begin());
insert_iterator<vector<int> > itb(b, b.begin()) ;
DemuxFunctor<int>::WrapperIterator< insert_iterator<vector<int> > >
wa(ita);
DemuxFunctor<int>::WrapperIterator< insert_iterator<vector<int> > >
wb(itb);
DemuxFunctor<int> df;
df.addChannel(&wa);
df.addChannel(&wb);
for (int i = 0; i < 5; ++i)
{
df(i);
}
copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
cout << endl;
copy(b.begin(), b.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Hope you may enjoy it~
Best Regards,
Junchen