~dgleich/matlab-bgl/master

« back to all changes in this revision

Viewing changes to libmbgl/yasmic/binary_ifstream_matrix.hpp

  • Committer: David Gleich
  • Date: 2008-09-29 22:06:39 UTC
  • mfrom: (1.1.19 work)
  • Revision ID: dgleich@stanford.edu-20080929220639-4ic8mxd20lu81dla
Incorporated misc. fixes and graph layout algorithms.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef YASMIC_BINARY_IFSTREAM_MATRIX
 
2
#define YASMIC_BINARY_IFSTREAM_MATRIX
 
3
 
 
4
#ifdef BOOST_MSVC
 
5
#if _MSC_VER >= 1400
 
6
        // disable the warning for ifstream::read
 
7
        #pragma warning( push )
 
8
        #pragma warning( disable : 4996 )
 
9
#endif // _MSC_VER >= 1400
 
10
#endif // BOOST_MSVC
 
11
 
 
12
 
 
13
#include <fstream>
 
14
#include <boost/tuple/tuple.hpp>
 
15
#include <iterator>
 
16
#include <boost/iterator/iterator_facade.hpp>
 
17
 
 
18
#include <yasmic/generic_matrix_operations.hpp>
 
19
 
 
20
 
 
21
namespace yasmic
 
22
{
 
23
        namespace impl
 
24
        {
 
25
        
 
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>,
 
30
            boost::tuple<
 
31
                i_index_type, i_index_type, i_value_type> const,
 
32
            boost::forward_traversal_tag, 
 
33
            boost::tuple<
 
34
                i_index_type, i_index_type, i_value_type> const >
 
35
        {
 
36
        public:
 
37
            binary_ifstream_matrix_const_iterator() 
 
38
                                : _str(0), _r(0), _c(0), _v(0)
 
39
                        {}
 
40
            
 
41
            binary_ifstream_matrix_const_iterator(std::istream &str)
 
42
                                : _str(&str), _r(0), _c(0), _v(0)
 
43
            { increment(); }
 
44
            
 
45
            
 
46
        private:
 
47
            friend class boost::iterator_core_access;
 
48
 
 
49
            void increment() 
 
50
            {  
 
51
                                if (_str != 0)
 
52
                                {
 
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));
 
56
 
 
57
                                        if (_str->eof()) { _str = 0; }
 
58
                                }
 
59
            }
 
60
            
 
61
            bool equal(binary_ifstream_matrix_const_iterator const& other) const
 
62
            {
 
63
                                return (_str == other._str);
 
64
            }
 
65
            
 
66
            boost::tuple<
 
67
                i_index_type, i_index_type, i_value_type>
 
68
            dereference() const 
 
69
            { 
 
70
                return boost::make_tuple(_r, _c, _v);
 
71
            }
 
72
 
 
73
                        i_index_type _r, _c;
 
74
                        i_value_type _v;
 
75
 
 
76
                        std::istream* _str;
 
77
        };
 
78
 
 
79
        }
 
80
 
 
81
 
 
82
        template <class index_type = int, class value_type = double, class size_type = int>
 
83
        struct binary_ifstream_matrix
 
84
        {
 
85
                std::istream& _f;
 
86
 
 
87
                binary_ifstream_matrix(std::istream& f)
 
88
                        : _f(f) 
 
89
                {}
 
90
 
 
91
                binary_ifstream_matrix(const binary_ifstream_matrix& bifm)
 
92
                        : _f(bifm._f)
 
93
                {}
 
94
        };
 
95
 
 
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> > 
 
98
    {
 
99
        typedef i_size_type size_type;
 
100
        typedef i_index_type index_type;
 
101
                typedef i_value_type value_type;
 
102
                
 
103
                typedef boost::tuple<index_type, index_type, value_type> nonzero_descriptor;
 
104
 
 
105
                typedef impl::binary_ifstream_matrix_const_iterator<i_index_type, i_value_type> nonzero_iterator;
 
106
 
 
107
                typedef size_type nz_index_type;
 
108
 
 
109
                typedef void row_iterator;
 
110
                
 
111
                typedef void row_nonzero_descriptor;
 
112
                typedef void row_nonzero_iterator;
 
113
    };
 
114
    
 
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)
 
119
    {
 
120
                typedef smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> > traits;
 
121
        typename traits::size_type nrows,ncols;
 
122
        
 
123
        m._f.clear();
 
124
        m._f.seekg(0, std::ios_base::beg);
 
125
        
 
126
        m._f.read((char *)&nrows, sizeof(i_index_type));
 
127
                m._f.read((char *)&ncols, sizeof(i_index_type));
 
128
        
 
129
        return (std::make_pair(nrows, ncols));
 
130
    }
 
131
        
 
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)
 
135
        {
 
136
                typedef smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> > traits;
 
137
        typename traits::size_type nnz;
 
138
                
 
139
                // clear any error bits
 
140
                m._f.clear();
 
141
                
 
142
                // seek after nrows, ncols
 
143
        m._f.seekg(2*sizeof(i_index_type), std::ios_base::beg);
 
144
 
 
145
        m._f.read((char *)&nnz, sizeof(i_size_type));
 
146
        
 
147
        return (nnz);
 
148
        }
 
149
        
 
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)
 
154
    {
 
155
        typedef smatrix_traits<binary_ifstream_matrix<i_index_type, i_value_type, i_size_type> > traits;
 
156
        
 
157
        m._f.clear();
 
158
 
 
159
                // seek after nrows,ncols,nnz
 
160
        m._f.seekg(2*sizeof(i_index_type)+sizeof(i_size_type), std::ios_base::beg);
 
161
        
 
162
        typedef typename traits::nonzero_iterator nz_iter;
 
163
        
 
164
        return (std::make_pair(nz_iter(m._f), nz_iter()));
 
165
    }
 
166
}
 
167
 
 
168
 
 
169
#ifdef BOOST_MSVC
 
170
#if _MSC_VER >= 1400
 
171
        // restore the warning for ifstream::read
 
172
        #pragma warning( pop )
 
173
#endif // _MSC_VER >= 1400
 
174
#endif // BOOST_MSVC
 
175
 
 
176
 
 
177
#endif //YASMIC_BINARY_IFSTREAM_MATRIX