Permalink
Browse files

Add khash foreach macros

This adds two convenient macros for iterating over all the keys
or all the values in a khash.
  • Loading branch information...
arrbee committed Aug 31, 2012
1 parent 24c0fe5 commit e65db95257bd7b46b1156cb03a8885d92cadfe5b
Showing with 28 additions and 0 deletions.
  1. +28 −0 khash.h
View
28 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

0 comments on commit e65db95

Please sign in to comment.