git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Karsten Blees <karsten.blees@gmail.com>
Date
Mar 16, 2016, 23:47 UTC
Message-ID
<56E9F0A8.5080308@gmail.com>
In-Reply-To
<1458146346-27959-1-git-send-email-kuleshovmail@gmail.com>
Am 16.03.2016 um 17:39 schrieb Alexander Kuleshov:
Show 6 quoted lines
> 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);
Previous: Alexander Kuleshov
Message 4 of 4 in “hashmap API: introduce for_each_hashmap_entry() helper macro”
  1. hashmap API: introduce for_each_hashmap_entry() helper macroAlexander Kuleshov, Mar 16, 2016
  2. Junio C HamanoMar 16, 2016
  3. Alexander KuleshovMar 16, 2016
  4. Karsten BleesMar 16, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.