]> git.kaiwu.me - klib.git/commitdiff
Add khash foreach macros
authorRussell Belfer <rb@github.com>
Fri, 31 Aug 2012 23:14:35 +0000 (16:14 -0700)
committerRussell Belfer <rb@github.com>
Fri, 31 Aug 2012 23:14:35 +0000 (16:14 -0700)
This adds two convenient macros for iterating over all the keys
or all the values in a khash.

khash.h

diff --git a/khash.h b/khash.h
index 0333510332c5551efd8f4d383f878f05d1db94e9..242204464aab0c4c3475bc46c5021a36159d9540 100644 (file)
--- 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