HASH_FIND_INT return value

188 views
Skip to first unread message

Hariprasad Govindarajan

unread,
May 25, 2019, 4:46:36 PM5/25/19
to uthash
Hi,
I have few questions. Can some one please clarify?

1) What does this MACRO return?
HASH_FIND_INT(htable, &key, sPtr);
From the user guide, I see it returns whether the key exists or not. Am I right?
why to check sPtr for NULL? what does that signify?

2) In this code example,

    HASH_FIND_INT(users, &user_id, s);  /* id already in the hash? */
    if (s==NULL) {
      s = (struct my_struct *)malloc(sizeof *s);
      s->id = user_id;
      HASH_ADD_INT( users, id, s );  /* id: name of key field */
    }
    strcpy(s->name, name);


why the strcpy is called outside the if condition?
what happens if s is NOT NULL? Does that mean if s == NULL, it means the id or the key is not in the hash already? what to do in the case?

Please clarify.


Thanks
G Hariprasad

Arthur O'Dwyer

unread,
May 26, 2019, 1:29:40 PM5/26/19
to uth...@googlegroups.com
On Sat, May 25, 2019 at 4:46 PM Hariprasad Govindarajan <harley...@gmail.com> wrote:
Hi,
I have few questions. Can some one please clarify?

1) What does this MACRO return?
HASH_FIND_INT(htable, &key, sPtr);
From the user guide, I see it returns whether the key exists or not. Am I right?
By the way, most of your questions can probably be answered by looking at uthash.h. It's pretty short and the convenience macros such as HASH_FIND_INT are really easy to read. For example:

#define HASH_FIND_INT(head,findint,out)                                          \

    HASH_FIND(hh,head,findint,sizeof(int),out)


#define HASH_FIND(hh,head,keyptr,keylen,out)                                     \

do {                                                                             \

  (out) = NULL;                                                                  \

  if (head) {                                                                    \

    unsigned _hf_hashv;                                                          \

    HASH_VALUE(keyptr, keylen, _hf_hashv);                                       \

    HASH_FIND_BYHASHVALUE(hh, head, keyptr, keylen, _hf_hashv, out);             \

  }                                                                              \

} while (0)


This answers your first question: HASH_FIND_INT is a statement-like macro; it doesn't "return" any expression per se.
It assigns a value into its third parameter `out`.

 why to check sPtr for NULL? what does that signify?

Well, you know that HASH_FIND_INT is documented to find "whether the key exists or not."
You know that it assigns a value into its `sPtr` parameter, which is of type "pointer to a hash node."
You know that the caller, immediately after invoking HASH_FIND_INT, checks the found pointer for NULL and allocates a new hash node in the NULL case.
What do you think "found hash node == NULL" might indicate?

2) In this code example, 

    HASH_FIND_INT(users, &user_id, s);  /* id already in the hash? */
    if (s==NULL) {
      s = (struct my_struct *)malloc(sizeof *s);
      s->id = user_id;
      HASH_ADD_INT( users, id, s );  /* id: name of key field */
    }
    strcpy(s->name, name);
why the strcpy is called outside the if condition?
what happens if s is NOT NULL? 
Follow the logic of the code. If s is not NULL, then the body of the `if` is skipped, and the `s->name` that is assigned to on the last line is the name of the hash node that was found by HASH_FIND_INT. 
Does that mean if s == NULL, it means the id or the key is not in the hash already? what to do in the case?
Follow the logic of the code. If s is NULL, then the body of the `if` is executed. `s` receives a new hash node; `s->id` and `s->name` are set with their appropriate values; and `s` is inserted into the hash via HASH_ADD_INT.

–Arthur

Hariprasad Govindarajan

unread,
May 27, 2019, 10:56:28 AM5/27/19
to uthash
Hi Arthur,
Thanks a lot for your reply. 


What do you think "found hash node == NULL" might indicate? 
Ans: I understand that it means the key doesn't exist already and there will be NO collision if this key is inserted. Right?

Follow the logic of the code. If s is not NULL, then the body of the `if` is skipped, and the `s->name` that is assigned to on the last line is the name of the hash node that was found by HASH_FIND_INT. 
So if you see the code snippet if S == NULL, then no collision occurs and the key and value are inserted.
But if S is not NULL which mean there is a collision, then shouldn't we find another key to insert?
Also this memory allocation s = (struct my_struct *)malloc(sizeof *s); happens only if S==NULL . If S is not NULL then how does this statement strcpy(s->name, name); work?
Memory is allocated only when no collision?

Sorry for bothering you. 
Some background about me:
I am just learning hashtable implementations in C and need some initial guidance.
As a first step trying to solve a problem where given an array of integers,return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

nums = [2, 7, 11, 15], target = 9
return [0, 1].

Regards,
G Hariprasad
Reply all
Reply to author
Forward
0 new messages