From 3eb6c9717c96530947c631c8a73ba62c62c8454e Mon Sep 17 00:00:00 2001 From: Lisandro Dalcin Date: Fri, 30 Nov 2018 00:01:02 +0300 Subject: [PATCH 1/2] Add kh_reset Reset a hash table to initial state deallocating memory --- khash.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/khash.h b/khash.h index f75f3474..a71195ea 100644 --- a/khash.h +++ b/khash.h @@ -202,6 +202,7 @@ static const double __ac_HASH_UPPER = 0.77; #define __KHASH_PROTOTYPES(name, khkey_t, khval_t) \ extern kh_##name##_t *kh_init_##name(void); \ extern void kh_destroy_##name(kh_##name##_t *h); \ + extern void kh_reset_##name(kh_##name##_t *h); \ extern void kh_clear_##name(kh_##name##_t *h); \ extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \ extern int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \ @@ -220,6 +221,14 @@ static const double __ac_HASH_UPPER = 0.77; kfree(h); \ } \ } \ + SCOPE void kh_reset_##name(kh_##name##_t *h) \ + { \ + if (h) { \ + kfree(h->keys); kfree(h->flags); \ + kfree(h->vals); \ + memset(h, 0x00, sizeof(*h)); \ + } \ + } \ SCOPE void kh_clear_##name(kh_##name##_t *h) \ { \ if (h && h->flags) { \ @@ -445,6 +454,13 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key) */ #define kh_destroy(name, h) kh_destroy_##name(h) +/*! @function + @abstract Reset a hash table to initial state deallocating memory. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + */ +#define kh_reset(name, h) kh_reset_##name(h) + /*! @function @abstract Reset a hash table without deallocating memory. @param name Name of the hash table [symbol] From 8e9e4d69bc8e70e0f8e5df4fb3b83a6937c40302 Mon Sep 17 00:00:00 2001 From: Lisandro Dalcin Date: Fri, 30 Nov 2018 00:05:56 +0300 Subject: [PATCH 2/2] Add khash foreach key macro --- khash.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/khash.h b/khash.h index a71195ea..ba2c6f1c 100644 --- a/khash.h +++ b/khash.h @@ -579,6 +579,19 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key) code; \ } } +/*! @function + @abstract Iterate over the keys in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @param kvar Variable to which key will be assigned + @param code Block of code to execute + */ +#define kh_foreach_key(h, kvar, code) { khint_t __i; \ + for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \ + if (!kh_exist(h,__i)) continue; \ + (kvar) = kh_key(h,__i); \ + code; \ + } } + /*! @function @abstract Iterate over the values in the hash table @param h Pointer to the hash table [khash_t(name)*]