Skip to content
This repository
Browse code

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...
commit e65db95257bd7b46b1156cb03a8885d92cadfe5b 1 parent 24c0fe5
Russell Belfer arrbee authored

Showing 1 changed file with 28 additions and 0 deletions. Show diff stats Hide diff stats

  1. +28 0 khash.h
28 khash.h
@@ -531,6 +531,34 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key)
531 531 */
532 532 #define kh_n_buckets(h) ((h)->n_buckets)
533 533
  534 +/*! @function
  535 + @abstract Iterate over the entries in the hash table
  536 + @param h Pointer to the hash table [khash_t(name)*]
  537 + @param kvar Variable to which key will be assigned
  538 + @param vvar Variable to which value will be assigned
  539 + @param code Block of code to execute
  540 + */
  541 +#define kh_foreach(h, kvar, vvar, code) { khint_t __i; \
  542 + for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
  543 + if (!kh_exist(h,__i)) continue; \
  544 + (kvar) = kh_key(h,__i); \
  545 + (vvar) = kh_val(h,__i); \
  546 + code; \
  547 + } }
  548 +
  549 +/*! @function
  550 + @abstract Iterate over the values in the hash table
  551 + @param h Pointer to the hash table [khash_t(name)*]
  552 + @param vvar Variable to which value will be assigned
  553 + @param code Block of code to execute
  554 + */
  555 +#define kh_foreach_value(h, vvar, code) { khint_t __i; \
  556 + for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
  557 + if (!kh_exist(h,__i)) continue; \
  558 + (vvar) = kh_val(h,__i); \
  559 + code; \
  560 + } }
  561 +
534 562 /* More conenient interfaces */
535 563
536 564 /*! @function

0 comments on commit e65db95

Please sign in to comment.
Something went wrong with that request. Please try again.