aboutsummaryrefslogtreecommitdiff
path: root/src/km-set-print.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-02-05 00:50:28 -0500
committerPaul Duncan <pabs@pablotron.org>2019-02-05 00:50:28 -0500
commite63a88ea567744d890d923d1d7aa46e583cbb0ec (patch)
treecc76b02e39dea66cd8a9c92e77dede1ea07ced4a /src/km-set-print.c
parentf2b6c7ea5884b65ef93ab999e4edf460ec86a1d1 (diff)
downloadkmeans-e63a88ea567744d890d923d1d7aa46e583cbb0ec.tar.bz2
kmeans-e63a88ea567744d890d923d1d7aa46e583cbb0ec.zip
add km_set_print(), refactor main.c
Diffstat (limited to 'src/km-set-print.c')
-rw-r--r--src/km-set-print.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/km-set-print.c b/src/km-set-print.c
new file mode 100644
index 0000000..aa93f96
--- /dev/null
+++ b/src/km-set-print.c
@@ -0,0 +1,41 @@
+#include <stdbool.h> // bool
+#include <stdio.h> // fprintf()
+#include "util.h"
+#include "km.h"
+
+bool
+km_set_print(
+ const km_set_t * const set,
+ FILE * const fh
+) {
+ // print shape
+ fprintf(fh, "%zu %zu\n", set->shape.num_floats, set->shape.num_ints);
+
+ // print rows
+ for (size_t i = 0; i < set->num_rows; i++) {
+ if (set->shape.num_floats > 0) {
+ const float * const vals = km_set_get_row(set, i);
+
+ // print floats
+ for (size_t j = 0; j < set->shape.num_floats; j++) {
+ fprintf(fh, "%s%f", (j > 0) ? " ": "", vals[j]);
+ }
+ }
+
+ // print ints
+ if (set->shape.num_ints > 0) {
+ const int * const vals = km_set_get_row_ints(set, i);
+
+ for (size_t j = 0; j < set->shape.num_ints; j++) {
+ const bool need_space = (set->shape.num_floats > 0) || (j > 0);
+ fprintf(fh, "%s%d", need_space ? " ": "", vals[j]);
+ }
+ }
+
+ // end row
+ fprintf(fh, "\n");
+ }
+
+ // return success
+ return true;
+}