~emersion/mrsh

34dcc71d5796f4c74c1bb43ec505b05ad57f89c0 — emersion 2 years ago b06e2d7
hashtable: make removing current element in iterator safe
2 files changed, 7 insertions(+), 1 deletions(-)

M hashtable.c
M include/mrsh/hashtable.h
M hashtable.c => hashtable.c +2 -1
@@ 109,8 109,9 @@ void mrsh_hashtable_for_each(struct mrsh_hashtable *table,
	for (size_t i = 0; i < MRSH_HASHTABLE_BUCKETS; ++i) {
		struct mrsh_hashtable_entry *entry = table->buckets[i];
		while (entry != NULL) {
			struct mrsh_hashtable_entry *next = entry->next;
			iterator(entry->key, entry->value, user_data);
			entry = entry->next;
			entry = next;
		}
	}
}

M include/mrsh/hashtable.h => include/mrsh/hashtable.h +5 -0
@@ 22,6 22,11 @@ void *mrsh_hashtable_get(struct mrsh_hashtable *table, const char *key);
void *mrsh_hashtable_set(struct mrsh_hashtable *table, const char *key,
	void *value);
void *mrsh_hashtable_del(struct mrsh_hashtable *table, const char *key);
/**
 * Calls `iterator` for each (key, value) pair in the hash table. It is safe to
 * call `mrsh_hashtable_del` on the current element, however it is not safe to
 * do so an any other element.
 */
void mrsh_hashtable_for_each(struct mrsh_hashtable *table,
	mrsh_hashtable_iterator_func_t iterator, void *user_data);