1

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.

3
  • Shall your hash be based on the identifying handle you gave, or on the underlying data in the database? Commented Sep 17, 2014 at 15:31
  • 3
    Use std::hash<int> and see stackoverflow.com/questions/2590677/… Commented Sep 17, 2014 at 15:34
  • @Deduplicator I need to find values on the basis of imgId,x and y position. That is the combination of them is primary key. Commented Sep 17, 2014 at 16:16

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I thought that is preferable that the 3 constants should be prime but different... Why use the same 3 times?
@BasileStarynkevitch It doesn't make all that much difference. I changed them, for no particular reason. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.