LocARNA-2.0.0
sparse_matrix.hh
1 #ifndef SPARSE_MATRIX_HH
2 #define SPARSE_MATRIX_HH
3 
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7 
8 #include <iostream>
9 
10 #include "aux.hh"
11 #include "sparse_vector_base.hh"
12 
13 namespace LocARNA {
14 
26  template <typename T>
27  class SparseMatrix : public SparseVectorBase< SparseMatrix<T>,T,std::pair<size_t,size_t> > {
28  public:
29  using parent_t = SparseVectorBase<SparseMatrix<T>,T,std::pair<size_t,size_t>>;
30  friend parent_t;
31 
32  using size_type = typename parent_t::size_type;
33  using value_type = T;
34  using key_type = std::pair<size_type, size_type>;
35  using map_type = std::unordered_map<key_type, value_type>;
36 
42  explicit
44 
45  SparseMatrix() : parent_t(value_type()) {}
46 
55  typename parent_t::element_proxy
57  return typename parent_t::element_proxy(this, key_type(i, j));
58  }
59 
60  const value_type &
61  operator()(size_type i, size_type j) const {
62  return parent_t::operator[]( key_type(i, j) );
63  }
64 
65  value_type &
66  ref(size_type i, size_type j) {
67  return parent_t::ref(key_type(i, j));
68  }
69 
70  void
71  set(size_type i, size_type j, const value_type &val) {
72  parent_t::set(key_type(i,j),val);
73  }
74 
75  void
76  reset(size_type i, size_type j) {
78  }
79  }; // end class SparseMatrix
80 
89  template <class T>
90  inline std::ostream &
91  operator<<(std::ostream &out, const SparseMatrix<T> &m) {
92  for (const auto &x: m) {
93  out << "(" << x.first.first << "," << x.first.second << ") "
94  << x.second << std::endl;
95  }
96  return out;
97  }
98 
99 } // end namespace LocARNA
100 
101 #endif // SPARSE_MATRIX_HH
Represents a sparse 2D matrix.
Definition: sparse_matrix.hh:27
std::pair< size_type, size_type > key_type
type of matrix index pair
Definition: sparse_matrix.hh:34
parent_t::element_proxy operator()(size_type i, size_type j)
Access to matrix element.
Definition: sparse_matrix.hh:56
T value_type
type of matrix entries
Definition: sparse_matrix.hh:33
SparseMatrix(const value_type &def)
Construct with default value.
Definition: sparse_matrix.hh:43
std::unordered_map< key_type, value_type > map_type
map type
Definition: sparse_matrix.hh:35
typename parent_t::size_type size_type
usual definition of size_type
Definition: sparse_matrix.hh:32
Base class template for sparse vector and matrix.
Definition: sparse_vector_base.hh:23
size_t size_type
usual definition of size_type
Definition: sparse_vector_base.hh:30
const value_type & def() const
Default value.
Definition: sparse_vector_base.hh:272
value_type & ref(const key_type &i)
Write access to element.
Definition: sparse_vector_base.hh:194
element_proxy operator[](const key_type &i)
Access to vector element.
Definition: sparse_vector_base.hh:143
void set(const key_type &i, const value_type &val)
Write access to vector entry.
Definition: sparse_vector_base.hh:175
void reset(const key_type &i)
Set vector entry to default value.
Definition: sparse_vector_base.hh:209
Definition: aligner.cc:15
std::ostream & operator<<(std::ostream &out, const AlignerRestriction &r)
Definition: aligner_restriction.hh:135
size_t size_type
general size type
Definition: aux.hh:120