From b30eb232f69c7cb48ce20b353f9575c4632209b6 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Mon, 15 Aug 2022 11:07:08 -0700 Subject: Chapter 30.2 --- clox/src/table.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clox/src/table.c b/clox/src/table.c index 2405397..040b277 100644 --- a/clox/src/table.c +++ b/clox/src/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 (;;) { @@ -39,7 +39,7 @@ static Entry *findEntry(Entry *entries, int capacity, ObjString *key) { 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) 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) { @@ -137,7 +137,7 @@ ObjString *tableFindString(Table *table, const char *chars, int length, return entry->key; } - index = (index + 1) % table->capacity; + index = (index + 1) & (table->capacity - 1); } } -- cgit v1.2.3-54-g00ecf