Re: scaled1d conversion operator: doesn't behave as expected
Rene van Paassen <[email protected]> Mon, 26 Apr 2004 10:36:49 +0200
| Newsgroups | gmane.comp.lib.mtl.devel |
|---|---|
| Organization | Delft University of Technology, Aerospace Engineering |
| Message-ID | <[email protected]> |
On Mon, 2004-04-26 at 09:43, Walter Daems wrote:
> Hi all,
>
> The conversion operator "operator Vector&()" in scaled1d.h does not
> behave as I expected (maybe my expectations were wrong..., if so,
> don't hesitate to correct me):
> instead of returning a scaled vector, it returns the original one.
> propose to make a change to scaled1d.h, removing the
> confusing behavior of the conversion operator. The disadvantage is
> O(nnz()) instead of O(1), but O(1) for an operation that (in my opinion)
> is not very useful makes less sense than spending O(nnz()) on a
> useful operation.
>
Your mail made me check again, and I found that this really confused me
before:
inline operator Vector&() { return rep; }
It hit me when the c++ compiler decided it needed a type conversion from
the scaled1D to the original to-be-scaled vector type. I thought I was
scaling the vector, but this circumvented it. Why is this conversion in
here?
The point of the scaled light-weight object is IIIIC (If I Interpret It
Correctly), that the vector is scaled on-the-fly.
Another thing that strikes me as odd in this context is that the forward
iterator correctly scales the values it gives back, but the reverse
iterator doesn't seem to do so.
I really like mtl, but I found out (the hard way) I have always got to
carefully check that it does what I am intending it to do.
> The patch in attachment shows exactly what my proposal is.
If you want to hard-scale a vector, I think we should use scale, not
scaled (implemented in oned_scale in mtl.h)
> bye,
>
> Walter.
>
> ______________________________________________________________________
> *** orig/scaled1D.h 2004-04-25 23:16:59.000000000 +0200
> --- adpt/scaled1D.h 2004-04-26 09:32:13.000000000 +0200
> ***************
> *** 124,135 ****
> //: Destructor
> inline ~scaled1D() { }
>
> /**@name Access Methods */
>
> ! //: Access base containers
> ! inline operator Vector&() { return rep; }
>
> //: Return a const iterator pointing to the beginning of the vector
> //!wheredef: Container
> inline const_iterator begin() const {
> return const_iterator(rep.begin(), scale);
> --- 124,145 ----
> //: Destructor
> inline ~scaled1D() { }
>
> /**@name Access Methods */
>
> ! //: Conversion operator
> ! inline operator Vector() {
> ! Vector v( rep.size() );
> !
> ! typename Vector::const_iterator it = rep.begin();
> ! const typename Vector::const_iterator endit = rep.end();
> !
> ! for ( ; it != endit; ++it )
> ! v[ it.index() ] = *it * scale;
> !
> ! return v;
> ! }
Now it tries to duplicate the original vector, this has unexpected side
effects (copy and assignment are shallow in mtl, meaning that both
vectors will share the same data). I think this operator should simply
be removed (although I don't know whether this breaks things).
> //: Return a const iterator pointing to the beginning of the vector
> //!wheredef: Container
> inline const_iterator begin() const {
> return const_iterator(rep.begin(), scale);
>
> ______________________________________________________________________
> _______________________________________________
> This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/
--
M.M. (René) van Paassen [email protected]
Assistant Professor tel. +31 15 278 5370
Faculty of Aerospace Engineering fax. +31 15 278 6480
Kluijverweg 1, NL-2629 HS Delft Delft University of Technology
>>>>>>>>>>>> http://www.cs.lr.tudelft.nl <<<<<<<<<<<<
_______________________________________________
This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/