From 9d393298ce21159907884ea9b7318c52585409ee Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Fri, 22 Apr 2011 13:38:32 -0400 Subject: make compatible with FastSparseVector --- utils/sparse_vector.h | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) (limited to 'utils/sparse_vector.h') diff --git a/utils/sparse_vector.h b/utils/sparse_vector.h index de8c0291..dbeacbe8 100644 --- a/utils/sparse_vector.h +++ b/utils/sparse_vector.h @@ -645,6 +645,19 @@ SparseVector operator+(const SparseVector& a, const SparseVector& b) { return result += b; } +template +SparseVector operator*(const double& a, const SparseVector& b) { + SparseVector result = b; + return result *= a; +} + +#else + +#include "fast_sparse_vector.h" +#define SparseVector FastSparseVector + +#endif + template SparseVector operator*(const SparseVector& a, const double& b) { SparseVector result = a; @@ -658,23 +671,36 @@ SparseVector operator*(const SparseVector& a, const T& b) { } template -SparseVector operator*(const double& a, const SparseVector& b) { - SparseVector result = b; - return result *= a; +SparseVector operator/(const SparseVector& a, const double& b) { + SparseVector result = a; + return result *= b; } template -std::ostream &operator<<(std::ostream &out, const SparseVector &vec) -{ - return vec.operator<<(out); +SparseVector operator/(const SparseVector& a, const T& b) { + SparseVector result = a; + return result *= b; } -#else - -#include "fast_sparse_vector.h" -#define SparseVector FastSparseVector +template +inline void print(O &o,const SparseVector& v, const char* kvsep="=",const char* pairsep=" ",const char* pre="",const char* post="") { + o << pre; + bool first=true; + for (typename SparseVector::const_iterator i=v.begin(),e=v.end();i!=e;++i) { + if (first) + first=false; + else + o<first)<second; + } + o << post; +} -#endif +template +inline std::ostream& operator<<(std::ostream& out, const SparseVector& v) { + print(out, v); + return out; +} namespace B64 { void Encode(double objective, const SparseVector& v, std::ostream* out); -- cgit v1.2.3