Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 
avatar of Captain Obvlious

Captain Obvlious

Captain Obvlious has asked 3 questions and find answers to 89 problems.

Stats

2.2k
EtPoint
714
Vote count
3
questions
89
answers

About

.

//////////////////////////
// strings as case labels
// this does not handle hash collisions
//////////////////////////

constexpr uint32_t djb2Hash(
    const char* str, int index = 0)
{
    return !str[index]
        ? 0x1505
        : (djb2Hash(str, index + 1) * 0x21) ^ str[index];
}

// Create a literal type for short-hand case strings
constexpr unsigned int operator"" _C(
    const char str[], size_t size)
{
    return djb2Hash(str);
}

void Bloblawblah(const std::string& cond)
{
    switch(djb2Hash(cond.c_str()))
    {
    case "Hello"_C: std::cout << "Goodbye"; break;
    case "World"_C: std::cout << "Planet"; break;
    default: std::cout << "BOGUS!";
    }
    std::cout << std::endl;
}