Require help/suggestion

S

sandeep chandra

Hey guys, I am new to this group.. i never know wot s going on in this
group.. but wot made be brought here is cpp.. guys am currently a
part of onw reaserch ... am new to everything.. i can prog in c and
a bit in cpp.. to say ..am a fair programmer... there is a new
problem in my research work.. which if solved , gives me a gud tool
to develop my prog in a much better way.. lemme describe my prob...

i have a routine in my prog which is currently calculating some (N-L *
L )matrixes... where both N and L vary.... so i have got a set of
matrixes which are varying in dimensions. n guys , wots the wrost part
is ... i am calculating these same matrixes again n again... waste of
time and memory... forget abt storing in memory, forget abt
recalculating them... i need to write these dimensionally varying
matrixes on to a file.. and read bak my one particular matrix bak again
when ever i need it. u can imagine ..N and L are in hundreds.. and i
need to get bak my required matrix from the pool of matrixes in a
file.. and one more thing... i cant use different files for different
matrixes.. alll of them must be contained in one single file.. and each
matrixes boundries must be well defined.. help me out buddiess....
how can solve this problem.. i will be thankful if u guys can give
me a solution to this problem...and i dont have much time also, so
please make it quick. Thank you buddy,

Sandeep Chandra
 
K

Kai-Uwe Bux

sandeep said:
Hey guys, I am new to this group.. i never know wot s going on in this
group.. but wot made be brought here is cpp.. guys am currently a
part of onw reaserch ... am new to everything.. i can prog in c and
a bit in cpp.. to say ..am a fair programmer... there is a new
problem in my research work.. which if solved , gives me a gud tool
to develop my prog in a much better way.. lemme describe my prob...

Especially for non-native speakers of English, it really would help if your
spellings looked familiar: the sound-letter correspondence in English is
far from a bijection, which makes it sometimes hard to guess which word you
abbreviate.

i have a routine in my prog which is currently calculating some (N-L *
L )matrixes... where both N and L vary.... so i have got a set of
matrixes which are varying in dimensions. n guys , wots the wrost part
is ... i am calculating these same matrixes again n again... waste of
time and memory... forget abt storing in memory, forget abt
recalculating them... i need to write these dimensionally varying
matrixes on to a file.. and read bak my one particular matrix bak again
when ever i need it. u can imagine ..N and L are in hundreds.. and i
need to get bak my required matrix from the pool of matrixes in a
file.. and one more thing... i cant use different files for different
matrixes.. alll of them must be contained in one single file.. and each
matrixes boundries must be well defined.. help me out buddiess....
how can solve this problem.. i will be thankful if u guys can give
me a solution to this problem...and i dont have much time also, so
please make it quick. Thank you buddy,

Just a request for clarifications:

a) Is this a "compute and write once -- read often" type of thing, or will
you need to write back changes to the matrices stored in the file?

b) If you need to update matrices in the file, will the size of the matrix
still be the same or might it change, too? (This would rule out some simple
minded ideas.)

c) Why is it that you cannot use different files for different dimensions of
the matrix? Do you just have a maximum number of files due to OS
constraints or is there is an unmentioned design constraint that says you
may use only one file? If so, why.

d) Do you need the data in the file to be persistent, i.e., remain existent
and meaningful after the program finished?


Best

Kai-Uwe Bux
 
J

John Harrison

sandeep said:
Hey guys, I am new to this group.. i never know wot s going on in this
group.. but wot made be brought here is cpp.. guys am currently a
part of onw reaserch ... am new to everything.. i can prog in c and
a bit in cpp.. to say ..am a fair programmer... there is a new
problem in my research work.. which if solved , gives me a gud tool
to develop my prog in a much better way.. lemme describe my prob...

i have a routine in my prog which is currently calculating some (N-L *
L )matrixes... where both N and L vary.... so i have got a set of
matrixes which are varying in dimensions. n guys , wots the wrost part
is ... i am calculating these same matrixes again n again... waste of
time and memory... forget abt storing in memory, forget abt
recalculating them... i need to write these dimensionally varying
matrixes on to a file.. and read bak my one particular matrix bak again
when ever i need it. u can imagine ..N and L are in hundreds.. and i
need to get bak my required matrix from the pool of matrixes in a
file.. and one more thing... i cant use different files for different
matrixes.. alll of them must be contained in one single file.. and each
matrixes boundries must be well defined.. help me out buddiess....
how can solve this problem.. i will be thankful if u guys can give
me a solution to this problem...and i dont have much time also, so
please make it quick. Thank you buddy,

Sandeep Chandra

The problem doesn't seem very difficult for a 'fair programmer', what
part are you struggling with?

To solve it however more information is needed, for instance are there
any limits on the number of matrices that you will store in this file?
You say you need only one file, would it be reasonable to use two files?
One file would act as an index into the other file.

john
 
H

Howard

