diff options
author | Tom Willemse | 2022-08-15 11:07:08 -0700 |
---|---|---|
committer | Tom Willemse | 2022-08-15 11:07:08 -0700 |
commit | b30eb232f69c7cb48ce20b353f9575c4632209b6 (patch) | |
tree | 0540aaac2c0609728df87a152504cc4eedd5fee7 | |
parent | 8450ae6db8d5e7b85295ae15c1032220aae86af1 (diff) | |
download | crafting-interpreters-b30eb232f69c7cb48ce20b353f9575c4632209b6.tar.gz crafting-interpreters-b30eb232f69c7cb48ce20b353f9575c4632209b6.zip |
Chapter 30.2
-rw-r--r-- | clox/src/table.c | 8 |
1 files 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); } } |