Skip to content

Commit

Permalink
refactor: change pr to new approach
Browse files Browse the repository at this point in the history
  • Loading branch information
liz3 committed Jan 19, 2024
1 parent eeef98c commit 547d2df
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 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
15 changes: 7 additions & 8 deletions src/vm/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ static DictItem *findDictEntry(DictItem *entries, int capacityMask,
Value key) {
uint32_t index = hashValue(key) & capacityMask;
DictItem *tombstone = NULL;
uint32_t count = 0;

for (;;) {
DictItem *entry = &entries[index];
Expand All @@ -89,8 +88,6 @@ static DictItem *findDictEntry(DictItem *entries, int capacityMask,
} else {
// We found a tombstone.
if (tombstone == NULL) tombstone = entry;
if (count >= (uint32_t)capacityMask)
return tombstone;
}
} else if (valuesEqual(key, entry->key)) {
// We found the key.
Expand All @@ -100,7 +97,6 @@ static DictItem *findDictEntry(DictItem *entries, int capacityMask,
// printf("%d - ", index);
index = (index + 1) & capacityMask;
// printf("%d - mask: %d\n", index, capacityMask);
count++;
}
}

Expand Down Expand Up @@ -151,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 @@ -163,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 @@ -433,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 547d2df

Please sign in to comment.