Skip to content

Commit

Permalink
Add binary file saving methods to SparseMatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
Grufoony committed Jan 31, 2025
1 parent 03c07d6 commit 85f981d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dsm/dsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

static constexpr uint8_t DSM_VERSION_MAJOR = 2;
static constexpr uint8_t DSM_VERSION_MINOR = 3;
static constexpr uint8_t DSM_VERSION_PATCH = 15;
static constexpr uint8_t DSM_VERSION_PATCH = 16;

static auto const DSM_VERSION =
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);
Expand Down
45 changes: 45 additions & 0 deletions src/dsm/headers/SparseMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <vector>
#include <cmath>
#include <format>
#include <fstream>

#include "../utility/Logger.hpp"
#include "../utility/Typedef.hpp"
Expand Down Expand Up @@ -190,6 +191,13 @@ namespace dsm {
/// @brief reshape the matrix
void reshape(Id dim);

/// @brief save the matrix to a binary file (.dsmcache)
/// @param filename the name of the file
void cache(std::string const& filename) const;
/// @brief load the matrix from a binary file (.dsmcache)
/// @param filename the name of the file
void load(std::string const& filename);

/// @brief return the begin iterator of the matrix
/// @return the begin iterator
typename std::unordered_map<Id, T>::const_iterator begin() const {
Expand Down Expand Up @@ -632,6 +640,43 @@ namespace dsm {
}
}

template <typename T>
void SparseMatrix<T>::cache(std::string const& filename) const {
std::ofstream file(filename, std::ios::binary);
if (!file.is_open()) {
Logger::error(std::format("Error opening file \"{}\" for writing.", filename));
}
file.write(reinterpret_cast<const char*>(&_rows), sizeof(Id));
file.write(reinterpret_cast<const char*>(&_cols), sizeof(Id));
size_t size = _matrix.size();
file.write(reinterpret_cast<const char*>(&size), sizeof(size_t));
for (auto& it : _matrix) {
file.write(reinterpret_cast<const char*>(&it.first), sizeof(Id));
file.write(reinterpret_cast<const char*>(&it.second), sizeof(T));
}
file.close();
}

template <typename T>
void SparseMatrix<T>::load(std::string const& filename) {
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
Logger::error(std::format("Error opening file \"{}\" for reading.", filename));
}
file.read(reinterpret_cast<char*>(&_rows), sizeof(Id));
file.read(reinterpret_cast<char*>(&_cols), sizeof(Id));
size_t size;
file.read(reinterpret_cast<char*>(&size), sizeof(size_t));
for (size_t i = 0; i < size; ++i) {
Id index;
T value;
file.read(reinterpret_cast<char*>(&index), sizeof(Id));
file.read(reinterpret_cast<char*>(&value), sizeof(T));
_matrix.insert_or_assign(index, value);
}
file.close();
}

template <typename T>
const T& SparseMatrix<T>::operator()(Id i, Id j) const {
if (i >= _rows || j >= _cols) {
Expand Down
21 changes: 21 additions & 0 deletions test/Test_sparsematrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,25 @@ TEST_CASE("Boolean Matrix") {
CHECK(m.getRowDim() == 5);
CHECK(m.getColDim() == 5);
}
SUBCASE("Caching") {
/*
The caching function should cache the matrix
GIVEN: the caching function is called
WHEN: the function is called on a matrix
THEN: the function should cache the matrix
*/
{
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(1, 2, true);
m.cache("./data/test.dsmcache");
}
SparseMatrix<bool> m;
m.load("./data/test.dsmcache");
CHECK(m(0, 0));
CHECK(m(1, 2));
CHECK(m.size() == 2);
CHECK(m.getRowDim() == 3);
CHECK(m.getColDim() == 3);
}
}

0 comments on commit 85f981d

Please sign in to comment.