-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspndio.c
32 lines (31 loc) · 858 Bytes
/
spndio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "spndarray.h"
#include <stdio.h>
/*
* spndarray_fwrite()
*
* writes the sparse array to the given file,
* applying the given format to the elements
*
* Inputs
* fmt - element format
* filepath - file to save to
* full - whether to save only nonzero elements (1) or all the elements (0)
*/
int spndarray_fwrite(const spndarray* m, const char* fmt, const char* filepath, const int full) {
FILE* fp = fopen(filepath, "w+");
if (!fmt) fmt = "%f,\n";
if (!full) {
size_t ndim = m->ndim;
for (size_t s = 0; s < m->nz; s++) {
fprintf(fp, "[");
for (size_t j = 0; j < ndim; j++)
fprintf(fp, (ndim-1 == j ? "%ld] = " : "%ld,"), m->dims[j][s]);
fprintf(fp, fmt, m->data[s]);
}
fclose(fp);
return 0;
} else {
fprintf(stderr, "nonsparse saving not implemented yet\n");
return 1;
}
}