Skip to content

Instantly share code, notes, and snippets.

@pr0crustes
Created August 4, 2018 14:51
Show Gist options
  • Save pr0crustes/a7972a2d0b9668ecfcfaa917abae53dc to your computer and use it in GitHub Desktop.
Save pr0crustes/a7972a2d0b9668ecfcfaa917abae53dc to your computer and use it in GitHub Desktop.
Made only to help :)
#include <iostream>
// THIS HASH FUNCTION IS A ADAPTATION FROM https://stackoverflow.com/questions/8317508/hash-function-for-a-string
// constexpr is C++11
constexpr unsigned hash_str(const char* s) {
unsigned h = 37;
while (*s) {
h = (h * 54059) ^ (s[0] * 76963);
s++;
}
return h;
}
// End HASH FUNCTION
void aFunction(const char* s) {
// We switch the hash of 's'
switch (hash_str(s)) {
// The possible values could also be in a ENUM
case hash_str("Case1"):
std::cout << "It's Case1" << std::endl;
break;
case hash_str("Case2"):
std::cout << "It's Case2" << std::endl;
break;
case hash_str("Case3"):
std::cout << "It's Case3" << std::endl;
break;
default:
std::cout << "Not Found" << std::endl;
break;
}
}
int main() {
aFunction("Case1");
aFunction("Case3");
aFunction("Other Case");
aFunction("Case2");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment