Chapter 30.2

This commit is contained in:
Tom Willemse 2022-08-15 11:07:08 -07:00
parent 8450ae6db8
commit b30eb232f6

View file

@ -20,7 +20,7 @@ void freeTable(Table *table) {
} }
static Entry *findEntry(Entry *entries, int capacity, ObjString *key) { 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; Entry *tombstone = NULL;
for (;;) { for (;;) {
@ -39,7 +39,7 @@ static Entry *findEntry(Entry *entries, int capacity, ObjString *key) {
return entry; return entry;
} }
index = (index + 1) % capacity; index = (index + 1) & (capacity - 1);
} }
} }
@ -124,7 +124,7 @@ ObjString *tableFindString(Table *table, const char *chars, int length,
if (table->count == 0) if (table->count == 0)
return NULL; return NULL;
uint32_t index = hash % table->capacity; uint32_t index = hash & (table->capacity - 1);
for (;;) { for (;;) {
Entry *entry = &table->entries[index]; Entry *entry = &table->entries[index];
if (entry->key == NULL) { if (entry->key == NULL) {
@ -137,7 +137,7 @@ ObjString *tableFindString(Table *table, const char *chars, int length,
return entry->key; return entry->key;
} }
index = (index + 1) % table->capacity; index = (index + 1) & (table->capacity - 1);
} }
} }