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.
The simplest seems to be to use run-time polymorphism.
being the concrete iterator type in question.
This is so common a technique that I'm almost sure it must have a design pattern
name.
A: Because it messes up the order in which people normally read text.
A: Top-posting.