sourcemap to pairmap

This commit is contained in:
Neil 2023-03-31 18:54:45 -07:00
parent b8b216b37a
commit 2e544eb1aa
6 changed files with 124 additions and 13 deletions

View File

@ -30,7 +30,7 @@ else
CF += -g
endif
projects := bin/test-text bin/test-journal bin/test-source bin/test-kjv bin/kjv bin/flight
projects := bin/test-text bin/test-journal bin/test-source bin/test-kjv bin/kjv bin/flight bin/score
#docs := $(patsubst test/test_%.c, doc/%.md, $(wildcard test/test_*.c))
default: $(projects)
@ -42,6 +42,7 @@ bin/test-source: build/text.o build/pair.o build/journal.o build/source.o build/
bin/test-kjv: build/text.o build/pair.o build/kjvcite.o build/test_kjv.o
bin/kjv: build/text.o build/pair.o build/journal.o build/kjvcite.o build/kjv.o build/source.o
bin/flight: build/text.o build/pair.o build/journal.o build/source.o build/flights.o build/flighthours.o
bin/score: build/text.o build/pair.o build/journal.o build/scan_score.o
bin/%:
@echo "\033[1;36mlinking $@\033[0m"

View File

@ -9,6 +9,7 @@
#include <stdint.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h> /* sprintf */
/** @return Constructs `a` and `b` as a pair. */
struct pair pair(const char *const a, const char *const b) {
@ -78,3 +79,18 @@ uint32_t pair_djb2(struct pair p) {
}
return hash;
}
/* Maps from substring keywords to indices, default zero. */
static void pairmap_to_string(const struct pair key, const size_t i,
char (*const a)[12]) { (void)key; sprintf(*a, "%zu", i); }
static int pairmap_is_equal(const struct pair a, const struct pair b)
{ return pair_is_equal(a, b); }
static uint32_t pairmap_hash(const struct pair p) { return pair_djb2(p); }
#define TABLE_NAME pairmap
#define TABLE_KEY struct pair
#define TABLE_UINT uint32_t
#define TABLE_VALUE size_t
#define TABLE_DEFAULT 0 /* Default set at zero. */
#define TABLE_TO_STRING
#define TABLE_BODY
#include "../src/table.h"

View File

@ -12,6 +12,15 @@ 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);
/* 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_HEAD
#include "../src/table.h"
#endif /* guard --> */

View File

@ -1,4 +1,33 @@
#include "helper.h"
#include "flight.h"
#include "pair.h" /* pair */
#include "journal.h" /* date32, line64 */
struct score { struct pair key, name; union date32 day; unsigned node; };
int scan_score(union date32, const char *, struct flight_tree *);
#define ARRAY_NAME scorelist
#define ARRAY_TYPE struct score
#define ARRAY_HEAD
#include "../src/array.h"
#include <stddef.h> /* size_t */
#include <stdint.h> /* uint32_t */
/* fixme: map is from pair -> size_t, nothing to do with score. Compress. */
#define TABLE_NAME scoremap
#define TABLE_KEY struct pair
#define TABLE_UINT uint32_t
#define TABLE_VALUE size_t
#define TABLE_HEAD
#include "../src/table.h"
#define TREE_NAME score
#define TREE_KEY union line64
#define TREE_VALUE size_t
#define TREE_HEAD
#include "../src/tree.h"
struct scores {
struct scorelist_array list;
struct scoremap_table map;
struct score_tree dates;
};
int scan_score(union date32, const char *, struct score_tree *);

View File

@ -1,22 +1,78 @@
/** @license 2022 Neil Edelman, distributed under the terms of the
[MIT License](https://opensource.org/licenses/MIT).
Scan journal entries for kjv references. */
Scan journal entries for score. */
#if 0
#include "../src/journal.h"
#include "../src/scan_flight.h"
#include "../src/scan_score.h"
#include <inttypes.h> /* C99 */
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#endif
#include "../src/journal.h"
#include "../src/scan_score.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
/* One array. */
static void scorelist_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';
}
#define ARRAY_NAME scorelist
#define ARRAY_TYPE struct score
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"
/* Maps from substring keywords to indices. */
static void scoremap_to_string(const struct pair key, const size_t i,
char (*const a)[12]) { (void)key; sprintf(*a, "%zu", i); }
static int scoremap_is_equal(const struct pair a, const struct pair b)
{ return pair_is_equal(a, b); }
static uint32_t scoremap_hash(const struct pair p) { return pair_djb2(p); }
#define TABLE_NAME scoremap
#define TABLE_KEY struct pair
#define TABLE_UINT uint32_t
#define TABLE_VALUE size_t /* Index into array. */
#define TABLE_DEFAULT 0 /* Default set at zero. */
#define TABLE_TO_STRING
#define TABLE_BODY
#include "../src/table.h"
/* Tree mapping from date-line to indices. */
static void score_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 score_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME score
#define TREE_KEY union line64
#define TREE_VALUE size_t
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_DEFAULT 0
#define TREE_BODY
#include "../src/tree.h"
/*!conditions:re2c*/
int flight_scan(union date32 date, const char *const buffer,
struct flights *const f) {
int scan_score(union date32 date, const char *const buffer,
struct score_tree *const score) {
const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *yyt3, *s0, *s1, *t0, *t1;
enum flight_type type = Glider;
enum YYCONDTYPE condition = yycline;
size_t line = 1;
char datestr[12] = {0};
@ -47,9 +103,9 @@ int flight_scan(union date32 date, const char *const buffer,
<line> "::" / [^:] :=> score
<score> * { why = "default score unrecognized"; goto catch; }
<source> @s0 keyword @s1 / "\n" => skip { also_add_to_tree: {
<score> @s0 keyword @s1 / "\n" => skip { also_add_to_tree: {
const struct pair keyword = pair(s0, s1);
const union line64 key = { { (uint32_t)line, date } };
const union line64 key = { { line.u32, date } };
size_t i, *pi;
if(line > UINT32_MAX)
{ errno = ERANGE; why = "too many lines of text"; goto catch; }

View File

@ -107,7 +107,7 @@ static int scan(union date32 date, const char *const buffer,
printf("%s: <%.*s>\n", datestr, (int)(s1 - s0), s0);*/
continue;
} }
/* This is lazy and will pickup trailing spaces. */
/* New source. This is lazy and will pickup trailing spaces. */
<source> @s0 keyword @s1 ":" [^\x00\n]+ / "\n" => skip {
struct pair keyword = pair(s0, s1);
size_t *idx;
@ -171,7 +171,7 @@ int sources_is_empty(const struct sources *const s)
{ return !s || !s->dates.root.node; }
/** Lookup the last source in `range` in sources `s`. They are invalidated on
adding a source, (probably fine.) */
adding a source, (currently fine because we get all at once.) */
const struct source *source_lookup(struct sources *const s,
const union line64 x) {
struct source_tree_iterator it;