Qs on 'late construction' and operator >>



 DEVELOP > c-Plus-Plus > Qs on 'late construction' and operator >>

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Michael Hopkins"
Date: 16 May 2005 09:47:26 AM
Object: Qs on 'late construction' and operator >>
Hi all
I have a class that contains as one of it's members a type that is
internally constructed like this:
std::vector< std::valarray< float > > foo
Think of it as a matrix. I cannot construct foo when the class itself is
constructed because I don't know how big it needs to be until I have loaded
that information from a file.
Within the member function that loads the file (including the information r
& c), this compiles OK:
std::vector< std::valarray< float > > temp( r, std::valarray<float>( c ));
foo = temp;

....but I'm sure there is a more elegant/efficient way to do this on foo
directly - any tips on this?
I have defined access to this type like a Fortran array i.e. foo( i, j )
either reads from or writes to the j'th valarray element of the i'th vector.
This works fine in general usage but, not when I am trying to load from the
input file in a loop:
std::ifstream infile( from_path );
// in i, j, loop
infile >> foo( i, j );
Again, any tips welcomed.
TIA
Michael
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/ _/ _/_/_/ Hopkins Research Ltd
_/ _/ _/ _/
_/_/_/_/ _/_/_/ http://www.hopkins-research.com/
_/ _/ _/ _/
_/ _/ _/ _/ 'touch the future'

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
.

User: "Dave Harris"

Title: Re: Qs on 'late construction' and operator >> 18 May 2005 04:15:17 AM
(Michael Hopkins) wrote (abridged):

I cannot construct foo when the class itself is constructed because
I don't know how big it needs to be until I have loaded that
information from a file.

Would it help to pass the input stream to the class's constructor?

std::vector< std::valarray< float > > temp( r, std::valarray<float>( c

));

foo = temp;

Replacing the assignment with:
foo.swap( temp );
ought to be an efficient solution. Vector's default constructor will
probably not allocate any memory, and the swap won't either.
-- Dave Harris, Nottingham, UK.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
.

User: "Kristo"

Title: Re: Qs on 'late construction' and operator >> 17 May 2005 05:00:50 AM
Michael Hopkins wrote:

Hi all

I have a class that contains as one of it's members a type that is
internally constructed like this:

std::vector< std::valarray< float > > foo

Think of it as a matrix. I cannot construct foo when the class
itself is constructed because I don't know how big it needs to be
until I have loaded that information from a file.

Within the member function that loads the file (including the
information r & c), this compiles OK:

std::vector< std::valarray< float > > temp( r, std::valarray<float>(

c ));

foo = temp;

...but I'm sure there is a more elegant/efficient way to do this on
foo directly - any tips on this?

[snip]
You could use a (smart) pointer type for 'foo' instead of making it a
local object. Have the class' constructor assign a null pointer to
'foo'. Treat this as a sort of "zombie" state for the object, i.e.,
the object isn't fully usable until the data is loaded from the file.
Then, once you have the necessary information, you can instantiate
'foo':
foo=new std::vector<std::valarray<float> >(r,std::valarray<float>(c));
Note that the above line assumes that foo is a raw pointer type.
You'll have to modify it if you use a smart pointer.
Kristo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
.

User: ""

Title: Re: Qs on 'late construction' and operator >> 17 May 2005 12:26:23 PM
Michael Hopkins wrote:

I have a class that contains as one of it's members a type
that is internally constructed like this:
std::vector< std::valarray< float > > foo
Think of it as a matrix. I cannot construct foo when the
class itself is constructed because I don't know how big it
needs to be until I have loaded that information from a file.

No problem. If you use the default constructor, the vector is
empty.

Within the member function that loads the file (including the
information r & c), this compiles OK:
std::vector< std::valarray< float > > temp( r, std::valarray<float>(

c ));

foo = temp;
...but I'm sure there is a more elegant/efficient way to do
this on foo directly - any tips on this?

I don't have any problem with doing it this way, although it can
be relatively inefficient if the array is large.

I have defined access to this type like a Fortran array
i.e. foo( i, j ) either reads from or writes to the j'th
valarray element of the i'th vector. This works fine in
general usage but, not when I am trying to load from the input
file in a loop:
std::ifstream infile( from_path );
// in i, j, loop
infile >> foo( i, j );
Again, any tips welcomed.

