Go to the documentation of this file.00001
00002 #ifndef vnl_io_sparse_matrix_txx_
00003 #define vnl_io_sparse_matrix_txx_
00004
00005
00006
00007 #include "vnl_io_sparse_matrix.h"
00008 #include <vnl/vnl_sparse_matrix.h>
00009 #include <vsl/vsl_binary_io.h>
00010 #include <vcl_cassert.h>
00011
00012
00013
00014
00015
00016
00017
00018
00019 template<class T>
00020 void vsl_b_write(vsl_b_ostream &os, const vnl_sparse_matrix_pair<T> & p)
00021 {
00022 const short io_version_no = 1;
00023 vsl_b_write(os, io_version_no);
00024 vsl_b_write(os, p.first);
00025 vsl_b_write(os, p.second);
00026 }
00027
00028
00029
00030 template<class T>
00031 void vsl_b_read(vsl_b_istream &is, vnl_sparse_matrix_pair<T> & p)
00032 {
00033 if (!is) return;
00034
00035 short ver;
00036 vsl_b_read(is, ver);
00037 switch (ver)
00038 {
00039 case 1:
00040 vsl_b_read(is, p.first);
00041 vsl_b_read(is, p.second);
00042 break;
00043
00044 default:
00045 vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_sparse_matrix_pair<T>&)\n"
00046 << " Unknown version number "<< ver << '\n';
00047 is.is().clear(vcl_ios::badbit);
00048 return;
00049 }
00050 }
00051
00052
00053
00054 template<class T>
00055 void vsl_print_summary(vcl_ostream& os,const vnl_sparse_matrix_pair<T>& p)
00056 {
00057 os<< "Sparse matrix pair ( " << p.first << ',' << p.second << " )\n";
00058 }
00059
00060
00061
00062
00063
00064 template<class T>
00065 void vsl_b_write(vsl_b_ostream & os, const vnl_sparse_matrix<T> & p)
00066 {
00067 typedef vnl_sparse_matrix_pair<T> pair_t;
00068 #if defined(VCL_SUNPRO_CC)
00069
00070 typedef vcl_vector < typename pair_t > row;
00071 typedef vcl_vector < typename row > vnl_sparse_matrix_elements;
00072 #else
00073 typedef vcl_vector < pair_t > row;
00074 typedef vcl_vector < row > vnl_sparse_matrix_elements;
00075 #endif
00076
00077 row rw;
00078 vnl_sparse_matrix<T> v=p;
00079
00080 const short io_version_no = 1;
00081 vsl_b_write(os, io_version_no);
00082 vsl_b_write(os, v.rows());
00083 vsl_b_write(os, v.columns());
00084
00085 for (unsigned int i=0;i<v.rows();i++)
00086 {
00087 rw=v.get_row(i);
00088 vsl_b_write(os, rw.size());
00089 for (unsigned int j=0;j<rw.size();j++)
00090 {
00091 vsl_b_write(os, rw[j]);
00092 }
00093 }
00094 }
00095
00096
00097
00098 template<class T>
00099 void vsl_b_read(vsl_b_istream &is, vnl_sparse_matrix<T> & p)
00100 {
00101 if (!is) return;
00102
00103 typedef vnl_sparse_matrix_pair<T> pair_t;
00104 #if defined(VCL_SUNPRO_CC)
00105
00106 typedef vcl_vector < typename pair_t > row;
00107 typedef vcl_vector < typename row > vnl_sparse_matrix_elements;
00108 #else
00109 typedef vcl_vector < pair_t > row;
00110 typedef vcl_vector < row > vnl_sparse_matrix_elements;
00111 #endif
00112
00113 short ver;
00114 int n_rows;
00115 int n_cols;
00116 int row_size=0;
00117 vsl_b_read(is, ver);
00118
00119 vcl_vector<int> indexes(row_size);
00120 vcl_vector<T> values(row_size);
00121 switch (ver)
00122 {
00123 case 1:
00124 vsl_b_read(is, n_rows);
00125 vsl_b_read(is, n_cols);
00126
00127 assert (n_rows==(int)p.rows());
00128 assert (n_cols==(int)p.columns());
00129 for (int i=0;i<n_rows;i++)
00130 {
00131 vsl_b_read(is,row_size);
00132 indexes.resize(row_size);
00133 values.resize(row_size);
00134
00135 for (int j=0;j<row_size;j++)
00136 {
00137 pair_t q;
00138 vsl_b_read(is, q);
00139 indexes[j] = q.first;
00140 values[j] = q.second;
00141 }
00142 p.set_row(i, indexes, values);
00143 }
00144 break;
00145
00146 default:
00147 vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_sparse_matrix<T>&)\n"
00148 << " Unknown version number "<< ver << '\n';
00149 is.is().clear(vcl_ios::badbit);
00150 return;
00151 }
00152 }
00153
00154
00155
00156 template<class T>
00157 void vsl_print_summary(vcl_ostream & os,const vnl_sparse_matrix<T> & p)
00158 {
00159 os<<"Rows x Columns: "<<p.rows()<<" x "<<p.columns()<<vcl_endl;
00160 vnl_sparse_matrix<T> v=p;
00161 v.reset();
00162 v.next();
00163 for (int i=0;i<5;i++)
00164 {
00165 os<<" ("<< v.getrow() <<','<< v.getcolumn() <<") value "<< v.value()<<'\n';
00166 if (!v.next()) break;
00167 }
00168 }
00169
00170 #define VNL_IO_SPARSE_MATRIX_INSTANTIATE(T) \
00171 template void vsl_print_summary(vcl_ostream &, const vnl_sparse_matrix<T > &); \
00172 template void vsl_b_read(vsl_b_istream &, vnl_sparse_matrix<T > &); \
00173 template void vsl_b_write(vsl_b_ostream &, const vnl_sparse_matrix<T > &)
00174
00175 #endif // vnl_io_sparse_matrix_txx_