Add FOREACH_SAFE macros + tests
This commit is contained in:
+31
-1
@@ -168,7 +168,22 @@ void uc_htable_swap(struct UHTable *a, struct UHTable *b);
|
||||
(node_iter) != NULL;\
|
||||
(node_iter) = uc_htable_next_hash((node_iter)))
|
||||
|
||||
/**
|
||||
/**
|
||||
* "Safe" variant of UC_HTABLE_FOREACH_HASH, the iterated node
|
||||
* (or any previously iterated nodes) can be freed while iterating
|
||||
*
|
||||
* @param table the struct UHTable*
|
||||
* @param node_iter struct UHNode* for the current node being iterated
|
||||
* @param next_iter struct UHNode* for the next node, required by the
|
||||
* iteration, next_iter must not be altered while iterating.
|
||||
* @param hash size_t hash for the node(s) to iterate over
|
||||
*/
|
||||
#define UC_HTABLE_FOREACH_HASH_SAFE(table, node_iter, next_iter, hash)\
|
||||
for ((node_iter) = uc_htable_first_hash((table), (hash));\
|
||||
(node_iter) != NULL && (((next_iter) = (node_iter->next)), 1);\
|
||||
(node_iter) = (next))
|
||||
|
||||
/**
|
||||
* Iterate over all nodes in a table
|
||||
* The table must not be changed while iterating.
|
||||
*
|
||||
@@ -180,6 +195,21 @@ void uc_htable_swap(struct UHTable *a, struct UHTable *b);
|
||||
(node_iter) != NULL;\
|
||||
(node_iter) = uc_htable_next((table), (node_iter)))
|
||||
|
||||
/**
|
||||
* "Safe" variant of UC_HTABLE_FOREACH, the iterated node
|
||||
* (or any previously iterated nodes) can be freed while iterating
|
||||
*
|
||||
* @param table the struct UHTable*
|
||||
* @param node_iter struct UHNode* for the current node being iterated
|
||||
* @param next_iter struct UHNode* for the next node, required by the
|
||||
* iteration, next_iter must not be altered while iterating.
|
||||
*/
|
||||
#define UC_HTABLE_FOREACH_SAFE(table, node_iter, next_iter)\
|
||||
for ((node_iter) = uc_htable_first((table));\
|
||||
(node_iter) != NULL &&\
|
||||
((next_iter) = uc_htable_next((table), (node_iter)), 1);\
|
||||
(node_iter) = (next))
|
||||
|
||||
|
||||
/**
|
||||
* Check if the given node pointer exists in the hash table
|
||||
|
||||
Reference in New Issue
Block a user