summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlrich Bauer <ulrich.bauer@tum.de>2015-10-18 15:41:38 +0200
committerUlrich Bauer <ulrich.bauer@tum.de>2015-10-18 15:41:38 +0200
commit505465cb6ff34465b398d8ece285132fcddbc2fd (patch)
tree82359dd4a486aad40fa6831e6bab990a6ee71d14
parenta08033c16619a00b33f767dd883bd042b67d2784 (diff)
compressed sparse matrix
-rw-r--r--ripser.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/ripser.cpp b/ripser.cpp
index 450e4b1..1a273f5 100644
--- a/ripser.cpp
+++ b/ripser.cpp
@@ -155,6 +155,45 @@ public:
};
+template <typename ValueType>
+class compressed_sparse_matrix {
+public:
+ std::vector<size_t> bounds;
+ std::vector<ValueType> entries;
+
+
+ size_t size() const {
+ return bounds.size();
+ }
+
+ typename std::vector<ValueType>::const_iterator cbegin(size_t index) {
+ assert(index < size());
+ return index == 0 ? entries.cbegin() : entries.cbegin() + bounds[index - 1];
+ }
+
+ typename std::vector<ValueType>::const_iterator cend(size_t index) {
+ assert(index < size());
+ return entries.cbegin() + bounds[index];
+ }
+
+ template <typename Iterator>
+ void append(Iterator begin, Iterator end) {
+ for (Iterator it = begin; it != end; ++it) {
+ entries.push_back(*it);
+ }
+ bounds.push_back(entries.size());
+ }
+
+
+ template <typename Collection>
+ void append(const Collection collection) {
+ append(collection.cbegin(), collection.cend());
+ }
+
+};
+
+
+
int main() {
distance_matrix dist;
@@ -222,5 +261,28 @@ int main() {
std::cout << " " << vertices << std::endl;
}
+
+
+ compressed_sparse_matrix<int> csm;
+
+ csm.append(std::vector<int>({1,2,3}));
+
+ csm.append(std::vector<int>({5,6,7,8}));
+
+ csm.append(std::vector<int>({10,11}));
+
+ csm.append(std::vector<int>());
+
+ csm.append(std::vector<int>({2}));
+
+ std::cout << "compressed sparse matrix: " << std::endl;
+
+ for (int i = 0; i < csm.size(); ++i) {
+ std::cout << " " << std::vector<int>(csm.cbegin(i), csm.cend(i)) << std::endl;
+ }
+
+ std::cout << "bounds: " << csm.bounds << std::endl;
+
+ std::cout << "entries: " << csm.entries << std::endl;
} \ No newline at end of file