diff --git a/value.c b/value.c index 0e3ef68..bd05bcc 100644 --- a/value.c +++ b/value.c @@ -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;