summaryrefslogtreecommitdiff
path: root/include/gudhi_laplacian/sparse_row.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/gudhi_laplacian/sparse_row.hpp')
-rw-r--r--include/gudhi_laplacian/sparse_row.hpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/include/gudhi_laplacian/sparse_row.hpp b/include/gudhi_laplacian/sparse_row.hpp
new file mode 100644
index 0000000..9896625
--- /dev/null
+++ b/include/gudhi_laplacian/sparse_row.hpp
@@ -0,0 +1,48 @@
+#pragma once
+
+#include <vector>
+#include <algorithm>
+#include <utility>
+#include <type_traits>
+
+namespace Gudhi_laplacian
+{
+ template <typename T>
+ using Sparse_row = std::vector<std::pair<unsigned int, T> >;
+
+ template <typename T>
+ void compress_sparse_row(Sparse_row<T> & row)
+ {
+ static_assert(std::is_arithmetic<T>::value, "Only arithmetic types are supported.");
+
+ Sparse_row<T> tmp(row);
+ row.clear();
+ std::sort(tmp.begin(), tmp.end());
+ for (auto it = tmp.cbegin(); it != tmp.cend(); ++it)
+ {
+ if (row.empty())
+ {
+ if (it->second != T())
+ {
+ row.push_back(*it);
+ }
+ }
+ else if (it->first == row.back().first)
+ {
+ T x = row.back().second + it->second;
+ if (x == T())
+ {
+ row.pop_back();
+ }
+ else
+ {
+ row.back().second = x;
+ }
+ }
+ else if (it->second != T())
+ {
+ row.push_back(*it);
+ }
+ }
+ }
+}