In a program I am writing, I would like to develop a hashing algorithm which can map either a RGB color, string, or both to a unique and relatively small index.
The goal here is to reduce as many collisions as possible, with the guarantee that no two colors being passed through the algorithm will be similar (perceptually; e.g. red, blue, orange).
With my limited knowledge, the array seems the optimal choice for a direct-access data structure, but I do not want to create an incredibly large array. Given that I have to allocate memory in C++ for the array, I am having trouble developing such an algorithm.
Any tips are appreciated!
You could use
std::mapto access theSubjectby its color or label. No need to develop own hashing algorithms for that, all you have to do is to create comparison operator, which should be easy on this case, assuming you use 32bit integer for the RGB color, and astd::stringfor thelabel.Edit: Actually you dont need to make anything else than the map (no custom operators), as simple as:
std::mapis very fast, I don’t think you need (or find) any faster solution than that.