Skip to content

Commit d9f61bf

Browse files
committed
Make write_matrix separately compiled
1 parent e83e90e commit d9f61bf

File tree

4 files changed

+165
-135
lines changed

4 files changed

+165
-135
lines changed

include/binsparse/convert_matrix.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <assert.h>
1010
#include <binsparse/matrix.h>
1111

12-
bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
13-
bsp_matrix_format_t format) {
12+
static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
13+
bsp_matrix_format_t format) {
1414
// Throw an error if matrix already in desired format.
1515
if (matrix.format == format) {
1616
assert(false);

include/binsparse/write_matrix.h

Lines changed: 14 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -6,144 +6,25 @@
66

77
#pragma once
88

9-
#include <assert.h>
10-
#include <binsparse/matrix.h>
11-
#include <cJSON/cJSON.h>
12-
#include <unistd.h>
13-
14-
char* bsp_generate_json(bsp_matrix_t matrix, cJSON* user_json) {
15-
cJSON* j = cJSON_CreateObject();
16-
assert(j != NULL);
17-
18-
cJSON* binsparse = cJSON_CreateObject();
19-
20-
assert(binsparse != NULL);
21-
22-
cJSON_AddItemToObject(j, "binsparse", binsparse);
23-
24-
cJSON* item;
25-
cJSON_ArrayForEach(item, user_json) {
26-
cJSON_AddItemToObject(j, item->string, item);
27-
}
28-
29-
cJSON_AddStringToObject(binsparse, "version", BINSPARSE_VERSION);
30-
31-
cJSON_AddStringToObject(binsparse, "format",
32-
bsp_get_matrix_format_string(matrix.format));
33-
34-
cJSON* shape = cJSON_AddArrayToObject(binsparse, "shape");
35-
36-
cJSON* nrows = cJSON_CreateNumber(matrix.nrows);
37-
cJSON* ncols = cJSON_CreateNumber(matrix.ncols);
38-
39-
cJSON_AddItemToArray(shape, nrows);
40-
cJSON_AddItemToArray(shape, ncols);
41-
42-
cJSON_AddNumberToObject(binsparse, "number_of_stored_values", matrix.nnz);
43-
44-
cJSON* data_types = cJSON_AddObjectToObject(binsparse, "data_types");
45-
46-
if (!matrix.is_iso) {
47-
cJSON_AddStringToObject(data_types, "values",
48-
bsp_get_type_string(matrix.values.type));
49-
} else {
50-
char* base_type_string = bsp_get_type_string(matrix.values.type);
51-
size_t len = strlen(base_type_string) + 6;
52-
char* type_string = (char*) malloc(sizeof(char) * len);
53-
54-
strncpy(type_string, "iso[", len);
55-
strncpy(type_string + 4, base_type_string, len - 4);
56-
strncpy(type_string + len - 2, "]", 2);
57-
58-
cJSON_AddStringToObject(data_types, "values", type_string);
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
5912

60-
free(type_string);
61-
}
13+
// TODO: make cJSON optional.
6214

63-
if (matrix.indices_0.data != NULL) {
64-
cJSON_AddStringToObject(data_types, "indices_0",
65-
bsp_get_type_string(matrix.indices_0.type));
66-
}
67-
68-
if (matrix.indices_1.data != NULL) {
69-
cJSON_AddStringToObject(data_types, "indices_1",
70-
bsp_get_type_string(matrix.indices_1.type));
71-
}
72-
73-
if (matrix.pointers_to_1.data != NULL) {
74-
cJSON_AddStringToObject(data_types, "pointers_to_1",
75-
bsp_get_type_string(matrix.pointers_to_1.type));
76-
}
77-
78-
if (matrix.structure != BSP_GENERAL) {
79-
cJSON_AddStringToObject(binsparse, "structure",
80-
bsp_get_structure_string(matrix.structure));
81-
}
82-
83-
char* string = cJSON_Print(j);
84-
85-
cJSON_Delete(j);
15+
#include <binsparse/matrix.h>
16+
#include <cJSON/cJSON.h>
8617

87-
return string;
88-
}
18+
#ifdef BSP_USE_HDF5
19+
#include <hdf5.h>
8920

9021
int bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix, cJSON* user_json,
91-
int compression_level) {
92-
int result =
93-
bsp_write_array(f, (char*) "values", matrix.values, compression_level);
94-
95-
if (result != 0)
96-
return result;
97-
98-
if (matrix.indices_0.size > 0) {
99-
result = bsp_write_array(f, (char*) "indices_0", matrix.indices_0,
100-
compression_level);
101-
if (result != 0) {
102-
return result;
103-
}
104-
}
105-
106-
if (matrix.indices_1.size > 0) {
107-
result = bsp_write_array(f, (char*) "indices_1", matrix.indices_1,
108-
compression_level);
109-
if (result != 0) {
110-
return result;
111-
}
112-
}
113-
114-
if (matrix.pointers_to_1.size > 0) {
115-
result = bsp_write_array(f, (char*) "pointers_to_1", matrix.pointers_to_1,
116-
compression_level);
117-
if (result != 0) {
118-
return result;
119-
}
120-
}
121-
122-
char* json_string = bsp_generate_json(matrix, user_json);
123-
124-
bsp_write_attribute(f, (char*) "binsparse", json_string);
125-
free(json_string);
126-
127-
return 0;
128-
}
22+
int compression_level);
23+
#endif
12924