sandeep chandra said:
Hey guys, I am new to this group.. i never know wot s going on in this
group.. but wot made be brought here is cpp.. guys am currently a
part of onw reaserch ... am new to everything.. i can prog in c and
a bit in cpp.. to say ..am a fair programmer... there is a new
problem in my research work.. which if solved , gives me a gud tool
to develop my prog in a much better way.. lemme describe my prob...

Since you're new to the group, let me make a suggestion regarding posts you
make here:

Please use complete sentences. And complete words, for that matter. This
is not Instant Messenger, or a cell phone. Reading the above paragraph is
like reading old telegrams from the World War 2 era. If you want help, take
the time to write legibly, and we're more likely to take the time to read
and respond, ok?

-Howard
 
S

sandeep chandra

Hi Kai,

Sorry for replying late and sorry for my abbrivation with english. ok
let me come to the point.

a) yes, it is compute once and read often type...but there might be
some cases where i need to append the file with more matrixes

b)The size of the previously calculated matrix does not change, only
the new ones get appended.

c)I just dint want to create a mess of files on the output folder as i
already have a lot of them. Moreover there will be hundreds or
thousands of matrixes, so it will be messy to create so many files.
There is no such rule, if we can avoid that mess.

d) the data need not to be persistent. program needs it, it need not
mean anything to user.

I admit that am not a good programmer, please see it from my
prospective. Thank you and sorry again for my bad english. Thank you.

Sandeep
 
S

sandeep chandra

Hey john,

I admit that i am not a good programmer, am still in the early stages
of programming.. Why would an Electrical engineer needs to be a c++
guru??.. any ways i need to do this work, so i have to do the
programming whether i know it or not. So plese try to understand my
problems with cpp. Let me come to your question.

Yes it is very good idea to use one file as an index to another. I have
no restriction to use only one file , but i need to keep them from
creating a mess. It would be really helpful for me if u can provide me
with the details of the coding part. I apologise for my late reply .
Thank you,

Sandeep
 
S

sandeep chandra

Hey Howard,

I am very sorry for my english. I was a bit casual with my language.
Now i can understand the problems with such type of english, especially
to non-english speakers. Thank you for your notice and sorry once
again. Thank you.

Sandeep
 
K

Kai-Uwe Bux

sandeep said:
Hi Kai,

Sorry for replying late and sorry for my abbrivation with english. ok
let me come to the point.

a) yes, it is compute once and read often type...but there might be
some cases where i need to append the file with more matrixes

b)The size of the previously calculated matrix does not change, only
the new ones get appended.

c)I just dint want to create a mess of files on the output folder as i
already have a lot of them. Moreover there will be hundreds or
thousands of matrixes, so it will be messy to create so many files.
There is no such rule, if we can avoid that mess.

d) the data need not to be persistent. program needs it, it need not
mean anything to user.

I admit that am not a good programmer, please see it from my
prospective. Thank you and sorry again for my bad english. Thank you.

Sandeep


Ok, here is a suggestion:


For each matrix in the file, keep the point where it starts and where it
ends in the file as a vector of pairs. Whenever you add a matrix to the
file, record the current length of the file as the start position of the
new matrix, write the matrix to the file, record the new length as the end
position for that matrix.

When you need to pull a matrix into memory, retrieve its start and end
positions from the vector that you keep, use seek() to jump to the start
poition in the file, and read the matrix from there.



Best

Kai-Uwe Bux
 
S

sandeep chandra

Hi Kai,

Thank you for your suggestion. At present i have implemented the code
in a very ugly way, with out using seek function. As you said, i tried
to implement the procedure using tellg() and seekg()... but the file
pointer was reading garbage values. I know i am wrong with the code
somewhere , but as i said, i am new to cpp programing, may be i was
wrong with the syntax. can u provide me with a sample program where
seek function is implemented the way you have suggested me to. If i
could use these functions , then my program looks good and less messy .
Thank you once again for your suggestion and help .

Sandeep chandra
 
S

sandeep chandra

Hey john,

Your idea of implementing with two files and using one as index to
other sounds very intresting. Can you please explain me in detail about
the procedure. Also please consider me as a newbie with little
knowledge about programming. Please reply as soon as possible. Thank
you

Sandeep Chandra
 
H

Howard

sandeep chandra said:
Hi Kai,

Thank you for your suggestion. At present i have implemented the code
in a very ugly way, with out using seek function. As you said, i tried
to implement the procedure using tellg() and seekg()... but the file
pointer was reading garbage values. I know i am wrong with the code
somewhere , but as i said, i am new to cpp programing, may be i was
wrong with the syntax. can u provide me with a sample program where
seek function is implemented the way you have suggested me to. If i
could use these functions , then my program looks good and less messy .
Thank you once again for your suggestion and help .

Sandeep chandra

How about posting the code you have, so we can help you see where it's
wrong?

-Howard
 
J

John Harrison

