sourcelook -> sourcemap

This commit is contained in:
Neil 2023-02-03 22:54:49 -08:00
parent 90e84e3774
commit 1a2fbccf23
3 changed files with 30 additions and 18 deletions

View File

@ -19,9 +19,9 @@ struct source_tree_iterator { struct tree_source_iterator _; };
struct sourcelist_array { struct source *data; size_t size, capacity; };
struct sourcelook_bucket;
struct sourcelook_table {
struct sourcelook_bucket *buckets;
struct sourcemap_bucket;
struct sourcemap_table {
struct sourcemap_bucket *buckets;
uint32_t log_capacity, size, top;
};
@ -33,7 +33,7 @@ struct sourcelook_table {
#include <stddef.h>
struct sources {
struct sourcelist_array list;
struct sourcelook_table look;
struct sourcemap_table map;
struct source_tree dates;
};
struct source *sources_add(struct sources *, const union line64);

View File

@ -10,6 +10,7 @@
#include <assert.h>
/* `sourcelist` is an array of all the sources. */
static void sourcelist_to_string(const struct source *const s,
char (*const z)[12]) {
const char *a = s->name.a, *b;
@ -24,24 +25,29 @@ static void sourcelist_to_string(const struct source *const s,
#include "../src/array.h"
static int sourcelook_is_equal(const struct pair a, const struct pair b)
/* `sourcemap` maps from substring keywords to indices in `sourcelist`. */
static void sourcemap_to_string(const struct pair key, const size_t i,
char (*const a)[12]) { (void)key; sprintf(*a, "%zu", i); }
static int sourcemap_is_equal(const struct pair a, const struct pair b)
{ return pair_is_equal(a, b); }
static uint32_t sourcelook_hash(const struct pair p) { return pair_djb2(p); }
#define TABLE_NAME sourcelook
static uint32_t sourcemap_hash(const struct pair p) { return pair_djb2(p); }
#define TABLE_NAME sourcemap
#define TABLE_KEY struct pair
#define TABLE_UINT uint32_t
#define TABLE_VALUE size_t /* Index into `lookup_sources`. */
#define TABLE_VALUE size_t /* Index into source list. */
#define TABLE_DEFAULT 0
#define TABLE_TO_STRING
#include "../src/table.h"
/* `source` is a tree mapping from date-line to indices in `sourcelist`. */
static void source_to_string(const union line64 line, const size_t *const u,
char (*const a)[12]) { (void)u; date32_to_string(line.date, a); }
static int source_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME source
#define TREE_KEY union line64
#define TREE_VALUE size_t /* Index into `lookup_sources`. */
#define TREE_VALUE size_t /* Index into source list. */
#define TREE_COMPARE
#define TREE_TO_STRING
#include "../src/tree.h"
@ -89,9 +95,14 @@ static int scan(union date32 date, const char *const buffer,
<source> @s0 keyword @s1 / "\n" => skip { also_add_to_tree: {
struct pair p = pair(s0, s1);
size_t i;
if(!(i = sourcelook_table_get(&s->look, p)))
//const union line64 key = { ... };
if(!(i = sourcemap_table_get(&s->map, p)))
{ why = "keyword not introduced"; goto catch; }
printf("extracted <%.*s>\n", (int)(s1 - s0), s0);
/*switch(source_tree_assign(&s->source, )) {
...
}*/
date32_to_string(date, &datestr);
printf("%s: <%.*s>\n", datestr, (int)(s1 - s0), s0);
continue;
} }
/* This is lazy and will pickup trailing spaces. */
@ -99,7 +110,7 @@ static int scan(union date32 date, const char *const buffer,
struct pair keyword = pair(s0, s1);
size_t *idx;
struct source *source;
switch(sourcelook_table_assign(&s->look, keyword, &idx)) {
switch(sourcemap_table_assign(&s->map, keyword, &idx)) {
case TABLE_PRESENT: errno = EDOM; why = "new keyword already used";
case TABLE_ERROR: goto catch; /* /\ _Sic_. */
case TABLE_ABSENT: break; /* Good. */
@ -122,18 +133,17 @@ catch:
"%s line %zu: %s.\n", buffer, datestr, line, why);
return 0;
}
/** Dynamic memory allocation for `s` will be zero, <fn:sources_is_valid> will
be false. */
/** Dynamic memory allocation for `s` will be zero. */
void sources_(struct sources *const s) {
if(!s) return;
source_tree_(&s->dates);
sourcelook_table_(&s->look);
sourcemap_table_(&s->map);
sourcelist_array_(&s->list);
}
struct sources sources(struct journal *const j) {
struct sources s
= { sourcelist_array(), sourcelook_table(), source_tree() };
= { sourcelist_array(), sourcemap_table(), source_tree() };
struct journal_iterator it;
union date32 k;
const char *v;
@ -145,7 +155,10 @@ struct sources sources(struct journal *const j) {
}
it = journal_begin(j);
while(journal_next(&it, &k, &v)) if(!scan(k, v, &s)) goto catch;
printf("%s\n", sourcelist_array_to_string(&s.list));
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));
goto finally;
catch:
sources_(&s);

View File

@ -11,7 +11,6 @@ int main(void) {
struct journal j = journal();
struct sources s = sources(&j);
if(errno) goto catch;
printf("***success\n");
goto finally;
catch:
success = EXIT_FAILURE, perror("source");