From: Karsten Blees <karsten.blees@gmail.com>
To: Alexander Kuleshov <kuleshovmail@gmail.com>,
Junio C Hamano <gitster@pobox.com>
Cc: Git <git@vger.kernel.org>
Subject: Re: [RFC PATCH] hashmap API: introduce for_each_hashmap_entry() helper macro
Date: Thu, 17 Mar 2016 00:47:52 +0100 [thread overview]
Message-ID: <56E9F0A8.5080308@gmail.com> (raw)
In-Reply-To: <1458146346-27959-1-git-send-email-kuleshovmail@gmail.com>
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);
prev parent reply other threads:[~2016-03-16 23:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-16 16:39 [RFC PATCH] hashmap API: introduce for_each_hashmap_entry() helper macro Alexander Kuleshov
2016-03-16 18:09 ` Junio C Hamano
2016-03-16 18:39 ` Alexander Kuleshov
2016-03-16 23:47 ` Karsten Blees [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=56E9F0A8.5080308@gmail.com \
--to=karsten.blees@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=kuleshovmail@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).