diff --git a/kmeans.c b/kmeans.c index a5885bcfa5291f0ec7701122682d7eb987ab738d..80f22599a333aa5b63ba7511e552a70be2cd6acc 100644 --- a/kmeans.c +++ b/kmeans.c @@ -2,3 +2,94 @@ // Author : Dario GENGA #include "kmeans.h" + +kmeans* kmeans_create(int k, point** data, int nb_points) { + kmeans* universe = malloc(sizeof(kmeans)); + universe->points_array = data; + universe->nb_points = nb_points; + universe->k = k; + universe->clusters_array = malloc(sizeof(cluster) * k); + + for (int i = 0; i < k; i++) { + universe->clusters_array[i] = cluster_create(NULL); + } + + return universe; +} + +point* point_create(double* value) { + point* p = malloc(sizeof(point)); + p->value = value; + p->cluster = NULL; + p->label = NULL; + return p; +} + +cluster* cluster_create(point* centroid) { + cluster* c = malloc(sizeof(cluster)); + c->centroid = centroid; + return c; +} + +void init_from_cmd_arguments(kmeans *universe) { + +} + +void read_data_source(kmeans *universe, char* source_file) { + +} + +void write_data_output(kmeans *universe, char* output_file) { + +} + +void init_clusters(kmeans *universe) { + +} + +double compute_distance(point* p1, point p2) { + return 0; +} + +void compute_center_of_gravity(cluster* clstr, kmeans* universe) { + +} + +void assign_points_to_cluster(point* p, kmeans* universe) { + +} + +void start_clustering(kmeans* universe) { + +} + +void destroy_point(point* p) { + if (p != NULL) { + if (p->label != NULL) + free(p->label); + + free(p); + } +} + +void destroy_cluster(cluster* clstr) { + if (clstr != NULL) { + destroy_point(clstr->centroid); + free(clstr); + } +} + +void destroy_universe(kmeans* kmeans) { + if (kmeans != NULL) { + for (int i = 0; i < kmeans->nb_points; i++) { + destroy_point(kmeans->points_array[i]); + } + for (int i = 0; i < kmeans->k; i++) { + destroy_cluster(kmeans->clusters_array[i]); + } + + free(kmeans->points_array); + free(kmeans->clusters_array); + free(kmeans); + } +} \ No newline at end of file diff --git a/kmeans.h b/kmeans.h index df7b8bce52179e86253bd138a8e5940ad8606a12..498de3dbef5e402a6646a6f7284100b1da6d9c3e 100644 --- a/kmeans.h +++ b/kmeans.h @@ -34,7 +34,7 @@ typedef struct _kmeans { /// The number of clusters in the universe. int k; /// A 'k' size array of clusters, who contains the data. - struct _cluster* clusters_array; + struct _cluster** clusters_array; /// An array of points, representing the data in the universe. struct _point** points_array; /// The number of points in the universe. @@ -46,17 +46,17 @@ typedef struct _kmeans { /// \param data The array of points /// \param nb_points The number of points in the universe. /// \return The kmeans object of the universe. -kmeans kmeans_create(int k, point** data, int nb_points); +kmeans* kmeans_create(int k, point** data, int nb_points); /// Create a point. /// \param value The coordinates of the point. /// \return The point objet initialized with its coordinates -point point_create(double* value); +point* point_create(double* value); /// Create a cluster. /// \param centroid The point representing the center of gravity of the cluster. /// \return The cluster object. -kmeans cluster_create(point centroid); +cluster* cluster_create(point* centroid); /// Initialize the universe with the file specified in argument when starting the program. If no arguments has been /// specified, use the standard input instead. @@ -97,16 +97,16 @@ void assign_points_to_cluster(point* p, kmeans* universe); /// \param universe The kmeans universe. void start_clustering(kmeans* universe); -/// Free the memory of the cluster. -/// \param clstr The cluster to free. -void destroy_cluster(cluster* clstr); - /// Free the memory of the point. /// \param p The point to free. void destroy_point(point* p); +/// Free the memory of the cluster. +/// \param clstr The cluster to free. +void destroy_cluster(cluster* clstr); + /// Free the memory of universe. /// \param kmeans The universe to free. -void destroy_universe(kmeans kmeans); +void destroy_universe(kmeans* kmeans); #endif diff --git a/main.c b/main.c index 90224f3a4abbb041d524829453a57acb0622149b..c586a14e27a829bcb2550d6d20e522d036ed3ecc 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,18 @@ #include "kmeans.h" int main() { + // The code below verify the create and destroy methods. + double d = 5.5; + double b = 3.3; + point* gravity = point_create(&b); + point* p = point_create(&d); + cluster* c = cluster_create(gravity); + point** points = malloc(sizeof(points) * 1); + points[0] = p; + kmeans* universe = kmeans_create(3, points, 1); + + destroy_cluster(c); + destroy_universe(universe); return EXIT_SUCCESS; }