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);
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to