From 87571c3f71ba41d89eef5202f8589daa26f984ca Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 6 Oct 2019 23:30:38 +0000 Subject: 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 Reviewed-by: Derrick Stolee Signed-off-by: Junio C Hamano --- hashmap.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'hashmap.h') diff --git a/hashmap.h b/hashmap.h index 8f5c163d56..8d4b3907b4 100644 --- a/hashmap.h +++ b/hashmap.h @@ -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 -- cgit v1.2.3