diff options
author | Eric Wong <e@80x24.org> | 2019-10-07 01:30:38 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2019-10-07 03:20:11 +0200 |
commit | 87571c3f71ba41d89eef5202f8589daa26f984ca (patch) | |
tree | d3b072917efce49f3f95d7d48763a8dfc83ef211 /hashmap.h | |
parent | hashmap_cmp_fn takes hashmap_entry params (diff) | |
download | git-87571c3f71ba41d89eef5202f8589daa26f984ca.tar.xz git-87571c3f71ba41d89eef5202f8589daa26f984ca.zip |
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>
Diffstat (limited to 'hashmap.h')
-rw-r--r-- | hashmap.h | 15 |
1 files changed, 13 insertions, 2 deletions
@@ -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 |