S
StephQ
I need to implement an algorithm that takes as input a container and
write some output in another container.
The containers involved are usually vectors, but I would like not to
rule out the possibility of using lists.
The problem is that I need two versions of it, depending if I'm adding
the generated (by the algorithm) values to the target container or if
I just modify pre-existing values of the target container.
Efficiency is important in this part of the software.
The first solution is to write two versions of the algorithm:
[firstTime, lastTime) define the input range, and firstTarget define
the begin of target container.
// Modify version, here V iterator, will use *firstTarget =
// More precisely V is usually std::vector<double>::iterator taken
from someVector.begin().
template<class V, class T>
void bmPathModify( V firstTarget, T firstTime, T lastTime )
// Append version, here V is the container, will use
firstTarget.push_back()
template<class V, class T>
void bmPathAppend( V& firstTarget, T firstTime, T lastTime )
However this situation is going to repeat itself a lot of times, so I
would like to use just one function and specify the behaviour using a
policy defined by an additional template parameter:
// P is the policy
template<class V, class T, class P>
void bmPathModify( V firstTarget, T firstTime, T lastTime )
{
...
P::Element( target, value );
..
}
class Modify // Policy class
{
public:
template<class V>
static void Element( V target, double value)
{
*target = value;
}
};
The problem is then:
I need to pass the iterator to the starting element of the target
container to cover the Modify case.
But then I can't figure out how to invoke the push_back() function to
the target container starting from this iterator. I suspect I can't.
What would be perfect would be the ability to deference the iterator
two times to get the actual container but I think you can't do
this....
Is there a way to solve this situation using a single function for the
algorithm?
Thank you
StephQ
write some output in another container.
The containers involved are usually vectors, but I would like not to
rule out the possibility of using lists.
The problem is that I need two versions of it, depending if I'm adding
the generated (by the algorithm) values to the target container or if
I just modify pre-existing values of the target container.
Efficiency is important in this part of the software.
The first solution is to write two versions of the algorithm:
[firstTime, lastTime) define the input range, and firstTarget define
the begin of target container.
// Modify version, here V iterator, will use *firstTarget =
// More precisely V is usually std::vector<double>::iterator taken
from someVector.begin().
template<class V, class T>
void bmPathModify( V firstTarget, T firstTime, T lastTime )
// Append version, here V is the container, will use
firstTarget.push_back()
template<class V, class T>
void bmPathAppend( V& firstTarget, T firstTime, T lastTime )
However this situation is going to repeat itself a lot of times, so I
would like to use just one function and specify the behaviour using a
policy defined by an additional template parameter:
// P is the policy
template<class V, class T, class P>
void bmPathModify( V firstTarget, T firstTime, T lastTime )
{
...
P::Element( target, value );
..
}
class Modify // Policy class
{
public:
template<class V>
static void Element( V target, double value)
{
*target = value;
}
};
The problem is then:
I need to pass the iterator to the starting element of the target
container to cover the Modify case.
But then I can't figure out how to invoke the push_back() function to
the target container starting from this iterator. I suspect I can't.
What would be perfect would be the ability to deference the iterator
two times to get the actual container but I think you can't do
this....
Is there a way to solve this situation using a single function for the
algorithm?
Thank you
StephQ