Skip to content
Snippets Groups Projects
Commit 9f9276e5 authored by dario.genga's avatar dario.genga
Browse files

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.
parent 916b9a01
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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
......@@ -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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment