summaryrefslogtreecommitdiff
path: root/fast/sparse_vector.hh
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-08-17 07:51:16 +0100
committerPatrick Simianer <p@simianer.de>2014-08-17 07:51:16 +0100
commit190f68c880eb27506669e95e2bc0493e2ec42c4c (patch)
tree26dbe66018a966613b2bf59761661131057a6ef5 /fast/sparse_vector.hh
parent0b3cdb4ae2fa176ba74a48ff7a1616395079c151 (diff)
functional again
Diffstat (limited to 'fast/sparse_vector.hh')
-rw-r--r--fast/sparse_vector.hh38
1 files changed, 25 insertions, 13 deletions
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 << " ";
}