Created
November 25, 2010 16:22
-
-
Save pervognsen/715590 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> | |
// Here's how I'd structure the code to eliminate code duplication: | |
struct String_Hasher | |
{ | |
size_t operator()(const char *value) const | |
{ | |
size_t hash = 0; | |
while (*value) | |
hash = (hash >> 7) + (hash << 25) + *value++; | |
return hash + (hash >> 16); | |
} | |
bool operator()(const char *left, const char *right) const | |
{ | |
return strcmp(left, right) < 0; | |
} | |
}; | |
#ifndef USE_STL_PORT | |
struct MSVC_String_Hasher : public stdext::hash_compare<const char*, std::less<const char*> >, public String_Hasher { }; | |
#endif | |
#ifdef USE_STL_PORT | |
typedef stdext::hash_map<const char*, const char*, String_Hasher, String_Hasher> StringHashMap; | |
#else | |
typedef stdext::hash_map<const char*, const char*, MSVC_String_Hasher> StringHashMap; | |
#endif | |
int main() | |
{ | |
stdext::hash_map<const char*, int, MSVC_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