diff options
author | 2021-03-03 14:57:28 +0200 | |
---|---|---|
committer | 2021-03-03 14:57:28 +0200 | |
commit | 068e3e6c5a74702c3e7db0e37b243f522c433a7f (patch) | |
tree | f786f107ff5759084e3c1f76f5734cad2084fdd2 | |
parent | b3072e146b4edd4a2422cee758aa341b0638491d (diff) | |
download | algos-ld1-068e3e6c5a74702c3e7db0e37b243f522c433a7f.tar.gz algos-ld1-068e3e6c5a74702c3e7db0e37b243f522c433a7f.tar.bz2 algos-ld1-068e3e6c5a74702c3e7db0e37b243f522c433a7f.zip |
straight up code refactor.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
-rw-r--r-- | src/cache.c | 70 | ||||
-rw-r--r-- | src/cache.h | 9 | ||||
-rw-r--r-- | src/datagen.c | 2 | ||||
-rw-r--r-- | src/defs.h | 14 | ||||
-rw-r--r-- | src/main.c | 71 | ||||
-rw-r--r-- | src/mergesort.c | 12 | ||||
-rw-r--r-- | src/stream.c | 28 |
7 files changed, 58 insertions, 148 deletions
diff --git a/src/cache.c b/src/cache.c index 86e3161..af33f30 100644 --- a/src/cache.c +++ b/src/cache.c @@ -41,9 +41,9 @@ int cache_populate(struct stream * const in) if (in->type == stream_randread) { for (i = 0; i < in->n && !ret; ++i) { errno = 0; - tmp = in->get_next_element_direct(in); + tmp = in->get(in); if (tmp) { /* non-cache reads CAN fail */ - put(in, tmp); + in->put(in, tmp); } else { ret = errno; break; @@ -84,7 +84,7 @@ err: return ret; } -int cache_block_copy(struct stream const * const src, struct stream * const dest) +int cache_block_copy(struct stream * const restrict src, struct stream * const restrict dest) { int ret = 0; @@ -100,7 +100,7 @@ err: return ret; } -int cache_list_copy(struct stream * const src, struct stream * const dest) +int cache_list_copy(struct stream * const restrict src, struct stream * const restrict dest) { int ret = 0; size_t ss; @@ -114,21 +114,21 @@ int cache_list_copy(struct stream * const src, struct stream * const dest) /* skip over to start position */ while (ss--) { - get(src); + src->get(src); } do { - put(dest, get(src)); + dest->put(dest, src->get(src)); } while (dest->index < (dest->n - 1)); - cache_rewind(src); - cache_rewind(dest); + src->rewind(src); + dest->rewind(dest); err: return ret; } -int cache_block_split(struct stream * const src, struct stream * const A, struct stream * const B) +int cache_split(struct stream * const src, struct stream * const A, struct stream * const B) { int ret = 0; struct settings tmp_settings; @@ -142,10 +142,11 @@ int cache_block_split(struct stream * const src, struct stream * const A, struct A->type = B->type = stream_cache; /* disallow any stream operations other than split/merge on children */ A->fd = B->fd = -1; /* if we're splitting, these are for holding cache only */ /* we only care about these three functions for these temporary streams */ - A->get_next_element_cache = B->get_next_element_cache = src->get_next_element_cache; - A->place_next_element_cache = B->place_next_element_cache = src->place_next_element_cache; + A->get = B->get = src->get; + A->put = B->put = src->put; A->split = B->split = src->split; A->rewind = B->rewind = src->rewind; + A->copy = B->copy = src->copy; tmp_settings = *src->settings; A->settings = B->settings = &tmp_settings; @@ -154,54 +155,13 @@ int cache_block_split(struct stream * const src, struct stream * const A, struct tmp_settings.ss = 0; tmp_settings.to = A->n; try_s((ret = cache_create(A)), err); - try_s((ret = cache_block_copy(src, A)), err); + try_s((ret = src->copy(src, A)), err); /* setting up B */ tmp_settings.ss = A->n; tmp_settings.to = src->n; try_s((ret = cache_create(B)), err); - try_s((ret = cache_block_copy(src, B)), err); - - A->settings = B->settings = src->settings; - -err: - return ret; -} - -int cache_list_split(struct stream * const src, struct stream * const A, struct stream * const B) -{ - int ret = 0; - struct settings tmp_settings; - - - try(src->n < 2, err, EINVAL, "cannot split single element stream."); - - /* setting up minimal stream basics */ - A->n = src->n / 2; - B->n = src->n / 2 + (src->n & 1ul); - A->index = B->index = 0; - A->type = B->type = stream_cache; /* disallow any stream operations other than split/merge on children */ - A->fd = B->fd = -1; /* if we're splitting, these are for holding cache only */ - /* we only care about these three functions for these temporary streams */ - A->get_next_element_cache = B->get_next_element_cache = src->get_next_element_cache; - A->place_next_element_cache = B->place_next_element_cache = src->place_next_element_cache; - A->split = B->split = src->split; - A->rewind = B->rewind = src->rewind; - - tmp_settings = *src->settings; - A->settings = B->settings = &tmp_settings; - - /* setting up A */ - tmp_settings.ss = 0; - tmp_settings.to = A->n; - try_s((ret = cache_create(A)), err); - try_s((ret = cache_list_copy(src, A)), err); - - /* setting up B */ - tmp_settings.ss = A->n; - tmp_settings.to = src->n; - try_s((ret = cache_create(B)), err); - try_s((ret = cache_list_copy(src, B)), err); + try_s((ret = src->copy(src, B)), err); A->settings = B->settings = src->settings; @@ -279,7 +239,7 @@ err: return ret; } -int cache_rewind(struct stream * const in) +int cache_rewind(struct stream * const restrict in) { int ret = 0; diff --git a/src/cache.h b/src/cache.h index dc22836..6255122 100644 --- a/src/cache.h +++ b/src/cache.h @@ -15,11 +15,10 @@ int cache_destroy(struct stream * const in); /* BLOCKMANIP */ int cache_transfer(struct stream * const src, struct stream * const dest); -int cache_block_copy(struct stream const * const src, struct stream * const dest); -int cache_list_copy(struct stream * const src, struct stream * const dest); -int cache_block_split(struct stream * const src, struct stream * const A, struct stream * const B); -int cache_list_split(struct stream * const src, struct stream * const A, struct stream * const B); -int cache_rewind(struct stream * const in); +int cache_block_copy(struct stream * const restrict src, struct stream * const restrict dest); +int cache_list_copy(struct stream * const restrict src, struct stream * const restrict dest); +int cache_split(struct stream * const src, struct stream * const A, struct stream * const B); +int cache_rewind(struct stream * const restrict in); /* GET */ struct entry_l *cached_get_array(struct stream * const in); diff --git a/src/datagen.c b/src/datagen.c index 98a64fe..52f1896 100644 --- a/src/datagen.c +++ b/src/datagen.c @@ -33,7 +33,7 @@ struct entry_l *gen_get_list(struct stream * const in) struct entry_l *ret; try_s((ret = gen_get_array(in)), err); - ret->next = 0; + ret->next = NULL; err: return ret; @@ -49,9 +49,6 @@ # endif #endif -#define get(in) (in->settings->access == cached ? in->get_next_element_cache(in) : in->get_next_element_direct(in)) -#define put(in, data) (in->settings->access == cached ? in->place_next_element_cache(in, data) : in->place_next_element_direct(in, data)) - union nextoff { struct entry_l *next; void *nextaddr; @@ -96,7 +93,7 @@ enum streamtype { stream_invalid, stream_in, stream_out, - stream_outlite, + stream_lite, stream_cache, stream_randread }; @@ -110,12 +107,11 @@ struct stream { struct entry_l *cnode; /* "current" node */ union cachewrap; struct settings *settings; - struct entry_l *(*get_next_element_direct)(struct stream * const); - struct entry_l *(*get_next_element_cache)(struct stream * const); - int (*place_next_element_direct)(struct stream * const, struct entry_l const * const); - int (*place_next_element_cache)(struct stream * const, struct entry_l const * const); + struct entry_l *(*get)(struct stream * const); + int (*put)(struct stream * const, struct entry_l const * const); int (*split)(struct stream * const, struct stream * const, struct stream * const); - int (*rewind)(struct stream * const); + int (*rewind)(struct stream * restrict const); + int (*copy)(struct stream * restrict const, struct stream * restrict const); }; struct settings { @@ -20,21 +20,14 @@ static struct settings settings = {0, 0, 0, NULL, NULL, mode_normal, array, cach static int parseargs(int argc, char **argv, struct settings * settings); static int load_io_functions(struct settings const * const s, struct stream * const in); static void printhelp(const char * const name); -static struct entry_l *stub_getnext(struct stream * const restrict in); -static int stub_put(struct stream * const restrict in, struct entry_l const * const data); -static int stub_split(struct stream * const in, struct stream * const a, struct stream * const b); static const struct stream stream_blank = { .n = 0, .type = stream_invalid, .fd = -1, .settings = &settings, - .name = NULL, .index = 0, .cnode = NULL, .cache = NULL, - .get_next_element_direct = stub_getnext, - .get_next_element_cache = stub_getnext, - .place_next_element_direct = stub_put, - .place_next_element_cache = stub_put, .split = stub_split }; + .name = NULL, .index = 0, .cnode = NULL, .cache = NULL }; static struct stream file_in = stream_blank; static struct stream file_out = stream_blank; -static struct stream file_tmp; +static struct stream file_tmp = stream_blank; static struct rin_bench_result bongholio; @@ -47,7 +40,6 @@ int main(int argc, char **argv) file_in.type = stream_in; file_out.type = stream_out; - file_tmp.type = stream_outlite; try_s((ret = parseargs(argc, argv, &settings)), early_out); @@ -93,7 +85,6 @@ int main(int argc, char **argv) rin_info("system: %lus %3lums %3luµs", bongholio.system.tv_sec, bongholio.system.tv_usec / 1000, bongholio.system.tv_usec % 1000); rin_info("user: %lus %3lums %3luµs", bongholio.user.tv_sec, bongholio.user.tv_usec / 1000, bongholio.user.tv_usec % 1000); rin_info("total: %lus %3lums %3luµs", bongholio.total.tv_sec, bongholio.total.tv_usec / 1000, bongholio.total.tv_usec % 1000); - try_s((ret = cache_transfer(&file_tmp, &file_out)), out); } @@ -203,23 +194,25 @@ static int load_io_functions(struct settings const * const s, struct stream * co if (in->settings->access == cached) { if (in->type == stream_randread) { /* data generation streams only support data generation and not much else */ if (s->format == array) { - in->get_next_element_direct = gen_get_array; - in->place_next_element_cache = cached_put_array; + in->get = gen_get_array; + in->put = cached_put_array; } else { /* if (s->format == list */ - in->get_next_element_direct = gen_get_list; - in->place_next_element_cache = cached_put_list; + in->get = gen_get_list; + in->put = cached_put_list; } } else { if (s->format == array) { - in->get_next_element_cache = cached_get_array; - in->place_next_element_cache = cached_put_array; - in->split = cache_block_split; + in->get = cached_get_array; + in->put = cached_put_array; + in->split = cache_split; in->rewind = cache_rewind; + in->copy = cache_block_copy; } else { /* if (s->format == list */ - in->get_next_element_cache = cached_get_list; - in->place_next_element_cache = cached_put_list; - in->split = cache_list_split; + in->get = cached_get_list; + in->put = cached_put_list; + in->split = cache_split; in->rewind = cache_rewind; + in->copy = cache_list_copy; } } } else { @@ -251,39 +244,3 @@ static void printhelp(const char * const name) name); return; } - -static struct entry_l *stub_getnext(struct stream * const restrict in) -{ - struct entry_l *ret = NULL; - - (void) in; - - rin_warn("stub!"); - - return ret; -} - -static int stub_put(struct stream * const restrict in, struct entry_l const * const data) -{ - int ret = ENOSYS; - - (void) in; - (void) data; - - rin_warn("stub!"); - - return ret; -} - -static int stub_split(struct stream * const in, struct stream * const a, struct stream * const b) -{ - int ret = ENOSYS; - - (void) in; - (void) a; - (void) b; - - rin_warn("stub!"); - - return ret; -} diff --git a/src/mergesort.c b/src/mergesort.c index 883d8ba..1b21db7 100644 --- a/src/mergesort.c +++ b/src/mergesort.c @@ -49,16 +49,16 @@ static int merge(struct stream * const dest, struct stream * const A, struct str struct entry_l *a; struct entry_l *b; - a = get(A); - b = get(B); + a = A->get(A); + b = B->get(B); while (a || b) { if (a && (!b || a->val <= b->val)) { - put(dest, a); - a = get(A); + dest->put(dest, a); + a = A->get(A); } else { - put(dest, b); - b = get(B); + dest->put(dest, b); + b = B->get(B); } } diff --git a/src/stream.c b/src/stream.c index e29d17d..83235a5 100644 --- a/src/stream.c +++ b/src/stream.c @@ -18,7 +18,7 @@ #include "cache.h" static int stream_open_out(struct stream * const in); -static int stream_open_out_lite(struct stream * const in); +static int stream_open_lite(struct stream * const in); static int stream_open_in(struct stream * const in); static int stream_flush(struct stream * const in); static int stream_flush_array(struct stream * const in); @@ -31,8 +31,8 @@ int stream_open(struct stream * const in) try(!in || in->fd > 0 || (!in->name && in->type != stream_randread), err, EINVAL, "invalid argunent"); switch (in->type) { - case (stream_outlite): - ret = stream_open_out_lite(in); + case (stream_lite): + ret = stream_open_lite(in); break; case (stream_out): try(!in->name, err, EINVAL, "no filename given"); @@ -104,17 +104,15 @@ int stream_shallow_copy(struct stream const * const restrict src, struct stream dest->settings = src->settings; dest->index = 0; - if (src->settings->access == cached) { - dest->type = stream_cache; - dest->get_next_element_cache = src->get_next_element_cache; - dest->place_next_element_cache = src->place_next_element_cache; - dest->split = src->split; - dest->rewind = src->rewind; - try_s((ret = cache_create(dest)), err); - } else { - rin_warn("stub!"); - ret = ENOSYS; - } + dest->get = src->get; + dest->put = src->put; + dest->split = src->split; + dest->rewind = src->rewind; + dest->copy = src->copy; + + dest->type = src->settings->access == cached ? stream_cache : stream_lite; + + try_s((ret = cache_create(dest)), err); err: return ret; @@ -150,7 +148,7 @@ err: return ret; } -static int stream_open_out_lite(struct stream * const in) +static int stream_open_lite(struct stream * const in) { struct stat st; char *dname = NULL; |