Created
November 25, 2010 16:08
-
-
Save pervognsen/715579 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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