From e5f97aafca24667ed6780a92d9bd451c66454b44 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 3 Feb 2019 05:03:07 -0500 Subject: s/rand_src/rand/, mv km-rand{-src,}.c --- Makefile | 2 +- km-rand-src.c | 57 --------------------------------------------------------- km-rand.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ km-set.c | 4 ++-- km.h | 21 ++++++++++----------- main.c | 4 ++-- 6 files changed, 72 insertions(+), 73 deletions(-) delete mode 100644 km-rand-src.c create mode 100644 km-rand.c diff --git a/Makefile b/Makefile index 61e8f9e..d23f364 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ APP=km-test CFLAGS=-W -Wall -Wextra -pedantic -std=c11 -O2 -OBJS=km-set.o km-draw.o km-load.o km-find.o km-rand-src.o km-solve.o main.o +OBJS=km-set.o km-draw.o km-load.o km-find.o km-rand.o km-solve.o main.o LIBS=-lm .PHONY=all clean diff --git a/km-rand-src.c b/km-rand-src.c deleted file mode 100644 index 0ddc1bb..0000000 --- a/km-rand-src.c +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include "util.h" -#include "km.h" - -// fill buffer with N random floats -bool -km_rand_src_fill( - km_rand_src_t * const rs, - const size_t num_floats, - float * const floats -) { - return rs->cbs->fill(rs, num_floats, floats); -} - -// finalize random source -void -km_rand_src_fini( - km_rand_src_t * const rs -) { - if (rs->cbs->fini) { - rs->cbs->fini(rs); - } -} - -// fill callback for system random source -static bool -rand_src_system_on_fill( - km_rand_src_t * const rs, - const size_t num_floats, - float * const floats -) { - UNUSED(rs); - - // generate random cluster centers - for (size_t i = 0; i < num_floats; i++) { - floats[i] = 1.0 * rand() / RAND_MAX; - } - - // return success - return true; -} - -// system random source callbacks -static const km_rand_src_cbs_t -RAND_SRC_SYSTEM_CBS = { - .fill = rand_src_system_on_fill, - .fini = NULL, -}; - -// init system random source (uses system rand()) -void -km_rand_src_system_init( - km_rand_src_t * const rs -) { - rs->cbs = &RAND_SRC_SYSTEM_CBS; - rs->data = NULL; -} diff --git a/km-rand.c b/km-rand.c new file mode 100644 index 0000000..fcc6a74 --- /dev/null +++ b/km-rand.c @@ -0,0 +1,57 @@ +#include +#include "util.h" +#include "km.h" + +// fill buffer with N random floats +bool +km_rand_fill( + km_rand_t * const rs, + const size_t num_floats, + float * const floats +) { + return rs->cbs->fill(rs, num_floats, floats); +} + +// finalize random source +void +km_rand_fini( + km_rand_t * const rs +) { + if (rs->cbs->fini) { + rs->cbs->fini(rs); + } +} + +// fill callback for system random source +static bool +system_on_fill( + km_rand_t * const rs, + const size_t num_floats, + float * const floats +) { + UNUSED(rs); + + // generate random cluster centers + for (size_t i = 0; i < num_floats; i++) { + floats[i] = 1.0 * rand() / RAND_MAX; + } + + // return success + return true; +} + +// system random source callbacks +static const km_rand_cbs_t +SYSTEM_CBS = { + .fill = system_on_fill, + .fini = NULL, +}; + +// init system random source (uses system rand()) +void +km_rand_init_system( + km_rand_t * const rs +) { + rs->cbs = &SYSTEM_CBS; + rs->data = NULL; +} diff --git a/km-set.c b/km-set.c index 5b9ae4c..63ec22a 100644 --- a/km-set.c +++ b/km-set.c @@ -219,7 +219,7 @@ km_set_init_rand_clusters( km_set_t * const cs, const size_t num_floats, const size_t num_clusters, - km_rand_src_t * const rs + km_rand_t * const rs ) { // init cluster shape const km_shape_t shape = { @@ -229,7 +229,7 @@ km_set_init_rand_clusters( // generate random cluster centers float floats[num_floats * num_clusters]; - if (!km_rand_src_fill(rs, num_floats * num_clusters, floats)) { + if (!km_rand_fill(rs, num_floats * num_clusters, floats)) { // return failure return false; } diff --git a/km.h b/km.h index 642560b..1d00d50 100644 --- a/km.h +++ b/km.h @@ -6,28 +6,28 @@ #include // FILE // forward typedef for callbacks -typedef struct km_rand_src_t_ km_rand_src_t; +typedef struct km_rand_t_ km_rand_t; // random number source callbacks typedef struct { - _Bool (*fill)(km_rand_src_t * const, const size_t, float * const); - void (*fini)(km_rand_src_t * const); -} km_rand_src_cbs_t; + _Bool (*fill)(km_rand_t * const, const size_t, float * const); + void (*fini)(km_rand_t * const); +} km_rand_cbs_t; // random number source -struct km_rand_src_t_ { - const km_rand_src_cbs_t *cbs; +struct km_rand_t_ { + const km_rand_cbs_t *cbs; void *data; }; // fill buffer with N random floats -_Bool km_rand_src_fill(km_rand_src_t * const, const size_t, float * const); +_Bool km_rand_fill(km_rand_t * const, const size_t, float * const); // finalize random source -void km_rand_src_fini(km_rand_src_t * const); +void km_rand_fini(km_rand_t * const); // init system random source (uses system rand()) -void km_rand_src_system_init(km_rand_src_t *); +void km_rand_init_system(km_rand_t *); // shape of data set typedef struct { @@ -87,7 +87,7 @@ _Bool km_set_init_rand_clusters( km_set_t *, const size_t, const size_t, - km_rand_src_t * + km_rand_t * ); typedef struct { @@ -136,7 +136,6 @@ typedef void (*km_find_data_cb_t)( typedef struct { size_t max_clusters; size_t num_tests; - km_rand_src_t *rand_src; km_find_init_cb_t on_init; km_find_fini_cb_t on_fini; diff --git a/main.c b/main.c index bd4192f..af04121 100644 --- a/main.c +++ b/main.c @@ -16,7 +16,7 @@ #define NUM_TESTS 100 typedef struct { - km_rand_src_t rs; + km_rand_t rs; struct { float distance, @@ -220,7 +220,7 @@ int main(int argc, char *argv[]) { // init find data find_t find_data; memset(find_data.rows, 0, sizeof(find_data.rows)); - km_rand_src_system_init(&(find_data.rs)); + km_rand_init_system(&(find_data.rs)); // init data set km_set_t set; -- cgit v1.2.3