Jun 11, 2022

[algorithm] fast string hash function

Reference: 
http://www.cse.yorku.ca/~oz/hash.html

auto hashed = [] (const char* str) consteval {
    std::size_t hash = 5381;
    while (*str != '\0') {
        hash = hash * 33 ^ *str++;
    }
    return hash;
};

// requires hashed() in compile-time context
enum class drinkHashes : long {
	beer = hashed("beer"),
    wine = hashed("wine"),
    water = hashed("water"),
};

std::array arr{hashed("beer"), hashed("wine"), hashed("water")};

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.