source -> kvpair

This commit is contained in:
Neil 2023-05-07 00:06:36 -07:00
parent 04c87cbfa6
commit e6a5242e73
2 changed files with 44 additions and 39 deletions

View File

@ -8,10 +8,10 @@
#define TREE_HEAD #define TREE_HEAD
#include "../src/tree.h" #include "../src/tree.h"
/* Source array. */ /* Eg, source array. */
struct source { struct pair name, desc; }; struct kvpair { struct pair key, value; };
#define ARRAY_NAME source #define ARRAY_NAME kvpair
#define ARRAY_TYPE struct source #define ARRAY_TYPE struct kvpair
#define ARRAY_HEAD #define ARRAY_HEAD
#include "../src/array.h" #include "../src/array.h"
@ -92,10 +92,14 @@ struct flight {
struct scan { struct scan {
struct { struct {
struct source_array array; struct kvpair_array array;
struct pairmap_table map; struct pairmap_table map;
struct linemap_tree dates; struct linemap_tree dates;
} sources, documents; } sources, documents;
struct {
struct kvpair_array array;
struct linemap_tree dates;
} contacts;
struct { struct {
struct place_array array; struct place_array array;
struct pairmap_table map; struct pairmap_table map;
@ -111,6 +115,7 @@ struct scan {
struct pairmap_table map; struct pairmap_table map;
struct linemap_tree dates; struct linemap_tree dates;
} edits; } edits;
//struct
struct glider_tree gliders; struct glider_tree gliders;
struct flight_tree flights; struct flight_tree flights;
struct kjv_tree kjvs; struct kjv_tree kjvs;
@ -118,7 +123,7 @@ struct scan {
void scan_(struct scan *); void scan_(struct scan *);
struct scan scan(struct journal *); struct scan scan(struct journal *);
const struct source *scan_source_lookup(struct scan *, const union line64); const struct kvpair *scan_source_lookup(struct scan *, const union line64);
void scan_score_graph(struct scan *); void scan_score_graph(struct scan *);
void scan_glider_graph(struct scan *); void scan_glider_graph(struct scan *);
void scan_flight_graph(struct scan *); void scan_flight_graph(struct scan *);

View File

@ -33,11 +33,11 @@ static void pair_to_string(const struct pair *const p,
*y = '\0'; *y = '\0';
} }
/* Array of sources. */ /* Array of kvpair. */
static void source_to_string(const struct source *const s, static void kvpair_to_string(const struct kvpair *const s,
char (*const z)[12]) { pair_to_string(&s->name, z); } char (*const z)[12]) { pair_to_string(&s->key, z); }
#define ARRAY_NAME source #define ARRAY_NAME kvpair
#define ARRAY_TYPE struct source #define ARRAY_TYPE struct kvpair
#define ARRAY_TO_STRING #define ARRAY_TO_STRING
#define ARRAY_BODY #define ARRAY_BODY
#include "../src/array.h" #include "../src/array.h"
@ -192,11 +192,11 @@ static int scan_day(struct scan *const scan, union date32 date,
<document> @s0 bralabel @s1 "]" => future { <document> @s0 bralabel @s1 "]" => future {
const union line64 key = { { (uint32_t)line, date } }; const union line64 key = { { (uint32_t)line, date } };
size_t *pi; size_t *pi;
struct source *doc; struct kvpair *doc;
if(!(doc = source_array_new(&scan->documents.array))) goto catch; if(!(doc = kvpair_array_new(&scan->documents.array))) goto catch;
doc->name.a = s0, doc->name.b = s1; doc->key.a = s0, doc->key.b = s1;
doc->desc.a = 0, doc->desc.b = 0; doc->value.a = 0, doc->value.b = 0;
assert(!future), future = &doc->desc; assert(!future), future = &doc->value;
switch(linemap_tree_bulk_assign(&scan->documents.dates, key, &pi)) { switch(linemap_tree_bulk_assign(&scan->documents.dates, key, &pi)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch; case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: case TREE_ABSENT:
@ -300,17 +300,17 @@ static int scan_day(struct scan *const scan, union date32 date,
<source> @s0 keyword @s1 ":" => future { <source> @s0 keyword @s1 ":" => future {
struct pair keyword = pair(s0, s1); struct pair keyword = pair(s0, s1);
size_t *idx; size_t *idx;
struct source *source; struct kvpair *source;
switch(pair_map_table_assign(&scan->sources.map, keyword, &idx)) { switch(pair_map_table_assign(&scan->sources.map, keyword, &idx)) {
case TABLE_PRESENT: errno = EDOM; fail = "new keyword already used"; case TABLE_PRESENT: errno = EDOM; fail = "new keyword already used";
case TABLE_ERROR: goto catch; /* /\ _Sic_. */ case TABLE_ERROR: goto catch; /* /\ _Sic_. */
case TABLE_ABSENT: *idx = 0; break; /* Good. */ case TABLE_ABSENT: *idx = 0; break; /* Good. */
} }
if(!(source = source_array_new(&scan->sources.array))) goto catch; if(!(source = kvpair_array_new(&scan->sources.array))) goto catch;
*idx = (size_t)(source - scan->sources.array.data); *idx = (size_t)(source - scan->sources.array.data);
source->name.a = s0, source->name.b = s1; source->key.a = s0, source->key.b = s1;
source->desc.a = 0, source->desc.b = 0; source->value.a = 0, source->value.b = 0;
assert(!future), future = &source->desc; assert(!future), future = &source->value;
fprintf(stderr, "%s[%zu]: new source <<%.*s>> stored at %zu.\n", fprintf(stderr, "%s[%zu]: new source <<%.*s>> stored at %zu.\n",
datestr, line, (int)(s1 - s0), s0, *idx); datestr, line, (int)(s1 - s0), s0, *idx);
goto also_source; goto also_source;
@ -713,11 +713,11 @@ void scan_(struct scan *const scan) {
linemap_tree_(&scan->documents.dates); linemap_tree_(&scan->documents.dates);
pair_map_table_(&scan->documents.map); pair_map_table_(&scan->documents.map);
source_array_(&scan->documents.array); kvpair_array_(&scan->documents.array);
linemap_tree_(&scan->sources.dates); linemap_tree_(&scan->sources.dates);
pair_map_table_(&scan->sources.map); pair_map_table_(&scan->sources.map);
source_array_(&scan->sources.array); kvpair_array_(&scan->sources.array);
} }
/** @param[jrnl] Must be constant throughout the use of the returned value. */ /** @param[jrnl] Must be constant throughout the use of the returned value. */
@ -730,11 +730,11 @@ struct scan scan(struct journal *const jrnl) {
/* Null is the first item for convenience, (TABLE_DEFAULT). */ /* Null is the first item for convenience, (TABLE_DEFAULT). */
{ {
struct source *nul; struct kvpair *nul;
if(!(nul = source_array_new(&scan.sources.array))) goto catch; if(!(nul = kvpair_array_new(&scan.sources.array))) goto catch;
*nul = (struct source){0}; *nul = (struct kvpair){0};
if(!(nul = source_array_new(&scan.documents.array))) goto catch; if(!(nul = kvpair_array_new(&scan.documents.array))) goto catch;
*nul = (struct source){0}; *nul = (struct kvpair){0};
} }
{ {
struct edit *nul; struct edit *nul;
@ -773,7 +773,7 @@ finally:
} }
/** Lookup the source of `x` in `scan`, if any. */ /** Lookup the source of `x` in `scan`, if any. */
static const struct source *source_lookup(struct scan *const scan, static const struct kvpair *source_lookup(struct scan *const scan,
const union line64 x) { const union line64 x) {
struct linemap_tree_iterator it; struct linemap_tree_iterator it;
assert(scan); assert(scan);
@ -786,7 +786,7 @@ static const struct source *source_lookup(struct scan *const scan,
} }
/** Lookup the source of `x` in `scan`, if any; public function for testing. */ /** Lookup the source of `x` in `scan`, if any; public function for testing. */
const struct source *scan_source_lookup(struct scan *const scan, const struct kvpair *scan_source_lookup(struct scan *const scan,
const union line64 x) { return source_lookup(scan, x); } const union line64 x) { return source_lookup(scan, x); }
void scan_score_graph(struct scan *const scan) { void scan_score_graph(struct scan *const scan) {
@ -881,9 +881,9 @@ void scan_glider_graph(struct scan *const scan) {
const struct glider *glider = glider_tree_value(&it); const struct glider *glider = glider_tree_value(&it);
char datestr[12]; char datestr[12];
date32_to_string(line.date, &datestr); date32_to_string(line.date, &datestr);
const struct source *src = source_lookup(scan, line); const struct kvpair *src = source_lookup(scan, line);
assert(src); assert(src);
if(!src->name.a) { fprintf(stderr, if(!src->key.a) { fprintf(stderr,
"Glider has no source at %s; ignoring.\n", datestr); continue; } "Glider has no source at %s; ignoring.\n", datestr); continue; }
printf("%s, ", datestr); printf("%s, ", datestr);
printf("%.*s, %" PRIu32 ", %" PRIu32, printf("%.*s, %" PRIu32 ", %" PRIu32,
@ -896,7 +896,7 @@ void scan_glider_graph(struct scan *const scan) {
flight->power.dual_min, flight->power.dual_min,
flight->power.pilot_min); flight->power.pilot_min);
break; */ break; */
printf(", %.*s\n", (int)(src->name.b - src->name.a), src->name.a); printf(", %.*s\n", (int)(src->key.b - src->key.a), src->key.a);
} }
printf("EOD\n" printf("EOD\n"
"# theozh https://stackoverflow.com/a/75466214/2472827\n" "# theozh https://stackoverflow.com/a/75466214/2472827\n"
@ -947,15 +947,15 @@ void scan_flight_graph(struct scan *const scan) {
const struct flight *flight = flight_tree_value(&it); const struct flight *flight = flight_tree_value(&it);
char datestr[12]; char datestr[12];
date32_to_string(line.date, &datestr); date32_to_string(line.date, &datestr);
const struct source *src = source_lookup(scan, line); const struct kvpair *src = source_lookup(scan, line);
assert(src); assert(src);
if(!src->name.a) { fprintf(stderr, if(!src->key.a) { fprintf(stderr,
"Source has no source at %s; ignoring.\n", datestr); continue; } "Source has no source at %s; ignoring.\n", datestr); continue; }
printf("%s, ", datestr); printf("%s, ", datestr);
printf("%.*s, %" PRIu32 ", %" PRIu32, printf("%.*s, %" PRIu32 ", %" PRIu32,
(int)(flight->reg.b - flight->reg.a), flight->reg.a, (int)(flight->reg.b - flight->reg.a), flight->reg.a,
flight->dual_min, flight->pilot_min); flight->dual_min, flight->pilot_min);
printf(", %.*s\n", (int)(src->name.b - src->name.a), src->name.a); printf(", %.*s\n", (int)(src->key.b - src->key.a), src->key.a);
} }
printf("EOD\n" printf("EOD\n"
"# theozh https://stackoverflow.com/a/75466214/2472827\n" "# theozh https://stackoverflow.com/a/75466214/2472827\n"
@ -1014,8 +1014,8 @@ void scan_kjv_graph(struct scan *const scan) {
const union line64 line = kjv_tree_key(&it); const union line64 line = kjv_tree_key(&it);
const struct kjvrange *const range = kjv_tree_value(&it); const struct kjvrange *const range = kjv_tree_value(&it);
size_t words = 0, newwords = 0; size_t words = 0, newwords = 0;
const struct source *src = source_lookup(scan, line); const struct kvpair *src = source_lookup(scan, line);
if(!src || !src->name.a) { errno = EDOM; goto catch; } if(!src || !src->key.a) { errno = EDOM; goto catch; }
date32_to_string(line.date, &datestr); /* Date. */ date32_to_string(line.date, &datestr); /* Date. */
kjvcite_to_string(range->start, &citestr); /* KJV cite. */ kjvcite_to_string(range->start, &citestr); /* KJV cite. */
for(union kjvcite c = range->start; ; c.verse++) { for(union kjvcite c = range->start; ; c.verse++) {
@ -1031,7 +1031,7 @@ void scan_kjv_graph(struct scan *const scan) {
printf("%s, %s", datestr, citestr); printf("%s, %s", datestr, citestr);
if(range->verse_end) printf("-%" PRIu32, range->verse_end); if(range->verse_end) printf("-%" PRIu32, range->verse_end);
printf(", %zu, %zu, %.*s\n", printf(", %zu, %zu, %.*s\n",
words, newwords, (int)(src->name.b - src->name.a), src->name.a); words, newwords, (int)(src->key.b - src->key.a), src->key.a);
} }
printf("EOD\n" printf("EOD\n"
"# theozh https://stackoverflow.com/a/75466214/2472827\n" "# theozh https://stackoverflow.com/a/75466214/2472827\n"