Qs on 'late construction' and operator >>

M

Michael Hopkins

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'

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 
R

Richard Wolf

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.
 
A

alexey.br

std::vector< std::valarray< float > > foo;
// load your info here
foo.resize(r, std::valarray<float>( c));
 
K

Kristo

Michael said:
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
 
K

kanze

Michael said:
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.
 
D

Dave Harris

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?

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.
 
M

Michael Hopkins

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'

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top