Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / hmap.h / hmap_init.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 <errno.h>
#include <limb/u64.h>
#include "hmap.h"

int
hmap_init(size_t dlen, size_t n, hmap *hmap)
{
    if (!hmap || !dlen || !n)
        return (errno = EINVAL, 0);

    /* number of elements (n) must be a power of 2 */
    u32 p = msb64((u64) n) - 1;
    if (n > 1U << p)
        n = 1 << (p + 1);

    hmap->sa = stralloc_zero;
    hmap->ilen = sizeof(u32) + dlen;
    if (!grow(n, hmap))
        return 0;

    return 1;
}