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
#include "../src/tree.h"
/* Source array. */
struct source { struct pair name, desc; };
#define ARRAY_NAME source
#define ARRAY_TYPE struct source
/* Eg, source array. */
struct kvpair { struct pair key, value; };
#define ARRAY_NAME kvpair
#define ARRAY_TYPE struct kvpair
#define ARRAY_HEAD
#include "../src/array.h"
@ -92,10 +92,14 @@ struct flight {
struct scan {
struct {
struct source_array array;
struct kvpair_array array;
struct pairmap_table map;
struct linemap_tree dates;
} sources, documents;
struct {
struct kvpair_array array;
struct linemap_tree dates;
} contacts;
struct {
struct place_array array;
struct pairmap_table map;
@ -111,6 +115,7 @@ struct scan {
struct pairmap_table map;
struct linemap_tree dates;
} edits;
//struct
struct glider_tree gliders;
struct flight_tree flights;
struct kjv_tree kjvs;
@ -118,7 +123,7 @@ struct scan {
void scan_(struct scan *);
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_glider_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';
}
/* 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
/* Array of kvpair. */
static void kvpair_to_string(const struct kvpair *const s,
char (*const z)[12]) { pair_to_string(&s->key, z); }
#define ARRAY_NAME kvpair
#define ARRAY_TYPE struct kvpair
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"
@ -192,11 +192,11 @@ static int scan_day(struct scan *const scan, union date32 date,
<document> @s0 bralabel @s1 "]" => future {
const union line64 key = { { (uint32_t)line, date } };
size_t *pi;
struct source *doc;
if(!(doc = source_array_new(&scan->documents.array))) goto catch;
doc->name.a = s0, doc->name.b = s1;
doc->desc.a = 0, doc->desc.b = 0;
assert(!future), future = &doc->desc;
struct kvpair *doc;
if(!(doc = kvpair_array_new(&scan->documents.array))) goto catch;
doc->key.a = s0, doc->key.b = s1;
doc->value.a = 0, doc->value.b = 0;
assert(!future), future = &doc->value;
switch(linemap_tree_bulk_assign(&scan->documents.dates, key, &pi)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT:
@ -300,17 +300,17 @@ static int scan_day(struct scan *const scan, union date32 date,
<source> @s0 keyword @s1 ":" => future {
struct pair keyword = pair(s0, s1);
size_t *idx;
struct source *source;
struct kvpair *source;
switch(pair_map_table_assign(&scan->sources.map, keyword, &idx)) {
case TABLE_PRESENT: errno = EDOM; fail = "new keyword already used";
case TABLE_ERROR: goto catch; /* /\ _Sic_. */
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);
source->name.a = s0, source->name.b = s1;
source->desc.a = 0, source->desc.b = 0;
assert(!future), future = &source->desc;
source->key.a = s0, source->key.b = s1;
source->value.a = 0, source->value.b = 0;
assert(!future), future = &source->value;
fprintf(stderr, "%s[%zu]: new source <<%.*s>> stored at %zu.\n",
datestr, line, (int)(s1 - s0), s0, *idx);
goto also_source;
@ -713,11 +713,11 @@ void scan_(struct scan *const scan) {
linemap_tree_(&scan->documents.dates);
pair_map_table_(&scan->documents.map);
source_array_(&scan->documents.array);
kvpair_array_(&scan->documents.array);
linemap_tree_(&scan->sources.dates);
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. */
@ -730,11 +730,11 @@ struct scan scan(struct journal *const jrnl) {
/* Null is the first item for convenience, (TABLE_DEFAULT). */
{
struct source *nul;
if(!(nul = source_array_new(&scan.sources.array))) goto catch;
*nul = (struct source){0};
if(!(nul = source_array_new(&scan.documents.array))) goto catch;
*nul = (struct source){0};
struct kvpair *nul;
if(!(nul = kvpair_array_new(&scan.sources.array))) goto catch;
*nul = (struct kvpair){0};
if(!(nul = kvpair_array_new(&scan.documents.array))) goto catch;
*nul = (struct kvpair){0};
}
{
struct edit *nul;
@ -773,7 +773,7 @@ finally:
}
/** 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) {
struct linemap_tree_iterator it;
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. */
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); }
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);
char datestr[12];
date32_to_string(line.date, &datestr);
const struct source *src = source_lookup(scan, line);
const struct kvpair *src = source_lookup(scan, line);
assert(src);
if(!src->name.a) { fprintf(stderr,
if(!src->key.a) { fprintf(stderr,
"Glider has no source at %s; ignoring.\n", datestr); continue; }
printf("%s, ", datestr);
printf("%.*s, %" PRIu32 ", %" PRIu32,
@ -896,7 +896,7 @@ void scan_glider_graph(struct scan *const scan) {
flight->power.dual_min,
flight->power.pilot_min);
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"
"# 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);
char datestr[12];
date32_to_string(line.date, &datestr);
const struct source *src = source_lookup(scan, line);
const struct kvpair *src = source_lookup(scan, line);
assert(src);
if(!src->name.a) { fprintf(stderr,
if(!src->key.a) { fprintf(stderr,
"Source has no source at %s; ignoring.\n", datestr); continue; }
printf("%s, ", datestr);
printf("%.*s, %" PRIu32 ", %" PRIu32,
(int)(flight->reg.b - flight->reg.a), flight->reg.a,
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"
"# 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 struct kjvrange *const range = kjv_tree_value(&it);
size_t words = 0, newwords = 0;
const struct source *src = source_lookup(scan, line);
if(!src || !src->name.a) { errno = EDOM; goto catch; }
const struct kvpair *src = source_lookup(scan, line);
if(!src || !src->key.a) { errno = EDOM; goto catch; }
date32_to_string(line.date, &datestr); /* Date. */
kjvcite_to_string(range->start, &citestr); /* KJV cite. */
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);
if(range->verse_end) printf("-%" PRIu32, range->verse_end);
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"
"# theozh https://stackoverflow.com/a/75466214/2472827\n"