khash.h (KHASH_FOREACH): a new macro to iterate over khash table

This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-06-22 05:24:00 +09:00
parent d41d477e22
commit bce8cee8f9
+20
View File
@@ -281,4 +281,24 @@ typedef const char *kh_cstr_t;
MRB_END_DECL
/**
* Macro for iterating over all elements in a khash.
*
* Usage:
* KHASH_FOREACH(mrb, kh, k) {
* // k is the khiter_t iterator
* // Access the key with kh_key(kh, k)
* // Access the value with kh_val(kh, k) if applicable
* // Your code here
* }
*
* @param mrb The mrb_state
* @param kh The khash to iterate over
* @param k The name to use for the khiter_t iterator variable
*/
#define KHASH_FOREACH(mrb, kh, k) \
if (kh) \
for (khiter_t k = kh_begin(kh); k != kh_end(kh); k++) \
if (kh_exist(kh, k))
#endif /* MRUBY_KHASH_H */