15-line hash table in C
pastes.archbsd.net
15-line hash table in C
1–10 of 104 posts
Re: 15-line hash table in C
#2This code reminds me why clever is the enemy of good.
Re: 15-line hash table in C
#3This code reminds me why clever is the enemy of good.
[deleted]
Re: 15-line hash table in C
#4will need the clockwise spiral rule to parse this:
http://c-faq.com/decl/spiral.anderson.html
Re: 15-line hash table in C
#5Thank god, I was just about to run out of newlines!
Re: 15-line hash table in C
#6A small improvement could be to hash the key using a prime multiplier and successive multiplications, instead of using the key and increments of one. It'd reduce the collisions at the expense of a more computationally expensive hash function.
Re: 15-line hash table in C
#7There's no bounds testing in hget(): some valid sequences of operations will cause buffer overflows. For example, this should segfault:
int (**table)[2] = hnew();
for (int j=0; j
The problem is, the probing function doesn't wrap (the "t += h" part), so if you have have several colliding keys, it will probe for them past the end of the table.Re: 15-line hash table in C
#8will need the clockwise spiral rule to parse this: http://c-faq.com/decl/spiral.anderson.html
Re: 15-line hash table in C
#9will need the clockwise spiral rule to parse this: http://c-faq.com/decl/spiral.anderson.html
The author helpfully explains the code by way of un-optimizing it to something readable: http://pastes.archbsd.net/graphitemaster/hashtable_explinati...
Re: 15-line hash table in C
#10A small improvement could be to hash the key using a prime multiplier and successive multiplications, instead of using the key and increments of one. It'd reduce the collisions at the expense of a more computationally expensive hash function.
The problem with that method is that it doesn't have data access locality, while linear probing does. Linear probing ends up being more efficient because it is easy on the cache.