1111#include < binsparse/c_bindings/allocator_wrapper.hpp>
1212#include < binsparse/matrix_market/matrix_market.hpp>
1313
14- #include < iostream>
15-
1614namespace binsparse {
1715
1816// CSR Format
@@ -46,7 +44,7 @@ void write_csr_matrix(std::string fname,
4644 j[v.key ()] = v.value ();
4745 }
4846
49- hdf5_tools::write_dataset (f, " metadata " , j.dump (2 ));
47+ hdf5_tools::set_attribute (f, " binsparse " , j.dump (2 ));
5048
5149 f.close ();
5250}
@@ -60,10 +58,12 @@ csr_matrix<T, I> read_csr_matrix(std::string fname, Allocator&& alloc) {
6058 using json = nlohmann::json;
6159 auto data = json::parse (metadata);
6260
63- if (data[" binsparse" ][" format" ] == " CSR" ) {
64- auto nrows = data[" binsparse" ][" shape" ][0 ];
65- auto ncols = data[" binsparse" ][" shape" ][1 ];
66- auto nnz = data[" binsparse" ][" nnz" ];
61+ auto binsparse_metadata = data[" binsparse" ];
62+
63+ if (binsparse_metadata[" format" ] == " CSR" ) {
64+ auto nrows = binsparse_metadata[" shape" ][0 ];
65+ auto ncols = binsparse_metadata[" shape" ][1 ];
66+ auto nnz = binsparse_metadata[" nnz" ];
6767
6868 typename std::allocator_traits<std::remove_cvref_t <Allocator>>
6969 :: template rebind_alloc<I> i_alloc (alloc);
@@ -114,7 +114,7 @@ void write_coo_matrix(std::string fname,
114114 j[v.key ()] = v.value ();
115115 }
116116
117- hdf5_tools::write_dataset (f, " metadata " , j.dump (2 ));
117+ hdf5_tools::set_attribute (f, " binsparse " , j.dump (2 ));
118118
119119 f.close ();
120120}
@@ -128,10 +128,12 @@ coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) {
128128 using json = nlohmann::json;
129129 auto data = json::parse (metadata);
130130
131- if (data[" binsparse" ][" format" ] == " COO" ) {
132- auto nrows = data[" binsparse" ][" shape" ][0 ];
133- auto ncols = data[" binsparse" ][" shape" ][1 ];
134- auto nnz = data[" binsparse" ][" nnz" ];
131+ auto binsparse_metadata = data[" binsparse" ];
132+
133+ if (binsparse_metadata[" format" ] == " COO" ) {
134+ auto nrows = binsparse_metadata[" shape" ][0 ];
135+ auto ncols = binsparse_metadata[" shape" ][1 ];
136+ auto nnz = binsparse_metadata[" nnz" ];
135137
136138 typename std::allocator_traits<std::remove_cvref_t <Allocator>>
137139 :: template rebind_alloc<I> i_alloc (alloc);
@@ -154,12 +156,14 @@ coo_matrix<T, I> read_coo_matrix(std::string fname) {
154156inline auto inspect (std::string fname) {
155157 H5::H5File f (fname.c_str (), H5F_ACC_RDWR);
156158
157- auto metadata = hdf5_tools::read_dataset< char > (f, " metadata " );
159+ auto metadata = hdf5_tools::get_attribute (f, " binsparse " );
158160
159161 using json = nlohmann::json;
160162 auto data = json::parse (metadata);
161163
162- if (data[" binsparse" ][" version" ] >= 0.1 ) {
164+ auto binsparse_metadata = data[" binsparse" ];
165+
166+ if (binsparse_metadata[" version" ] >= 0.1 ) {
163167 return data;
164168 } else {
165169 assert (false );
0 commit comments