Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created November 25, 2010 16:08
Show Gist options
  • Save pervognsen/715579 to your computer and use it in GitHub Desktop.
Save pervognsen/715579 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <assert.h>
#include <hash_map>
struct String_Hasher
{
static const size_t bucket_size = 4;
static const size_t min_buckets = 8;
size_t operator()(const char *value) const
{
size_t hash = 0;
while (*value)
hash = (hash >> 7) + (*value++ << 25);
return hash;
}
bool operator()(const char *left, const char *right) const
{
return strcmp(left, right) < 0;
}
};
int main()
{
stdext::hash_map<const char*, int, String_Hasher> m;
m["foo"] = 42;
m["bar"] = 24;
assert(m["foo"] == 42);
assert(m["bar"] == 24);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment