Passing equals method to hash table

Hi,

I am using google hash map. This is the way to pass an equals method from their documentation:
1
2
3
4
5
6
7
8
9
10
11
struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return (s1 == s2) || (s1 && s2 && strcmp(s1, s2) == 0);
  }
};

int main()
{
  sparse_hash_map<const char*, int, hash<const char*>, eqstr> months;


Rather than comparing two strings, my operator() method will compare an array of bytes. The problem is, the length of this array can only be determined at run time, and thus I cannot hard-code the number of elements to compare into the function.

Using a specially designated terminating byte (null terminator in the case of strings) embedded into the hash key is undesirable because I want to keep my hash keys as small as possible (they will be < 10 bytes long, and I want to pack as many as possible into memory).

So my question is: Is there any way to have the operator() method compare the correct number of elements without embedding this information into the key?

Thanks in advance.
If sparse_hash_map gives you a constructor that allows you specify a comparator
function, then you can give eqstr a constructor which specifies the length. Store
the length in the object.
Topic archived. No new replies allowed.