From e65db95257bd7b46b1156cb03a8885d92cadfe5b Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Fri, 31 Aug 2012 16:14:35 -0700 Subject: [PATCH] Add khash foreach macros This adds two convenient macros for iterating over all the keys or all the values in a khash. --- khash.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/khash.h b/khash.h index 0333510..2422044 100644 --- a/khash.h +++ b/khash.h @@ -531,6 +531,34 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key) */ #define kh_n_buckets(h) ((h)->n_buckets) +/*! @function + @abstract Iterate over the entries in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @param kvar Variable to which key will be assigned + @param vvar Variable to which value will be assigned + @param code Block of code to execute + */ +#define kh_foreach(h, kvar, vvar, 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); \ + (vvar) = kh_val(h,__i); \ + code; \ + } } + +/*! @function + @abstract Iterate over the values in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @param vvar Variable to which value will be assigned + @param code Block of code to execute + */ +#define kh_foreach_value(h, vvar, code) { khint_t __i; \ + for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \ + if (!kh_exist(h,__i)) continue; \ + (vvar) = kh_val(h,__i); \ + code; \ + } } + /* More conenient interfaces */ /*! @function -- 2.47.3