@@ -16,7 +16,7 @@ namespace binsparse {
16
16
17
17
inline constexpr double version = 0.1 ;
18
18
19
- // CSR Format
19
+ // Dense Format
20
20
21
21
template <typename T, typename I, typename Order>
22
22
void write_dense_matrix (std::string fname,
@@ -31,7 +31,7 @@ void write_dense_matrix(std::string fname,
31
31
using json = nlohmann::json;
32
32
json j;
33
33
j[" binsparse" ][" version" ] = version;
34
- j[" binsparse" ][" format" ] = __detail::get_matrix_string (m);
34
+ j[" binsparse" ][" format" ] = __detail::get_matrix_format_string (m);
35
35
j[" binsparse" ][" shape" ] = {m.m , m.n };
36
36
j[" binsparse" ][" nnz" ] = m.m * m.n ;
37
37
j[" binsparse" ][" data_types" ][" values" ] = type_info<T>::label ();
@@ -57,7 +57,9 @@ auto read_dense_matrix(std::string fname, Allocator&& alloc = Allocator{}) {
57
57
std::cout << " Reading values...\n " ;
58
58
auto binsparse_metadata = data[" binsparse" ];
59
59
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>{}));
61
63
62
64
auto nrows = binsparse_metadata[" shape" ][0 ];
63
65
auto ncols = binsparse_metadata[" shape" ][1 ];
@@ -68,6 +70,8 @@ auto read_dense_matrix(std::string fname, Allocator&& alloc = Allocator{}) {
68
70
return dense_matrix<T, I, Order>{values.data (), nrows, ncols};
69
71
}
70
72
73
+ // CSR Format
74
+
71
75
template <typename T, typename I>
72
76
void write_csr_matrix (std::string fname,
73
77
csr_matrix<T, I> m,
@@ -129,11 +133,80 @@ csr_matrix<T, I> read_csr_matrix(std::string fname, Allocator&& alloc) {
129
133
return csr_matrix<T, I>{values.data (), colind.data (), row_ptr.data (), nrows, ncols, nnz};
130
134
}
131
135
136
+
132
137
template <typename T, typename I>
133
138
csr_matrix<T, I> read_csr_matrix (std::string fname) {
134
139
return read_csr_matrix<T, I>(fname, std::allocator<T>{});
135
140
}
136
141
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
+
137
210
// COO Format
138
211
139
212
template <typename T, typename I>
@@ -181,7 +254,9 @@ coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) {
181
254
182
255
auto binsparse_metadata = data[" binsparse" ];
183
256
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" );
185
260
186
261
auto nrows = binsparse_metadata[" shape" ][0 ];
187
262
auto ncols = binsparse_metadata[" shape" ][1 ];
@@ -202,6 +277,7 @@ coo_matrix<T, I> read_coo_matrix(std::string fname) {
202
277
return read_coo_matrix<T, I>(fname, std::allocator<T>{});
203
278
}
204
279
280
+
205
281
inline auto inspect (std::string fname) {
206
282
H5::H5File f (fname.c_str (), H5F_ACC_RDWR);
207
283
0 commit comments