From 505465cb6ff34465b398d8ece285132fcddbc2fd Mon Sep 17 00:00:00 2001 From: Ulrich Bauer Date: Sun, 18 Oct 2015 15:41:38 +0200 Subject: compressed sparse matrix --- ripser.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/ripser.cpp b/ripser.cpp index 450e4b1..1a273f5 100644 --- a/ripser.cpp +++ b/ripser.cpp @@ -155,6 +155,45 @@ public: }; +template +class compressed_sparse_matrix { +public: + std::vector bounds; + std::vector entries; + + + size_t size() const { + return bounds.size(); + } + + typename std::vector::const_iterator cbegin(size_t index) { + assert(index < size()); + return index == 0 ? entries.cbegin() : entries.cbegin() + bounds[index - 1]; + } + + typename std::vector::const_iterator cend(size_t index) { + assert(index < size()); + return entries.cbegin() + bounds[index]; + } + + template + void append(Iterator begin, Iterator end) { + for (Iterator it = begin; it != end; ++it) { + entries.push_back(*it); + } + bounds.push_back(entries.size()); + } + + + template + 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 csm; + + csm.append(std::vector({1,2,3})); + + csm.append(std::vector({5,6,7,8})); + + csm.append(std::vector({10,11})); + + csm.append(std::vector()); + + csm.append(std::vector({2})); + + std::cout << "compressed sparse matrix: " << std::endl; + + for (int i = 0; i < csm.size(); ++i) { + std::cout << " " << std::vector(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 -- cgit v1.2.3