Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hash value of int is the same number

Tags:

c++

hash

stl

Why does hash return itself as a hash value?

I've set a hash and a hash side-by-side, and the one for string works as expected, while the int one produces itself as a hash value!

Is this how it should work ?

hash<int> h;
for( int i=0 ; i<15 ; ++i )
{
    cout << "int hash for " << i << " is " << h(i) << endl; 
}

hash<string> hs;
for( int i=0; i<15; ++i) {
    stringstream ss;
    ss << i;
    cout << "string hash for " << i << " is " << hs(ss.str()) << endl; 
}

and the result is

+ g++-4.8 -std=c++11 -O2 -Wall -pedantic -Weffc++ -Wextra main.cpp
+ ./a.out
int hash for 0 is 0
int hash for 1 is 1
int hash for 2 is 2
int hash for 3 is 3
int hash for 4 is 4
int hash for 5 is 5
int hash for 6 is 6
int hash for 7 is 7
int hash for 8 is 8
int hash for 9 is 9
int hash for 10 is 10
int hash for 11 is 11
int hash for 12 is 12
int hash for 13 is 13
int hash for 14 is 14
string hash for 0 is 2297668033614959926
string hash for 1 is 10159970873491820195
string hash for 2 is 4551451650890805270
string hash for 3 is 8248777770799913213
string hash for 4 is 16215888864653804456
string hash for 5 is 7995238595416485409
string hash for 6 is 3835993668518112351
string hash for 7 is 905566266843721762
string hash for 8 is 17899137375351314596
string hash for 9 is 6623666755989985924
string hash for 10 is 2594088158685378223
string hash for 11 is 9717255990760309898
string hash for 12 is 11194622142508650503
string hash for 13 is 15638310844945205280
string hash for 14 is 1116181219906060362

you can see it running on: http://coliru.stacked-crooked.com/a/0c0e1536d19c533f

like image 382
Grim Fandango Avatar asked Nov 01 '13 20:11

Grim Fandango


2 Answers

Why does hash return itself as a hash value?

Quite bluntly, because it can. This is the most efficient thing to do, and it gives you a perfect hash function for integers. You simply cannot do better than that!

like image 158
Sergey Kalinichenko Avatar answered Sep 20 '22 11:09

Sergey Kalinichenko


Don't confuse an object's hash value with it's slot index in the hash you may be storing it in. The hash value is simply a best-attempt at a near-to-unique numerical value for a given input value.

What your code demonstrated it was that, for each integer value you hashed, a unique hash value was returned.

That's an ideal hash function.

std::hash<int, std::string> hashTable;

hashTable[42] = "hello";

The hash value of 42 is 42. But there probably aren't 42 buckets in this hash. operator[] is overloaded here and is going to constrain the hash value by the hash distribution (number of buckets) to determine which slot to put you in.

key = 42
hashValue = 42
hashKey = 42 % m_bucket.size() // which bucket to look for this key in
like image 33
kfsone Avatar answered Sep 18 '22 11:09

kfsone