Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / hmap.h / hmap_next.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include "hmap.h"

void *
hmap_next(u32 *key, hmap *hmap)
{
    struct item *it;
    u32 i, j;

    if (!hmap->sa.len)
        return NULL;

    if (*key) {
        for (i = *key, j = 1; ; i += j++) {
            it = ITEM(i & hmap->sa.a, hmap);
            if (!it->key || it->key == *key)
                break;
        }
        i &= hmap->sa.a;
        if (++i > hmap->sa.a)
            return NULL;
    } else {
        i = 0;
        it = ITEM(i, hmap);
    }

    for ( ; i <= hmap->sa.a; ++i) {
        it = ITEM(i, hmap);
        if (it->key) break;
    }

    if (!it->key)
        return NULL;

    *key = it->key;
    return it->data;
}