qlibs / mph

C++20 [Minimal] Static Perfect Hash library
176 stars 9 forks source link

Incorrect lookup result when using array of keys and len(keys) is greater than 256 #12

Closed zifeitong closed 1 month ago

zifeitong commented 1 month ago

Thanks for this amazing library!

I ran it an issue when using array of keys as input and the number of keys is greater than 256.

Reproducer:

#include <https://raw.githubusercontent.com/qlibs/mph/main/mph>
#include <array>
#include <utility>

template <std::size_t N>
std::array<int, N> constexpr make_array() {
    std::array<int, N> values;
    int count = 0;
    for (auto &v : values) {
        v = count++;
    }
    return values;
}

constexpr auto values = make_array<400>();

static_assert(0 == mph::lookup<values>(0));
static_assert(255 == mph::lookup<values>(255));

// BUG
static_assert(0 == mph::lookup<values>(256));
static_assert(1 == mph::lookup<values>(257));
// BUG

https://godbolt.org/z/8a7976dcq

kris-jusiak commented 1 month ago

Hi @zifeitong, thanks for reporting, indeed there was an issue with smallest required type deduction based on entries, fixed now, you example is now working correctly - https://godbolt.org/z/YPeae3Eao, thanks