diff options
author | Paul Duncan <pabs@pablotron.org> | 2019-02-04 11:18:30 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2019-02-04 11:18:30 -0500 |
commit | f4a38b43d43f9395d6042d234a5e0ada7455ace1 (patch) | |
tree | 6357eac32d23a1fcaad5f4a8dd457a78f286855a | |
parent | 33e7c5bfdb9099e915876b8dbf6d3129459c2bc3 (diff) | |
download | kmeans-f4a38b43d43f9395d6042d234a5e0ada7455ace1.tar.bz2 kmeans-f4a38b43d43f9395d6042d234a5e0ada7455ace1.zip |
refactor random names
-rw-r--r-- | km-init-forgy.c | 2 | ||||
-rw-r--r-- | km-init-kmeans.c | 4 | ||||
-rw-r--r-- | km-init-rand.c | 2 | ||||
-rw-r--r-- | km-rand.c | 44 | ||||
-rw-r--r-- | km.h | 15 | ||||
-rw-r--r-- | main.c | 2 |
6 files changed, 36 insertions, 33 deletions
diff --git a/km-init-forgy.c b/km-init-forgy.c index 687c528..5ff0c12 100644 --- a/km-init-forgy.c +++ b/km-init-forgy.c @@ -23,7 +23,7 @@ km_init_forgy( // get random row offsets size_t rows[num_clusters]; - if (!km_rand_fill_sizes(rs, num_clusters, rows)) { + if (!km_rand_get_sizes(rs, num_clusters, rows)) { // return failure return false; } diff --git a/km-init-kmeans.c b/km-init-kmeans.c index e49f879..61d67a7 100644 --- a/km-init-kmeans.c +++ b/km-init-kmeans.c @@ -11,7 +11,7 @@ get_random_row( ) { // get random offset size_t ofs = 0; - if (!km_rand_fill_sizes(rs, 1, &ofs)) { + if (!km_rand_get_sizes(rs, 1, &ofs)) { die("km_rand_fill_sizes()"); } @@ -58,7 +58,7 @@ km_init_kmeans( // get a random floating point value float rand_val = 0; - if (!km_rand_fill(rs, 1, &rand_val)) { + if (!km_rand_get_floats(rs, 1, &rand_val)) { // return failure return false; } diff --git a/km-init-rand.c b/km-init-rand.c index c6000bd..1f59da8 100644 --- a/km-init-rand.c +++ b/km-init-rand.c @@ -23,7 +23,7 @@ km_init_rand( // generate random cluster centers float floats[num_floats * num_clusters]; - if (!km_rand_fill(rs, num_floats * num_clusters, floats)) { + if (!km_rand_get_floats(rs, num_floats * num_clusters, floats)) { // return failure return false; } @@ -4,22 +4,22 @@ // fill buffer with N random floats bool -km_rand_fill( +km_rand_get_floats( km_rand_t * const rs, const size_t num_floats, float * const floats ) { - return rs->cbs->fill(rs, num_floats, floats); + return rs->cbs->get_floats(rs, num_floats, floats); } // fill buffer with N random size_ts bool -km_rand_fill_sizes( +km_rand_get_sizes( km_rand_t * const rs, const size_t num_sizes, size_t * const sizes ) { - return rs->cbs->fill_sizes(rs, num_sizes, sizes); + return rs->cbs->get_sizes(rs, num_sizes, sizes); } // finalize random source @@ -32,18 +32,18 @@ km_rand_fini( } } -// fill callback for system random source +// libc random source get_floats static bool -system_on_fill( +rand_libc_get_floats( km_rand_t * const rs, - const size_t num_floats, - float * const floats + const size_t num_vals, + float * const vals ) { UNUSED(rs); - // generate random cluster centers - for (size_t i = 0; i < num_floats; i++) { - floats[i] = 1.0 * rand() / RAND_MAX; + // generate results + for (size_t i = 0; i < num_vals; i++) { + vals[i] = 1.0 * rand() / RAND_MAX; } // return success @@ -52,16 +52,16 @@ system_on_fill( // fill sizes callback for system random source static bool -system_on_fill_sizes( +rand_libc_get_sizes( km_rand_t * const rs, - const size_t num_sizes, - size_t * const sizes + const size_t num_vals, + size_t * const vals ) { UNUSED(rs); - // generate random size_ts - for (size_t i = 0; i < num_sizes; i++) { - sizes[i] = rand(); + // generate results + for (size_t i = 0; i < num_vals; i++) { + vals[i] = rand(); } // return success @@ -70,17 +70,17 @@ system_on_fill_sizes( // system random source callbacks static const km_rand_cbs_t -SYSTEM_CBS = { - .fill = system_on_fill, - .fill_sizes = system_on_fill_sizes, +RAND_LIBC_CBS = { + .get_floats = rand_libc_get_floats, + .get_sizes = rand_libc_get_sizes, .fini = NULL, }; // init system random source (uses system rand()) void -km_rand_init_system( +km_rand_init_libc( km_rand_t * const rs ) { - rs->cbs = &SYSTEM_CBS; + rs->cbs = &RAND_LIBC_CBS; rs->data = NULL; } @@ -10,8 +10,8 @@ typedef struct km_rand_t_ km_rand_t; // random number source callbacks typedef struct { - _Bool (*fill)(km_rand_t * const, const size_t, float * const); - _Bool (*fill_sizes)(km_rand_t * const, const size_t, size_t * const); + _Bool (*get_floats)(km_rand_t * const, const size_t, float * const); + _Bool (*get_sizes)(km_rand_t * const, const size_t, size_t * const); void (*fini)(km_rand_t * const); } km_rand_cbs_t; @@ -22,16 +22,19 @@ struct km_rand_t_ { }; // fill buffer with N random floats -_Bool km_rand_fill(km_rand_t * const, const size_t, float * const); +_Bool km_rand_get_floats(km_rand_t * const, const size_t, float * const); // fill buffer with N random size_ts -_Bool km_rand_fill_sizes(km_rand_t * const, const size_t, size_t * const); +_Bool km_rand_get_sizes(km_rand_t * const, const size_t, size_t * const); // finalize random source void km_rand_fini(km_rand_t * const); -// init system random source (uses system rand()) -void km_rand_init_system(km_rand_t *); +// init libc random source (use libc rand()) +void km_rand_init_libc(km_rand_t *); + +// init random file source (e.g. "/dev/urandom") +_Bool km_rand_init_path(km_rand_t * const, const char *); // shape of data set typedef struct { @@ -324,7 +324,7 @@ int main(int argc, char *argv[]) { // init context ctx_t ctx; memset(&ctx, 0, sizeof(ctx_t)); - km_rand_init_system(&(ctx.rs)); + km_rand_init_libc(&(ctx.rs)); ctx.init_type = km_init_get_type(init_type_name); // init data set |