From 5f223b1ca2d3e99a9b678ea5af4a26b3c4c896d0 Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 31 Mar 2023 20:15:02 -0700 Subject: [PATCH] table --- src/pair.c | 2 +- src/pair.h | 2 +- src/scan_score.h | 21 ++++-------- src/scan_score.re.c | 84 +++++++++++++++++++++++++++++---------------- src/source.re.c | 6 ++-- 5 files changed, 65 insertions(+), 50 deletions(-) diff --git a/src/pair.c b/src/pair.c index 278a938..20a8cae 100644 --- a/src/pair.c +++ b/src/pair.c @@ -97,7 +97,7 @@ static uint32_t pairmap_hash(const struct pair p) { return pair_djb2(p); } struct pairmap_table pair_map_table(void) { return pairmap_table(); } void pair_map_table_(struct pairmap_table *const t) { pairmap_table_(t); } -const char *pair_map_to_string(const struct pairmap_table *const t) +const char *pair_map_table_to_string(const struct pairmap_table *const t) { return pairmap_table_to_string(t); } enum table_result pair_map_table_assign(struct pairmap_table *const t, const struct pair key, size_t **const content) diff --git a/src/pair.h b/src/pair.h index fbcaff1..56e769b 100644 --- a/src/pair.h +++ b/src/pair.h @@ -24,7 +24,7 @@ uint32_t pair_djb2(const struct pair p);*/ #include "../src/table.h" struct pairmap_table pair_map_table(void); void pair_map_table_(struct pairmap_table *); -const char *pair_map_to_string(const struct pairmap_table *); +const char *pair_map_table_to_string(const struct pairmap_table *); enum table_result pair_map_table_assign(struct pairmap_table *, const struct pair, size_t **); size_t pair_map_table_get(struct pairmap_table *, struct pair); diff --git a/src/scan_score.h b/src/scan_score.h index f278889..4a2210d 100644 --- a/src/scan_score.h +++ b/src/scan_score.h @@ -1,23 +1,12 @@ #include "pair.h" /* pair */ -#include "journal.h" /* date32, line64 */ -struct score { struct pair key, name; union date32 day; unsigned node; }; +#include "journal.h" /* size_t, date32, line64 */ +struct score { struct pair key, name; union date32 date; unsigned edges; }; #define ARRAY_NAME scorelist #define ARRAY_TYPE struct score #define ARRAY_HEAD #include "../src/array.h" -#include /* size_t */ -#include /* uint32_t */ - -/* fixme: map is from pair -> size_t, nothing to do with score. Compress. */ -#define TABLE_NAME scoremap -#define TABLE_KEY struct pair -#define TABLE_UINT uint32_t -#define TABLE_VALUE size_t -#define TABLE_HEAD -#include "../src/table.h" - #define TREE_NAME score #define TREE_KEY union line64 #define TREE_VALUE size_t @@ -26,8 +15,10 @@ struct score { struct pair key, name; union date32 day; unsigned node; }; struct scores { struct scorelist_array list; - struct scoremap_table map; + struct pairmap_table map; struct score_tree dates; }; -int scan_score(union date32, const char *, struct score_tree *); +void scores_(struct scores *); +struct scores scores(struct journal *); +int scores_is_empty(const struct scores *); diff --git a/src/scan_score.re.c b/src/scan_score.re.c index 37e273a..3371dba 100644 --- a/src/scan_score.re.c +++ b/src/scan_score.re.c @@ -16,8 +16,8 @@ #include "../src/journal.h" #include "../src/scan_score.h" #include -#include -#include +//#include +//#include #include @@ -37,22 +37,6 @@ static void scorelist_to_string(const struct score *const s, #include "../src/array.h" -/* Maps from substring keywords to indices. */ -static void scoremap_to_string(const struct pair key, const size_t i, - char (*const a)[12]) { (void)key; sprintf(*a, "%zu", i); } -static int scoremap_is_equal(const struct pair a, const struct pair b) - { return pair_is_equal(a, b); } -static uint32_t scoremap_hash(const struct pair p) { return pair_djb2(p); } -#define TABLE_NAME scoremap -#define TABLE_KEY struct pair -#define TABLE_UINT uint32_t -#define TABLE_VALUE size_t /* Index into array. */ -#define TABLE_DEFAULT 0 /* Default set at zero. */ -#define TABLE_TO_STRING -#define TABLE_BODY -#include "../src/table.h" - - /* Tree mapping from date-line to indices. */ static void score_to_string(const union line64 line, const size_t *const u, char (*const a)[12]) { (void)u; date32_to_string(line.date, a); } @@ -70,14 +54,14 @@ static int score_compare(const union line64 a, const union line64 b) /*!conditions:re2c*/ -int scan_score(union date32 date, const char *const buffer, - struct score_tree *const score) { +static int scan(union date32 date, const char *const buffer, + struct scores *const scores) { const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *yyt3, *s0, *s1, *t0, *t1; enum YYCONDTYPE condition = yycline; size_t line = 1; char datestr[12] = {0}; const char *why = "unexpected"; - assert(buffer && f); + assert(buffer && scores); YYCURSOR = YYMARKER = yyt1 = buffer; /*!re2c /**/ re2c:define:YYCTYPE = char; @@ -105,13 +89,13 @@ int scan_score(union date32 date, const char *const buffer, * { why = "default score unrecognized"; goto catch; } @s0 keyword @s1 / "\n" => skip { also_add_to_tree: { const struct pair keyword = pair(s0, s1); - const union line64 key = { { line.u32, date } }; + const union line64 key = { { (uint32_t)line, date } }; size_t i, *pi; if(line > UINT32_MAX) { errno = ERANGE; why = "too many lines of text"; goto catch; } - if(!(i = scoremap_table_get(&s->map, keyword))) + if(!(i = pair_map_table_get(&scores->map, keyword))) { why = "keyword not introduced"; goto catch; } - switch(score_tree_try(&s->dates, key, &pi)) { + switch(score_tree_try(&scores->dates, key, &pi)) { case TREE_PRESENT: why = "duplicate key"; /* _Sic_. */ case TREE_ERROR: goto catch; case TREE_ABSENT: *pi = i; break; @@ -121,19 +105,22 @@ int scan_score(union date32 date, const char *const buffer, continue; } } /* This is lazy and will pickup trailing spaces. */ - @s0 keyword @s1 ":" [^\x00\n]+ / "\n" => skip { + @s0 keyword @s1 ":" [^\x00\n]+ / "\n" => skip { struct pair keyword = pair(s0, s1); size_t *idx; struct score *score; - switch(scoremap_table_assign(&s->map, keyword, &idx)) { + switch(pair_map_table_assign(&scores->map, keyword, &idx)) { case TABLE_PRESENT: errno = EDOM; why = "new keyword already used"; case TABLE_ERROR: goto catch; /* /\ _Sic_. */ case TABLE_ABSENT: *idx = 0; break; /* Good. */ } - if(!(source = scorelist_array_new(&s->list))) goto catch; - *idx = (size_t)(score - s->list.data); - score->name.a = s0, score->name.b = s1; - score->desc.a = 0, score->desc.b = 0; + if(!(score = scorelist_array_new(&scores->list))) goto catch; + *idx = (size_t)(score - scores->list.data); + /*struct pair key, name; union date32 date; unsigned edges;*/ + score->key.a = s0, score->key.b = s1; + score->name.a = 0, score->name.b = 0; + score->date.year = 0, score->date.month = 0, score->date.day = 0; + score->edges = 0; date32_to_string(date, &datestr); fprintf(stderr, "%s: new source <%.*s> stored in list at %zu.\n", datestr, (int)(s1 - s0), s0, *idx); @@ -148,3 +135,40 @@ catch: "%s line %zu: %s.\n", buffer, datestr, line, why); return 0; } + +void scores_(struct scores *const s) { + if(!s) return; + score_tree_(&s->dates); + pair_map_table_(&s->map); + scorelist_array_(&s->list); +} + +struct scores scores(struct journal *const j) { + struct scores s + = { scorelist_array(), pair_map_table(), score_tree() }; + struct journal_iterator it; + union date32 k; + const char *v; + assert(j); + { /* Null is the first item for convenience, (TABLE_DEFAULT). */ + struct score *nul; + if(!(nul = scorelist_array_new(&s.list))) goto catch; + nul->key.a = nul->key.b = nul->name.a = nul->name.b = 0; + nul->date.u32 = 0; + nul->edges = 0; + } + it = journal_iterator(j); + while(journal_next(&it, &k, &v)) if(!scan(k, v, &s)) goto catch; + fprintf(stderr, "List of scores: %s.\n" + "Mapped to indices: %s.\n" + "Date-line tree: %s.\n", scorelist_array_to_string(&s.list), + pair_map_table_to_string(&s.map), score_tree_to_string(&s.dates)); + goto finally; +catch: + scores_(&s); +finally: + return s; +} + +int scores_is_empty(const struct scores *const s) + { return !s || !s->dates.root.node; } diff --git a/src/source.re.c b/src/source.re.c index a3dba70..cbdebe2 100644 --- a/src/source.re.c +++ b/src/source.re.c @@ -118,7 +118,7 @@ catch: fprintf(stderr, "%s line %zu: %s.\n", datestr, line, why); return 0; } -/** Dynamic memory allocation for `s` will be zero. */ + void sources_(struct sources *const s) { if(!s) return; source_tree_(&s->dates); @@ -140,10 +140,10 @@ struct sources sources(struct journal *const j) { } it = journal_iterator(j); while(journal_next(&it, &k, &v)) if(!scan(k, v, &s)) goto catch; - /*fprintf(stderr, "List of sources: %s.\n" + fprintf(stderr, "List of sources: %s.\n" "Mapped to indices: %s.\n" "Date-line tree: %s.\n", sourcelist_array_to_string(&s.list), - sourcemap_table_to_string(&s.map), source_tree_to_string(&s.dates));*/ + pair_map_table_to_string(&s.map), source_tree_to_string(&s.dates)); goto finally; catch: sources_(&s);