diff options
author | Paul Duncan <pabs@pablotron.org> | 2019-02-05 00:22:15 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2019-02-05 00:22:15 -0500 |
commit | f557d1f49a2914c6084dd18efc783395228d8ce0 (patch) | |
tree | 51111fc899f01956cbab948b87c241478576870d /km-rand-path.c | |
parent | b5065ea43cb13c0b553874305b53963176c70f59 (diff) | |
download | kmeans-f557d1f49a2914c6084dd18efc783395228d8ce0.tar.bz2 kmeans-f557d1f49a2914c6084dd18efc783395228d8ce0.zip |
mv *.[hc] src/
Diffstat (limited to 'km-rand-path.c')
-rw-r--r-- | km-rand-path.c | 152 |
1 files changed, 0 insertions, 152 deletions
diff --git a/km-rand-path.c b/km-rand-path.c deleted file mode 100644 index 6597038..0000000 --- a/km-rand-path.c +++ /dev/null @@ -1,152 +0,0 @@ -#include <stdbool.h> // bool -#include <string.h> // memset() -#include <stdio.h> // fopen() -#include "util.h" -#include "km.h" - -#define MAX_BUF_ITEMS (1 << 20) - -typedef struct { - FILE *fh; - size_t ofs; - uint64_t buf[MAX_BUF_ITEMS]; -} ctx_t; - -static inline bool -refill( - ctx_t * const ctx -) { - ctx->ofs = 0; - // D("reading %zu bytes", sizeof(ctx->buf)); - return !!fread(ctx->buf, sizeof(ctx->buf), 1, ctx->fh); -} - -static bool -next( - ctx_t * const ctx, - uint64_t * const r -) { - if ((ctx->ofs >= MAX_BUF_ITEMS - 1) && !refill(ctx)) { - // return failure - return false; - } - - // get next value, increment offset - *r = ctx->buf[ctx->ofs]; - ctx->ofs++; - - // return success - return true; -} - -// get N get_floats -static bool -on_get_floats( - km_rand_t * const rs, - const size_t num_vals, - float * const vals -) { - ctx_t * const ctx = rs->data; - - // generate results - for (size_t i = 0; i < num_vals; i++) { - uint64_t v; - - if (!next(ctx, &v)) { - // return failure - return false; - } - - // add value - vals[i] = 1.0 * v / RAND_MAX; - } - - // return success - return true; -} - -// fill sizes callback for system random source -static bool -on_get_sizes( - km_rand_t * const rs, - const size_t num_vals, - size_t * const vals -) { - ctx_t * const ctx = rs->data; - - // generate results - for (size_t i = 0; i < num_vals; i++) { - uint64_t v; - - if (!next(ctx, &v)) { - // return failure - return false; - } - - // add value - vals[i] = v; - } - - // return success - return true; -} - -static void -on_fini( - km_rand_t * const rs -) { - ctx_t * const ctx = rs->data; - - // close file - fclose(ctx->fh); - - // free context - free(ctx); - - rs->data = NULL; -} - -// path random source callbacks -static const km_rand_cbs_t -PATH_RAND_CBS = { - .get_floats = on_get_floats, - .get_sizes = on_get_sizes, - .fini = on_fini, -}; - -// init system random source (uses system rand()) -bool -km_rand_init_path( - km_rand_t * const rs, - const char * const path -) { - // open file - FILE *fh = fopen(path, "rb"); - if (!fh) { - // return failure - return false; - } - - // alloc context - ctx_t * const ctx = malloc(sizeof(ctx_t)); - if (!ctx) { - // return failure - return false; - } - - // populate context - ctx->fh = fh; - - // fill buffer - if (!refill(ctx)) { - // return failure - return false; - } - - // populate random source - rs->cbs = &PATH_RAND_CBS; - rs->data = ctx; - - // return success - return true; -} |