Is there any function in C ++ that calculates the footprint or hash of a string that must be at least 64 bits wide?
is there any function in C ++ that calculates the fingerprint or hash of a string that must be at least 64 bits?
I want to replace mine unordered_map<string, int>
with unordered_map<long long, int>
.
Given the answers I get (thanks to the Stack Overflow community ...) the technique I am describing is not known. The reason I want to get an unordered fingerprint card instead of lines is space and speed. The second map does not need to store rows, and it does not take extra extra cache misses when performing a search to retrieve those rows. The only drawback is the low probability of collision. Therefore, the key must be 64 bits: the probability 2 ^ (- 64) is basically impossible. Of course this is based on a good hash function, which is why my question is looking for.
Thanks again, Stack Overflowers.
a source to share
C ++ has no built-in 128-bit type and has no built-in hashing support. Such hashing extensions are supposed to be added to TR1, but as far as I know, 128 bit ints are not supported by many of my compilers. (Microsoft supports the type __int128
- x64 platforms only)
I would expect the functions included in unordered_map to be faster anyway.
If you really want to do this, MD5 provides a nice 128-bit hash.
a source to share
If you want to match any string to a unique integer:
typedef std::map<string,long long> Strings;
static Strings s_strings;
long long s_highWaterMark = 0;
long long my_function(const string& s)
{
Strings::const_iterator it = s_strings.find(s);
if (it != s_strings.end())
{
//we've previously returned a fingerprint for this string
//now return the same fingerprint again
return it->second;
}
//else new fingerprint
long long rc = ++s_highWaterMark;
//... remember it for next time
s_strings.insert(Strings::value_type(s, rc));
//... and return it this time
return rc;
}
a source to share