Compilation of Time Hashing C ++ 0x

Using GCC 4.4 (usually the maximum available for Android and IOS) is a way to make string hashing compilation.

We have a resource manager that displays string keys for resources. While searching is fast, hashing and string creation is slow. Sort of:

 ResourcesManager::get<Texture>("someKey");

Spends a lot of time highlighting the string "someKey" and then hashing it.

I am wondering if there is a trick that I can use for hashing at compile time.

+5
source share
3 answers

You will need to implement the correct hashing algorithm, but this can work using constexpr C ++ 11 functions:

#include <iostream>

// Dummy hashing algorithm. Adds the value of every char in the cstring.
constexpr unsigned compile_time_hash(const char* str) {
    // Modify as you wish
    return (*str == 0) ? 0 : (*str + compile_time_hash(str + 1));
}   

int main() {
    unsigned some_hash = compile_time_hash("hallou");
    std::cout << some_hash << std::endl;
}

ResourcesManager::get, compile_time_hash ( ).

, , , . - SHA * constexpr .

, constexpr GCC >= 4.6 clang >= 3.1.

+7

, .

- , . , , .

enum KeyType
{
    someKey,
    someOtherKey
};

ResourcesManager::get<Texture>(someKey);

, , .

static char * keyNames = 
{
    "someKey",
    "someOtherKey"
};
+2

...

Myself, I do not actually hash the lines, I just list the elements and output the header file with the listing. Pleasant and easy, no conflicts, etc.

0
source