diff options
Diffstat (limited to 'ht.c')
-rw-r--r-- | ht.c | 26 |
1 files changed, 11 insertions, 15 deletions
@@ -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; i<newsz; i++) { new[i].k = NULL; new[i].v = VAL_NIL; @@ -58,17 +54,17 @@ static void grow(Ht *h) { } } - h->b = 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 { |