From 60b3369ab24f9bd2a4a6d638ab1b3013ebc29814 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 20 Jun 2024 16:00:34 +0100 Subject: pass State *S everywhere contains changes from a million years ago that i don't remember much about --- ht.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'ht.c') diff --git a/ht.c b/ht.c index ca93222..474361a 100644 --- a/ht.c +++ b/ht.c @@ -20,11 +20,7 @@ uint32_t hash(char *s, size_t len) { } Ht ht_new() { - return (Ht){ - .len = 0, - .cap = 0, - .b = NULL - }; + return (Ht){ 0 }; } static HtEntry *find(HtEntry *b, size_t cap, ObjString *k) { @@ -38,10 +34,10 @@ static HtEntry *find(HtEntry *b, size_t cap, ObjString *k) { } } -static void grow(Ht *h) { - HtEntry *old = h->b; +static void grow(State *S, Ht *h) { + HtEntry *old = h->d; size_t newsz = h->cap == 0 ? 8 : h->cap * 2; - HtEntry *new = NEW_ARR(HtEntry, newsz); + HtEntry *new = NEW_ARR(S, HtEntry, newsz); for (int i = 0; ib = new; - FREE_ARR(old, HtEntry, h->cap); + h->d = new; + FREE_ARR(S, old, HtEntry, h->cap); h->cap = newsz; } -void ht_put(Ht *h, ObjString *k, Val v) { +void ht_put(State *S, Ht *h, ObjString *k, Val v) { if (h->cap == 0 || h->len >= (h->cap/2)) { - grow(h); + grow(S, h); } - HtEntry *ent = find(h->b, h->cap, k); + HtEntry *ent = find(h->d, h->cap, k); if (ent->k == NULL) { ent->k = k; h->len++; @@ -76,8 +72,8 @@ void ht_put(Ht *h, ObjString *k, Val v) { ent->v = v; } -Val ht_get(Ht *h, ObjString *k) { - HtEntry *ent = find(h->b, h->cap, k); +Val ht_get(State *S, Ht *h, ObjString *k) { + HtEntry *ent = find(h->d, h->cap, k); if (ent->k == NULL) { return VAL_NIL; } else { -- cgit v1.2.3