diff --git a/doc/kmeans.md b/doc/kmeans.md index 7efdcf92730fa4e97103f9159e6bb6c8caf8ddb5..6ee769f5e7665eaf6c7a9b80db280d78766f8fae 100644 --- a/doc/kmeans.md +++ b/doc/kmeans.md @@ -23,12 +23,12 @@ header-includes: - "common.h" contient les définitions de `int_t` et `fpt_t` ```c -typedef struct vector_int_t_ { +typedef struct vector_int { size_t dim; int_t* data; } vector_int_t; -typedef struct vector_fpt_t_ { +typedef struct vector_fpt { size_t dim; fpt_t* data; } vector_fpt_t; @@ -55,10 +55,10 @@ typedef vector_int_t* cluster_int_t; ## Point de Cluster ```c -typedef struct cluster_point_int { +typedef struct point_int { const vector_int_t* vector; cluster_int_t* cluster; -} cluster_point_int_t; +} point_int_t; ``` ## Ensemble de Points @@ -69,7 +69,7 @@ typedef struct cluster_point_int { ```c typedef struct ll_point_int_node { - const cluster_point_int_t* point; + const point_int_t* point; struct ll_point_int_node* next; } ll_point_int_node_t; diff --git a/src/cluster.c b/src/cluster.c index 42e6abbd187e8586273058a72654d964850f7ba6..9898f8b82b82521138534d9ca04508acea9f2508 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -7,16 +7,33 @@ #include "vector.h" -cluster_point_int_t* cluster_point_int_create(vector_int_t* vector) { - cluster_point_int_t* elem = malloc(sizeof(cluster_point_int_t)); - if (NULL == elem) return NULL; - elem->vector = vector; - elem->cluster = NULL; +point_int_t* point_int_create(vector_int_t* vector) { + point_int_t* point = malloc(sizeof(point_int_t)); + if (NULL == point) return NULL; + point->vector = vector; + point->cluster = NULL; + return point; } -void cluster_point_int_destroy(cluster_point_int_t* cp) { +point_fpt_t* point_fpt_create(vector_fpt_t* vector) { + point_fpt_t* point = malloc(sizeof(point_fpt_t)); + if (NULL == point) return NULL; + point->vector = vector; + point->cluster = NULL; + return point; +} + + +void point_int_destroy(point_int_t* cp) { if (NULL == cp) return; vector_int_destroy(cp->vector); free(cp); } + + +void point_fpt_destroy(point_fpt_t* cp) { + if (NULL == cp) return; + vector_fpt_destroy(cp->vector); + free(cp); +} diff --git a/src/cluster.h b/src/cluster.h index 20085a7d10687c8fbfc10802e50de22e2cc98796..f329132d5ff37c335dc6bc722d263675bef9be81 100644 --- a/src/cluster.h +++ b/src/cluster.h @@ -2,22 +2,34 @@ // by Boris Stefanovic on 01/06/22 // +// cluster id inside point struct is justified by "many-to-one" relationship and several passes over all points + #ifndef PROG_KMEANS_CLUSTER_H #define PROG_KMEANS_CLUSTER_H #include "vector.h" -typedef vector_int_t* cluster_int_t; // a cluster may be represented by its center - -typedef struct cluster_point_int { +typedef struct point_int { vector_int_t* vector; - cluster_int_t cluster; // justified by "many-to-one" relationship and several passes over all points -} cluster_point_int_t; + vector_int_t* cluster; +} point_int_t; + + +typedef struct point_fpt { + vector_fpt_t* vector; + vector_fpt_t* cluster; +} point_fpt_t; + + +point_int_t* point_int_create(vector_int_t* vector); + +point_fpt_t* point_fpt_create(vector_fpt_t* vector); + -cluster_point_int_t* cluster_point_int_create(vector_int_t* vector); +void point_int_destroy(point_int_t* cp); -void cluster_point_int_destroy(cluster_point_int_t* cp); +void point_fpt_destroy(point_fpt_t* cp); #endif //PROG_KMEANS_CLUSTER_H diff --git a/src/kmeans.h b/src/kmeans.h index a0d4ca35a2f2ec1e86112632c4ca45770192f77e..377fc541a0b556408c2e44dd0c54a2515b309d1a 100644 --- a/src/kmeans.h +++ b/src/kmeans.h @@ -9,10 +9,10 @@ #include "linkedlist.h" -cluster_int_t* kmeans_init_clusters_int(const cluster_point_int_t** points, const size_t point_count, const size_t nclusters); +cluster_int_t* kmeans_init_clusters_int(const point_int_t** points, const size_t point_count, const size_t nclusters); void kmeans_int( - cluster_point_int_t** points, const size_t point_count, + point_int_t** points, const size_t point_count, cluster_int_t* clusters, const size_t nb_clusters, fpt_t (* distance_function)(const vector_fpt_t*, const vector_fpt_t*)); diff --git a/src/linkedlist.h b/src/linkedlist.h index 8f4250d38e65792ab175fbe107303fa0799ffdb8..fbb3dedec5093698718e682d63f2ccd2e7c8e078 100644 --- a/src/linkedlist.h +++ b/src/linkedlist.h @@ -11,7 +11,7 @@ typedef struct ll_point_int_node { - cluster_point_int_t* point; + point_int_t* point; struct ll_point_int_node* next; } ll_point_int_node_t; @@ -31,7 +31,7 @@ void ll_point_int_destroy(ll_point_int_t* list, const bool full); void ll_point_int_append(ll_point_int_t* list, vector_int_t* vector); -cluster_point_int_t** ll_point_int_to_array(const ll_point_int_t* list, size_t* size_ptr); +point_int_t** ll_point_int_to_array(const ll_point_int_t* list, size_t* size_ptr); #endif //PROG_KMEANS_LINKEDLIST_H diff --git a/src/main.c b/src/main.c index 754cdc71911bbcdff0f9c19cfd1609c0398736bc..b5bef704d3b6b4a274212f4ff99531d5918f2dfd 100644 --- a/src/main.c +++ b/src/main.c @@ -77,7 +77,7 @@ int main(int argc, char** argv) { } ll_point_int_t* list = get_vector_list_int(ifile, dim); size_t count; - const cluster_point_int_t** points = ll_point_int_to_array(list, &count); + const point_int_t** points = ll_point_int_to_array(list, &count); ll_point_int_destroy(list, false); list = NULL; // ALGORITHM diff --git a/src/vector.c b/src/vector.c index 46e57464f4ace8494c63055f830cad90262559c0..085a10afabd13add7ca5b9b37c584e77dbe28bc3 100644 --- a/src/vector.c +++ b/src/vector.c @@ -32,7 +32,7 @@ void vector_int_destroy(vector_int_t* vp) { } -void vector_fpt_destroy(vector_int_t* vp) { +void vector_fpt_destroy(vector_fpt_t* vp) { if (NULL == vp) return; free(vp->data); free(vp); @@ -50,6 +50,17 @@ vector_int_t* vector_int_copy(const vector_int_t* v) { } +vector_fpt_t* vector_fpt_copy(const vector_fpt_t* v) { + if (NULL == v) return NULL; + vector_fpt_t* c = vector_fpt_create(v->dim); + if (NULL == c) return NULL; + for (size_t i = 0; i < v->dim; ++i) { + c->data[i] = v->data[i]; + } + return c; +} + + bool vector_int_equals(const vector_int_t* v1, const vector_int_t* v2) { if (v1->dim != v2->dim) return false; for (size_t i = 0; i < v1->dim; ++i) { diff --git a/src/vector.h b/src/vector.h index 6b8f665698ea28aa488877e6ba460d6dd99eef1c..2d2db1a119fa3c8e47c1793f5d5fafe194fd1814 100644 --- a/src/vector.h +++ b/src/vector.h @@ -15,28 +15,33 @@ #include "common.h" -typedef struct vector_int_t_ { +typedef struct vector_int { size_t dim; int_t* data; } vector_int_t; -typedef struct vector_fpt_t_ { + +typedef struct vector_fpt { size_t dim; fpt_t* data; } vector_fpt_t; -vector_int_t* vector_int_create(const size_t dim); -vector_fpt_t* vector_fpt_create(const size_t dim); +vector_int_t* vector_int_create(const size_t dim); void vector_int_destroy(vector_int_t* vp); -void vector_fpt_destroy(vector_int_t* vp); - vector_int_t* vector_int_copy(const vector_int_t* v); bool vector_int_equals(const vector_int_t* v1, const vector_int_t* v2); + +vector_fpt_t* vector_fpt_create(const size_t dim); + +void vector_fpt_destroy(vector_fpt_t* vp); + +vector_fpt_t* vector_fpt_copy(const vector_fpt_t* v); + bool vector_fpt_equals(const vector_fpt_t* v1, const vector_fpt_t* v2);