diff --git a/table.c b/table.c index b3f10af..83cdcbb 100644 --- a/table.c +++ b/table.c @@ -20,7 +20,7 @@ void freeTable(Table* table) { } static Entry* findEntry(Entry* entries, int capacity, ObjString* key) { - uint32_t index = key->hash % capacity; + uint32_t index = key->hash & (capacity-1); Entry* tombstone = NULL; for (;;) { @@ -35,7 +35,7 @@ static Entry* findEntry(Entry* entries, int capacity, ObjString* key) { return entry; } - index = (index+1) % capacity; + index = (index+1) & (capacity-1); } } @@ -90,7 +90,7 @@ ObjString* tableFindString(Table* table, const char* chars, int length, uint32_t hash) { if (table->count == 0) return NULL; - uint32_t index = hash % table->capacity; + uint32_t index = hash & (table->capacity-1); for (;;) { Entry* entry = &table->entries[index]; if (entry->key == NULL) { @@ -102,7 +102,7 @@ ObjString* tableFindString(Table* table, const char* chars, // We found it. return entry->key; } - index = (index + 1) % table->capacity; + index = (index + 1) & (table->capacity-1); } }