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 @@
#include "vector.h"
int_t read_int(FILE* file) {
int_t io_read_int(FILE* file) {
char* line;
size_t len;
getline(&line, &len, file);
return strtol(line, NULL, 10);
}
fpt_t read_fpt(FILE* file) {
fpt_t io_read_fpt(FILE* file) {
char* line;
size_t len;
getline(&line, &len, 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);
char* tgt = line;
char* token = NULL;
......@@ -32,7 +32,7 @@ vector_int_t* line_to_vector_int(char* line, const size_t dim) {
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);
char* tgt = line;
char* token = NULL;
......@@ -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();
char* line = NULL;
size_t len = 0;
while (getline(&line, &len, ifile) != -1) {
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);
free(line);
}
......@@ -59,13 +59,13 @@ list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim) {
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();
char* line = NULL;
size_t len = 0;
while (getline(&line, &len, ifile) != -1) {
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);
free(line);
}
......
......@@ -8,19 +8,19 @@
#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);
......
......@@ -28,21 +28,27 @@ bool init(int argc, char** argv, char** ipath, char** opath) {
fprintf(stderr, "IFILE: [ %s ] file does not exist !", *ipath);
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) {
// INIT
char* ipath = NULL;
char* opath = NULL;
if (!init(argc, argv, &ipath, &opath))
return EXIT_FAILURE;
char (* ipath), (* opath);
if (!init(argc, argv, &ipath, &opath)) return EXIT_FAILURE;
// READ
FILE* ifile = ipath != NULL ? fopen(ipath, "r") : stdin;
const size_t dim = read_int(ifile);
const size_t nb_clusters = read_int(ifile);
const size_t dim = io_read_int(ifile);
const size_t nb_clusters = io_read_int(ifile);
if (0 <= dim) {
printf("DIMENSION MUST BE STRICTLY POSITIVE !\n");
return EXIT_FAILURE;
......@@ -51,20 +57,20 @@ int main(int argc, char** argv) {
printf("NUMBER OF CLUSTERS MUST BE STRICTLY POSITIVE !\n");
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);
ifile = NULL;
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 = NULL;
// 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
// WRITE
FILE* ofile = opath != NULL ? fopen(opath, "w") : stdout;
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);
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment