From 7dc59ac342d315dbb352becbeabc4e7057992de0 Mon Sep 17 00:00:00 2001 From: Yip Coekjan <69834864+Coekjan@users.noreply.github.com> Date: Fri, 6 Sep 2024 20:35:32 +0800 Subject: Eliminate duplicate hash calculations for `getSymbolInSymbolMaps` (#1801) --- src/include/khash.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/include/khash.h') diff --git a/src/include/khash.h b/src/include/khash.h index 823757bf..80711c3d 100644 --- a/src/include/khash.h +++ b/src/include/khash.h @@ -217,6 +217,22 @@ static const double __ac_HASH_UPPER = 0.77; h->size = h->n_occupied = 0; \ } \ } \ + SCOPE khint_t kh_hash_##name(khkey_t key) { \ + return __hash_func(key); \ + } \ + SCOPE khint_t kh_get_##name##_with_hash(const kh_##name##_t *h, khkey_t key, khint_t hash) { \ + if (h->n_buckets) { \ + khint_t k = hash, i, last, mask, step = 0; \ + mask = h->n_buckets - 1; \ + i = k & mask; \ + last = i; \ + while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \ + i = (i + (++step)) & mask; \ + if (i == last) return h->n_buckets; \ + } \ + return __ac_iseither(h->flags, i)? h->n_buckets : i; \ + } else return 0; \ + } \ SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \ { \ if (h->n_buckets) { \ @@ -462,6 +478,24 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key) */ #define kh_put(name, h, k, r) kh_put_##name(h, k, r) +/*! @function + @abstract Hash a key. + @param name Name of the hash table [symbol] + @param key Key [type of keys] + @return Hash value [khint_t] + */ +#define kh_hash(name, key) kh_hash_##name(key) + +/*! @function + @abstract Retrieve a key from the hash table with a given hash value. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param k Key [type of keys] + @param hash Hash value [khint_t] + @return Iterator to the found element, or kh_end(h) if the element is absent [khint_t] + */ +#define kh_get_with_hash(name, h, k, hash) kh_get_##name##_with_hash(h, k, hash) + /*! @function @abstract Retrieve a key from the hash table. @param name Name of the hash table [symbol] -- cgit 1.4.1