MDA
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
CRS_Mat.hpp
Go to the documentation of this file.
1 /*
2  * CRS.hpp
3  *
4  * Created on: Jul 26, 2013
5  * Author: Carsten Kemena
6  */
7 
13 #ifndef CRS_Mat_HPP_
14 #define CRS_Mat_HPP_
15 
16 // C++ header
17 #include <string>
18 #include <fstream>
19 #include <vector>
20 
21 namespace MDAT {
22 
23 
41 template<typename DataType>
42 class CRS_Mat
43 {
44 private:
45  std::string _name; //stores the matrix name
46  int _n_domains; // Number of domains
47  int _n_vals; // Number of values stored
48  std::vector<int> _ids; // Contains the PFAM identifiers as number (e.g. PF00005 = 5)
49  std::vector<int> _row_ids; // Contains the row id
50  std::vector<int> _col_ids; // Contains the column_id
51  std::vector<DataType> _vals; // Contains the values stored in the matrix
52 
53 public:
54  // Constructors & Destructors
55 
59  CRS_Mat();
60 
65  CRS_Mat(const std::string &mat_f);
66 
70  ~CRS_Mat();
71 
72  // Basic functions
77  std::string name()
78  {
79  return _name;
80  }
81 
86  void read(const std::string &mat_f);
87 
94  DataType value(int i, int j) const;
95 
96 };
97 
98 
99 template<typename DataType>
100 CRS_Mat<DataType>::CRS_Mat():_name(""), _n_domains(0), _n_vals(0), _ids(), _row_ids(), _col_ids(), _vals()
101 {}
102 
103 template<typename DataType>
104 CRS_Mat<DataType>::CRS_Mat(const std::string &mat_f):_name(""), _n_domains(0), _n_vals(0), _ids(), _row_ids(), _col_ids(), _vals()
105 {
106  read(mat_f);
107 }
108 
109 template<typename DataType>
111 {}
112 
113 
114 template<typename DataType>
115 void
116 CRS_Mat<DataType>::read(const std::string &mat_f)
117 {
118  std::ifstream mat_F;
119  mat_F.open(mat_f.c_str(), std::ios::in|std::ios::binary);
120  mat_F.exceptions( std::ifstream::failbit | std::ifstream::badbit);
121  _name.resize(11);
122  mat_F.read(&_name[0], 10*sizeof(char));
123  _name[10]='\0';
124  int i;
125  for (i=0; i<11; ++i)
126  if (_name[i]=='\0')
127  break;
128  _name.resize(i);
129 
130  mat_F.read((char*)&_n_domains, sizeof(int));
131  mat_F.read((char*)&_n_vals, sizeof(int));
132  _ids.resize(_n_domains);
133  _row_ids.resize(_n_domains);
134  _col_ids.resize(_n_vals);
135  _vals.resize(_n_vals);
136  mat_F.read((char*)&_ids[0], sizeof(int)*_n_domains);
137  mat_F.read((char*)&_row_ids[0], sizeof(int)*_n_domains);
138  mat_F.read((char*)&_col_ids[0], sizeof(int)*_n_vals);
139  mat_F.read((char*)&_vals[0], sizeof(DataType)*_n_vals);
140  mat_F.close();
141 }
142 
143 
144 
145 
146 template<typename DataType>
147 DataType
148 CRS_Mat<DataType>::value(int i, int j) const
149 {
150  std::vector<int>::const_iterator it1, it2, it_begin=_ids.begin(), it_end=_ids.end();
151 
152  it1=std::lower_bound (it_begin, it_end, i);
153  it2=std::lower_bound (it_begin, it_end, j);
154  if ((it1 == it_end) || (it2 == it_end))
155  return -1;
156 
157  if (j<i)
158  swap(it1, it2);
159 
160  int id1=it1-it_begin;
161  int id2=it2-it_begin;
162 
163  int id_col=_row_ids[id1];
164  int end=(id1!=(_n_domains-1)) ? _row_ids[id1+1] : _n_vals;
165  for (int i =id_col; i<end; ++i)
166  {
167  if (_col_ids[i]==id2)
168  return _vals[i];
169  }
170  return -1;
171 }
172 
173 }
174 
175 
176 #endif /* CRS_Mat_HPP_ */