13025
int bsp_write_matrix(const char* fname, bsp_matrix_t matrix, const char* group,
131-
cJSON* user_json, int compression_level) {
132-
if (group == NULL) {
133-
hid_t f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
134-
bsp_write_matrix_to_group(f, matrix, user_json, compression_level);
135-
H5Fclose(f);
136-
} else {
137-
hid_t f;
138-
if (access(fname, F_OK) == 0) {
139-
f = H5Fopen(fname, H5F_ACC_RDWR, H5P_DEFAULT);
140-
} else {
141-
f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
142-
}
143-
hid_t g = H5Gcreate1(f, group, H5P_DEFAULT);
144-
bsp_write_matrix_to_group(g, matrix, user_json, compression_level);
145-
H5Gclose(g);
146-
H5Fclose(f);
147-
}
148-
return 0;
26+
cJSON* user_json, int compression_level);
27+
28+
#ifdef __cplusplus
14929
}
30+
#endif

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
target_sources(binsparse-rc PRIVATE
66
src/read_matrix.c
7+
src/write_matrix.c
78
)

src/write_matrix.c

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Binsparse Developers
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <assert.h>
8+
#include <binsparse/binsparse.h>
9+
#include <binsparse/matrix.h>
10+
#include <cJSON/cJSON.h>
11+
#include <unistd.h>
12+
13+
char* bsp_generate_json(bsp_matrix_t matrix, cJSON* user_json) {
14+
cJSON* j = cJSON_CreateObject();
15+
assert(j != NULL);
16+
17+
cJSON* binsparse = cJSON_CreateObject();
18+
19+
assert(binsparse != NULL);
20+
21+
cJSON_AddItemToObject(j, "binsparse", binsparse);
22+
23+
cJSON* item;
24+
cJSON_ArrayForEach(item, user_json) {
25+
cJSON_AddItemToObject(j, item->string, item);
26+
}
27+
28+
cJSON_AddStringToObject(binsparse, "version", BINSPARSE_VERSION);
29+
30+
cJSON_AddStringToObject(binsparse, "format",
31+
bsp_get_matrix_format_string(matrix.format));
32+
33+
cJSON* shape = cJSON_AddArrayToObject(binsparse, "shape");
34+
35+
cJSON* nrows = cJSON_CreateNumber(matrix.nrows);
36+
cJSON* ncols = cJSON_CreateNumber(matrix.ncols);
37+
38+
cJSON_AddItemToArray(shape, nrows);
39+
cJSON_AddItemToArray(shape, ncols);
40+
41+
cJSON_AddNumberToObject(binsparse, "number_of_stored_values", matrix.nnz);
42+
43+
cJSON* data_types = cJSON_AddObjectToObject(binsparse, "data_types");
44+
45+
if (!matrix.is_iso) {
46+
cJSON_AddStringToObject(data_types, "values",
47+
bsp_get_type_string(matrix.values.type));
48+
} else {
49+
char* base_type_string = bsp_get_type_string(matrix.values.type);
50+
size_t len = strlen(base_type_string) + 6;
51+
char* type_string = (char*) malloc(sizeof(char) * len);
52+
53+
strncpy(type_string, "iso[", len);
54+
strncpy(type_string + 4, base_type_string, len - 4);
55+
strncpy(type_string + len - 2, "]", 2);
56+
57+
cJSON_AddStringToObject(data_types, "values", type_string);
58+
59+
free(type_string);
60+
}
61+
62+
if (matrix.indices_0.data != NULL) {
63+
cJSON_AddStringToObject(data_types, "indices_0",
64+
bsp_get_type_string(matrix.indices_0.type));
65+
}
66+
67+
if (matrix.indices_1.data != NULL) {
68+
cJSON_AddStringToObject(data_types, "indices_1",
69+
bsp_get_type_string(matrix.indices_1.type));
70+
}
71+
72+
if (matrix.pointers_to_1.data != NULL) {
73+
cJSON_AddStringToObject(data_types, "pointers_to_1",
74+
bsp_get_type_string(matrix.pointers_to_1.type));
75+
}
76+
77+
if (matrix.structure != BSP_GENERAL) {
78+
cJSON_AddStringToObject(binsparse, "structure",
79+
bsp_get_structure_string(matrix.structure));
80+
}
81+
82+
char* string = cJSON_Print(j);
83+
84+
cJSON_Delete(j);
85+
86+
return string;
87+
}
88+
89+
int bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix, cJSON* user_json,
90+
int compression_level) {
91+
int result =
92+
bsp_write_array(f, (char*) "values", matrix.values, compression_level);
93+
94+
if (result != 0)
95+
return result;
96+
97+
if (matrix.indices_0.size > 0) {
98+
result = bsp_write_array(f, (char*) "indices_0", matrix.indices_0,
99+
compression_level);
100+
if (result != 0) {
101+
return result;
102+
}
103+
}
104+
105+
if (matrix.indices_1.size > 0) {
106+
result = bsp_write_array(f, (char*) "indices_1", matrix.indices_1,
107+
compression_level);
108+
if (result != 0) {
109+
return result;
110+
}
111+
}
112+
113+
if (matrix.pointers_to_1.size > 0) {
114+
result = bsp_write_array(f, (char*) "pointers_to_1", matrix.pointers_to_1,
115+
compression_level);
116+
if (result != 0) {
117+
return result;
118+
}
119+
}
120+
121+
char* json_string = bsp_generate_json(matrix, user_json);
122+
123+
bsp_write_attribute(f, (char*) "binsparse", json_string);
124+
free(json_string);
125+
126+
return 0;
127+
}
128+
129+
int bsp_write_matrix(const char* fname, bsp_matrix_t matrix, const char* group,
130+
cJSON* user_json, int compression_level) {
131+
if (group == NULL) {
132+
hid_t f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
133+
bsp_write_matrix_to_group(f, matrix, user_json, compression_level);
134+
H5Fclose(f);
135+
} else {
136+
hid_t f;
137+
if (access(fname, F_OK) == 0) {
138+
f = H5Fopen(fname, H5F_ACC_RDWR, H5P_DEFAULT);
139+
} else {
140+
f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
141+
}
142+
hid_t g = H5Gcreate1(f, group, H5P_DEFAULT);
143+
bsp_write_matrix_to_group(g, matrix, user_json, compression_level);
144+
H5Gclose(g);
145+
H5Fclose(f);
146+
}
147+
return 0;
148+
}

0 commit comments

Comments
 (0)