summaryrefslogtreecommitdiff
path: root/decoder/sparse_vector.h
diff options
context:
space:
mode:
authorgraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-16 01:56:34 +0000
committergraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-16 01:56:34 +0000
commitd7d59c4bb81262f1dfece384ec68fa2c25096843 (patch)
tree5521dc624dc23adeb3bc9d9c8f8fecc7feb57724 /decoder/sparse_vector.h
parentff323448416bbfa691a9697ddf3b30a0398fa08a (diff)
oracle directions
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@276 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'decoder/sparse_vector.h')
-rw-r--r--decoder/sparse_vector.h39
1 files changed, 35 insertions, 4 deletions
diff --git a/decoder/sparse_vector.h b/decoder/sparse_vector.h
index bda10974..c6c57150 100644
--- a/decoder/sparse_vector.h
+++ b/decoder/sparse_vector.h
@@ -20,10 +20,21 @@ public:
SparseVector() {}
explicit SparseVector(std::vector<T> const& v) {
typename MapType::iterator p=values_.begin();
- for (unsigned i=0;i<v.size();++i)
- p=values_.insert(p,typename MapType::value_type(i,v[i])); //faster
+ const T z=T(0);
+ for (unsigned i=0;i<v.size();++i) {
+ T const& t=v[i];
+ if (t!=z)
+ p=values_.insert(p,typename MapType::value_type(i,t)); //hint makes insertion faster
+ }
+
+ }
+
+ void set_new_value(int index, T const& val) {
+ assert(values_.find(index)==values_.end());
+ values_[index]=val;
}
+
const T operator[](int index) const {
typename MapType::const_iterator found = values_.find(index);
if (found == values_.end())
@@ -265,9 +276,29 @@ private:
MapType values_;
};
+// doesn't support fast indexing directly
+template <class T>
+class SparseVectorList {
+ typedef std::vector<const int,T> ListType;
+ typedef typename ListType::value_type pair_type;
+ typedef typename ListType::const_iterator const_iterator;
+ SparseVectorList() { }
+ explicit SparseVectorList(std::vector<T> const& v) {
+ const T z=T(0);
+ for (unsigned i=0;i<v.size();++i) {
+ T const& t=v[i];
+ if (t!=z)
+ p.push_back(pair_type(i,t));
+ }
+ p.resize(p.size());
+ }
+private:
+ ListType p;
+};
+
+
typedef SparseVector<double> FeatureVector;
-typedef std::vector<double> FeatureWeights;
-typedef FeatureWeights WeightVector;
+typedef SparseVector<double> WeightVector;
template <typename T>
SparseVector<T> operator+(const SparseVector<T>& a, const SparseVector<T>& b) {