From 510b10b96b546fcc6c6b6be85050305ddd192a41 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Sat, 5 Feb 2022 17:30:11 +0800 Subject: replace some hash table usage by tuples Previously I used hash tables, which consume too much memory. Now the critical parts are replaced by a new hand-written library called "tuple.h". Now we can easily parse larger inputs. I haven't tested its limits, though. --- src/test/check_crf.c | 195 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 159 insertions(+), 36 deletions(-) (limited to 'src/test/check_crf.c') diff --git a/src/test/check_crf.c b/src/test/check_crf.c index d99a838..72931f8 100644 --- a/src/test/check_crf.c +++ b/src/test/check_crf.c @@ -2,9 +2,116 @@ #include "../util.h" #include "../crf.h" +#define READ_INTO_CPA(N, U, I, VA, VI, CP) do { \ + U = new_utf8(N, 1); \ + I = get_info((str *)U, 0); \ + VA = MYALLOC(NUM, 1); \ + VI = 0; \ + for (NUM index = 0; \ + I.value >= 0 && index < str_length((str *) U); \ + index += I.step, VI++) { \ + I = get_info((str *)U, index); \ + *(VA+VI) = I.value; \ + } \ + CP = MYALLOC(cpa, 1); \ + CP->array = VA; \ + CP->size = VI; \ + add_to_list(names, CP); \ + destroy_str((str *)U, 1); \ + } while (0) + +#define READ_TNT_STRING(LEFT, FORMAT, LEN, ...) do { \ + tnt_string = new_tnt_string(FORMAT, LEN, __VA_ARGS__); \ + if (!tnt_string) { \ + fleprintf("left = %d, f = %s, l = %d, " \ + "cannot create tnt string\n", \ + LEFT, FORMAT, LEN); \ + map_list(rules, destroy_rule_and_free_all); \ + destroy_list(rules, 0); \ + map_list(names, destroy_cpa_and_free_all); \ + destroy_list(names, 0); \ + return 1; \ + } \ + rule = new_rule(LEFT, tnt_string); \ + add_to_list(rules, rule); \ + } while (0) + +static void +print_label3(pair3 label) +{ + printf("(%ld, %ld, %ld)\n", label.x, label.y, label.z); +} + int main(int UNUSED argc, char ** UNUSED argv) { + /* Have a grammar for testing */ + + List *tnt_string = NULL; + Rule *rule = NULL; + List *rules = new_list(); + List *names = new_list(); + + char *name = MYALLOC(char, 2); + *(name+1) = 0; + *name = 'E'; + + utf8* uname = NULL; + + str_info info = EMPTY_STR_INFO; + + NUM *varray = NULL; + NUM vindex = 0; + cpa *cpap = NULL; + + READ_INTO_CPA(name, uname, info, varray, vindex, cpap); + + name = MYALLOC(char, 2); + *(name+1) = 0; + *name = 'T'; + + READ_INTO_CPA(name, uname, info, varray, vindex, cpap); + + name = MYALLOC(char, 2); + *(name+1) = 0; + *name = 'F'; + + READ_INTO_CPA(name, uname, info, varray, vindex, cpap); + + name = MYALLOC(char, 2); + *(name+1) = 0; + *name = 'D'; + + READ_INTO_CPA(name, uname, info, varray, vindex, cpap); + + READ_TNT_STRING(0, "n", 1, (NT) 1); + READ_TNT_STRING(0, "ntn", 3, (NT) 1, (T) 43, (NT) 1); + + READ_TNT_STRING(1, "n", 1, (NT) 2); + READ_TNT_STRING(1, "ntn", 3, (NT) 2, (T) 42, (NT) 2); + + READ_TNT_STRING(2, "n", 1, (NT) 3); + READ_TNT_STRING(2, "tnt", 3, (T) 40, (NT) 0, (T) 41); + + READ_TNT_STRING(3, "tn", 2, (T) 48, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 49, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 50, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 51, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 52, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 53, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 54, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 55, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 56, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 57, (NT) 3); + rule = new_rule(3, new_list()); + add_to_list(rules, rule); + + Grammar *g = new_grammar(); + + build_grammar(g, rules, names, NULL); + + print_grammar(g); + procr *pr = NULL; procu *pu = NULL; @@ -12,14 +119,21 @@ main(int UNUSED argc, char ** UNUSED argv) prodecor *prod = NULL; + NUM input_len = 10; + /* check crf */ - crf *crfp = new_crf(); + crf *crfp = new_crf(g, input_len); + if (crfp == NULL) { + fleprintf0("Fail to create new CRF\n"); + goto cleanup; + } + pair2 p = { 0 }; p.x = 0; p.y = 1; - if (crf_find_node(crfp, p) == NULL) { + if (!(crf_find_node(crfp, p))) { if (crf_add_node(crfp, p)) { fleprintf0("fail to add node\n"); goto cleanup; @@ -28,7 +142,7 @@ main(int UNUSED argc, char ** UNUSED argv) } } - if (crf_find_node(crfp, p) == NULL) { + if (!(crf_find_node(crfp, p))) { fleprintf0("Fail to find node\n"); goto cleanup; } else { @@ -41,39 +155,46 @@ main(int UNUSED argc, char ** UNUSED argv) fleprintf0("Fail to add edge\n"); goto cleanup; } else { - printf("Successfully add edge from (0, 1) to zero\n"); + printf("Successfully add edge from (0, 1) to (0, 0, 0, 0)\n"); } - if (crf_find_node(crfp, p)) { - printf("Printing edges for (0, 1)...\n"); + p4.u = 1; -#define TABLE (crf_find_node(crfp, p)) + if (crf_add_edge(crfp, p, p4)) { + fleprintf0("Fail to add edge\n"); + goto cleanup; + } else { + printf("Successfully add edge from (0, 1) to (0, 0, 0, 1)\n"); + } - for (NUM i = 0; i < ht_size(TABLE); i++) { -#define KEY (*((pair4**) ht_keys(TABLE)+i)) - printf("(%ld, %ld, %ld, %ld)", - KEY->x, KEY->y, KEY->z, KEY->w); - if (i+1x, prod->y, prod->z, prod->w, grade); + prod->x, prod->y, prod->z, prod->u, grade); cleanup: - destroy_crf(crfp); + if (crfp) destroy_crf(crfp); + if (spap) destroy_spa(spap); + + destroy_grammar(g, 1); + destroy_list(rules, 1); if (pr) destroy_procr(pr); if (pu) destroy_procu(pu); - if (spap) destroy_spa(spap); if (prod) free(prod); return 0; -- cgit v1.2.3-18-g5258