Live data from Hacker News

Write a hash table in C

github.com

41–50 of 58 posts

Re: Write a hash table in C

#41

"The language doesn't come with [a hash table implementation] included" The standard library does come with a half-baked hash table implementation[1] (hcreate/hdestroy/hsearch) that only allows creation of _one_ global hash table. GNU libc[2] adds hcreate_r/hdestroy_r/hsearch_r that allows multiple tables to be created. The APIs are strange and antiquated. On the other hand, the hash table implementation described in…

This is absurd, who could have ever thought it is a good idea?

Re: Write a hash table in C

#42
post #39
post #24

Earlier quoted context omitted.

The hcreate nonsense is POSIX, not C99, so a conforming C implementation might not include it.

How does junk like this ever get into a standard? Didn't a whole committee of very experienced systems programmers have to sit down and say, "yes, these are routines that i think people will find generally useful"? It looks like the same header, search.h, also includes functions for working with binary trees, where the user passes in the root pointer, and so which support multiple trees: http://pubs.opengroup.org/onl…

A standards process is frequently about documenting what already exists in common implementations so that there is a specification for future implementations. POSIX was very much one of these processes.

Re: Write a hash table in C

#43
post #34

I've heard of two possibilities (as a recommendation) for using data structures in C without having to develop them: - BSD queue.h offers linked lists, queues, etc [1]. - UT-hash offers hash tables [2]. I've also read some people being frustrated with them, probably due to the quirky syntax/API. On the plus side (BSD at least) it's just a header file that you can download, include and start using. [1] http://bxr.su/O…

Also there is the header-only khash.h [3], complete with benchmarks:

https://attractivechaos.wordpress.com/2008/08/28/comparison-...

(Also provides kbtree.h, ksort.h, kstream.h, kvec.h)

[3] https://attractivechaos.wordpress.com/programs/

Also there are the stb header-only libraries. There is a hash table implementation buried inside stb.h

[4] https://github.com/nothings/stb

Re: Write a hash table in C

#44
post #24

"The language doesn't come with [a hash table implementation] included" The standard library does come with a half-baked hash table implementation[1] (hcreate/hdestroy/hsearch) that only allows creation of _one_ global hash table. GNU libc[2] adds hcreate_r/hdestroy_r/hsearch_r that allows multiple tables to be created. The APIs are strange and antiquated. On the other hand, the hash table implementation described in…

The hcreate nonsense is POSIX, not C99, so a conforming C implementation might not include it.

I was just thinking, "Where the hell did that come from?"

Signed,

POSIX_ME_HARDER

Re: Write a hash table in C

#45
post #34

I've heard of two possibilities (as a recommendation) for using data structures in C without having to develop them: - BSD queue.h offers linked lists, queues, etc [1]. - UT-hash offers hash tables [2]. I've also read some people being frustrated with them, probably due to the quirky syntax/API. On the plus side (BSD at least) it's just a header file that you can download, include and start using. [1] http://bxr.su/O…

I think at this point if you aren't in a very constrained platform, it doesn't really make sense to use someone else's data structure implementations unless they at least support some kind of CPU parallelism. Preferably openmp or whatever suits the algorithm at hand.

Re: Write a hash table in C

#46
"(long)pow(a, len_s - (i+1))" is both inefficient and inaccurate. You should store the current power in a variable which you multiply by 'a' and reduce by 'm' on each iteration.

Also, your double hashing scheme has a bug. You identified that it's problematic if hash_b returns 0, but adding 1 to the result doesn't actually solve anything, because now you have the same problem if hash_b returns num_buckets-1.

Re: Write a hash table in C

#47
post #34

I've heard of two possibilities (as a recommendation) for using data structures in C without having to develop them: - BSD queue.h offers linked lists, queues, etc [1]. - UT-hash offers hash tables [2]. I've also read some people being frustrated with them, probably due to the quirky syntax/API. On the plus side (BSD at least) it's just a header file that you can download, include and start using. [1] http://bxr.su/O…

Definitely look into the Linux kernel data structures. Container_of is a fantastic way to do data structures in C.

Re: Write a hash table in C

#48
post #34

I've heard of two possibilities (as a recommendation) for using data structures in C without having to develop them: - BSD queue.h offers linked lists, queues, etc [1]. - UT-hash offers hash tables [2]. I've also read some people being frustrated with them, probably due to the quirky syntax/API. On the plus side (BSD at least) it's just a header file that you can download, include and start using. [1] http://bxr.su/O…

Also, BSD tree.h for red-black trees and splay trees http://bxr.su/OpenBSD/sys/sys/tree.h

Re: Write a hash table in C

#50
post #42
post #39

Earlier quoted context omitted.

How does junk like this ever get into a standard? Didn't a whole committee of very experienced systems programmers have to sit down and say, "yes, these are routines that i think people will find generally useful"? It looks like the same header, search.h, also includes functions for working with binary trees, where the user passes in the root pointer, and so which support multiple trees: http://pubs.opengroup.org/onl…

A standards process is frequently about documenting what already exists in common implementations so that there is a specification for future implementations. POSIX was very much one of these processes.

OK... so how did junk like this ever get into enough implementations that people were forced to include it in the standard? ;)
Post reply on HN