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

EDIT: main cleanup

parent b440d343
No related branches found
No related tags found
No related merge requests found
...@@ -5,14 +5,14 @@ ...@@ -5,14 +5,14 @@
#include "vector.h" #include "vector.h"
int_t read_int(FILE* file) { int_t io_read_int(FILE* file) {
char* line; char* line;
size_t len; size_t len;
getline(&line, &len, file); getline(&line, &len, file);
return strtol(line, NULL, 10); return strtol(line, NULL, 10);
} }
fpt_t read_fpt(FILE* file) { fpt_t io_read_fpt(FILE* file) {
char* line; char* line;
size_t len; size_t len;
getline(&line, &len, file); getline(&line, &len, file);
...@@ -20,7 +20,7 @@ fpt_t read_fpt(FILE* file) { ...@@ -20,7 +20,7 @@ fpt_t read_fpt(FILE* file) {
} }
vector_int_t* line_to_vector_int(char* line, const size_t dim) { vector_int_t* io_line_to_vector_int(char* line, const size_t dim) {
vector_int_t* vector = vector_create_int(dim); vector_int_t* vector = vector_create_int(dim);
char* tgt = line; char* tgt = line;
char* token = NULL; char* token = NULL;
...@@ -32,7 +32,7 @@ vector_int_t* line_to_vector_int(char* line, const size_t dim) { ...@@ -32,7 +32,7 @@ vector_int_t* line_to_vector_int(char* line, const size_t dim) {
return vector; return vector;
} }
vector_fpt_t* line_to_vector_fpt(char* line, const size_t dim) { vector_fpt_t* io_line_to_vector_fpt(char* line, const size_t dim) {
vector_fpt_t* vector = vector_create_fpt(dim); vector_fpt_t* vector = vector_create_fpt(dim);
char* tgt = line; char* tgt = line;
char* token = NULL; char* token = NULL;
...@@ -45,13 +45,13 @@ vector_fpt_t* line_to_vector_fpt(char* line, const size_t dim) { ...@@ -45,13 +45,13 @@ vector_fpt_t* line_to_vector_fpt(char* line, const size_t dim) {
} }
list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim) { list_points_int_t* io_get_vector_list_int(FILE* ifile, const size_t dim) {
list_points_int_t* list = list_points_create_int(); list_points_int_t* list = list_points_create_int();
char* line = NULL; char* line = NULL;
size_t len = 0; size_t len = 0;
while (getline(&line, &len, ifile) != -1) { while (getline(&line, &len, ifile) != -1) {
if (len != 0) { if (len != 0) {
vector_int_t* vector = line_to_vector_int(line, dim); vector_int_t* vector = io_line_to_vector_int(line, dim);
list_points_append_int(list, vector); list_points_append_int(list, vector);
free(line); free(line);
} }
...@@ -59,13 +59,13 @@ list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim) { ...@@ -59,13 +59,13 @@ list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim) {
return list; return list;
} }
list_points_fpt_t* get_vector_list_fpt(FILE* ifile, const size_t dim) { list_points_fpt_t* io_get_vector_list_fpt(FILE* ifile, const size_t dim) {
list_points_fpt_t* list = list_points_create_fpt(); list_points_fpt_t* list = list_points_create_fpt();
char* line = NULL; char* line = NULL;
size_t len = 0; size_t len = 0;
while (getline(&line, &len, ifile) != -1) { while (getline(&line, &len, ifile) != -1) {
if (len != 0) { if (len != 0) {
vector_fpt_t* vector = line_to_vector_fpt(line, dim); vector_fpt_t* vector = io_line_to_vector_fpt(line, dim);
list_points_append_fpt(list, vector); list_points_append_fpt(list, vector);
free(line); free(line);
} }
......
...@@ -8,19 +8,19 @@ ...@@ -8,19 +8,19 @@
#include "vector.h" #include "vector.h"
int_t read_int(FILE* file); int_t io_read_int(FILE* file);
fpt_t read_fpt(FILE* file); fpt_t io_read_fpt(FILE* file);
vector_int_t* line_to_vector_int(char* line, const size_t dim); vector_int_t* io_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* io_line_to_vector_fpt(char* line, const size_t dim);
list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim); list_points_int_t* io_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* io_get_vector_list_fpt(FILE* ifile, const size_t dim);
void io_write_clusters_to_file_int(FILE* file, cluster_int_t** clusters, const size_t cluster_count); void io_write_clusters_to_file_int(FILE* file, cluster_int_t** clusters, const size_t cluster_count);
......
...@@ -28,21 +28,27 @@ bool init(int argc, char** argv, char** ipath, char** opath) { ...@@ -28,21 +28,27 @@ bool init(int argc, char** argv, char** ipath, char** opath) {
fprintf(stderr, "IFILE: [ %s ] file does not exist !", *ipath); fprintf(stderr, "IFILE: [ %s ] file does not exist !", *ipath);
return false; return false;
} }
} else {
*ipath = NULL;
} }
if (argc > 2) *opath = argv[2]; *opath = argc > 2 ? argv[2] : NULL;
return true;
}
void read_points_int(const char* ipath) {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
// INIT // INIT
char* ipath = NULL; char (* ipath), (* opath);
char* opath = NULL; if (!init(argc, argv, &ipath, &opath)) return EXIT_FAILURE;
if (!init(argc, argv, &ipath, &opath))
return EXIT_FAILURE;
// READ // READ
FILE* ifile = ipath != NULL ? fopen(ipath, "r") : stdin; FILE* ifile = ipath != NULL ? fopen(ipath, "r") : stdin;
const size_t dim = read_int(ifile); const size_t dim = io_read_int(ifile);
const size_t nb_clusters = read_int(ifile); const size_t nb_clusters = io_read_int(ifile);
if (0 <= dim) { if (0 <= dim) {
printf("DIMENSION MUST BE STRICTLY POSITIVE !\n"); printf("DIMENSION MUST BE STRICTLY POSITIVE !\n");
return EXIT_FAILURE; return EXIT_FAILURE;
...@@ -51,20 +57,20 @@ int main(int argc, char** argv) { ...@@ -51,20 +57,20 @@ int main(int argc, char** argv) {
printf("NUMBER OF CLUSTERS MUST BE STRICTLY POSITIVE !\n"); printf("NUMBER OF CLUSTERS MUST BE STRICTLY POSITIVE !\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
list_points_int_t* list = get_vector_list_int(ifile, dim); list_points_int_t* list = io_get_vector_list_int(ifile, dim);
fclose(ifile); fclose(ifile);
ifile = NULL; ifile = NULL;
const size_t point_count = list->size; const size_t point_count = list->size;
point_int_t** points = list_points_to_array_int(list); vector_int_t** points = list_points_to_array_int(list);
list_points_destroy_int(list, false); list_points_destroy_int(list, false);
list = NULL; list = NULL;
// ALGORITHM // ALGORITHM
vector_int_t** clusters = kmeans_init_clusters_int((const point_int_t**) points, point_count, nb_clusters); cluster_int_t** clusters = kmeans_init_clusters_int((const vector_int_t**) points, point_count, nb_clusters);
kmeans_int(points, point_count, clusters, nb_clusters, distance_euclid_int); //TODO: choose dist func with command line kmeans_int(points, point_count, clusters, nb_clusters, distance_euclid_int); //TODO: choose dist func with command line
// WRITE // WRITE
FILE* ofile = opath != NULL ? fopen(opath, "w") : stdout; FILE* ofile = opath != NULL ? fopen(opath, "w") : stdout;
fprintf(ofile, "%lud\n%lud\n", dim, nb_clusters); fprintf(ofile, "%lud\n%lud\n", dim, nb_clusters);
io_write_clusters_to_file_int(ofile, points, point_count); io_write_clusters_to_file_int(ofile, clusters, point_count);
fclose(ofile); fclose(ofile);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment