diff options
Diffstat (limited to 'decoder/sparse_vector.h')
-rw-r--r-- | decoder/sparse_vector.h | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/decoder/sparse_vector.h b/decoder/sparse_vector.h index 9c7c9c79..43880014 100644 --- a/decoder/sparse_vector.h +++ b/decoder/sparse_vector.h @@ -12,6 +12,13 @@ #include "fdict.h" +template <class T> +inline T & extend_vector(std::vector<T> &v,int i) { + if (i>=v.size()) + v.resize(i+1); + return v[i]; +} + template <typename T> class SparseVector { public: @@ -29,6 +36,17 @@ public: } + void init_vector(std::vector<T> *vp) const { + init_vector(*vp); + } + + void init_vector(std::vector<T> &v) const { + v.clear(); + for (const_iterator i=values_.begin(),e=values_.end();i!=e;++i) + extend_vector(v,i->first)=i->second; + } + + void set_new_value(int index, T const& val) { assert(values_.find(index)==values_.end()); values_[index]=val; @@ -312,7 +330,7 @@ private: typedef SparseVector<double> FeatureVector; typedef SparseVector<double> WeightVector; - +typedef std::vector<double> DenseWeightVector; template <typename T> SparseVector<T> operator+(const SparseVector<T>& a, const SparseVector<T>& b) { SparseVector<T> result = a; |