From 74bb80f852405cb93120c0bf4931dda1e182bda6 Mon Sep 17 00:00:00 2001
From: Boris Stefanovic <owldev@bluewin.ch>
Date: Mon, 6 Jun 2022 23:54:14 +0200
Subject: [PATCH] cleanup: vector and cluster

---
 doc/kmeans.md    | 10 +++++-----
 src/cluster.c    | 29 +++++++++++++++++++++++------
 src/cluster.h    | 26 +++++++++++++++++++-------
 src/kmeans.h     |  4 ++--
 src/linkedlist.h |  4 ++--
 src/main.c       |  2 +-
 src/vector.c     | 13 ++++++++++++-
 src/vector.h     | 17 +++++++++++------
 8 files changed, 75 insertions(+), 30 deletions(-)

diff --git a/doc/kmeans.md b/doc/kmeans.md
index 7efdcf9..6ee769f 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 42e6abb..9898f8b 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 20085a7..f329132 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 a0d4ca3..377fc54 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 8f4250d..fbb3ded 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 754cdc7..b5bef70 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 46e5746..085a10a 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 6b8f665..2d2db1a 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);
 
 
-- 
GitLab