slayze
slayze

Reputation: 21

uthash C HASH_FIND

I've got a homework from university and are supposed to use the library uthash to implement a hashtable including a set-, get- and delete-function. Hashtable is our struct containing a key, its length, a value, its length too and a UT_hash_handle (which seems to be necessary).

The struct looks like this:

typedef struct hashtable {
    unsigned char *key;
    size_t k_len; 
    unsigned char *value; 
    size_t v_len;
    UT_hash_handle hashhandle;
} hashtable;

And our predefined function-head is the following:

hashtable get_value(hashtable **ht, const unsigned char *key, size_t key_len);

Regarding to our task I should use the HASH_FIND-function from uthash to get a hashvalue by key. I thought this would be easy (and probably it is) but sadly I don't understand the usage of the HASH_FIND-function in detail and the official documentation isn't very helpful because it just shows the usage of HASH_FIND_INT.

So, HASH_FIND wants me to give it five parameters:

HASH_FIND(hh, head, keyptr, keylen, out)

The values, which I should use for keyptr and keylen, are no problem (I'll just give it the parameter key and key_len which I receive as a parameter in my function). For the value out, I think I should create a new struct from our hashtable-struct and give it to HASH_FIND. For the head-parameter, I think it's just necessary to give the pointer to my hashtable (which I get as a parameter to my function by our specification). But what exactly does the hash_handle means? Should I get the hash_handle-value out of our predefined struct and then give it to HASH_FIND?

Thank you for helping me out!

I want to look up a value out of the hashtable using HASH_FIND from uthash.

Upvotes: 0

Views: 1088

Answers (1)

Hasturkun
Hasturkun

Reputation: 36412

hh is meant to be the name of the UT_hash_handle member of your value struct (which in the convenience macros like HASH_FIND_INT is hardcoded to be hh), so would just be hashhandle here.

The out parameter should be a hashtable *, which will either be set to point to the found element, or to NULL if not found.

Upvotes: 1

Related Questions