44#include < nlohmann/json.hpp>
55#include < binsparse/containers/matrices.hpp>
66#include " hdf5_tools.hpp"
7+ #include " type_info.hpp"
78#include < memory>
89#include < type_traits>
910
1011#include < binsparse/c_bindings/allocator_wrapper.hpp>
12+ #include < binsparse/matrix_market/matrix_market.hpp>
13+
14+ #include < iostream>
1115
1216namespace binsparse {
1317
@@ -27,16 +31,17 @@ void write_csr_matrix(std::string fname,
2731 hdf5_tools::write_dataset (f, " indices_1" , colind);
2832 hdf5_tools::write_dataset (f, " pointers_to_1" , row_ptr);
2933
30- std::string json_string =
31- " {\n "
32- " \" format\" : \" CSR\" ,\n "
33- " \" shape\" : [" ;
34- json_string += std::to_string (m.m ) + " , " + std::to_string (m.n ) +
35- " ],\n " +
36- " \" nnz\" : " + std::to_string (m.nnz ) + " \n " +
37- " }\n " ;
34+ using json = nlohmann::json;
35+ json j;
36+ j[" binsparse" ][" version" ] = 0.5 ;
37+ j[" binsparse" ][" format" ] = " CSR" ;
38+ j[" binsparse" ][" shape" ] = {m.m , m.n };
39+ j[" binsparse" ][" nnz" ] = m.nnz ;
40+ j[" binsparse" ][" data_types" ][" pointers_to_1" ] = type_info<I>::label ();
41+ j[" binsparse" ][" data_types" ][" indices_1" ] = type_info<I>::label ();
42+ j[" binsparse" ][" data_types" ][" values" ] = type_info<T>::label ();
3843
39- hdf5_tools::write_dataset (f, " metadata" , json_string );
44+ hdf5_tools::write_dataset (f, " metadata" , j. dump ( 2 ) );
4045
4146 f.close ();
4247}
@@ -50,10 +55,10 @@ csr_matrix<T, I> read_csr_matrix(std::string fname, Allocator&& alloc) {
5055 using json = nlohmann::json;
5156 auto data = json::parse (metadata);
5257
53- if (data[" format" ] == " CSR" ) {
54- auto nrows = data[" shape" ][0 ];
55- auto ncols = data[" shape" ][1 ];
56- auto nnz = data[" nnz" ];
58+ if (data[" binsparse " ][ " format" ] == " CSR" ) {
59+ auto nrows = data[" binsparse " ][ " shape" ][0 ];
60+ auto ncols = data[" binsparse " ][ " shape" ][1 ];
61+ auto nnz = data[" binsparse " ][ " nnz" ];
5762
5863 typename std::allocator_traits<std::remove_cvref_t <Allocator>>
5964 :: template rebind_alloc<I> i_alloc (alloc);
@@ -89,16 +94,17 @@ void write_coo_matrix(std::string fname,
8994 hdf5_tools::write_dataset (f, " indices_0" , rowind);
9095 hdf5_tools::write_dataset (f, " indices_1" , colind);
9196
92- std::string json_string =
93- " {\n "
94- " \" format\" : \" COO\" ,\n "
95- " \" shape\" : [" ;
96- json_string += std::to_string (m.m ) + " , " + std::to_string (m.n ) +
97- " ],\n " +
98- " \" nnz\" : " + std::to_string (m.nnz ) + " \n " +
99- " }\n " ;
97+ using json = nlohmann::json;
98+ json j;
99+ j[" binsparse" ][" version" ] = 0.5 ;
100+ j[" binsparse" ][" format" ] = " COO" ;
101+ j[" binsparse" ][" shape" ] = {m.m , m.n };
102+ j[" binsparse" ][" nnz" ] = m.nnz ;
103+ j[" binsparse" ][" data_types" ][" indices_0" ] = type_info<I>::label ();
104+ j[" binsparse" ][" data_types" ][" indices_1" ] = type_info<I>::label ();
105+ j[" binsparse" ][" data_types" ][" values" ] = type_info<T>::label ();
100106
101- hdf5_tools::write_dataset (f, " metadata" , json_string );
107+ hdf5_tools::write_dataset (f, " metadata" , j. dump ( 2 ) );
102108
103109 f.close ();
104110}
@@ -112,10 +118,10 @@ coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) {
112118 using json = nlohmann::json;
113119 auto data = json::parse (metadata);
114120
115- if (data[" format" ] == " COO" ) {
116- auto nrows = data[" shape" ][0 ];
117- auto ncols = data[" shape" ][1 ];
118- auto nnz = data[" nnz" ];
121+ if (data[" binsparse " ][ " format" ] == " COO" ) {
122+ auto nrows = data[" binsparse " ][ " shape" ][0 ];
123+ auto ncols = data[" binsparse " ][ " shape" ][1 ];
124+ auto nnz = data[" binsparse " ][ " nnz" ];
119125
120126 typename std::allocator_traits<std::remove_cvref_t <Allocator>>
121127 :: template rebind_alloc<I> i_alloc (alloc);
0 commit comments