glib-aux: clear iterator in nm_dedup_multi_iter_{next,prev}() at the end

It seems slightly nicer not to leave a dangling pointer at the
end of the iteration. Then you could do something like

    nm_dedup_multi_iter_init(&iter, head_entry);
    while (nm_dedup_multi_iter_next(&iter)) {

        if (some_condition())
            break;
    }
    if (!iter.current)
        printf("iterated to the end\n");

As nm_dedup_multi_iter_next() and nm_dedup_multi_iter_init() are inline
functions, the compiler should even be able to see that the initial
setting becomes unnecessary (the field will be initialized by the
first nm_dedup_multi_iter_next()). Likewise, the final clearing
of the field might also be optimized away at the end of the iteration
(if, as in the common case, the iterator is not accessed afterwards).
This commit is contained in:
Thomas Haller 2021-08-17 19:18:42 +02:00
parent 57a519cc03
commit 53070705b0
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -352,8 +352,10 @@ nm_dedup_multi_iter_next(NMDedupMultiIter *iter)
{
g_return_val_if_fail(iter, FALSE);
if (!iter->_next)
if (!iter->_next) {
iter->current = NULL;
return FALSE;
}
/* we always look ahead for the next. This way, the user
* may delete the current entry (but no other entries). */
@ -370,8 +372,10 @@ nm_dedup_multi_iter_prev(NMDedupMultiIter *iter)
{
g_return_val_if_fail(iter, FALSE);
if (!iter->_prev)
if (!iter->_prev) {
iter->current = NULL;
return FALSE;
}
/* we always look ahead for the prev. This way, the user
* may delete the current entry (but no other entries). */