diff --git a/src/cluster.c b/src/cluster.c index 84e6beb82636b3446668219fb52766ea4177a0c0..939ae5ce1ab29fb8b7ea9a9a2b96cb9e60fa702e 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -32,26 +32,28 @@ void cluster_destroy_fpt(cluster_fpt_t* cluster) { } -void cluster_add_point_int(cluster_int_t* cluster, point_int_t* point) { +void cluster_add_point_int(cluster_int_t* cluster, vector_int_t* point) { //TODO if (NULL == cluster || NULL == point) return; + list_points_append_int(cluster->points, point); if (NULL == cluster->center) { - cluster->center = vector_copy_int(point->vector); + cluster->center = vector_copy_int(point); } else { - vector_int_t* delta = vector_copy_int(point->vector); + vector_int_t* delta = vector_copy_int(point); vector_div_inplace_int(delta, cluster->points->size); vector_add_inplace_int(cluster->center, *delta); vector_destroy_int(delta); } } -void cluster_add_point_fpt(cluster_fpt_t* cluster, point_fpt_t* point) { +void cluster_add_point_fpt(cluster_fpt_t* cluster, vector_fpt_t* point) { //TODO if (NULL == cluster || NULL == point) return; + list_points_append_fpt(cluster->points, point); if (NULL == cluster->center) { - cluster->center = vector_copy_fpt(point->vector); + cluster->center = vector_copy_fpt(point); } else { - vector_fpt_t* delta = vector_copy_fpt(point->vector); + vector_fpt_t* delta = vector_copy_fpt(point); vector_div_inplace_fpt(delta, cluster->points->size); vector_add_inplace_fpt(cluster->center, *delta); vector_destroy_fpt(delta); diff --git a/src/cluster.h b/src/cluster.h index 963e417c27a38e58d9f62304e988c55a65cfbedb..0e2993aff11b27d54429e703540872263be5ddfc 100644 --- a/src/cluster.h +++ b/src/cluster.h @@ -3,7 +3,6 @@ #include <stdlib.h> #include "linkedlist.h" -#include "point.h" #include "vector.h" @@ -28,9 +27,9 @@ void cluster_destroy_int(cluster_int_t* center); void cluster_destroy_fpt(cluster_fpt_t* center); -void cluster_add_point_int(cluster_int_t* cluster, point_int_t* point); +void cluster_add_point_int(cluster_int_t* cluster, vector_int_t* point); -void cluster_add_point_fpt(cluster_fpt_t* cluster, point_fpt_t* point); +void cluster_add_point_fpt(cluster_fpt_t* cluster, vector_fpt_t* point); void cluster_update_center_int(cluster_int_t* cluster); diff --git a/src/kmeans.c b/src/kmeans.c index 55ddccd5e784ec37f171e4c753ca37c60a995b5b..7ee9705c032a4b98853f2402c75204b2d4347f4f 100644 --- a/src/kmeans.c +++ b/src/kmeans.c @@ -1,18 +1,16 @@ #include "kmeans.h" -#include "point.h" -vector_int_t** kmeans_init_clusters_int(const point_int_t** points, const size_t point_count, const size_t nclusters) { - if (nclusters < 2) return NULL; - if (NULL == points) return NULL; - vector_int_t** clusters = calloc(nclusters, sizeof(vector_int_t*)); +cluster_int_t** kmeans_init_clusters_int(const vector_int_t** points, const size_t point_count, const size_t nclusters) { + if (NULL == points || point_count < 2 || nclusters < 2) return NULL; + cluster_int_t** clusters = calloc(nclusters, sizeof(vector_int_t*)); if (NULL == clusters) return NULL; // determine range in which we are working - vector_int_t* min = vector_copy_int(points[0]->vector); - vector_int_t* max = vector_copy_int(points[0]->vector); + vector_int_t* min = vector_copy_int(points[0]); + vector_int_t* max = vector_copy_int(points[0]); for (size_t i = 0; i < point_count; ++i) { for (size_t p = 0; p < max->dim; ++p) { - const int_t value = points[i]->vector->data[p]; + const int_t value = points[i]->data[p]; if (value < min->data[p]) min->data[p] = value; if (value > max->data[p]) max->data[p] = value; } @@ -24,15 +22,17 @@ vector_int_t** kmeans_init_clusters_int(const point_int_t** points, const size_t center->data[p] = rand_int_range(min->data[p], max->data[p]); } // TODO: maybe check center is not already in clusters, although probability is extremely low... - clusters[i] = center; + clusters[i]->center = center; } return clusters; } void kmeans_int( - point_int_t** points, const size_t point_count, - vector_int_t** clusters, const size_t nb_clusters, + vector_int_t** points, const size_t point_count, + cluster_int_t** clusters, const size_t nb_clusters, fpt_t (* distance_function)(const vector_int_t*, const vector_int_t*)) { //TODO + bool changed = true; + while (changed) {} } diff --git a/src/kmeans.h b/src/kmeans.h index c1c5ff7acf9554146de1bbd0305f6f0832c66a48..34a83c8d4df56d52aad598c9e7a201439a90b913 100644 --- a/src/kmeans.h +++ b/src/kmeans.h @@ -1,15 +1,15 @@ #ifndef PROG_KMEANS_KMEANS_H #define PROG_KMEANS_KMEANS_H -#include "point.h" +#include "cluster.h" #include "linkedlist.h" -vector_int_t** kmeans_init_clusters_int(const point_int_t** points, const size_t point_count, const size_t nclusters); +cluster_int_t** kmeans_init_clusters_int(const vector_int_t** points, const size_t point_count, const size_t nclusters); void kmeans_int( - point_int_t** points, const size_t point_count, - vector_int_t** clusters, const size_t nb_clusters, + vector_int_t** points, const size_t point_count, + cluster_int_t** clusters, const size_t nb_clusters, fpt_t (* distance_function)(const vector_int_t*, const vector_int_t*)); diff --git a/src/linkedlist.c b/src/linkedlist.c index 898c7d3c124dbc724476ceed2b502c35e0e1875e..3ea5994b376336d26850bfe34c4bc5a7eae70429 100644 --- a/src/linkedlist.c +++ b/src/linkedlist.c @@ -1,24 +1,21 @@ #include "linkedlist.h" #include <assert.h> #include <stdbool.h> -#include "point.h" -list_points_node_int_t* list_points_node_create_int(vector_int_t* vec) { +list_points_node_int_t* list_points_node_create_int(vector_int_t* point) { + if (NULL == point) return NULL; list_points_node_int_t* node = malloc(sizeof(list_points_node_int_t)); if (NULL == node) return NULL; - point_int_t* point = point_create_int(vec); - if (NULL == point) return NULL; node->point = point; node->next = NULL; return node; } -list_points_node_fpt_t* list_points_node_create_fpt(vector_fpt_t* vec) { +list_points_node_fpt_t* list_points_node_create_fpt(vector_fpt_t* point) { + if (NULL == point) return NULL; list_points_node_fpt_t* node = malloc(sizeof(list_points_node_fpt_t)); if (NULL == node) return NULL; - point_fpt_t* point = point_create_fpt(vec); - if (NULL == point) return NULL; node->point = point; node->next = NULL; return node; @@ -27,20 +24,19 @@ list_points_node_fpt_t* list_points_node_create_fpt(vector_fpt_t* vec) { void list_points_node_destroy_int(list_points_node_int_t* node, const bool full) { if (NULL == node) return; - if (full) point_destroy_int(node->point); + if (full) vector_destroy_int(node->point); free(node); } void list_points_node_destroy_fpt(list_points_node_fpt_t* node, const bool full) { if (NULL == node) return; - if (full) point_destroy_fpt(node->point); + if (full) vector_destroy_fpt(node->point); free(node); } list_points_int_t* list_points_create_int() { - list_points_int_t* list = NULL; - list = malloc(sizeof(list_points_int_t)); + list_points_int_t* list = malloc(sizeof(list_points_int_t)); if (NULL == list) return NULL; list->head = NULL; list->tail = NULL; @@ -49,8 +45,7 @@ list_points_int_t* list_points_create_int() { } list_points_fpt_t* list_points_create_fpt() { - list_points_fpt_t* list = NULL; - list = malloc(sizeof(list_points_fpt_t)); + list_points_fpt_t* list = malloc(sizeof(list_points_fpt_t)); if (NULL == list) return NULL; list->head = NULL; list->tail = NULL; @@ -80,9 +75,9 @@ void list_points_destroy_fpt(list_points_fpt_t* list, const bool full) { } -void list_points_append_int(list_points_int_t* list, vector_int_t* vector) { - if (NULL == vector) return; - list_points_node_int_t* node = list_points_node_create_int(vector); +void list_points_append_int(list_points_int_t* list, vector_int_t* point) { + if (NULL == list || NULL == point) return; + list_points_node_int_t* node = list_points_node_create_int(point); if (NULL == list->head) { // if list is empty list->head = node; list->tail = list->head; @@ -93,9 +88,9 @@ void list_points_append_int(list_points_int_t* list, vector_int_t* vector) { ++list->size; } -void list_points_append_fpt(list_points_fpt_t* list, vector_fpt_t* vector) { - if (NULL == vector) return; - list_points_node_fpt_t* node = list_points_node_create_fpt(vector); +void list_points_append_fpt(list_points_fpt_t* list, vector_fpt_t* point) { + if (NULL == list || NULL == point) return; + list_points_node_fpt_t* node = list_points_node_create_fpt(point); if (NULL == list->head) { // if list is empty list->head = node; list->tail = list->head; @@ -107,8 +102,9 @@ void list_points_append_fpt(list_points_fpt_t* list, vector_fpt_t* vector) { } -point_int_t** list_points_to_array_int(const list_points_int_t* list) { - point_int_t** a = calloc(list->size, sizeof(point_int_t*)); +vector_int_t** list_points_to_array_int(const list_points_int_t* list) { + if (NULL == list) return NULL; + vector_int_t** a = calloc(list->size, sizeof(vector_int_t*)); if (NULL == a) return NULL; list_points_node_int_t* cur = list->head; size_t idx = 0; @@ -121,8 +117,9 @@ point_int_t** list_points_to_array_int(const list_points_int_t* list) { return a; } -point_fpt_t** list_points_to_array_fpt(const list_points_fpt_t* list) { - point_fpt_t** a = calloc(list->size, sizeof(point_fpt_t*)); +vector_fpt_t** list_points_to_array_fpt(const list_points_fpt_t* list) { + if (NULL == list) return NULL; + vector_fpt_t** a = calloc(list->size, sizeof(vector_fpt_t*)); if (NULL == a) return NULL; list_points_node_fpt_t* cur = list->head; size_t idx = 0; diff --git a/src/linkedlist.h b/src/linkedlist.h index adfdeb9c1ab0d9b8f09cea9517fe14dd80f26d7a..de857efeac7617a16e51e2be4c6b9905a5590b99 100644 --- a/src/linkedlist.h +++ b/src/linkedlist.h @@ -2,24 +2,23 @@ #define PROG_KMEANS_LINKEDLIST_H #include <stdbool.h> -#include "point.h" #include "vector.h" typedef struct list_points_node_int { - point_int_t* point; + vector_int_t* point; struct list_points_node_int* next; } list_points_node_int_t; typedef struct list_points_node_fpt { - point_fpt_t* point; + vector_fpt_t* point; struct list_points_node_fpt* next; } list_points_node_fpt_t; -list_points_node_int_t* list_points_node_create_int(vector_int_t* vec); +list_points_node_int_t* list_points_node_create_int(vector_int_t* point); -list_points_node_fpt_t* list_points_node_create_fpt(vector_fpt_t* vec); +list_points_node_fpt_t* list_points_node_create_fpt(vector_fpt_t* point); void list_points_node_destroy_int(list_points_node_int_t* node, const bool full); @@ -55,9 +54,9 @@ void list_points_append_int(list_points_int_t* list, vector_int_t* vector); void list_points_append_fpt(list_points_fpt_t* list, vector_fpt_t* vector); -point_int_t** list_points_to_array_int(const list_points_int_t* list); +vector_int_t** list_points_to_array_int(const list_points_int_t* list); -point_fpt_t** list_points_to_array_fpt(const list_points_fpt_t* list); +vector_fpt_t** list_points_to_array_fpt(const list_points_fpt_t* list); #endif //PROG_KMEANS_LINKEDLIST_H diff --git a/src/point.c b/src/point.c deleted file mode 100644 index 3dcb58f75293d920c1e1d1db5ac364bbc45aa179..0000000000000000000000000000000000000000 --- a/src/point.c +++ /dev/null @@ -1,33 +0,0 @@ -#include "point.h" -#include <stdlib.h> -#include "vector.h" - - -point_int_t* point_create_int(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; -} - -point_fpt_t* point_create_fpt(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_destroy_int(point_int_t* cp) { - if (NULL == cp) return; - vector_destroy_int(cp->vector); - free(cp); -} - -void point_destroy_fpt(point_fpt_t* cp) { - if (NULL == cp) return; - vector_destroy_fpt(cp->vector); - free(cp); -} diff --git a/src/point.h b/src/point.h deleted file mode 100644 index 381dabe7c1781dbb22b70b52c03cdd4f80f8140c..0000000000000000000000000000000000000000 --- a/src/point.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef PROG_KMEANS_POINT_H -#define PROG_KMEANS_POINT_H - -#include "vector.h" - - -typedef struct point_int { - vector_int_t* vector; - 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_create_int(vector_int_t* vector); - -point_fpt_t* point_create_fpt(vector_fpt_t* vector); - - -void point_destroy_int(point_int_t* cp); - -void point_destroy_fpt(point_fpt_t* cp); - - -#endif //PROG_KMEANS_POINT_H