简体   繁体   中英

std::hash(c++) equivalent in Node js

I am trying to implement std::hash functionality of C++ in Node.js and compare them. Since std::hash is not based on any cryptographic algorithms, I couldn't understand how it is generating internally.

Usage reference in C++: https://iq.opengenus.org/std-hash-cpp/

Ten seconds of searching yielded the following fragment for libcxx (the C++ STL that comes with Clang):

// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
// is 64 bits.  This is because cityhash64 uses 64bit x 64bit
// multiplication, which can be very slow on 32-bit systems.
template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
struct __murmur2_or_cityhash;

template <class _Size>
struct __murmur2_or_cityhash<_Size, 32>
{
    inline _Size operator()(const void* __key, _Size __len)
         _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
};

libstdc++ seems to use murmur hash as well, although the hash function is the identity function for a lot of simple types (eg char, int, double, ...)

Note for future readers: this answer was true at the time of writing. The algorithm behind std::hash can change in the future!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM