33#include < binsparse/containers/matrices.hpp>
44#include < nlohmann/json.hpp>
55#include < binsparse/containers/matrices.hpp>
6+ #include < binsparse/detail.hpp>
67#include " hdf5_tools.hpp"
78#include " type_info.hpp"
89#include < memory>
1314
1415namespace binsparse {
1516
17+ inline constexpr double version = 0.1 ;
18+
1619// CSR Format
1720
21+ template <typename T, typename I, typename Order>
22+ void write_dense_matrix (std::string fname,
23+ dense_matrix<T, I, Order> m,
24+ nlohmann::json user_keys = {}) {
25+ H5::H5File f (fname.c_str (), H5F_ACC_TRUNC);
26+
27+ std::span<T> values (m.values , m.m *m.n );
28+
29+ hdf5_tools::write_dataset (f, " values" , values);
30+
31+ using json = nlohmann::json;
32+ json j;
33+ j[" binsparse" ][" version" ] = version;
34+ j[" binsparse" ][" format" ] = __detail::get_matrix_string (m);
35+ j[" binsparse" ][" shape" ] = {m.m , m.n };
36+ j[" binsparse" ][" nnz" ] = m.m * m.n ;
37+ j[" binsparse" ][" data_types" ][" values" ] = type_info<T>::label ();
38+
39+ for (auto && v : user_keys.items ()) {
40+ j[v.key ()] = v.value ();
41+ }
42+
43+ hdf5_tools::set_attribute (f, " binsparse" , j.dump (2 ));
44+
45+ f.close ();
46+ }
47+
48+ template <typename T, typename I, typename Order, typename Allocator = std::allocator<T>>
49+ auto read_dense_matrix (std::string fname, Allocator&& alloc = Allocator{}) {
50+ H5::H5File f (fname.c_str (), H5F_ACC_RDWR);
51+
52+ auto metadata = hdf5_tools::get_attribute (f, " binsparse" );
53+
54+ using json = nlohmann::json;
55+ auto data = json::parse (metadata);
56+
57+ std::cout << " Reading values...\n " ;
58+ auto binsparse_metadata = data[" binsparse" ];
59+
60+ assert (binsparse_metadata[" format" ] == __detail::get_matrix_string (dense_matrix<T, I, Order>{}));
61+
62+ auto nrows = binsparse_metadata[" shape" ][0 ];
63+ auto ncols = binsparse_metadata[" shape" ][1 ];
64+ auto nnz = binsparse_metadata[" nnz" ];
65+
66+ auto values = hdf5_tools::read_dataset<T>(f, " values" , alloc);
67+
68+ return dense_matrix<T, I, Order>{values.data (), nrows, ncols};
69+ }
70+
1871template <typename T, typename I>
1972void write_csr_matrix (std::string fname,
2073 csr_matrix<T, I> m,
@@ -32,7 +85,7 @@ void write_csr_matrix(std::string fname,
3285
3386 using json = nlohmann::json;
3487 json j;
35- j[" binsparse" ][" version" ] = 0.5 ;
88+ j[" binsparse" ][" version" ] = version ;
3689 j[" binsparse" ][" format" ] = " CSR" ;
3790 j[" binsparse" ][" shape" ] = {m.m , m.n };
3891 j[" binsparse" ][" nnz" ] = m.nnz ;
@@ -53,7 +106,7 @@ template <typename T, typename I, typename Allocator>
53106csr_matrix<T, I> read_csr_matrix (std::string fname, Allocator&& alloc) {
54107 H5::H5File f (fname.c_str (), H5F_ACC_RDWR);
55108
56- auto metadata = hdf5_tools::read_dataset< char > (f, " metadata " );
109+ auto metadata = hdf5_tools::get_attribute (f, " binsparse " );
57110
58111 using json = nlohmann::json;
59112 auto data = json::parse (metadata);
@@ -100,7 +153,7 @@ void write_coo_matrix(std::string fname,
100153
101154 using json = nlohmann::json;
102155 json j;
103- j[" binsparse" ][" version" ] = 0.5 ;
156+ j[" binsparse" ][" version" ] = version ;
104157 j[" binsparse" ][" format" ] = " COO" ;
105158 j[" binsparse" ][" shape" ] = {m.m , m.n };
106159 j[" binsparse" ][" nnz" ] = m.nnz ;
@@ -121,14 +174,14 @@ template <typename T, typename I, typename Allocator>
121174coo_matrix<T, I> read_coo_matrix (std::string fname, Allocator&& alloc) {
122175 H5::H5File f (fname.c_str (), H5F_ACC_RDWR);
123176
124- auto metadata = hdf5_tools::read_dataset< char > (f, " metadata " );
177+ auto metadata = hdf5_tools::get_attribute (f, " binsparse " );
125178
126179 using json = nlohmann::json;
127180 auto data = json::parse (metadata);
128181
129182 auto binsparse_metadata = data[" binsparse" ];
130183
131- assert (binsparse_metadata[" format" ] == " COO" );
184+ assert (binsparse_metadata[" format" ] == " COO" || binsparse_metadata[ " format " ] == " COOR " );
132185
133186 auto nrows = binsparse_metadata[" shape" ][0 ];
134187 auto ncols = binsparse_metadata[" shape" ][1 ];
0 commit comments