Tweaked dictionary hashing to not hash null keys

This commit is contained in:
2022-08-11 11:38:27 +01:00
parent 603d9d2b06
commit 52651c5f2c

View File

@@ -222,6 +222,8 @@ bool literalsAreEqual(Literal lhs, Literal rhs) {
} }
return !strncmp(AS_STRING(lhs), AS_STRING(rhs), STRLEN(lhs)); return !strncmp(AS_STRING(lhs), AS_STRING(rhs), STRLEN(lhs));
//TODO: literal array and literal dictionary equality checks
case LITERAL_IDENTIFIER: case LITERAL_IDENTIFIER:
if (STRLEN_I(lhs) != STRLEN_I(rhs)) { if (STRLEN_I(lhs) != STRLEN_I(rhs)) {
return false; return false;
@@ -230,7 +232,7 @@ bool literalsAreEqual(Literal lhs, Literal rhs) {
default: default:
//should never bee seen //should never bee seen
fprintf(stderr, "[internal] Unrecognized literal type: %d\n", lhs.type); fprintf(stderr, "[Internal] Unrecognized literal type in equality: %d\n", lhs.type);
return false; return false;
} }
} }
@@ -282,8 +284,10 @@ int hashLiteral(Literal lit) {
case LITERAL_DICTIONARY: { case LITERAL_DICTIONARY: {
unsigned int res = 0; unsigned int res = 0;
for (int i = 0; i < AS_DICTIONARY(lit)->count; i++) { for (int i = 0; i < AS_DICTIONARY(lit)->count; i++) {
res += hashLiteral(AS_DICTIONARY(lit)->entries[i].key); if (!IS_NULL(AS_DICTIONARY(lit)->entries[i].key)) { //only hash non-null keys
res += hashLiteral(AS_DICTIONARY(lit)->entries[i].value); res += hashLiteral(AS_DICTIONARY(lit)->entries[i].key);
res += hashLiteral(AS_DICTIONARY(lit)->entries[i].value);
}
} }
return hash(res); return hash(res);
} }