sandeep said:
Hey john,

Your idea of implementing with two files and using one as index to
other sounds very intresting. Can you please explain me in detail about
the procedure. Also please consider me as a newbie with little
knowledge about programming. Please reply as soon as possible. Thank
you

Sandeep Chandra

One file stores the arrays. Very simple flat file.

The other index file stores all the information needed to lookup an
array, for instance it could store the array 'name', maybe the array
bounds, and crucially it stores the offset in the first file where the
array is stored. The index file is small enough that it can be saved in
memory, you would only need to write it out to file when your program
exits. Also the index file is sorted by name for fast lookup (that's why
it's an index).

john
 
K

Kai-Uwe Bux

sandeep said:
Hi Kai,

Thank you for your suggestion. At present i have implemented the code
in a very ugly way, with out using seek function. As you said, i tried
to implement the procedure using tellg() and seekg()... but the file
pointer was reading garbage values. I know i am wrong with the code
somewhere , but as i said, i am new to cpp programing, may be i was
wrong with the syntax. can u provide me with a sample program where
seek function is implemented the way you have suggested me to. If i
could use these functions , then my program looks good and less messy .
Thank you once again for your suggestion and help .

Sandeep chandra

Here is the basic idea. I hope this gets you started:

#include <fstream>
#include <vector>
#include <string>

template < typename T >
class indexed_file {

typedef std::fstream buffer_type;
typedef buffer_type::pos_type pos_type;
typedef std::vector< pos_type > pos_vector;

public:

typedef pos_vector::size_type size_type;
typedef T value_type;

private:

mutable buffer_type file;
pos_vector pos;

public:

indexed_file ( char const * file_name )
: file ( file_name, std::ios::trunc | std::ios::in | std::ios::eek:ut )
{}

~indexed_file ( void ) {}

value_type operator[] ( size_type where ) const {
file.rdbuf()->pubseekpos( pos[ where ] );
value_type result;
file >> result;
return result;
}

void push_back ( T const & t ) {
pos.push_back( file.rdbuf()->pubseekoff( 0, std::ios::end ) );
file << t << ' ';
}

size_type size ( void ) const {
return ( pos.size() );
}

};

#include <iostream>

typedef indexed_file< std::string > wordlist;

int main ( void ) {
wordlist file ( "hello.txt" );
file.push_back( "mother" );
file.push_back( "father" );
file.push_back( "kid" );
file.push_back( "sister" );
for ( wordlist::size_type i = 0; i < file.size(); ++ i ) {
std::cout << file << '\n';
}
}

In order to apply this to your matrix problem, you need to make stream i/o
via operator>> and operator<< work for your matrices.


Hope this helps

Kai-Uwe Bux
 
S

sandeep chandra

Thank you for your help. This part of code must be useful to me for
understanding many things in c++. Thank you all for your help.

Sandeep


Kai-Uwe Bux said:
sandeep said:
Hi Kai,

Thank you for your suggestion. At present i have implemented the code
in a very ugly way, with out using seek function. As you said, i tried
to implement the procedure using tellg() and seekg()... but the file
pointer was reading garbage values. I know i am wrong with the code
somewhere , but as i said, i am new to cpp programing, may be i was
wrong with the syntax. can u provide me with a sample program where
seek function is implemented the way you have suggested me to. If i
could use these functions , then my program looks good and less messy .
Thank you once again for your suggestion and help .

Sandeep chandra

Here is the basic idea. I hope this gets you started:

#include <fstream>
#include <vector>
#include <string>

template < typename T >
class indexed_file {

typedef std::fstream buffer_type;
typedef buffer_type::pos_type pos_type;
typedef std::vector< pos_type > pos_vector;

public:

typedef pos_vector::size_type size_type;
typedef T value_type;

private:

mutable buffer_type file;
pos_vector pos;

public:

indexed_file ( char const * file_name )
: file ( file_name, std::ios::trunc | std::ios::in | std::ios::eek:ut )
{}

~indexed_file ( void ) {}

value_type operator[] ( size_type where ) const {
file.rdbuf()->pubseekpos( pos[ where ] );
value_type result;
file >> result;
return result;
}

void push_back ( T const & t ) {
pos.push_back( file.rdbuf()->pubseekoff( 0, std::ios::end ) );
file << t << ' ';
}

size_type size ( void ) const {
return ( pos.size() );
}

};

#include <iostream>

typedef indexed_file< std::string > wordlist;

int main ( void ) {
wordlist file ( "hello.txt" );
file.push_back( "mother" );
file.push_back( "father" );
file.push_back( "kid" );
file.push_back( "sister" );
for ( wordlist::size_type i = 0; i < file.size(); ++ i ) {
std::cout << file << '\n';
}
}

In order to apply this to your matrix problem, you need to make stream i/o
via operator>> and operator<< work for your matrices.


Hope this helps

Kai-Uwe Bux
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top