Hacker News new | ask | show | jobs
by msarnoff 3224 days ago
"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 article presents a much nicer interface.

[1] http://pubs.opengroup.org/onlinepubs/009695399/functions/hcr...

[2] http://man7.org/linux/man-pages/man3/hsearch.3.html

2 comments

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/onlinepubs/9699919799/functions/ts...

So this committee must have signed off on the hashtable stuff even though they had an example of how to do it properly right next to it!

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? ;)
tsearch/hsearch/lsearch have been around for at least 30 years. My guess is that they've been around for a lot longer.

Why not have these anyway? Otherwise people will make their own (probably buggy) equivalents.

I'm actually surprised that people aren't aware of them.

This is specifically about hcreate/hsearch, which are utterly useless, since you only get one hash table at a time. The reason you don't want crap like this is just that: it's crap.

The _r versions look perfectly sensible.

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

Signed,

POSIX_ME_HARDER

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