Captain Obvlious's user avatar
Captain Obvlious's user avatar
Captain Obvlious's user avatar
Captain Obvlious
  • Member for 3 years, 2 months
  • Last seen more than a month ago
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;
}
Badges
This user doesn’t have any gold badges yet.
This user doesn’t have any silver badges yet.
3
bronze badges
Top tags
8
Score
1
Posts
100
Posts %
8
Score
1
Posts
100
Posts %
8
Score
1
Posts
100
Posts %
8
Score
1
Posts
100
Posts %