Skip to content

Commit

Permalink
Merge pull request #728 from liz3/fix-dict
Browse files Browse the repository at this point in the history
fix: correctly return tombstone when theres no free element
  • Loading branch information
Jason2605 authored Jan 19, 2024
2 parents 325f342 + 547d2df commit a548039
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/vm/datatypes/dicts/dicts.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static Value lenDict(DictuVM *vm, int argCount, Value *args) {
}

ObjDict *dict = AS_DICT(args[0]);
return NUMBER_VAL(dict->count);
return NUMBER_VAL(dict->activeCount);
}

static Value keysDict(DictuVM *vm, int argCount, Value *args) {
Expand Down
1 change: 1 addition & 0 deletions src/vm/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ ObjList *newList(DictuVM *vm) {
ObjDict *newDict(DictuVM *vm) {
ObjDict *dict = ALLOCATE_OBJ(vm, ObjDict, OBJ_DICT);
dict->count = 0;
dict->activeCount = 0;
dict->capacityMask = -1;
dict->entries = NULL;
return dict;
Expand Down
1 change: 1 addition & 0 deletions src/vm/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ typedef struct {
struct sObjDict {
Obj obj;
int count;
int activeCount;
int capacityMask;
DictItem *entries;
};
Expand Down
11 changes: 7 additions & 4 deletions src/vm/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ bool dictSet(DictuVM *vm, ObjDict *dict, Value key, Value value) {
entry->key = key;
entry->value = value;

if (isNewKey) dict->count++;
if (isNewKey) {
dict->activeCount++;
dict->count++;
}

return isNewKey;
}
Expand All @@ -159,7 +162,7 @@ bool dictDelete(DictuVM *vm, ObjDict *dict, Value key) {
if (IS_EMPTY(entry->key)) return false;

// Place a tombstone in the entry.
dict->count--;
dict->activeCount--;
entry->key = EMPTY_VAL;
entry->value = BOOL_VAL(true);

Expand Down Expand Up @@ -429,12 +432,12 @@ static bool dictComparison(Value a, Value b) {
ObjDict *dictB = AS_DICT(b);

// Different lengths, not the same
if (dict->count != dictB->count)
if (dict->activeCount != dictB->activeCount)
return false;

// Lengths are the same, and dict 1 has 0 length
// therefore both are empty
if (dict->count == 0)
if (dict->activeCount == 0)
return true;

for (int i = 0; i <= dict->capacityMask; ++i) {
Expand Down

0 comments on commit a548039

Please sign in to comment.