LocARNA-1.8.11
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 
12 namespace LocARNA {
13 
26  template <typename T>
27  class SparseMatrix {
28  public:
29 
30  typedef T value_t;
31 
32  typedef size_t size_type;
33 
34  typedef std::pair<size_type,size_type> key_t;
35 
36  protected:
37 
39  map_t the_map_;
40  value_t def_;
41 
42  public:
43 
49  typedef typename map_t::const_iterator const_iterator;
50 
51 
60  class element {
61  private:
62  SparseMatrix<T> *m_;
63  key_t k_;
64  public:
72  element(SparseMatrix<T> *m,key_t k): m_(m),k_(k) {}
73 
81  operator value_t() {
82  typename map_t::const_iterator it = m_->the_map_.find(k_);
83  if ( it == m_->the_map_.end() )
84  return m_->def_;
85  else
86  return it->second;
87  }
88 
100  element
101  operator +=(const value_t &x) {
102  const_iterator it = m_->the_map_.find(k_);
103  if ( it == m_->the_map_.end() )
104  m_->the_map_[k_] = m_->def_ + x;
105  else
106  m_->the_map_[k_] += x;
107 
108  return *this;
109  }
110 
122  element &
123  operator =(const value_t &x) {
124  if (x==m_->def_) {
125  m_->the_map_.erase(k_);
126  } else {
127  // the following replaces m_->the_map_[k_] = x;
128  // but never calls the default constructor for value_t
129 
130  typename map_t::iterator it = m_->the_map_.find(k_);
131  if ( it != m_->the_map_.end() ) {
132  it->second = x;
133  } else {
134  m_->the_map_.insert(typename map_t::value_type(k_,x));
135  }
136  }
137  return *this;
138  }
139  };
140 
141 
145  SparseMatrix() : the_map_(),def_() {}
146 
152  explicit
153  SparseMatrix(const value_t &def) : the_map_(),def_(def) {}
154 
163  element operator() (size_type i, size_type j) {
164  return element(this,key_t(i,j));
165  }
166 
175  const value_t & operator() (size_type i, size_type j) const {
176  const_iterator it = the_map_.find(key_t(i,j));
177  if ( it == the_map_.end() )
178  return def_;
179  else
180  return it->second;
181  }
182 
196  void
197  set(size_type i, size_type j, const value_t &val) {
198  typename map_t::iterator it = the_map_.find(key_t(i,j));
199  if ( it != the_map_.end() ) {
200  it->second = val;
201  } else {
202  the_map_.insert(typename map_t::value_type(key_t(i,j),val));
203  }
204  }
205 
216  value_t &
217  ref(size_type i, size_type j) {
218  typename map_t::iterator it = the_map_.find(key_t(i,j));
219  if ( it == the_map_.end() ) {
220  the_map_.insert(typename map_t::value_type(key_t(i,j),def_));
221  it = the_map_.find(key_t(i,j));
222  }
223  return it->second;
224  }
225 
232  void
233  reset(size_type i, size_type j) {
234  typename map_t::iterator it = the_map_.find(key_t(i,j));
235  if ( it != the_map_.end() ) {
236  the_map_.erase(key_t(i,j));
237  }
238  }
239 
244  size_type
245  size() const {
246  return the_map_.size();
247  }
248 
254  bool
255  empty() const {
256  return the_map_.empty();
257  }
258 
262  void
263  clear() {
264  the_map_.clear();
265  }
266 
274  const_iterator
275  begin() const {
276  return the_map_.begin();
277  }
278 
285  const_iterator
286  end() const {
287  return the_map_.end();
288  }
289 
290 
296  const value_t &
297  def() const {
298  return def_;
299  }
300  };
301 
310  template<class T>
311  inline
312  std::ostream &
313  operator <<(std::ostream &out, const SparseMatrix<T> &m) {
314  for (typename SparseMatrix<T>::const_iterator it=m.begin();
315  m.end()!=it;
316  ++it) {
317  out << "("<<it->first.first<<","<<it->first.second << ") " << it->second << std::endl;
318  }
319  return out;
320  }
321 
322 } //end namespace LocARNA
323 
324 #endif // SPARSE_MATRIX_HH
element operator()(size_type i, size_type j)
Access to matrix element.
Definition: sparse_matrix.hh:163
const value_t & def() const
Default value.
Definition: sparse_matrix.hh:297
void reset(size_type i, size_type j)
Set matrix entry to default value.
Definition: sparse_matrix.hh:233
element operator+=(const value_t &x)
Operator for in place addition.
Definition: sparse_matrix.hh:101
T value_t
type of matrix entries
Definition: sparse_matrix.hh:30
void clear()
Clear the matrix.
Definition: sparse_matrix.hh:263
const_iterator begin() const
Begin const iterator over matrix entries.
Definition: sparse_matrix.hh:275
Definition: aligner.cc:17
value_t def_
default value of matrix entries
Definition: sparse_matrix.hh:40
size_type size() const
Size of sparse matrix.
Definition: sparse_matrix.hh:245
bool empty() const
Check for emptiness.
Definition: sparse_matrix.hh:255
size_t size_type
usual definition of size_type
Definition: sparse_matrix.hh:32
map_t::const_iterator const_iterator
Stl-compatible constant iterator over matrix elements.
Definition: sparse_matrix.hh:49
map_t the_map_
internal representation of sparse matrix
Definition: sparse_matrix.hh:39
const_iterator end() const
End const iterator over matrix entries.
Definition: sparse_matrix.hh:286
std::pair< size_type, size_type > key_t
type of matrix index pair
Definition: sparse_matrix.hh:34
Element of sparse matrix.
Definition: sparse_matrix.hh:60
Definition: aux.hh:51
SparseMatrix(const value_t &def)
Construct with default value.
Definition: sparse_matrix.hh:153
element(SparseMatrix< T > *m, key_t k)
Construct as proxy for specified element in given sparse matrix.
Definition: sparse_matrix.hh:72
SparseMatrix()
Empty constructor (with default default value)
Definition: sparse_matrix.hh:145
Represents a sparse 2D matrix.
Definition: sparse_matrix.hh:27
unordered_map< key_t, value_t, pair_of_size_t_hash >::type map_t
map type
Definition: sparse_matrix.hh:38
element & operator=(const value_t &x)
Assignment operator.
Definition: sparse_matrix.hh:123
value_t & ref(size_type i, size_type j)
Write access to matrix element of const matrix.
Definition: sparse_matrix.hh:217