Skip to content

Commit 5cec281

Browse files
committed
Add writing
1 parent 327fb71 commit 5cec281

File tree

2 files changed

+13
-48
lines changed

2 files changed

+13
-48
lines changed

examples/simple_io.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
#include <binsparse/binsparse.hpp>
33

44
int main(int argc, char** argv) {
5-
printf("Hello, world!\n");
65
auto mat = binsparse::read_coo_matrix<float>("data/matrix.hdf5");
76

87
for (size_t i = 0; i < mat.nnz; i++) {
98
std::cout << mat.rowind[i] << ", " << mat.colind[i] << ": " << mat.values[i] << std::endl;
109
}
1110

11+
binsparse::write_coo_matrix("new_matrix.hdf5", mat);
12+
1213
return 0;
1314
}

include/binsparse/binsparse.hpp

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,33 @@
88

99
namespace binsparse {
1010

11-
/*
1211
template <typename T, typename I>
13-
void write_matrix(std::string fname,
14-
const std::vector<std::tuple<std::tuple<I, I>, T>>& tuples) {
15-
16-
using value_type = T
17-
using index_type = I
18-
19-
std::vector<std::ranges::range_value_t<M>> tuples(matrix.begin(), matrix.end());
20-
21-
auto sort_fn = [](const auto& a, const auto& b) {
22-
auto&& [a_index, a_value] = a;
23-
auto&& [b_index, b_value] = b;
24-
auto&& [a_i, a_j] = a_index;
25-
auto&& [b_i, b_j] = b_index;
26-
if (a_i < b_i) {
27-
return true;
28-
}
29-
else if (a_i == b_i) {
30-
if (a_j < b_j) {
31-
return true;
32-
}
33-
}
34-
return false;
35-
};
36-
37-
std::sort(tuples.begin(), tuples.end(), sort_fn);
38-
39-
std::vector<grb::matrix_index_t<M>> rows;
40-
std::vector<grb::matrix_index_t<M>> cols;
41-
std::vector<grb::matrix_scalar_t<M>> vals;
42-
43-
rows.reserve(matrix.size());
44-
cols.reserve(matrix.size());
45-
vals.reserve(matrix.size());
46-
47-
for (auto&& [index, value] : tuples) {
48-
auto&& [row, col] = index;
49-
rows.push_back(row);
50-
cols.push_back(col);
51-
vals.push_back(value);
52-
}
12+
void write_coo_matrix(std::string fname,
13+
coo_matrix<T, I> m) {
5314

5415
H5::H5File f(fname.c_str(), H5F_ACC_TRUNC);
5516

56-
hdf5_tools::write_dataset(f, "values", vals);
57-
hdf5_tools::write_dataset(f, "indices_0", rows);
58-
hdf5_tools::write_dataset(f, "indices_1", cols);
17+
std::span<T> values(m.values, m.nnz);
18+
std::span<I> rowind(m.rowind, m.nnz);
19+
std::span<I> colind(m.colind, m.nnz);
20+
21+
hdf5_tools::write_dataset(f, "values", values);
22+
hdf5_tools::write_dataset(f, "indices_0", rowind);
23+
hdf5_tools::write_dataset(f, "indices_1", colind);
5924

6025
std::string json_string =
6126
"{\n"
6227
" \"format\": \"COO\",\n"
6328
" \"shape\": [";
64-
json_string += std::to_string(matrix.shape()[0]) + ", " + std::to_string(matrix.shape()[1]) +
29+
json_string += std::to_string(m.m) + ", " + std::to_string(m.n) +
6530
"],\n" +
66-
" \"nnz\": " + std::to_string(matrix.size()) + "\n" +
31+
" \"nnz\": " + std::to_string(m.nnz) + "\n" +
6732
"}\n";
6833

6934
hdf5_tools::write_dataset(f, "metadata", json_string);
7035

7136
f.close();
7237
}
73-
*/
7438

7539
template <typename T, typename I, typename Allocator>
7640
coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) {

0 commit comments

Comments
 (0)