summaryrefslogtreecommitdiff
path: root/fast/sparse_vector.hh
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-07-22 00:34:01 +0200
committerPatrick Simianer <p@simianer.de>2014-07-22 00:34:01 +0200
commit4b7b2693e829166ccec8707b59fb2bc26179551b (patch)
tree1c103865488aa8cefabf9714cd571066135b8ca4 /fast/sparse_vector.hh
parent02bbe0c6bc69283a988caf8f0ab3dadb5d9d72b5 (diff)
simple sparse vector type
Diffstat (limited to 'fast/sparse_vector.hh')
-rw-r--r--fast/sparse_vector.hh116
1 files changed, 116 insertions, 0 deletions
diff --git a/fast/sparse_vector.hh b/fast/sparse_vector.hh
new file mode 100644
index 0000000..8fdc1b9
--- /dev/null
+++ b/fast/sparse_vector.hh
@@ -0,0 +1,116 @@
+#pragma once
+
+#include <unordered_map>
+#include <vector>
+#include <sstream>
+
+#include "hypergraph.hh" // FIXME
+
+using namespace std;
+
+
+namespace Sv {
+
+template<typename K, typename V>
+struct SparseVector {
+ unordered_map<K, V> m_;
+ V zero = 0.0;
+
+ void
+ insert(K k, V v) { m_[k] = v; };
+
+ weight_t
+ dot(SparseVector& other)
+ {
+ };
+
+ V&
+ operator[](const K& k)
+ {
+ return at(k);
+ };
+
+ const V&
+ at(const K& k) const
+ {
+ if (m_.find(k) == m_.end())
+ return zero;
+ else
+ return m_.at(k);
+ }
+
+ 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);
+ return v;
+ };
+
+ SparseVector&
+ operator+=(const SparseVector& other)
+ {
+ for (auto it = other.m_.begin(); it != other.m_.end(); it++)
+ m_[it->first] += it->second;
+ return *this;
+ };
+
+ 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);
+ return v;
+ };
+
+ SparseVector&
+ operator-=(const SparseVector& other)
+ {
+ for (auto it = other.m_.begin(); it != other.m_.end(); it++)
+ m_[it->first] -= it->second;
+ return *this;
+ };
+
+ 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;
+ return v;
+ };
+
+ SparseVector&
+ operator*=(V f)
+ {
+ for (auto it = m_.begin(); it != m_.end(); it++)
+ m_[it->first] *= f;
+ return *this;
+ };
+
+ string
+ repr() const
+ {
+ ostringstream os;
+ os << "SparseVector<{";
+ for (auto it = m_.begin(); it != m_.end(); it ++) {
+ os << "'" << it->first << "'=" << it->second;
+ if (next(it) != m_.end())
+ os << ", ";
+ }
+ os << "}>";
+ return os.str();
+ };
+
+ friend ostream&
+ operator<<(ostream& os, const SparseVector& v) { return os << v.repr(); }
+};
+
+} // namespace
+