From 9f9276e54e93569ae5e9f1a2fd33464859e1d878 Mon Sep 17 00:00:00 2001
From: "dario.genga" <dario.genga@etu.hesge.ch>
Date: Tue, 24 May 2022 16:25:03 +0200
Subject: [PATCH] Add create and destroy methods

In kmeans.h :
    - Fix some missing pointers.
    - Swap the destroy_point and destroy_cluster.

In kmeans.c :
    - Add allocation and free for kmeans, point and cluster create
    methods.
    - Add the signature of the other methods.

In main.c :
    - Add some code to test the allocations.
---
 kmeans.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 kmeans.h | 18 +++++------
 main.c   | 12 ++++++++
 3 files changed, 112 insertions(+), 9 deletions(-)

diff --git a/kmeans.c b/kmeans.c
index a5885bc..80f2259 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 df7b8bc..498de3d 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 90224f3..c586a14 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;
 }
-- 
GitLab