How to implement a hash table in C (2021)
benhoyt.com
How to implement a hash table in C (2021)
1–10 of 43 posts
Re: How to implement a hash table in C (2021)
#2Re: How to implement a hash table in C (2021)
#3Re: How to implement a hash table in C (2021)
#4Also, in terms of this table, if you add the computed key hash value to the stored hash entry, you can check that the value of the hashes match before you do the full strcmp. If you have a weak hash you might also get a benefit from checking that the first characters match before calling the full strcmp.
It would also make rehashing easier since you already have the full key available to you and don't have to use your internal set function to move entries into the new table. In the posted implementation the big-O semantics of a rehash are worst case.
Anyways.. "man hsearch.3" if you want something cheap and easy.
Re: How to implement a hash table in C (2021)
#5"void* ht_get(...)"
Wait. What? A void pointer? Interesting... I have no clue.
I like articles like these. For someone not familiar with C it's a perfect level. In terms of explanation and the code itself.
Re: How to implement a hash table in C (2021)
#6or even better, a full fledged linear probing hash lookup instruction.
Re: How to implement a hash table in C (2021)
#7I played around with C++ when I was at university. Then never touched it again. So, with a grin I stumble over things like "void* ht_get(...)" Wait. What? A void pointer? Interesting... I have no clue. I like articles like these. For someone not familiar with C it's a perfect level. In terms of explanation and the code itself.
The main use case is generic functions. And in data structures like this.
Re: How to implement a hash table in C (2021)
#8Unless I really didn't want to introduce dependencies, or reduce code size, I think I'd use an off the shelf hash table implementation these days. It's still a fun exercise building your own though.
Re: How to implement a hash table in C (2021)
#9How to implement a hash table in C - https://news.ycombinator.com/item?id=26590234 - March 2021 (156 comments)
Re: How to implement a hash table in C (2021)
#10I played around with C++ when I was at university. Then never touched it again. So, with a grin I stumble over things like "void* ht_get(...)" Wait. What? A void pointer? Interesting... I have no clue. I like articles like these. For someone not familiar with C it's a perfect level. In terms of explanation and the code itself.
And yeah, a void * is how you express pointer to data of unknown type in C.