aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemse2022-08-15 11:07:08 -0700
committerGravatar Tom Willemse2022-08-15 11:07:08 -0700
commitb30eb232f69c7cb48ce20b353f9575c4632209b6 (patch)
tree0540aaac2c0609728df87a152504cc4eedd5fee7
parent8450ae6db8d5e7b85295ae15c1032220aae86af1 (diff)
downloadcrafting-interpreters-b30eb232f69c7cb48ce20b353f9575c4632209b6.tar.gz
crafting-interpreters-b30eb232f69c7cb48ce20b353f9575c4632209b6.zip
Chapter 30.2
-rw-r--r--clox/src/table.c8
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);
}
}