Skip to content
Snippets Groups Projects
Commit 72f5364e authored by Mark Jenkinson's avatar Mark Jenkinson
Browse files

Added memory efficient read_binary_matrix routines

parent e5bd7fa4
No related branches found
No related tags found
No related merge requests found
......@@ -191,21 +191,35 @@ namespace MISCMATHS {
ReturnMatrix read_binary_matrix(const string& filename)
{
Matrix mat;
if ( filename.size()<1 ) return mat;
Matrix mres;
read_binary_matrix(mres,filename);
mres.Release();
return mres;
}
int read_binary_matrix(Matrix& mres, const string& filename)
{
if ( filename.size()<1 ) return 1;
ifstream fs(filename.c_str(), ios::in | ios::binary);
if (!fs) {
cerr << "Could not open matrix file " << filename << endl;
return mat;
return 2;
}
mat = read_binary_matrix(fs);
read_binary_matrix(mres,fs);
fs.close();
mat.Release();
return mat;
return 0;
}
ReturnMatrix read_binary_matrix(ifstream& fs)
{
Matrix mres;
read_binary_matrix(mres,fs);
mres.Release();
return mres;
}
int read_binary_matrix(Matrix& mres, ifstream& fs)
{
bool swapbytes = false;
unsigned int testval;
......@@ -216,9 +230,7 @@ namespace MISCMATHS {
Swap_Nbytes(1,sizeof(testval),&testval);
if (testval!=BINFLAG) {
cerr << "Unrecognised binary matrix file format" << endl;
Matrix mres;
mres.Release();
return mres;
return 2;
}
}
......@@ -235,7 +247,9 @@ namespace MISCMATHS {
// set up and read matrix (rows fast, cols slow)
double val;
Matrix mres(nx,ny);
if ( (((unsigned int) mres.Ncols())!=ny) || (((unsigned int) mres.Nrows())<nx) ) {
mres.ReSize(nx,ny);
}
for (unsigned int y=1; y<=ny; y++) {
for (unsigned int x=1; x<=nx; x++) {
fs.read((char*)&val,sizeof(val));
......@@ -244,8 +258,7 @@ namespace MISCMATHS {
}
}
mres.Release();
return mres;
return 0;
}
......
......@@ -55,6 +55,7 @@ namespace MISCMATHS {
ReturnMatrix read_ascii_matrix(int nrows, int ncols, const string& filename);
ReturnMatrix read_ascii_matrix(const string& filename);
ReturnMatrix read_vest(string p_fname);
int read_binary_matrix(Matrix& mres, const string& filename);
ReturnMatrix read_binary_matrix(const string& filename);
ReturnMatrix read_matrix(const string& filename);
......@@ -71,6 +72,7 @@ namespace MISCMATHS {
ReturnMatrix read_ascii_matrix(ifstream& fs, int nrows, int ncols);
ReturnMatrix read_ascii_matrix(int nrows, int ncols, ifstream& fs);
ReturnMatrix read_ascii_matrix(ifstream& fs);
int read_binary_matrix(Matrix& mres, ifstream& fs);
ReturnMatrix read_binary_matrix(ifstream& fs);
int write_ascii_matrix(const Matrix& mat, ofstream& fs, int precision=-1);
int write_ascii_matrix(ofstream& fs, const Matrix& mat, int precision=-1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment