@@ -16,7 +16,7 @@ namespace binsparse {
1616
1717inline constexpr double version = 0.1 ;
1818
19- // CSR Format
19+ // Dense Format
2020
2121template <typename T, typename I, typename Order>
2222void write_dense_matrix (std::string fname,
@@ -31,7 +31,7 @@ void write_dense_matrix(std::string fname,
3131 using json = nlohmann::json;
3232 json j;
3333 j[" binsparse" ][" version" ] = version;
34- j[" binsparse" ][" format" ] = __detail::get_matrix_string (m);
34+ j[" binsparse" ][" format" ] = __detail::get_matrix_format_string (m);
3535 j[" binsparse" ][" shape" ] = {m.m , m.n };
3636 j[" binsparse" ][" nnz" ] = m.m * m.n ;
3737 j[" binsparse" ][" data_types" ][" values" ] = type_info<T>::label ();
@@ -57,7 +57,9 @@ auto read_dense_matrix(std::string fname, Allocator&& alloc = Allocator{}) {
5757 std::cout << " Reading values...\n " ;
5858 auto binsparse_metadata = data[" binsparse" ];
5959
60- assert (binsparse_metadata[" format" ] == __detail::get_matrix_string (dense_matrix<T, I, Order>{}));
60+ auto format = __detail::unalias_format (binsparse_metadata[" format" ]);
61+
62+ assert (format == __detail::get_matrix_format_string (dense_matrix<T, I, Order>{}));
6163
6264 auto nrows = binsparse_metadata[" shape" ][0 ];
6365 auto ncols = binsparse_metadata[" shape" ][1 ];
@@ -68,6 +70,8 @@ auto read_dense_matrix(std::string fname, Allocator&& alloc = Allocator{}) {
6870 return dense_matrix<T, I, Order>{values.data (), nrows, ncols};
6971}
7072
73+ // CSR Format
74+
7175template <typename T, typename I>
7276void write_csr_matrix (std::string fname,
7377 csr_matrix<T, I> m,
@@ -129,11 +133,80 @@ csr_matrix<T, I> read_csr_matrix(std::string fname, Allocator&& alloc) {
129133 return csr_matrix<T, I>{values.data (), colind.data (), row_ptr.data (), nrows, ncols, nnz};
130134}
131135
136+
132137template <typename T, typename I>
133138csr_matrix<T, I> read_csr_matrix (std::string fname) {
134139 return read_csr_matrix<T, I>(fname, std::allocator<T>{});
135140}
136141
142+ // CSC Format
143+
144+ template <typename T, typename I>
145+ void write_csc_matrix (std::string fname,
146+ csc_matrix<T, I> m,
147+ nlohmann::json user_keys = {}) {
148+
149+ H5::H5File f (fname.c_str (), H5F_ACC_TRUNC);
150+
151+ std::span<T> values (m.values , m.nnz );
152+ std::span<I> rowind (m.rowind , m.nnz );
153+ std::span<I> col_ptr (m.col_ptr , m.m +1 );
154+
155+ hdf5_tools::write_dataset (f, " values" , values);
156+ hdf5_tools::write_dataset (f, " indices_1" , rowind);
157+ hdf5_tools::write_dataset (f, " pointers_to_1" , col_ptr);
158+
159+ using json = nlohmann::json;
160+ json j;
161+ j[" binsparse" ][" version" ] = version;
162+ j[" binsparse" ][" format" ] = " CSR" ;
163+ j[" binsparse" ][" shape" ] = {m.m , m.n };
164+ j[" binsparse" ][" nnz" ] = m.nnz ;
165+ j[" binsparse" ][" data_types" ][" pointers_to_1" ] = type_info<I>::label ();
166+ j[" binsparse" ][" data_types" ][" indices_1" ] = type_info<I>::label ();
167+ j[" binsparse" ][" data_types" ][" values" ] = type_info<T>::label ();
168+
169+ for (auto && v : user_keys.items ()) {
170+ j[v.key ()] = v.value ();
171+ }
172+
173+ hdf5_tools::set_attribute (f, " binsparse" , j.dump (2 ));
174+
175+ f.close ();
176+ }
177+
178+ template <typename T, typename I, typename Allocator>
179+ csc_matrix<T, I> read_csc_matrix (std::string fname, Allocator&& alloc) {
180+ H5::H5File f (fname.c_str (), H5F_ACC_RDWR);
181+
182+ auto metadata = hdf5_tools::get_attribute (f, " binsparse" );
183+
184+ using json = nlohmann::json;
185+ auto data = json::parse (metadata);
186+
187+ auto binsparse_metadata = data[" binsparse" ];
188+
189+ assert (binsparse_metadata[" format" ] == " CSC" );
190+
191+ auto nrows = binsparse_metadata[" shape" ][0 ];
192+ auto ncols = binsparse_metadata[" shape" ][1 ];
193+ auto nnz = binsparse_metadata[" nnz" ];
194+
195+ typename std::allocator_traits<std::remove_cvref_t <Allocator>>
196+ :: template rebind_alloc<I> i_alloc (alloc);
197+
198+ auto values = hdf5_tools::read_dataset<T>(f, " values" , alloc);
199+ auto rowind = hdf5_tools::read_dataset<I>(f, " indices_1" , i_alloc);
200+ auto col_ptr = hdf5_tools::read_dataset<I>(f, " pointers_to_1" , i_alloc);
201+
202+ return csc_matrix<T, I>{values.data (), rowind.data (), col_ptr.data (), nrows, ncols, nnz};
203+ }
204+
205+ template <typename T, typename I>
206+ csc_matrix<T, I> read_csc_matrix (std::string fname) {
207+ return read_csc_matrix<T, I>(fname, std::allocator<T>{});
208+ }
209+
137210// COO Format
138211
139212template <typename T, typename I>
@@ -181,7 +254,9 @@ coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) {
181254
182255 auto binsparse_metadata = data[" binsparse" ];
183256
184- assert (binsparse_metadata[" format" ] == " COO" || binsparse_metadata[" format" ] == " COOR" );
257+ auto format = __detail::unalias_format (binsparse_metadata[" format" ]);
258+
259+ assert (format == " COOR" || format == " COOC" );
185260
186261 auto nrows = binsparse_metadata[" shape" ][0 ];
187262 auto ncols = binsparse_metadata[" shape" ][1 ];
@@ -202,6 +277,7 @@ coo_matrix<T, I> read_coo_matrix(std::string fname) {
202277 return read_coo_matrix<T, I>(fname, std::allocator<T>{});
203278}
204279
280+
205281inline auto inspect (std::string fname) {
206282 H5::H5File f (fname.c_str (), H5F_ACC_RDWR);
207283
0 commit comments