Improve array hash function and add hash function for hash tables

This commit is contained in:
pjht 2019-06-03 16:11:02 -05:00
parent c5b12fe8c9
commit b355295982

21
value.c
View File

@ -108,21 +108,32 @@ uint32_t hashValue(Value value) {
case OBJ_STRING:
return AS_STRING(value)->hash;
break;
case OBJ_ARRAY: { // TODO: Figure out how to properly hash an array
case OBJ_ARRAY: {
ValueArray* valArray = AS_VARRAY(value);
unsigned int sum = 0;
for (int i = 0; i < valArray->count; i++) {
uint32_t valHash = hashValue(valArray->values[i]);
uint32_t valHash = hashValue(valArray->values[i] * i);
if ((UINT_MAX - sum) < valHash) {
sum = hashUint(sum);
}
sum += valHash;
}
return sum;
return hashUInt(sum);
break;
}
case OBJ_HASH: // TODO: Figure out how to hash a hash
return 0;
case OBJ_HASH:
Table* hash = AS_HASH(value);
Entry** entries = getEntries(hash);
unsigned int sum = 0;
for (int i = 0; entries[i] != NULL; i++) {
Entry* entry = entries[i];
uint32_t valHash = hashValue((hashValue(entry->key)*i)+(hashValue(entry->value)*i));
if ((UINT_MAX - sum) < valHash) {
sum = hashUint(sum);
}
sum += valHash;
}
return hashUInt(sum);
break;
default:
return 0;