This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Valarray


Marc Lepage <mlepage@molecularmining.com> writes:


[...]

| > | > | 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?

I thought you were suggesting to implement the valarray library in
a "more OO fashion".  It appears that I misunderstood your question.

No, I didn't consider serialization (or such).  My main concern is to
get the library right and fix its interface.  

-- Gaby

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]