hashmap: use *_entry APIs for iteration

Inspired by list_for_each_entry in the Linux kernel.
Once again, these are somewhat compromised usability-wise
by compilers lacking __typeof__ support.

Signed-off-by: Eric Wong <e@80x24.org>
Reviewed-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Eric Wong
2019-10-06 23:30:38 +00:00
committed by Junio C Hamano
parent 939af16eac
commit 87571c3f71
13 changed files with 70 additions and 47 deletions

View File

@ -382,16 +382,27 @@ struct hashmap_iter {
void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter);
/* Returns the next hashmap_entry, or NULL if there are no more entries. */
void *hashmap_iter_next(struct hashmap_iter *iter);
struct hashmap_entry *hashmap_iter_next(struct hashmap_iter *iter);
/* Initializes the iterator and returns the first entry, if any. */
static inline void *hashmap_iter_first(struct hashmap *map,
static inline struct hashmap_entry *hashmap_iter_first(struct hashmap *map,
struct hashmap_iter *iter)
{
hashmap_iter_init(map, iter);
return hashmap_iter_next(iter);
}
#define hashmap_iter_next_entry(iter, type, member) \
container_of_or_null(hashmap_iter_next(iter), type, member)
#define hashmap_iter_first_entry(map, iter, type, member) \
container_of_or_null(hashmap_iter_first(map, iter), type, member)
#define hashmap_for_each_entry(map, iter, var, type, member) \
for (var = hashmap_iter_first_entry(map, iter, type, member); \
var; \
var = hashmap_iter_next_entry(iter, type, member))
/*
* returns a @pointer of @type matching @keyvar, or NULL if nothing found.
* @keyvar is a pointer of @type