by benhoyt on 7/6/24, 2:36 AM with 41 comments
by commandersaki on 7/6/24, 6:15 AM
Unless 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.
by akira2501 on 7/6/24, 5:22 AM
Also, 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.
by SloopJon on 7/6/24, 7:50 PM
$ echo abcdef |tr abc ghi
ghidef
For an eight-bit character set, I found that building an array to map every character improved on linear search, even for short replacement strings and relatively short input strings.There isn't as easy a win for Unicode, so I played with some simple hash tables. Although the conventional wisdom is to use the high-order bits of a hash function's output, FNV-1a is not so good for short inputs of one or two bytes. (I used the 32-bit variant.) It was better just to use the low-order bits.
by iamevn on 7/6/24, 8:32 AM
I know size_t wraps around on overflow so this would actually work, but this still makes me do a double take.
by suprjami on 7/6/24, 5:12 AM
by MrVandemar on 7/6/24, 2:43 AM
by Joker_vD on 7/6/24, 10:58 AM
I am fairly certain that in C it's actually impossible to have an object whose size is larger than half of the range of size_t unless ptrdiff_t is wider than size_t, which normally isn't. Unless, of course, C standard decided to make subtracting two valid pointers into the same array (or one past the end) a potential UB, just because.
by dang on 7/6/24, 6:22 AM
How to implement a hash table in C - https://news.ycombinator.com/item?id=26590234 - March 2021 (156 comments)
by foresto on 7/6/24, 9:09 PM
by glouwbug on 7/7/24, 3:14 PM
by aronhegedus on 7/6/24, 8:36 PM
by a-dub on 7/6/24, 5:43 AM
or even better, a full fledged linear probing hash lookup instruction.
by timo-e-aus-e on 7/6/24, 5:35 AM
"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.