vnl_io_sparse_matrix.hxx
Go to the documentation of this file.
1 // This is core/vnl/io/vnl_io_sparse_matrix.hxx
2 #ifndef vnl_io_sparse_matrix_hxx_
3 #define vnl_io_sparse_matrix_hxx_
4 //:
5 // \file
6 
7 #include <iostream>
8 #include "vnl_io_sparse_matrix.h"
10 #include <vsl/vsl_binary_io.h>
11 #include <cassert>
12 #ifdef _MSC_VER
13 # include <vcl_msvc_warnings.h>
14 #endif
15 
16 // I/O for vnl_sparse_matrix_pair
17 //==================================================================================
18 // IO Helper functions
19 //==================================================================================
20 
21 //=================================================================================
22 //: Binary save self to stream.
23 template<class T>
24 void vsl_b_write(vsl_b_ostream &os, const vnl_sparse_matrix_pair<T> & p)
25 {
26  constexpr short io_version_no = 1;
27  vsl_b_write(os, io_version_no);
28  vsl_b_write(os, p.first);
29  vsl_b_write(os, p.second);
30 }
31 
32 //=================================================================================
33 //: Binary load self from stream.
34 template<class T>
35 void vsl_b_read(vsl_b_istream &is, vnl_sparse_matrix_pair<T> & p)
36 {
37  if (!is) return;
38 
39  short ver;
40  vsl_b_read(is, ver);
41  switch (ver)
42  {
43  case 1:
44  vsl_b_read(is, p.first);
45  vsl_b_read(is, p.second);
46  break;
47 
48  default:
49  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_sparse_matrix_pair<T>&)\n"
50  << " Unknown version number "<< ver << '\n';
51  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
52  return;
53  }
54 }
55 
56 //================================================================================
57 //: Output a human readable summary to the stream
58 template<class T>
59 void vsl_print_summary(std::ostream& os,const vnl_sparse_matrix_pair<T>& p)
60 {
61  os<< "Sparse matrix pair ( " << p.first << ',' << p.second << " )\n";
62 }
63 
64 // I/O for vnl_sparse_matrix
65 
66 //=================================================================================
67 //: Binary save self to stream.
68 template<class T>
69 void vsl_b_write(vsl_b_ostream & os, const vnl_sparse_matrix<T> & p)
70 {
71  typedef vnl_sparse_matrix_pair<T> pair_t;
72  typedef std::vector < pair_t > row;
73 
74  row rw;
76 
77  constexpr short io_version_no = 1;
78  vsl_b_write(os, io_version_no);
79  vsl_b_write(os, v.rows());
80  vsl_b_write(os, v.columns());
81 
82  for (unsigned int i=0;i<v.rows();i++)
83  {
84  rw=v.get_row(i);
85  vsl_b_write(os, rw.size());
86  for (unsigned int j=0;j<rw.size();j++)
87  {
88  vsl_b_write(os, rw[j]);
89  }
90  }
91 }
92 
93 //=================================================================================
94 //: Binary load self from stream.
95 template<class T>
96 void vsl_b_read(vsl_b_istream &is, vnl_sparse_matrix<T> & p)
97 {
98  if (!is) return;
99 
100  typedef vnl_sparse_matrix_pair<T> pair_t;
101 
102  short ver;
103  unsigned n_rows;
104  unsigned n_cols;
105  unsigned row_size=0;
106  vsl_b_read(is, ver);
107 
108  std::vector<int> indexes(row_size);
109  std::vector<T> values(row_size);
110  switch (ver)
111  {
112  case 1:
113  vsl_b_read(is, n_rows);
114  vsl_b_read(is, n_cols);
115  // As we cannot resize the matrix, check that it is the correct size.
116  assert (n_rows==p.rows());
117  assert (n_cols==p.columns());
118  for (unsigned i=0;i<n_rows;++i)
119  {
120  vsl_b_read(is,row_size);
121  indexes.resize(row_size);
122  values.resize(row_size);
123 
124  for (unsigned j=0;j<row_size;j++)
125  {
126  pair_t q;
127  vsl_b_read(is, q);
128  indexes[j] = q.first;
129  values[j] = q.second;
130  }
131  p.set_row(i, indexes, values);
132  }
133  break;
134 
135  default:
136  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_sparse_matrix<T>&)\n"
137  << " Unknown version number "<< ver << '\n';
138  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
139  return;
140  }
141 }
142 
143 //====================================================================================
144 //: Output a human readable summary to the stream
145 template<class T>
146 void vsl_print_summary(std::ostream & os,const vnl_sparse_matrix<T> & p)
147 {
148  os<<"Rows x Columns: "<<p.rows()<<" x "<<p.columns()<<std::endl;
150  v.reset();
151  v.next();
152  for (int i=0;i<5;i++)
153  {
154  os<<" ("<< v.getrow() <<','<< v.getcolumn() <<") value "<< v.value()<<'\n';
155  if (!v.next()) break;
156  }
157 }
158 
159 #define VNL_IO_SPARSE_MATRIX_INSTANTIATE(T) \
160  template VNL_EXPORT void vsl_print_summary(std::ostream &, const vnl_sparse_matrix<T > &); \
161  template VNL_EXPORT void vsl_b_read(vsl_b_istream &, vnl_sparse_matrix<T > &); \
162  template VNL_EXPORT void vsl_b_write(vsl_b_ostream &, const vnl_sparse_matrix<T > &)
163 
164 #endif // vnl_io_sparse_matrix_hxx_
unsigned int columns() const
Get the number of columns in the matrix.
void vsl_print_summary(std::ostream &os, vnl_bignum const &b)
Print human readable summary of object to a stream.
Simple sparse matrix.
vnl_sparse_matrix & set_row(unsigned int r, std::vector< int > const &cols, std::vector< T > const &vals)
Set a whole row at once. Much faster. Returns *this.
void clear()
Set all elements to null.
#define v
Definition: vnl_vector.h:42
void vsl_b_read(vsl_b_istream &is, vnl_bignum &v)
Binary load vnl_bignum from stream.
void vsl_b_write(vsl_b_ostream &os, vnl_bignum const &v)
Binary save vnl_bignum to stream.
unsigned int rows() const
Get the number of rows in the matrix.
Simple sparse matrix.
Stores elements of sparse matrix.