Valarray
Marc Lepage
mlepage@molecularmining.com
Sat Nov 20 11:20:00 GMT 1999
Gabriel Dos Reis wrote:
>
> OK, I see. Believe it or not but I often happen to read the files and
> I do want them to be readable, especially when debugging. I just don't
> like Cpp abuse.
Agreed, just curious as to why the preprocessor was used in some places but not
others. Now I understand. I also read the implementation files and have learned
a lot from them!
> | > | P.S. Not related to libstdc++-v3, has anyone had any experience wrapping
> | > | functors/valarray in more OO fashion, for dynamic binding and persistence? I've
> | > | been playing with that recently, and wouldn't mind comparing notes.
> | >
> | > What do call "more OO fashion" ?
> |
> | I mean:
> |
> | class AdaptableUnaryFunction :
> | public std::unary_function<std::valarray<double>, std::valarray<double>
> | >
> | {
> | public:
> | virtual ~AdaptableUnaryFunction() = 0;
> | virtual result_type operator ()(const argument_type& arg) const = 0;
> | };
>
> Do you realize the implications?
I realize some of the implications, though perhaps not all.
On the one hand, this class is an abstract base class. It's main methods
(function call operator and destructor) are virtual. It can be used in this "OO
fashion" as would any other Fruit or Shape base class. The fact that it derives
from a templated class in the STL is not relevant.
On the other hand, using it with the STL has traps and pitfalls. You cannot use
a generic algorithm with the ABC, else:
pure virtual method called
Aborted (core dumped)
However, this works fine:
// Fill this with data
vector<valarray<double> > cData;
// These derive from AdaptableUnaryFunction, support serialization, etc.
Plus fn1(1); // like binder2nd(plus, 1)
Abs fn2; // like abs(valarray)
UnaryCompose fn(fn1, fn2); // like unary_compose(fn1, fn2)
// Works as expected
transform(cData.begin(), cData.end(), cData.begin(), fn);
// Blows up as above
AdaptableUnaryFunction& fn_ref = fn;
transform(cData.begin(), cData.end(), cData.begin(), fn_ref);
// Prints <UnaryCompose><Plus m_value="1"/><Abs/></UnaryCompose>
cout << fn << endl;
Basically, I'm trying to get the functionality and efficiency of STL function
objects and valarray operations, with the flexibility and dynamic binding of
virtual methods. I need to be able to change the computation at run time,
serialize/deserialize it, etc. Since the data can be of arbitrary width, I use
unary arguments of valarray, and currying of operations.
So far it's working out as well as I could expect, I just wondered if anyone
else had ever done anything like this. Any implications I am missing?
--
Marc Lepage
Software Developer
Molecular Mining Corporation
http://www.molecularmining.com/
More information about the Libstdc++
mailing list