munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.42k stars 1.01k forks source link

Find Entry in Table.c will enter endless loop if not found? #1132

Closed derekdogg closed 11 months ago

derekdogg commented 12 months ago

Code below, commented out makes more sense? This will go back to 1st slot. (i.e. loop around array). Code as it stands will not go back to 1st slot

Apologies if this off the mark, not a c coder, just reading the code.

/ Hash Tables find-entry < Optimization next-index index = (index + 1) % capacity; /

hugomg commented 11 months ago

I believe it's an optimization. Because the capacity is always a power of 2, the current code

index = (index + 1) & (table->capacity - 1);

is equivalent to the commented out

index = (index + 1) % capacity;