From 190f68c880eb27506669e95e2bc0493e2ec42c4c Mon Sep 17 00:00:00 2001 From: Patrick Simianer
Date: Sun, 17 Aug 2014 07:51:16 +0100 Subject: functional again --- fast/sparse_vector.hh | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'fast/sparse_vector.hh') diff --git a/fast/sparse_vector.hh b/fast/sparse_vector.hh index e497769..3583240 100644 --- a/fast/sparse_vector.hh +++ b/fast/sparse_vector.hh @@ -22,17 +22,7 @@ struct SparseVector { SparseVector() {}; SparseVector(string& s) { - stringstream ss(s); - while (!ss.eof()) { - string t; - ss >> t; - size_t eq = t.find_first_of("="); - t.replace(eq, 1, " "); - stringstream tt(t); - K k; V v; - tt >> k >> v; - m_.emplace(k.substr(k.find_first_of("\"")+1, k.find_last_of("\"")-1), v); - } + from_s(this, s); }; void @@ -138,6 +128,25 @@ struct SparseVector { return *this; }; + static void + from_s(SparseVector* w, const string& s) + { + stringstream ss(s); + while (!ss.eof()) { + string t; + ss >> t; + size_t eq = t.find_first_of("="); + if (eq == string::npos) { + return; + } + t.replace(eq, 1, " "); + stringstream tt(t); + K k; V v; + tt >> k >> v; + w->m_.emplace(k.substr(k.find_first_of("\"")+1, k.find_last_of("\"")-1), v); + } + } + string repr() const { @@ -154,10 +163,13 @@ struct SparseVector { }; string - escaped() const { + escaped(bool quote_keys=false) const { ostringstream os; for (auto it = m_.cbegin(); it != m_.cend(); it++) { - os << '"' << util::json_escape(it->first) << '"' << "=" << it->second; + if (quote_keys) os << '"'; + os << util::json_escape(it->first); + if (quote_keys) os << '"'; + os << "=" << it->second; if (next(it) != m_.cend()) os << " "; } -- cgit v1.2.3