Skip to content

Commit 8e9ef66

Browse files
committed
Update to support writing matrix to HDF5 group
1 parent 3cbb9be commit 8e9ef66

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

examples/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ function(add_example example_name)
33
target_link_libraries(${example_name} binsparse fmt)
44
endfunction()
55

6-
add_example(test)
76
add_example(convert_binsparse)
87
add_example(inspect_binsparse)

include/binsparse/binsparse.hpp

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ inline constexpr double version = 0.1;
1818
// Dense Format
1919

2020
template <typename T, typename I, typename Order>
21-
void write_dense_matrix(std::string fname, dense_matrix<T, I, Order> m,
21+
void write_dense_matrix(H5::Group& f, dense_matrix<T, I, Order> m,
2222
nlohmann::json user_keys = {}) {
23-
H5::H5File f(fname.c_str(), H5F_ACC_TRUNC);
24-
2523
std::span<T> values(m.values, m.m * m.n);
2624

2725
hdf5_tools::write_dataset(f, "values", values);
@@ -44,7 +42,13 @@ void write_dense_matrix(std::string fname, dense_matrix<T, I, Order> m,
4442
}
4543

4644
hdf5_tools::set_attribute(f, "binsparse", j.dump(2));
45+
}
4746

47+
template <typename T, typename I, typename Order>
48+
void write_dense_matrix(std::string fname, dense_matrix<T, I, Order> m,
49+
nlohmann::json user_keys = {}) {
50+
H5::H5File f(fname.c_str(), H5F_ACC_TRUNC);
51+
write_dense_matrix(f, m, user_keys);
4852
f.close();
4953
}
5054

@@ -84,11 +88,8 @@ auto read_dense_matrix(std::string fname, Allocator&& alloc = Allocator{}) {
8488
// CSR Format
8589

8690
template <typename T, typename I>
87-
void write_csr_matrix(std::string fname, csr_matrix<T, I> m,
91+
void write_csr_matrix(H5::Group& f, csr_matrix<T, I> m,
8892
nlohmann::json user_keys = {}) {
89-
90-
H5::H5File f(fname.c_str(), H5F_ACC_TRUNC);
91-
9293
std::span<T> values(m.values, m.nnz);
9394
std::span<I> colind(m.colind, m.nnz);
9495
std::span<I> row_ptr(m.row_ptr, m.m + 1);
@@ -117,7 +118,13 @@ void write_csr_matrix(std::string fname, csr_matrix<T, I> m,
117118
}
118119

119120
hdf5_tools::set_attribute(f, "binsparse", j.dump(2));
121+
}
120122

123+
template <typename T, typename I>
124+
void write_csr_matrix(std::string fname, csr_matrix<T, I> m,
125+
nlohmann::json user_keys = {}) {
126+
H5::H5File f(fname.c_str(), H5F_ACC_TRUNC);
127+
write_csr_matrix(f, m, user_keys);
121128
f.close();
122129
}
123130

@@ -164,11 +171,8 @@ csr_matrix<T, I> read_csr_matrix(std::string fname) {
164171
// CSC Format
165172

166173
template <typename T, typename I>
167-
void write_csc_matrix(std::string fname, csc_matrix<T, I> m,
174+
void write_csc_matrix(H5::Group& f, csc_matrix<T, I> m,
168175
nlohmann::json user_keys = {}) {
169-
170-
H5::H5File f(fname.c_str(), H5F_ACC_TRUNC);
171-
172176
std::span<T> values(m.values, m.nnz);
173177
std::span<I> rowind(m.rowind, m.nnz);
174178
std::span<I> col_ptr(m.col_ptr, m.m + 1);
@@ -197,7 +201,13 @@ void write_csc_matrix(std::string fname, csc_matrix<T, I> m,
197201
}
198202

199203
hdf5_tools::set_attribute(f, "binsparse", j.dump(2));
204+
}
200205

206+
template <typename T, typename I>
207+
void write_csc_matrix(std::string fname, csc_matrix<T, I> m,
208+
nlohmann::json user_keys = {}) {
209+
H5::H5File f(fname.c_str(), H5F_ACC_TRUNC);
210+
write_csc_matrix(f, m, user_keys);
201211
f.close();
202212
}
203213

@@ -244,11 +254,8 @@ csc_matrix<T, I> read_csc_matrix(std::string fname) {
244254
// COO Format
245255

246256
template <typename T, typename I>
247-
void write_coo_matrix(std::string fname, coo_matrix<T, I> m,
257+
void write_coo_matrix(H5::Group& f, coo_matrix<T, I> m,
248258
nlohmann::json user_keys = {}) {
249-
250-
H5::H5File f(fname.c_str(), H5F_ACC_TRUNC);
251-
252259
std::span<T> values(m.values, m.nnz);
253260
std::span<I> rowind(m.rowind, m.nnz);
254261
std::span<I> colind(m.colind, m.nnz);
@@ -277,7 +284,13 @@ void write_coo_matrix(std::string fname, coo_matrix<T, I> m,
277284
}
278285

279286
hdf5_tools::set_attribute(f, "binsparse", j.dump(2));
287+
}
280288

289+
template <typename T, typename I>
290+
void write_coo_matrix(std::string fname, coo_matrix<T, I> m,
291+
nlohmann::json user_keys = {}) {
292+
H5::H5File f(fname.c_str(), H5F_ACC_TRUNC);
293+
write_coo_matrix(f, m, user_keys);
281294
f.close();
282295
}
283296

0 commit comments

Comments
 (0)