diff --git a/src/ctable.c b/src/ctable.c index 46a96e0..60fd1cb 100644 --- a/src/ctable.c +++ b/src/ctable.c @@ -122,7 +122,7 @@ static void resizeTbl(CState *state, CTable *tbl, size_t newCapacity) { cosmoM_freearray(state, CTableEntry, entries, newCapacity); int tombs = tbl->tombstones; tbl->tombstones = 0; - resizeTbl(state, tbl, nextPow2((tbl->count - tombs) * GROW_FACTOR)); + resizeTbl(state, tbl, nextPow2((tbl->capacity - tombs) * GROW_FACTOR)); cosmoM_updateThreshhold(state); // force a threshhold update since this *could* be such a huge memory difference return; } @@ -154,7 +154,6 @@ static void resizeTbl(CState *state, CTable *tbl, size_t newCapacity) { tbl->table = entries; tbl->capacity = newCapacity; tbl->count = newCount; - tbl->tombstones = 0; } // returns a pointer to the allocated value @@ -169,8 +168,11 @@ COSMO_API CValue* cosmoT_insert(CState *state, CTable *tbl, CValue key) { // insert into the table CTableEntry *entry = findEntry(tbl->table, tbl->capacity - 1, key); // -1 for our capacity mask - if (IS_NIL(entry->key) && IS_NIL(entry->val)) // is it empty? - tbl->count++; + if (IS_NIL(entry->key)) + if (IS_NIL(entry->val)) // is it empty? + tbl->count++; + else // it's a tombstone, mark it alive! + tbl->tombstones--; entry->key = key; return &entry->val; diff --git a/src/cvalue.h b/src/cvalue.h index 176e7b4..02dc190 100644 --- a/src/cvalue.h +++ b/src/cvalue.h @@ -39,10 +39,10 @@ void printValue(CValue val); COSMO_API bool cosmoV_equal(CValue valA, CValue valB); COSMO_API CObjString *cosmoV_toString(CState *state, CValue val); -#define IS_NUMBER(x) x.type == COSMO_TNUMBER -#define IS_BOOLEAN(x) x.type == COSMO_TBOOLEAN -#define IS_NIL(x) x.type == COSMO_TNIL -#define IS_OBJ(x) x.type == COSMO_TOBJ +#define IS_NUMBER(x) (x.type == COSMO_TNUMBER) +#define IS_BOOLEAN(x) (x.type == COSMO_TBOOLEAN) +#define IS_NIL(x) (x.type == COSMO_TNIL) +#define IS_OBJ(x) (x.type == COSMO_TOBJ) // create CValues