user3747190
user3747190

Reputation: 1707

How should I define my own hash function for unordered_map in C++

I have a database of image patches i.e. a 300*300 images with each patch of size 60*60. Thus giving me a grid of 5*5. I want to store some information against each of these patch. Since, my database can consist of millions of images, I wanted to use unordered_map so that finding a patch become easy.

My patch information consist of imgId, x_position of patch and y_position of patch (all integers). I don't have much experience with unordered_map. But going through some tutorials, I realised that I will have to use my own hash function. Can anyone suggest some efficient way of storing the above info in unordered_map with proper hash function.

Upvotes: 1

Views: 212

Answers (1)

David Schwartz
David Schwartz

Reputation: 182761

You can use any hash function you like to combine the three integers. Here's a classic Knuth 32-bit combiner:

int hash(int v1, int v2, int v3)
{
  int v = v1;
  v *= 2654435741;
  v += v2;
  v *= 2654435761;
  v ^= v3;
  v *= 2654435789;
  return v;
}

Upvotes: 1

Related Questions