I'm not sure how you could define access to the foo object like
this -- you can't add functions to std::vector, and the
operator()() must be a member.
Anyway, with regards to your problem, the obvious solution would
be to use push_back on the vector for each line of data you
read. With regards to the valarray part, I have relatively
little experience; I think you'll have to experiment to find out
whether it is faster to use a local valarray, then push_back it,
or to push_back an empty (default constructed) valarray, then
use resize on the element in the vector.
I might add that if you need a Fortran compatible array, this
will not do the job. For that, you'll probable have to use a
simple std::vector< float >, and manage the dimensions yourself.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
.
User: "Michael Hopkins"

Title: Re: Qs on 'late construction' and operator >> 18 May 2005 04:22:45 AM
Thanks for the input guys.
The >> problem was a dumb thing on my part - fixed easily.
For the 'late construction' part I experimented with the smart pointer/new
approach as some suggested, but ended up initialising the:
std::vector< std::valarray< float > > foo;
...as a placeholder and then using the built in .resize() abilities of both
vector and valarray appropriately. This turns out to be fast and means I
don't have to mess about with memory allocation/deallocation myself. I have
a preference for avoiding pointers and using the STL as much as possible to
let other people deal with the headaches!
Someone asked about the ( i, j ) access - I do this on all my linear
algebra/maths code to mimic Fortran syntax from 1 to n, just as I did before
with C (but with different syntax in that case). One way is to use:
valarray<T> v( rows * cols );
...for storage and set up another valarray of pointers (uo_row_ptr) to the
start of each 'row' in the matrix and then:
T & operator ()( const iter r, const iter c )
{
return *(uo_row_ptr( r )+c);
};
There are many options but this one works well, usually just as fast as or
sometimes even faster than raw array access with a good compliler and
optimisation in fact (especially when you use valarray).
It allows me to easily escape the dreaded and almost universal requirement
amongst the C++ community to do access/loops from 0 to n-1 which is so
unnatural for mathematical work. This is one of the main reasons why I
don't use some of the powerful libraries like uBlas, MTL and Blitz.
M
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/ _/ _/_/_/ Hopkins Research Ltd
_/ _/ _/ _/
_/_/_/_/ _/_/_/ http://www.hopkins-research.com/
_/ _/ _/ _/
_/ _/ _/ _/ 'touch the future'
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
.


User: "Richard Wolf"

Title: Re: Qs on 'late construction' and operator >> 16 May 2005 10:59:25 PM
On Mon, 2005-05-16 at 10:47 -0400, Michael Hopkins wrote:


Hi all

I have a class that contains as one of it's members a type that is
internally constructed like this:

std::vector< std::valarray< float > > foo

Think of it as a matrix. I cannot construct foo when the class itself is
constructed because I don't know how big it needs to be until I have loaded
that information from a file.

Within the member function that loads the file (including the information r
& c), this compiles OK:

std::vector< std::valarray< float > > temp( r, std::valarray<float>( c ));
foo = temp;

...but I'm sure there is a more elegant/efficient way to do this on foo
directly - any tips on this?

Seems like a smart approach to me.
However replacing :
foo = temp
with:
std::swap(foo, temp)
will probably be more efficient, since it will avoid allocating memory
twice and copying. I don't know if this will matter to you or not,
considering that you then read the contents from disk.

I have defined access to this type like a Fortran array i.e. foo( i, j )
either reads from or writes to the j'th valarray element of the i'th vector.
This works fine in general usage but, not when I am trying to load from the
input file in a loop:

std::ifstream infile( from_path );
// in i, j, loop
infile >> foo( i, j );

What compiler error or unwanted behaviour do you get?
Post code.

Again, any tips welcomed.

TIA

Michael

.

User: ""

Title: Re: Qs on 'late construction' and operator >> 17 May 2005 04:42:01 AM
std::vector< std::valarray< float > > foo;
// load your info here
foo.resize(r, std::valarray<float>( c));
---
1) why don't u use foo[i][j] operators ?
2) It should works fine, I think something wrong in your defined access
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER