1
#ifndef YASMIC_BINARY_IFSTREAM_MATRIX
2
#define YASMIC_BINARY_IFSTREAM_MATRIX
6
// disable the warning for ifstream::read
7
#pragma warning( push )
8
#pragma warning( disable : 4996 )
9
#endif // _MSC_VER >= 1400
14
#include <boost/tuple/tuple.hpp>
16
#include <boost/iterator/iterator_facade.hpp>
18
#include <yasmic/generic_matrix_operations.hpp>
26
template <class i_index_type, class i_value_type>
27
class binary_ifstream_matrix_const_iterator
28
: public boost::iterator_facade<
29
binary_ifstream_matrix_const_iterator<i_index_type, i_value_type>,
31
i_index_type, i_index_type, i_value_type> const,
32
boost::forward_traversal_tag,
34
i_index_type, i_index_type, i_value_type> const >
37
binary_ifstream_matrix_const_iterator()
38
: _str(0), _r(0), _c(0), _v(0)
41
binary_ifstream_matrix_const_iterator(std::istream &str)
42
: _str(&str), _r(0), _c(0), _v(0)
47
friend class boost::iterator_core_access;
53
_str->read((char *)&_r, sizeof(i_index_type));
54
_str->read((char *)&_c, sizeof(i_index_type));
55
_str->read((char *)&_v, sizeof(i_value_type));
57
if (_str->eof()) { _str = 0; }
61
bool equal(binary_ifstream_matrix_const_iterator const& other) const
63
return (_str == other._str);
67
i_index_type, i_index_type, i_value_type>
70
return boost::make_tuple(_r, _c, _v);
82
template <class index_type = int, class value_type = double, class size_type = int>
83
struct binary_ifstream_matrix
87
binary_ifstream_matrix(std::istream& f)
91
binary_ifstream_matrix(const binary_ifstream_matrix& bifm)
96
template <class i_index_type, class i_value_type, class i_size_type>
97
struct smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> >
99
typedef i_size_type size_type;
100
typedef i_index_type index_type;
101
typedef i_value_type value_type;
103
typedef boost::tuple<index_type, index_type, value_type> nonzero_descriptor;
105
typedef impl::binary_ifstream_matrix_const_iterator<i_index_type, i_value_type> nonzero_iterator;
107
typedef size_type nz_index_type;
109
typedef void row_iterator;
111
typedef void row_nonzero_descriptor;
112
typedef void row_nonzero_iterator;
115
template <class i_index_type, class i_value_type, class i_size_type>
116
inline std::pair<typename smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> >::size_type,
117
typename smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> >::size_type >
118
dimensions(binary_ifstream_matrix<i_index_type, i_value_type, i_size_type>& m)
120
typedef smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> > traits;
121
typename traits::size_type nrows,ncols;
124
m._f.seekg(0, std::ios_base::beg);
126
m._f.read((char *)&nrows, sizeof(i_index_type));
127
m._f.read((char *)&ncols, sizeof(i_index_type));
129
return (std::make_pair(nrows, ncols));
132
template <class i_index_type, class i_value_type, class i_size_type>
133
typename smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> >::size_type
134
nnz(binary_ifstream_matrix<i_index_type, i_value_type, i_size_type>& m)
136
typedef smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> > traits;
137
typename traits::size_type nnz;
139
// clear any error bits
142
// seek after nrows, ncols
143
m._f.seekg(2*sizeof(i_index_type), std::ios_base::beg);
145
m._f.read((char *)&nnz, sizeof(i_size_type));
150
template <class i_index_type, class i_value_type, class i_size_type>
151
std::pair<typename smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> >::nonzero_iterator,
152
typename smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> >::nonzero_iterator>
153
nonzeros(binary_ifstream_matrix<i_index_type, i_value_type, i_size_type>& m)
155
typedef smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> > traits;
159
// seek after nrows,ncols,nnz
160
m._f.seekg(2*sizeof(i_index_type)+sizeof(i_size_type), std::ios_base::beg);
162
typedef typename traits::nonzero_iterator nz_iter;
164
return (std::make_pair(nz_iter(m._f), nz_iter()));
171
// restore the warning for ifstream::read
172
#pragma warning( pop )
173
#endif // _MSC_VER >= 1400
177
#endif //YASMIC_BINARY_IFSTREAM_MATRIX