From bfd8d797f98efb7ecfab1290b88d619df9b9497a Mon Sep 17 00:00:00 2001 From: Neil Date: Sun, 30 Apr 2023 11:06:27 -0700 Subject: [PATCH] factored out date32_to_string --- src/scan.h | 20 ++++++++------- src/scan.re.c | 68 ++++++++++++++++++++++++--------------------------- 2 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/scan.h b/src/scan.h index ccaca4e..d05f7da 100644 --- a/src/scan.h +++ b/src/scan.h @@ -15,14 +15,12 @@ struct source { struct pair name, desc; }; #define ARRAY_HEAD #include "../src/array.h" -/* Place table. */ -struct place { double x, y; }; -#define TABLE_NAME place -#define TABLE_KEY struct pair -#define TABLE_VALUE struct place -#define TABLE_UINT uint32_t -#define TABLE_HEAD -#include "../src/table.h" +/* Place array. */ +struct place { struct pair name; double x, y; }; +#define ARRAY_NAME place +#define ARRAY_TYPE struct place +#define ARRAY_HEAD +#include "../src/array.h" /* Score array. */ struct score { @@ -88,12 +86,16 @@ struct scan { struct pairmap_table map; struct linemap_tree dates; } sources; + struct { + struct place_array array; + struct pairmap_table map; + struct linemap_tree dates; + } places; struct { struct score_array array; struct pairmap_table map; struct linemap_tree dates; } scores; - struct place_table places; struct glider_tree gliders; struct flight_tree flights; struct kjv_tree kjvs; diff --git a/src/scan.re.c b/src/scan.re.c index 2d954bd..cbc47ec 100644 --- a/src/scan.re.c +++ b/src/scan.re.c @@ -23,42 +23,37 @@ static int linemap_compare(const union line64 a, const union line64 b) #define TREE_BODY #include "../src/tree.h" -/* Looking up places with labels. */ -static int place_is_equal(const struct pair a, const struct pair b) - { return pair_is_equal(a, b); } -static uint32_t place_hash(const struct pair a) - { return pair_djb2(a); } -#define TABLE_NAME place -#define TABLE_KEY struct pair -#define TABLE_VALUE struct place -#define TABLE_UINT uint32_t -#define TABLE_BODY -#include "../src/table.h" - -/* Array of sources. */ -static void source_to_string(const struct source *const s, +/** `p` to `z`. */ +static void pair_to_string(const struct pair *const p, char (*const z)[12]) { - const char *a = s->name.a, *b; + const char *a = p->a, *b; char *y = *z; - b = s->name.b <= a + 11 ? s->name.b : a + 11; + b = p->b <= a + 11 ? p->b : a + 11; while(a < b) *(y++) = *(a++); *y = '\0'; } + +/* Array of sources. */ +static void source_to_string(const struct source *const s, + char (*const z)[12]) { pair_to_string(&s->name, z); } #define ARRAY_NAME source #define ARRAY_TYPE struct source #define ARRAY_TO_STRING #define ARRAY_BODY #include "../src/array.h" +/* Array of places. */ +static void place_to_string(const struct place *const p, + char (*const z)[12]) { pair_to_string(&p->name, z); } +#define ARRAY_NAME place +#define ARRAY_TYPE struct place +#define ARRAY_TO_STRING +#define ARRAY_BODY +#include "../src/array.h" + /* Array of scores. */ static void score_to_string(const struct score *const s, - char (*const z)[12]) { - const char *a = s->name.a, *b; - char *y = *z; - b = s->name.b <= a + 11 ? s->name.b : a + 11; - while(a < b) *(y++) = *(a++); - *y = '\0'; -} + char (*const z)[12]) { pair_to_string(&s->name, z); } #define ARRAY_NAME score #define ARRAY_TYPE struct score #define ARRAY_TO_STRING @@ -122,6 +117,8 @@ static int scan_day(struct scan *const scan, union date32 date, assert(scan && date.u32 && buffer); YYCURSOR = YYMARKER = yyt1 = buffer; + date32_to_string(date, &datestr); + /*!re2c /**/ re2c:define:YYCTYPE = char; re2c:yyfill:enable = 0; @@ -162,12 +159,12 @@ static int scan_day(struct scan *const scan, union date32 date, * { fail = "place unrecognized"; goto catch; } - @t0 "(" decimal "," decimal ")" @t1 ws+ @s0 label @s1 / "\n" - => skip { + "(" @t0 decimal "," @t1 decimal ")" + ws+ @s0 label @s1 / "\n" => skip { const struct pair keyword = pair(s0, s1); - const struct pair coords = pair(t0, t1); - fprintf(stderr, "place: %.*s <<%.*s>>\n", - (int)(t1-t0), t0, (int)(s1-s0), s0); + const double x = strtod(t0, 0), y = strtod(t1, 0); /* Safe? */ + fprintf(stderr, "%s: new place: (%.*s)(%f,%f) <<%.*s>>\n", + datestr, (int)(s0 - t0), t0 - 1, x, y, (int)(s1-s0), s0); continue; } @s0 label @s1 / "\n" => skip { @@ -189,7 +186,6 @@ static int scan_day(struct scan *const scan, union date32 date, case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch; case TREE_ABSENT: *pi = i; break; } - date32_to_string(date, &datestr); fprintf(stderr, "%s: source <<%.*s>>\n", datestr, (int)(s1 - s0), s0); } continue; } @@ -207,7 +203,6 @@ static int scan_day(struct scan *const scan, union date32 date, *idx = (size_t)(source - scan->sources.array.data); source->name.a = s0, source->name.b = s1; source->desc.a = 0, source->desc.b = 0; - date32_to_string(date, &datestr); fprintf(stderr, "%s: new source <<%.*s>> stored in list at %zu.\n", datestr, (int)(s1 - s0), s0, *idx); goto also_source; @@ -233,7 +228,6 @@ static int scan_day(struct scan *const scan, union date32 date, case TREE_ERROR: goto catch; case TREE_ABSENT: *pidx = idx; break; } - date32_to_string(date, &datestr); fprintf(stderr, "%s: score <%.*s>\n", datestr, (int)(s1 - s0), s0); } continue; } /* New score. */ @@ -255,7 +249,6 @@ static int scan_day(struct scan *const scan, union date32 date, new_score->name.a = 0, new_score->name.b = 0; new_score->date.u32 = new_score->last.u32 = 0; new_score->edges = 0, new_score->score = 0; - date32_to_string(date, &datestr); fprintf(stderr, "%s: new score <%.*s> stored in list at %zu.\n", datestr, (int)(s1 - s0), s0, *idx); goto new_score; @@ -302,7 +295,6 @@ static int scan_day(struct scan *const scan, union date32 date, * { fail = "glider launch"; goto catch; } ws* @s0 airport @s1 ws* ";" => glider_how { new_glider->launch.a = s0, new_glider->launch.b = s1; - date32_to_string(date, &datestr); fprintf(stderr, "%s: glider <%.*s> at <%.*s>\n", datestr, (int)(new_glider->reg.b - new_glider->reg.a), new_glider->reg.a, @@ -378,7 +370,6 @@ static int scan_day(struct scan *const scan, union date32 date, ws* @t0 airport @t1 ws* ";" => flight_pic { new_flight->launch.a = s0, new_flight->launch.b = s1; new_flight->landing.a = t0, new_flight->landing.b = t1; - date32_to_string(date, &datestr); fprintf(stderr, "%s: flight <%.*s> at <%.*s>\n", datestr, (int)(new_flight->reg.b - new_flight->reg.a), new_flight->reg.a, @@ -546,7 +537,6 @@ static int scan_day(struct scan *const scan, union date32 date, value->verse_end = verse_end; break; } - date32_to_string(date, &datestr); fprintf(stderr, "%s: KJV %s %" PRIu32 ":%" PRIu32, datestr, kjv_book_string[book], chapter, verse); if(verse_end) fprintf(stderr, "-%u", verse_end); @@ -572,10 +562,15 @@ void scan_(struct scan *const scan) { kjv_tree_(&scan->kjvs); flight_tree_(&scan->flights); glider_tree_(&scan->gliders); - place_table_(&scan->places); + linemap_tree_(&scan->scores.dates); pair_map_table_(&scan->scores.map); score_array_(&scan->scores.array); + + linemap_tree_(&scan->places.dates); + pair_map_table_(&scan->places.map); + place_array_(&scan->places.array); + linemap_tree_(&scan->sources.dates); pair_map_table_(&scan->sources.map); source_array_(&scan->sources.array); @@ -608,6 +603,7 @@ struct scan scan(struct journal *const jrnl) { /* Scans make trees bulk-loaded; fix to real tree. */ if(!linemap_tree_bulk_finish(&scan.sources.dates) + || !linemap_tree_bulk_finish(&scan.places.dates) || !linemap_tree_bulk_finish(&scan.scores.dates) || !glider_tree_bulk_finish(&scan.gliders) || !flight_tree_bulk_finish(&scan.flights)