summaryrefslogtreecommitdiff
path: root/fast/sparse_vector.hh
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-08-05 22:46:43 +0200
committerPatrick Simianer <p@simianer.de>2014-08-05 22:46:43 +0200
commit0b3cdb4ae2fa176ba74a48ff7a1616395079c151 (patch)
tree7092a57ed7d29920df3ae2f97c39efc17c5e6095 /fast/sparse_vector.hh
parenta27e2c529ddb7dc1be0c6bcc44e3ab558126d15d (diff)
too much to tell
Diffstat (limited to 'fast/sparse_vector.hh')
-rw-r--r--fast/sparse_vector.hh106
1 files changed, 80 insertions, 26 deletions
diff --git a/fast/sparse_vector.hh b/fast/sparse_vector.hh
index dd7f3cf..e497769 100644
--- a/fast/sparse_vector.hh
+++ b/fast/sparse_vector.hh
@@ -1,11 +1,13 @@
#pragma once
+#include <iostream>
+#include <sstream>
+#include <string>
#include <unordered_map>
#include <vector>
-#include <sstream>
-typedef double score_t; // FIXME
-typedef double weight_t;
+#include "util.hh"
+#include "weaver.hh"
using namespace std;
@@ -14,17 +16,52 @@ namespace Sv {
template<typename K, typename V>
struct SparseVector {
- unordered_map<K, V> m_;
- V zero = 0.0;
+ unordered_map<K,V> m_;
+ V zero = 0.f;
+
+ 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);
+ }
+ };
void
insert(K k, V v) { m_[k] = v; };
- weight_t
+ V
dot(SparseVector& other)
{
+ V r;
+ unordered_map<K,V>* o = &m_;
+ auto b = m_.cbegin();
+ auto e = m_.cend();
+ if (other.size() < size()) {
+ b = other.m_.cbegin();
+ e = other.m_.cend();
+ o = &other.m_;
+ }
+ for (auto it = b; it != e; it++)
+ r += it->second * o->at(it->first);
+
+ return r;
};
+ size_t
+ size()
+ {
+ return m_.size();
+ }
+
V&
operator[](const K& k)
{
@@ -44,18 +81,20 @@ struct SparseVector {
operator+(const SparseVector& other) const
{
SparseVector<K,V> v;
- v.m_.insert(m_.begin(), m_.end());
- v.m_.insert(other.m_.begin(), other.m_.end());
- for (auto it = v.m_.begin(); it != v.m_.end(); it++)
- v.m_[it->first] = this->at(it->first) + other.at(it->first);
+ v.m_.insert(m_.cbegin(), m_.cend());
+ v.m_.insert(other.m_.cbegin(), other.m_.cend());
+ for (const auto it: v.m_)
+ v.m_[it.first] = this->at(it.first) + other.at(it.first);
+
return v;
};
SparseVector&
operator+=(const SparseVector& other)
{
- for (auto it = other.m_.begin(); it != other.m_.end(); it++)
- m_[it->first] += it->second;
+ for (const auto it: other.m_)
+ m_[it.first] += it.second;
+
return *this;
};
@@ -63,18 +102,20 @@ struct SparseVector {
operator-(const SparseVector& other) const
{
SparseVector<K,V> v;
- v.m_.insert(m_.begin(), m_.end());
- v.m_.insert(other.m_.begin(), other.m_.end());
- for (auto it = v.m_.begin(); it != v.m_.end(); it++)
- v.m_[it->first] = this->at(it->first) - other.at(it->first);
+ v.m_.insert(m_.cbegin(), m_.cend());
+ v.m_.insert(other.m_.cbegin(), other.m_.cend());
+ for (const auto it: v.m_)
+ v.m_[it.first] = this->at(it.first) - other.at(it.first);
+
return v;
};
SparseVector&
operator-=(const SparseVector& other)
{
- for (auto it = other.m_.begin(); it != other.m_.end(); it++)
- m_[it->first] -= it->second;
+ for (const auto it: other.m_)
+ m_[it.first] -= it.second;
+
return *this;
};
@@ -82,35 +123,48 @@ struct SparseVector {
operator*(V f) const
{
SparseVector<K,V> v;
- for (auto it = m_.begin(); it != m_.end(); it++)
- v.m_[it->first] = this->at(it->first) * f;
+ for (const auto it: m_)
+ v.m_[it.first] = this->at(it.first) * f;
+
return v;
};
SparseVector&
operator*=(V f)
{
- for (auto it = m_.begin(); it != m_.end(); it++)
- m_[it->first] *= f;
+ for (const auto it: m_)
+ m_[it.first] *= f;
+
return *this;
};
string
repr() const
{
- ostringstream os;
+ ostringstream os;
os << "SparseVector<{";
- for (auto it = m_.begin(); it != m_.end(); it ++) {
+ for (auto it = m_.cbegin(); it != m_.cend(); it++) {
os << "'" << it->first << "'=" << it->second;
if (next(it) != m_.end())
os << ", ";
}
os << "}>";
+
+ return os.str();
+ };
+
+ string
+ escaped() const {
+ ostringstream os;
+ for (auto it = m_.cbegin(); it != m_.cend(); it++) {
+ os << '"' << util::json_escape(it->first) << '"' << "=" << it->second;
+ if (next(it) != m_.cend()) os << " ";
+ }
+
return os.str();
};
- friend ostream&
- operator<<(ostream& os, const SparseVector& v) { return os << v.repr(); }
+ friend ostream& operator<<(ostream& os, const SparseVector& v) { return os << v.repr(); }
};
} // namespace