Skip to content
Snippets Groups Projects
Commit b440d343 authored by Boris Stefanovic's avatar Boris Stefanovic
Browse files

ADD: write clusters to file

parent 35d53fa5
No related branches found
No related tags found
No related merge requests found
...@@ -21,10 +21,10 @@ fpt_t read_fpt(FILE* file) { ...@@ -21,10 +21,10 @@ fpt_t read_fpt(FILE* file) {
vector_int_t* line_to_vector_int(char* line, const size_t dim) { vector_int_t* line_to_vector_int(char* line, const size_t dim) {
vector_int_t* vector = vector_int_create(dim); vector_int_t* vector = vector_create_int(dim);
char* tgt = line; char* tgt = line;
char* token = NULL; char* token = NULL;
for (size_t i = 0; i < vector->dim; ++i, tgt = NULL) { for (size_t i = 0; i < dim; ++i, tgt = NULL) {
token = strtok(tgt, ","); token = strtok(tgt, ",");
// strtol returns 0 if number not read, which is the desired behaviour: // strtol returns 0 if number not read, which is the desired behaviour:
vector->data[i] = token != NULL ? strtol(token, NULL, 10) : 0; vector->data[i] = token != NULL ? strtol(token, NULL, 10) : 0;
...@@ -33,10 +33,10 @@ vector_int_t* line_to_vector_int(char* line, const size_t dim) { ...@@ -33,10 +33,10 @@ vector_int_t* line_to_vector_int(char* line, const size_t dim) {
} }
vector_fpt_t* line_to_vector_fpt(char* line, const size_t dim) { vector_fpt_t* line_to_vector_fpt(char* line, const size_t dim) {
vector_fpt_t* vector = vector_fpt_create(dim); vector_fpt_t* vector = vector_create_fpt(dim);
char* tgt = line; char* tgt = line;
char* token = NULL; char* token = NULL;
for (size_t i = 0; i < vector->dim; ++i, tgt = NULL) { for (size_t i = 0; i < dim; ++i, tgt = NULL) {
token = strtok(tgt, ","); token = strtok(tgt, ",");
// strtol returns 0 if number not read, which is the desired behaviour: // strtol returns 0 if number not read, which is the desired behaviour:
vector->data[i] = token != NULL ? strtod(token, NULL) : 0; vector->data[i] = token != NULL ? strtod(token, NULL) : 0;
...@@ -74,25 +74,34 @@ list_points_fpt_t* get_vector_list_fpt(FILE* ifile, const size_t dim) { ...@@ -74,25 +74,34 @@ list_points_fpt_t* get_vector_list_fpt(FILE* ifile, const size_t dim) {
} }
static int _point_compare_clusters_int_(const void* p1, const void* p2) { void io_write_clusters_to_file_int(FILE* file, cluster_int_t** clusters, const size_t cluster_count) {
const point_int_t* point1 = (const point_int_t*) p1; for (size_t i = 0; i < cluster_count; ++i) {
const point_int_t* point2 = (const point_int_t*) p2; fprintf(file, "\n*\n");
return point1->cluster < point2->cluster ? -1 : point1->cluster > point2->cluster ? 1 : 0; list_points_node_int_t* node = clusters[i]->points->head;
while (node != NULL) {
const vector_int_t point = *(node->point);
fprintf(file, "%ld", point.data[0]);
for (size_t p = 1; p < point.dim; ++p) {
fprintf(file, " , %ld", point.data[p]);
}
fprintf(file, "\n");
node = node->next;
}
}
} }
void io_write_clusters_to_file_fpt(FILE* file, cluster_fpt_t** clusters, const size_t cluster_count) {
void io_write_clusters_to_file_int(FILE* file, point_int_t** points, const size_t point_count) { for (size_t i = 0; i < cluster_count; ++i) {
qsort(points, point_count, sizeof(point_int_t*), _point_compare_clusters_int_); // group points by cluster fprintf(file, "\n*\n");
vector_int_t* current_cluster = NULL; list_points_node_fpt_t* node = clusters[i]->points->head;
point_int_t* current_point = NULL; while (node != NULL) {
for (size_t i = 0; i < point_count; ++i) { const vector_fpt_t point = *(node->point);
current_point = points[i]; fprintf(file, "%lf", point.data[0]);
if (current_point->cluster != current_cluster) { for (size_t p = 1; p < point.dim; ++p) {
current_cluster = current_point->cluster; fprintf(file, " , %lf", point.data[p]);
fprintf(file, "\n*\n"); }
fprintf(file, "\n");
node = node->next;
} }
vector_print_to_file_int(file, current_point->vector);
} }
} }
void io_write_clusters_to_file_fpt(FILE* file, point_fpt_t** points, const size_t point_count) {}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define PROG_KMEANS_IO_H #define PROG_KMEANS_IO_H
#include <stdio.h> #include <stdio.h>
#include "cluster.h"
#include "common.h" #include "common.h"
#include "linkedlist.h" #include "linkedlist.h"
#include "vector.h" #include "vector.h"
...@@ -22,9 +23,9 @@ list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim); ...@@ -22,9 +23,9 @@ list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim);
list_points_fpt_t* get_vector_list_fpt(FILE* ifile, const size_t dim); list_points_fpt_t* get_vector_list_fpt(FILE* ifile, const size_t dim);
void io_write_clusters_to_file_int(FILE* file, point_int_t** points, const size_t point_count); void io_write_clusters_to_file_int(FILE* file, cluster_int_t** clusters, const size_t cluster_count);
void io_write_clusters_to_file_fpt(FILE* file, point_fpt_t** points, const size_t point_count); void io_write_clusters_to_file_fpt(FILE* file, cluster_fpt_t** clusters, const size_t cluster_count);
#endif //PROG_KMEANS_IO_H #endif //PROG_KMEANS_IO_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment