Add FOREACH_SAFE macros + tests

This commit is contained in:
Nils O. Selåsdal
2013-10-24 22:13:16 +02:00
parent 45ecb3c1e0
commit 0acb4789b0
2 changed files with 104 additions and 1 deletions
+31 -1
View File
@@ -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