Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One question about the khash's example. #148

Open
lxyscls opened this issue Jul 16, 2020 · 0 comments
Open

One question about the khash's example. #148

lxyscls opened this issue Jul 16, 2020 · 0 comments

Comments

@lxyscls
Copy link

lxyscls commented Jul 16, 2020

#include "khash.h"
KHASH_MAP_INIT_INT(m32, char)        // instantiate structs and methods
int main() {
    int ret, is_missing;
    khint_t k;
    khash_t(m32) *h = kh_init(m32);  // allocate a hash table
    k = kh_put(m32, h, 5, &ret);     // insert a key to the hash table
#8    if (!ret) kh_del(m32, h, k);
#9    kh_value(h, k) = 10;             // set the value
    k = kh_get(m32, h, 10);          // query the hash table
    is_missing = (k == kh_end(h));   // test if the key is present
    k = kh_get(m32, h, 5);
    kh_del(m32, h, k);               // remove a key-value pair
    for (k = kh_begin(h); k != kh_end(h); ++k)  // traverse
        if (kh_exist(h, k))          // test if a bucket contains data
			kh_value(h, k) = 1;
    kh_destroy(m32, h);              // deallocate the hash table
    return 0;
}

Line 8, kh_del(m32, h, k) will unset the flag, if there is a duplication with key 5.
Line 9, give a new value.

I think those statements can't go through a kh_exist test after line 9. It does not make sense!

Because this is an int_char map, so there is nothing need to do. But for some other int_(void *) map, we need to do some resource free when (!ret) but not kh_del.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant