From 3fb5430080199a6d92a63f8259fe4a88df9b83ba Mon Sep 17 00:00:00 2001 From: JSDurand Date: Tue, 1 Feb 2022 12:22:34 +0800 Subject: need to stop abusing hash tables Hash tables take too much space! If I use hash tables, the length of the input will be severely limited, to an unacceptable extent. So we have to use arrays instead. --- src/grammar.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 8 deletions(-) (limited to 'src/grammar.c') diff --git a/src/grammar.c b/src/grammar.c index afb9353..5ed0b96 100644 --- a/src/grammar.c +++ b/src/grammar.c @@ -46,12 +46,44 @@ rg_nth(CCR_MOD(Rule_group *) rg, NUM n) struct PT_DATA_s { dfa *dfap; - /* NULL-terminated strings */ - char *user_name; - char *raw_name; + str *user_name; + str *raw_name; }; -typedef struct PT_DATA_s PTD; +PTD * +new_ptd(str *user_name, str *raw_name, dfa *dfap) +{ + PTD *result = NULL; + SAFE_MALLOC(PTD, result, 1, return NULL;); + + result->dfap = dfap; + result->user_name = user_name; + result->raw_name = raw_name; + + return result; +} + +void +destroy_ptd(PTD *p, int flag) +{ + destroy_dfa(p->dfap); + destroy_str(p->user_name, flag); + destroy_str(p->raw_name, flag); + + free(p); +} + +static void +destroy_ptd_free_all(void *e) +{ + destroy_ptd((PTD *)e, 1); +} + +static void +destroy_ptd_no_free(void *e) +{ + destroy_ptd((PTD *)e, 0); +} /* TODO: Add some statistic counts to assist the hash tables. For example, store the total number of terminals as an integer; then @@ -72,8 +104,8 @@ struct Grammar_s { To print them, first encode them into normal strings. */ List *names; - /* an array of predicates. */ - PTD *predicates; + /* a list of predicates, of type PTD. */ + List *predicates; }; static void @@ -186,10 +218,11 @@ new_grammar() /* We classify the rules into different rule groups. */ BOOL -build_grammar(Grammar *g, const List *rules, CC_MOD(List *) names) +build_grammar(Grammar *g, const List *rules, + CC_MOD(List *) names, CC_MOD(List *) predicates) { g->names = (List *) names; - g->predicates = NULL; + g->predicates = predicates; NUM len = list_length(names); NUM rule_len = list_length(rules); @@ -486,6 +519,12 @@ destroy_grammar(void *grammar, int flag) destroy_list(g->names, 0); + if (g->predicates) { + if (flag) map_list(g->predicates, destroy_ptd_free_all); + else map_list(g->predicates, destroy_ptd_no_free); + destroy_list(g->predicates, 0); + } + free(grammar); } -- cgit v1.2.3-18-g5258