LocARNA-2.0.0
sparse_vector_base.hh
1 #ifndef SPARSE_VECTOR_BASE_HH
2 #define SPARSE_VECTOR_BASE_HH
3 
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7 
8 #include <iosfwd>
9 #include <unordered_map>
10 
11 #include "aux.hh"
12 
13 namespace LocARNA {
14 
22  template <typename Derived,typename ValueType, typename KeyType=size_t>
24  public:
25  using derived_type = Derived;
26  using value_type = ValueType;
27  using key_type = KeyType;
28  using map_type = std::unordered_map<KeyType, ValueType>;
29 
30  using size_type = size_t;
31 
37  using const_iterator = typename map_type::const_iterator;
38 
39  using iterator = typename map_type::iterator;
40 
49  class element_proxy {
50  public:
59  element_proxy(derived_type *v, key_type k) : v_(v), k_(k) {}
60 
68  operator value_type() {
69  typename map_type::const_iterator it = v_->the_map_.find(k_);
70  if (it == v_->the_map_.end())
71  return v_->def_;
72  else
73  return it->second;
74  }
75 
88  operator+=(const value_type &x) {
89  const_iterator it = v_->the_map_.find(k_);
90  if (it == v_->the_map_.end())
91  v_->the_map_[k_] = v_->def_ + x;
92  else
93  v_->the_map_[k_] += x;
94 
95  return *this;
96  }
97 
110  element_proxy &
111  operator=(const value_type &x) {
112  if (x == v_->def_) {
113  v_->the_map_.erase(k_);
114  } else {
115  // the following replaces v_->the_map_[k_] = x;
116  // but never calls the default constructor for value_type
117 
118  typename map_type::iterator it = v_->the_map_.find(k_);
119  if (it != v_->the_map_.end()) {
120  it->second = x;
121  } else {
122  v_->the_map_.insert(typename map_type::value_type(k_, x));
123  }
124  }
125  return *this;
126  }
127 
128  private:
129  derived_type *v_;
130  key_type k_;
131  }; //end class element_proxy
132 
133 
134  explicit SparseVectorBase(const value_type def) : def_(def) {}
135 
144  return element_proxy( static_cast<derived_type*>(this), i );
145  }
146 
154  const value_type &operator[](const key_type &i) const {
155  const_iterator it = the_map_.find(key_type(i));
156  if (it == the_map_.end())
157  return def_;
158  else
159  return it->second;
160  }
161 
174  void
175  set(const key_type &i, const value_type &val) {
176  typename map_type::iterator it = the_map_.find(key_type(i));
177  if (it != the_map_.end()) {
178  it->second = val;
179  } else {
180  the_map_.insert(typename map_type::value_type(key_type(i), val));
181  }
182  }
183 
193  value_type &
194  ref(const key_type &i) {
195  auto it = the_map_.find(i);
196  if (it == the_map_.end()) {
197  the_map_[i] = def_;
198  it = the_map_.find(i);
199  }
200  return it->second;
201  }
202 
208  void
209  reset(const key_type &i) {
210  typename map_type::iterator it = the_map_.find(key_type(i));
211  if (it != the_map_.end()) {
212  the_map_.erase(key_type(i));
213  }
214  }
215 
220  size_type
221  size() const {
222  return the_map_.size();
223  }
224 
230  bool
231  empty() const {
232  return the_map_.empty();
233  }
234 
238  void
239  clear() {
240  the_map_.clear();
241  }
242 
251  begin() const {
252  return the_map_.begin();
253  }
254 
262  end() const {
263  return the_map_.end();
264  }
265 
271  const value_type &
272  def() const {
273  return def_;
274  }
275 
276  protected:
278  map_type the_map_;
279  };
280 
281 } // end namespace LocARNA
282 
283 #endif // SPARSE_VECTOR_HH
Proxy for element of sparse vector.
Definition: sparse_vector_base.hh:49
element_proxy(derived_type *v, key_type k)
Construct as proxy for specified element in given sparse vector.
Definition: sparse_vector_base.hh:59
element_proxy operator+=(const value_type &x)
Operator for in place addition.
Definition: sparse_vector_base.hh:88
element_proxy & operator=(const value_type &x)
Assignment operator.
Definition: sparse_vector_base.hh:111
Base class template for sparse vector and matrix.
Definition: sparse_vector_base.hh:23
map_type the_map_
internal representation of sparse vector
Definition: sparse_vector_base.hh:278
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 def_
default value of vector entries
Definition: sparse_vector_base.hh:277
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
KeyType key_type
type of vector index
Definition: sparse_vector_base.hh:27
void set(const key_type &i, const value_type &val)
Write access to vector entry.
Definition: sparse_vector_base.hh:175
const_iterator begin() const
Begin const iterator over vector entries.
Definition: sparse_vector_base.hh:251
ValueType value_type
type of vector entries
Definition: sparse_vector_base.hh:26
const value_type & operator[](const key_type &i) const
Read-only access to vector element of const vector.
Definition: sparse_vector_base.hh:154
void clear()
Clear the vector.
Definition: sparse_vector_base.hh:239
const_iterator end() const
End const iterator over vector entries.
Definition: sparse_vector_base.hh:262
typename map_type::const_iterator const_iterator
Stl-compatible constant iterator over vector elements.
Definition: sparse_vector_base.hh:37
bool empty() const
Check for emptiness.
Definition: sparse_vector_base.hh:231
size_type size() const
Size of sparse vector.
Definition: sparse_vector_base.hh:221
void reset(const key_type &i)
Set vector entry to default value.
Definition: sparse_vector_base.hh:209
Definition: aligner.cc:15
size_t size_type
general size type
Definition: aux.hh:120