i need some kind of HashSet in C which can dynamicly grow in size. I could of course write everything on my own but maybe there is a good lib for that?
My Keys are 32bit hashes and i need to save a pointer (struct dirent *) as value.
3 Answers
I would recommend (as always) the nice GLib (part of GTK+). It has the GHashTable API which implements a hash table. It grows dynamically as needed.
To use 32-bit keys, reference the g_int_hash() and g_int_equal() functions when creating your hash table.
UPDATE: DO NOT USE THIS LIBRARY FOR STRINGS!
See this ticket for more info:
The HashSet code from Couchbase seems nice:
Example:
#include "hashset.h"
char *foo = "foo";
char *missing = "missing";
hashset_t set = hashset_create();
if (set == NULL) {
fprintf(stderr, "failed to create hashset instance\n");
abort();
}
hashset_add(set, foo);
assert(hashset_is_member(set, foo) == 1);
assert(hashset_is_member(set, missing) == 0);
I successfully use this one: KoanLogic Libu - Hmap module
The example in the link is enough self explaining.
For your needs i guess you should use U_HMAP_OPTS_DATATYPE_OPAQUE as datatype and set the length of your key to 4 bytes with u_hmap_opts_set_val_sz().