Skip to content
Snippets Groups Projects
Commit 74bb80f8 authored by Boris Stefanovic's avatar Boris Stefanovic
Browse files

cleanup: vector and cluster

parent d2167c32
No related branches found
No related tags found
No related merge requests found
...@@ -23,12 +23,12 @@ header-includes: ...@@ -23,12 +23,12 @@ header-includes:
- "common.h" contient les définitions de `int_t` et `fpt_t` - "common.h" contient les définitions de `int_t` et `fpt_t`
```c ```c
typedef struct vector_int_t_ { typedef struct vector_int {
size_t dim; size_t dim;
int_t* data; int_t* data;
} vector_int_t; } vector_int_t;
typedef struct vector_fpt_t_ { typedef struct vector_fpt {
size_t dim; size_t dim;
fpt_t* data; fpt_t* data;
} vector_fpt_t; } vector_fpt_t;
...@@ -55,10 +55,10 @@ typedef vector_int_t* cluster_int_t; ...@@ -55,10 +55,10 @@ typedef vector_int_t* cluster_int_t;
## Point de Cluster ## Point de Cluster
```c ```c
typedef struct cluster_point_int { typedef struct point_int {
const vector_int_t* vector; const vector_int_t* vector;
cluster_int_t* cluster; cluster_int_t* cluster;
} cluster_point_int_t; } point_int_t;
``` ```
## Ensemble de Points ## Ensemble de Points
...@@ -69,7 +69,7 @@ typedef struct cluster_point_int { ...@@ -69,7 +69,7 @@ typedef struct cluster_point_int {
```c ```c
typedef struct ll_point_int_node { typedef struct ll_point_int_node {
const cluster_point_int_t* point; const point_int_t* point;
struct ll_point_int_node* next; struct ll_point_int_node* next;
} ll_point_int_node_t; } ll_point_int_node_t;
......
...@@ -7,16 +7,33 @@ ...@@ -7,16 +7,33 @@
#include "vector.h" #include "vector.h"
cluster_point_int_t* cluster_point_int_create(vector_int_t* vector) { point_int_t* point_int_create(vector_int_t* vector) {
cluster_point_int_t* elem = malloc(sizeof(cluster_point_int_t)); point_int_t* point = malloc(sizeof(point_int_t));
if (NULL == elem) return NULL; if (NULL == point) return NULL;
elem->vector = vector; point->vector = vector;
elem->cluster = NULL; 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; if (NULL == cp) return;
vector_int_destroy(cp->vector); vector_int_destroy(cp->vector);
free(cp); free(cp);
} }
void point_fpt_destroy(point_fpt_t* cp) {
if (NULL == cp) return;
vector_fpt_destroy(cp->vector);
free(cp);
}
...@@ -2,22 +2,34 @@ ...@@ -2,22 +2,34 @@
// by Boris Stefanovic on 01/06/22 // 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 #ifndef PROG_KMEANS_CLUSTER_H
#define PROG_KMEANS_CLUSTER_H #define PROG_KMEANS_CLUSTER_H
#include "vector.h" #include "vector.h"
typedef vector_int_t* cluster_int_t; // a cluster may be represented by its center typedef struct point_int {
typedef struct cluster_point_int {
vector_int_t* vector; vector_int_t* vector;
cluster_int_t cluster; // justified by "many-to-one" relationship and several passes over all points vector_int_t* cluster;
} cluster_point_int_t; } 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 #endif //PROG_KMEANS_CLUSTER_H
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
#include "linkedlist.h" #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( 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, cluster_int_t* clusters, const size_t nb_clusters,
fpt_t (* distance_function)(const vector_fpt_t*, const vector_fpt_t*)); fpt_t (* distance_function)(const vector_fpt_t*, const vector_fpt_t*));
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
typedef struct ll_point_int_node { typedef struct ll_point_int_node {
cluster_point_int_t* point; point_int_t* point;
struct ll_point_int_node* next; struct ll_point_int_node* next;
} ll_point_int_node_t; } ll_point_int_node_t;
...@@ -31,7 +31,7 @@ void ll_point_int_destroy(ll_point_int_t* list, const bool full); ...@@ -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); 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 #endif //PROG_KMEANS_LINKEDLIST_H
...@@ -77,7 +77,7 @@ int main(int argc, char** argv) { ...@@ -77,7 +77,7 @@ int main(int argc, char** argv) {
} }
ll_point_int_t* list = get_vector_list_int(ifile, dim); ll_point_int_t* list = get_vector_list_int(ifile, dim);
size_t count; 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); ll_point_int_destroy(list, false);
list = NULL; list = NULL;
// ALGORITHM // ALGORITHM
......
...@@ -32,7 +32,7 @@ void vector_int_destroy(vector_int_t* vp) { ...@@ -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; if (NULL == vp) return;
free(vp->data); free(vp->data);
free(vp); free(vp);
...@@ -50,6 +50,17 @@ vector_int_t* vector_int_copy(const vector_int_t* v) { ...@@ -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) { bool vector_int_equals(const vector_int_t* v1, const vector_int_t* v2) {
if (v1->dim != v2->dim) return false; if (v1->dim != v2->dim) return false;
for (size_t i = 0; i < v1->dim; ++i) { for (size_t i = 0; i < v1->dim; ++i) {
......
...@@ -15,28 +15,33 @@ ...@@ -15,28 +15,33 @@
#include "common.h" #include "common.h"
typedef struct vector_int_t_ { typedef struct vector_int {
size_t dim; size_t dim;
int_t* data; int_t* data;
} vector_int_t; } vector_int_t;
typedef struct vector_fpt_t_ {
typedef struct vector_fpt {
size_t dim; size_t dim;
fpt_t* data; fpt_t* data;
} vector_fpt_t; } 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_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); 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); 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); bool vector_fpt_equals(const vector_fpt_t* v1, const vector_fpt_t* v2);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment