diff options
author | Marvin Borner | 2023-05-29 23:06:37 +0200 |
---|---|---|
committer | Marvin Borner | 2023-05-29 23:06:37 +0200 |
commit | d347a2fa6483059e6397d2b70e82aa657f1144d2 (patch) | |
tree | 70742871fb40bfe6fef3a50a2c307b3ba2682f4e /src/term.c | |
parent | bc9c636a4768ded2726f54fa15b296eef992ae97 (diff) |
checkpoint
Diffstat (limited to 'src/term.c')
-rw-r--r-- | src/term.c | 39 |
1 files changed, 20 insertions, 19 deletions
@@ -46,8 +46,7 @@ void term_print(struct term *term) struct term *term_rehash_abs(struct term *head, struct term *term) { - if (head->u.abs.term->hash == term->hash) - return head; + /* assert(head->u.abs.term->hash != term->hash); */ hash_t res = hash((uint8_t *)&head->type, sizeof(head->type), term->hash); @@ -56,14 +55,10 @@ struct term *term_rehash_abs(struct term *head, struct term *term) struct term *match = map_get(res); if (match) { // already exists - term_refer(match, head->depth); - term_deref(head); return match; } else { // create new struct term *new = term_new(ABS, res, head->depth); new->u.abs.term = term; - term_refer(term, head->depth + 1); - term_deref(head); map_set(new, res); return new; } @@ -72,9 +67,8 @@ struct term *term_rehash_abs(struct term *head, struct term *term) struct term *term_rehash_app(struct term *head, struct term *lhs, struct term *rhs) { - if (head->u.app.lhs->hash == lhs->hash && - head->u.app.rhs->hash == rhs->hash) - return head; + /* assert(head->u.app.lhs->hash != lhs->hash || */ + /* head->u.app.rhs->hash != rhs->hash); */ hash_t res = hash((uint8_t *)&head->type, sizeof(head->type), lhs->hash); @@ -84,18 +78,18 @@ struct term *term_rehash_app(struct term *head, struct term *lhs, struct term *match = map_get(res); if (match) { // already exists - term_refer(match, head->depth); - term_deref(head); + /* term_refer(match, head->depth); */ + /* term_deref(head); */ return match; } else { // create new struct term *new = term_new(APP, res, head->depth); new->u.app.lhs = lhs; new->u.app.rhs = rhs; - if (head->u.app.lhs->hash != lhs->hash) - term_refer(lhs, head->depth + 1); - if (head->u.app.rhs->hash != rhs->hash) - term_refer(rhs, head->depth + 1); - term_deref(head); + /* if (head->u.app.lhs->hash != lhs->hash) */ + /* term_refer(lhs, head->depth + 1); */ + /* if (head->u.app.rhs->hash != rhs->hash) */ + /* term_refer(rhs, head->depth + 1); */ + /* term_deref(head); */ map_set(new, res); return new; } @@ -120,6 +114,15 @@ void term_refer(struct term *term, size_t depth) term_refer_head(term, depth); } +void term_deref_head(struct term *term) +{ + term->refs--; + if (term->refs == 0) { + map_delete(term); + free(term); + } +} + void term_deref(struct term *term) { if (term->type == ABS) { @@ -129,7 +132,5 @@ void term_deref(struct term *term) term_deref(term->u.app.rhs); } - // TODO: remove from hashmap? - if (--term->refs == 0) - free(term); + term_deref_head(term); } |