Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
n8sh opened this issue Mar 5, 2021 · 2 comments

Comments

@n8sh
Copy link

n8sh commented Mar 5, 2021

#!/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
Copy link
Owner

ikod commented Mar 5, 2021

Thanks for report, will check

@n8sh
Copy link
Author

n8sh commented Mar 5, 2021

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.

else static if ( is(T == string) ) {
// // FNV-1a hash
// ulong h = 0xcbf29ce484222325;
// foreach (const ubyte c; cast(ubyte[]) v)
// {
// h ^= c;
// h *= 0x100000001b3;
// }
// return cast(hash_t)h;
import core.internal.hash : bytesHash;
return bytesHash(cast(void*)v.ptr, v.length, 0);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants