From: Karsten Blees Date: Wed, 16 Mar 2016 23:47:52 GMT Subject: Re: [RFC PATCH] hashmap API: introduce for_each_hashmap_entry() helper macro 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: > 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);