Subject: Re: [RFC PATCH] hashmap API: introduce for_each_hashmap_entry() helper macro

From: Karsten Blees <[email-masked]>
Date: Thu, 17 Mar 2016 00:47:52 +0100

Am 16.03.2016 um 17:39 schrieb Alexander Kuleshov:

> There is common pattern to traverse a hashmap in git source code:
> 
>         hashmap_iter_init(map, &iter);
>         while ((entry = hashmap_iter_next(&iter)))
>              // do something with entry
> 

The hashmap_iter_first() function allows you to do this instead:

	for (entry = hashmap_iter_first(map, &iter); entry; entry = hashmap_iter_next(&iter))
		doSomething(entry);

With an appropriate macro definition, this could be simplified to:

	#define hashmap_for_each(map, iter, entry) for (entry = hashmap_iter_first(map, iter); entry; entry = hashmap_iter_next(iter))
	...
	hashmap_for_each(map, &iter, entry)
		doSomething(entry);

You would still need to declare the 'iter' and 'entry' variables, but
there is no danger of decl-after-statement or variable shadowing
mentioned by Junio. That is, you can do this:

	hashmap_for_each(map, &iter, entry)
		if (checkCondition(entry))
			break;
	// work with found entry

Or even this:

	hashmap_for_each(map, &iter1, entry1)
		hashmap_for_each(map, &iter2, entry2)
			doSomething(entry1, entry2);