ikod / ikod-containers

some standard containers, safe, nogc
8 stars 3 forks source link

ikod.containers.hashmap.HashMap does not behave as expected for `const(char)[]` keys #3

Open n8sh opened 3 years ago

n8sh commented 3 years ago
#!/usr/bin/env dub
/+dub.sdl:
dependency "ikod-containers" version="~>0.0.20"
+/
void main()
{
    import ikod.containers.hashmap;

    HashMap!(const(char)[], size_t) map;
    const(char)[] key = "key";
    const(char)[] dupKey = key.dup;
    map[dupKey] = 1;
    assert(map[dupKey] == 1); // passes
    assert(key == dupKey); // passes
    assert(map[key] == 1); // fails: key not found
}
ikod commented 3 years ago

Thanks for report, will check

n8sh commented 3 years ago

I see what's going on, immutable(char)[] hashing is special cased but other arrays are not, so when I changed from string to const(char)[] it switched to hashing the pointer + length instead of the contents pointed to.

https://github.com/ikod/ikod-containers/blob/b8fe3caea4dd68cd4edfa92b27fe070b17e497bb/source/ikod/containers/hash.d#L41-L52