Fix 'del' on table entries not updating count

This commit is contained in:
K. Lange 2021-12-15 16:37:19 +09:00
parent 6a431e9aa3
commit abd49b93a7
5 changed files with 68 additions and 2 deletions

View File

@ -67,6 +67,7 @@ KrkTableEntry * krk_findEntry(KrkTableEntry * entries, size_t capacity, KrkValue
if (IS_NONE(entry->value)) {
return tombstone != NULL ? tombstone : entry;
} else {
if (tombstone == entry) return tombstone;
if (tombstone == NULL) tombstone = entry;
}
} else if (krk_valuesEqual(entry->key, key)) {
@ -121,7 +122,7 @@ int krk_tableSet(KrkTable * table, KrkValue key, KrkValue value) {
KrkTableEntry * entry = krk_findEntry(table->entries, table->capacity, key);
if (!entry) return 0;
int isNewKey = IS_KWARGS(entry->key);
if (isNewKey && IS_NONE(entry->value)) table->count++;
if (isNewKey) table->count++;
entry->key = key;
entry->value = value;
return isNewKey;
@ -167,8 +168,9 @@ int krk_tableDelete(KrkTable * table, KrkValue key) {
if (!entry || IS_KWARGS(entry->key)) {
return 0;
}
table->count--;
entry->key = KWARGS_VAL(0);
entry->value = BOOLEAN_VAL(1);
entry->value = KWARGS_VAL(0);
return 1;
}

View File

@ -0,0 +1,44 @@
let open = {(0,0)}
open.remove( (0, 0) )
(1, 0) not in open
open.add( (1, 0) )
(0, 1) not in open
open.add( (0, 1) )
open.remove( (0, 1) )
(1, 1) not in open
open.add( (1, 1) )
(0, 2) not in open
open.add( (0, 2) )
open.remove( (1, 0) )
(2, 0) not in open
open.add( (2, 0) )
open.remove( (2, 0) )
(3, 0) not in open
open.add( (3, 0) )
(2, 1) not in open
open.add( (2, 1) )
open.remove( (1, 1) )
(1, 2) not in open
open.add( (1, 2) )
open.remove( (0, 2) )
(0, 3) not in open
open.add( (0, 3) )
open.remove( (2, 1) )
(3, 1) not in open
open.add( (3, 1) )
(2, 2) not in open
open.add( (2, 2) )
open.remove( (3, 0) )
(4, 0) not in open
open.add( (4, 0) )
open.remove( (0, 3) )
(1, 3) not in open
open.add( (1, 3) )
print((0, 4) not in open)
open.add( (0, 4) )
open.remove( (1, 2) )
open.remove( (2, 2) )
print((3, 2) not in open)
print("done")

View File

@ -0,0 +1,3 @@
True
True
done

12
test/testTablesDelLen.krk Normal file
View File

@ -0,0 +1,12 @@
let d = {1: 2, 3: 4, 5: 6}
print(len(d))
del d[3]
print(len(d))
let s = {1,2,3}
print(len(s))
s.remove(2)
print(len(s))
s.add(2)
print(len(s))

View File

@ -0,0 +1,5 @@
3
2
3
2
3