LocARNA-1.8.11
sparse_vector.hh
1 #ifndef SPARSE_VECTOR_HH
2 #define SPARSE_VECTOR_HH
3 
4 #ifdef HAVE_CONFIG_H
5 # include <config.h>
6 #endif
7 
8 #include <iosfwd>
9 
10 #include "aux.hh"
11 
12 namespace LocARNA {
13 
24  template <typename T>
25  class SparseVector {
26  public:
27 
28  typedef T value_t;
29 
30  typedef size_t size_type;
31 
32  typedef size_type key_t;
33 
34  protected:
35 
37  map_t the_map_;
38  value_t def_;
39 
40  public:
41 
47  typedef typename map_t::const_iterator const_iterator;
48 
49 
58  class element {
59  private:
60  SparseVector<T> *m_;
61  key_t k_;
62  public:
70  element(SparseVector<T> *m,key_t k): m_(m),k_(k) {}
71 
79  operator value_t() {
80  typename map_t::const_iterator it = m_->the_map_.find(k_);
81  if ( it == m_->the_map_.end() )
82  return m_->def_;
83  else
84  return it->second;
85  }
86 
98  element
99  operator +=(const value_t &x) {
100  const_iterator it = m_->the_map_.find(k_);
101  if ( it == m_->the_map_.end() )
102  m_->the_map_[k_] = m_->def_ + x;
103  else
104  m_->the_map_[k_] += x;
105 
106  return *this;
107  }
108 
120  element &
121  operator =(const value_t &x) {
122  if (x==m_->def_) {
123  m_->the_map_.erase(k_);
124  } else {
125  // the following replaces m_->the_map_[k_] = x;
126  // but never calls the default constructor for value_t
127 
128  typename map_t::iterator it = m_->the_map_.find(k_);
129  if ( it != m_->the_map_.end() ) {
130  it->second = x;
131  } else {
132  m_->the_map_.insert(typename map_t::value_type(k_,x));
133  }
134  }
135  return *this;
136  }
137  };
138 
139 
145  explicit
146  SparseVector(const value_t &def) : the_map_(),def_(def) {}
147 
155  element operator[] (size_type i) {
156  return element(this,key_t(i));
157  }
158 
166  const value_t & operator[] (size_type i) const {
167  const_iterator it = the_map_.find(key_t(i));
168  if ( it == the_map_.end() )
169  return def_;
170  else
171  return it->second;
172  }
173 
186  void
187  set(size_type i, const value_t &val) {
188  typename map_t::iterator it = the_map_.find(key_t(i));
189  if ( it != the_map_.end() ) {
190  it->second = val;
191  } else {
192  the_map_.insert(typename map_t::value_type(key_t(i),val));
193  }
194  }
195 
201  void
202  reset(size_type i) {
203  typename map_t::iterator it = the_map_.find(key_t(i));
204  if ( it != the_map_.end() ) {
205  the_map_.erase(key_t(i));
206  }
207  }
208 
213  size_type
214  size() const {
215  return the_map_.size();
216  }
217 
223  bool
224  empty() const {
225  return the_map_.empty();
226  }
227 
231  void
232  clear() {
233  the_map_.clear();
234  }
235 
243  const_iterator begin() const {
244  return the_map_.begin();
245  }
246 
253  const_iterator end() const {
254  return the_map_.end();
255  }
256 
257  };
258 
267  template<class T>
268  inline
269  std::ostream &
270  operator <<(std::ostream &out, const SparseVector<T> &v) {
271  for (typename SparseVector<T>::const_iterator it=v.begin();
272  v.end()!=it;
273  ++it) {
274  out <<it->first <<":" << it->second << " ";
275  }
276  out << std::endl;
277  return out;
278  }
279 
280 } //end namespace LocARNA
281 
282 #endif // SPARSE_VECTOR_HH
const_iterator end() const
End const iterator over vector entries.
Definition: sparse_vector.hh:253
void reset(size_type i)
Set vector entry to default value.
Definition: sparse_vector.hh:202
size_t size_type
usual definition of size_type
Definition: sparse_vector.hh:30
bool empty() const
Check for emptiness.
Definition: sparse_vector.hh:224
element operator+=(const value_t &x)
Operator for in place addition.
Definition: sparse_vector.hh:99
SparseVector(const value_t &def)
Construct with default value.
Definition: sparse_vector.hh:146
const_iterator begin() const
Begin const iterator over vector entries.
Definition: sparse_vector.hh:243
element operator[](size_type i)
Access to vector element.
Definition: sparse_vector.hh:155
Definition: aligner.cc:17
value_t def_
default value of vector entries
Definition: sparse_vector.hh:38
map_t the_map_
internal representation of sparse vector
Definition: sparse_vector.hh:37
map_t::const_iterator const_iterator
Stl-compatible constant iterator over vector elements.
Definition: sparse_vector.hh:47
size_type key_t
type of vector index pair
Definition: sparse_vector.hh:32
Element of sparse vector.
Definition: sparse_vector.hh:58
Represents a sparse vector.
Definition: sparse_vector.hh:25
void clear()
Clear the vector.
Definition: sparse_vector.hh:232
unordered_map< key_t, value_t >::type map_t
map type
Definition: sparse_vector.hh:36
T value_t
type of vector entries
Definition: sparse_vector.hh:28
Definition: aux.hh:51
element & operator=(const value_t &x)
Assignment operator.
Definition: sparse_vector.hh:121
size_type size() const
Size of sparse vector.
Definition: sparse_vector.hh:214
element(SparseVector< T > *m, key_t k)
Construct as proxy for specified element in given sparse vector.
Definition: sparse_vector.hh:70