sourcemap becomes pairmap

This commit is contained in:
Neil 2023-03-31 19:31:25 -07:00
parent 2e544eb1aa
commit bed77ed4f2
4 changed files with 25 additions and 34 deletions

View File

@ -71,7 +71,7 @@ int pair_is_string(struct pair x, const char *y) {
}
/** @return A djb2 <http://www.cse.yorku.ca/~oz/hash.html> hash of `p`. */
uint32_t pair_djb2(struct pair p) {
static uint32_t pair_djb2(struct pair p) {
uint32_t hash = 5381, c;
while(p.a < p.b) {
c = (unsigned char)*p.a++;
@ -94,3 +94,13 @@ static uint32_t pairmap_hash(const struct pair p) { return pair_djb2(p); }
#define TABLE_TO_STRING
#define TABLE_BODY
#include "../src/table.h"
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)
{ 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)
{ return pairmap_table_assign(t, key, content); }
size_t pair_map_table_get(struct pairmap_table *const t, const struct pair key)
{ return pairmap_table_get(t, key); }

View File

@ -12,15 +12,21 @@ int pair_hours_to_minutes(const char *h0, const char *const h1,
const char *m0, const char *const m1, uint32_t *const n);
int pair_is_equal(struct pair, struct pair);
int pair_is_string(struct pair, const char *);
/*fixme*/
uint32_t pair_djb2(const struct pair p);
/*fixme
uint32_t pair_djb2(const struct pair p);*/
/* Supporting pair -> size_t for looking up in arrays. */
#define TABLE_NAME pairmap
#define TABLE_KEY struct pair
#define TABLE_UINT uint32_t
#define TABLE_VALUE size_t
#define TABLE_UINT uint32_t
#define TABLE_HEAD
#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 *);
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);
#endif /* guard --> */

View File

@ -7,15 +7,6 @@ struct source { struct pair name, desc; };
#include "../src/array.h"
#include <stddef.h> /* size_t */
#include <stdint.h> /* uint32_t */
#define TABLE_NAME sourcemap
#define TABLE_KEY struct pair
#define TABLE_UINT uint32_t
#define TABLE_VALUE size_t
#define TABLE_HEAD
#include "../src/table.h"
#include "../src/journal.h" /* line64 */
#define TREE_NAME source
@ -26,7 +17,7 @@ struct source { struct pair name, desc; };
struct sources {
struct sourcelist_array list;
struct sourcemap_table map;
struct pairmap_table map;
struct source_tree dates;
};
struct source *sources_add(struct sources *, const union line64);

View File

@ -24,22 +24,6 @@ static void sourcelist_to_string(const struct source *const s,
#include "../src/array.h"
/* `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 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 source list. */
#define TABLE_DEFAULT 0 /* Default set at zero. */
#define TABLE_TO_STRING
#define TABLE_BODY
#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); }
@ -96,7 +80,7 @@ static int scan(union date32 date, const char *const buffer,
size_t i, *pi;
if(line > UINT32_MAX)
{ errno = ERANGE; why = "too many lines of text"; goto catch; }
if(!(i = sourcemap_table_get(&s->map, keyword)))
if(!(i = pair_map_table_get(&s->map, keyword)))
{ why = "keyword not introduced"; goto catch; }
switch(source_tree_try(&s->dates, key, &pi)) {
case TREE_PRESENT: why = "duplicate key"; /* _Sic_. */
@ -112,7 +96,7 @@ static int scan(union date32 date, const char *const buffer,
struct pair keyword = pair(s0, s1);
size_t *idx;
struct source *source;
switch(sourcemap_table_assign(&s->map, keyword, &idx)) {
switch(pair_map_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: *idx = 0; break; /* Good. */
@ -138,13 +122,13 @@ catch:
void sources_(struct sources *const s) {
if(!s) return;
source_tree_(&s->dates);
sourcemap_table_(&s->map);
pair_map_table_(&s->map);
sourcelist_array_(&s->list);
}
struct sources sources(struct journal *const j) {
struct sources s
= { sourcelist_array(), sourcemap_table(), source_tree() };
= { sourcelist_array(), pair_map_table(), source_tree() };
struct journal_iterator it;
union date32 k;
const char *v;