Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Add std::hash<pmem::obj::string> specialization #1007

Open
karczex opened this issue Jan 5, 2021 · 2 comments
Open

Add std::hash<pmem::obj::string> specialization #1007

karczex opened this issue Jan 5, 2021 · 2 comments
Labels
Type: Feature New feature or request

Comments

@karczex
Copy link

karczex commented Jan 5, 2021

FEAT: Add std::hash<pmem::obj::string>

Rationale

It's needed to easily use pmem::obj::concurrent_hash_map with pmem::obj::string as a key.

API Changes

Add Default implementation of hasher for pmem::obj::string. Currently user have to provide custom one.

Implementation details

Probably, it would be enough to move implementation of fibonacci hashing from tests

@karczex karczex added the Type: Feature New feature or request label Jan 5, 2021
@jan-konczak-cs-put
Copy link

jan-konczak-cs-put commented Mar 26, 2021

Probably, it would be enough to move implementation of fibonacci hashing from tests

That's a terrible idea from my (==user) point of view.

If you do, then please add a hash that enables lookups and insertions by std::string or std::string_view rather than implementing an incompatible one. So far this works as a cheap replacement:

struct MyComp {
    template <typename T>
    size_t operator()(T a, const pm::string & b) const {
        return a == std::string_view(b.data(), b.length());
    }
};

struct MyHash{
    auto operator()(const pmem::obj::string & str) const {
        return operator()(std::string_view(str.data(), str.length()));
    }
    template<typename T>
    size_t operator()(T str) const {
        return std::hash<T>{}(str);
    }
    typedef MyComp transparent_key_equal;
};

and this allows for:

pm::concurrent_hash_map<pm::string, pm::string, MyHash, MyComp> kvmap;

std::string key, value;

decltype(kvmap)::const_accessor acc;
bool found = kvmap.find(acc, key);

kvmap.insert_or_assign(key, value);

@igchor
Copy link
Contributor

igchor commented Mar 26, 2021

@jan-konczak-cs-put right, pmem::obj::string and std::string with the same content should hash to the same value. For C++17 we can just implement std::hash<pmem::obj::string> as you did. The problem is only for older standards (there is no easy way to use existing std hash on a sequence of characters). But maybe, we don't have to provide it for older standards...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants