|
8 | 8 |
|
9 | 9 | namespace binsparse { |
10 | 10 |
|
11 | | -/* |
12 | 11 | 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) { |
53 | 14 |
|
54 | 15 | H5::H5File f(fname.c_str(), H5F_ACC_TRUNC); |
55 | 16 |
|
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); |
59 | 24 |
|
60 | 25 | std::string json_string = |
61 | 26 | "{\n" |
62 | 27 | " \"format\": \"COO\",\n" |
63 | 28 | " \"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) + |
65 | 30 | "],\n" + |
66 | | - " \"nnz\": " + std::to_string(matrix.size()) + "\n" + |
| 31 | + " \"nnz\": " + std::to_string(m.nnz) + "\n" + |
67 | 32 | "}\n"; |
68 | 33 |
|
69 | 34 | hdf5_tools::write_dataset(f, "metadata", json_string); |
70 | 35 |
|
71 | 36 | f.close(); |
72 | 37 | } |
73 | | -*/ |
74 | 38 |
|
75 | 39 | template <typename T, typename I, typename Allocator> |
76 | 40 | coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) { |
|
0 commit comments