I have:
const unsigned int hash_1 = 0xaf019b0c;
const unsigned int hash_2 = 0xf864e55c;
const unsigned int hash_3 = 0xfaea8ed5;
Hashes come from an automatically generated header. These hashes are indirectly associated with tags 1, 2, 3. The tags are associated with classes through a simple compile-time generated id. That way I can GetTag<Class1>() and get my int-tag for Class1.
My goal is to simplify the hash->tag association. Preferably this should be compile-time generated and O(1) access time. Memory in this case is not an issue. I can not use any third-party software.
I have tried the following:
template<uint32 t> size_t GetTagByHash() { return 0; }
with specific implementations like:
template<> size_t GetTagByHash<hash_1>() { return GetTag<Class1>(); }
but that kind of implementation is difficult to use since if I have a local variable uint32 my_hash; that the compiler can’t determine what value it has in compile-time then the compiler is unable to resolve the correct implementation of GetTagByHash() to call.
As I understand it, your problem is how to do this lookup with run-time values as well as compile-time ones.
You’ve really got two questions. First, what algorithm do you want to use to do the lookup, and second, how do you tell C++ to implement that?
The algorithm to use is somewhat of a non-obvious question; you’ve got a list of effectively-random numbers, and you want to look up something in that list and return an associated tag. Probably you want some sort of hashtable, but to start with, I’ll show some examples with something simpler — and likely better for small numbers of hashes: A simple O(N) lookup, in pseudocode:
Now, how do you tell C++ to do this? You’ve got to create a list of all your hash tags, and instructions for doing that. Here’s a simple way:
And so forth. Then, at the end, you can have
Writing out all those identical definitions is a pain, though, so it’s best to let C++ do that — and, to do that, you need to define the hashes in such a way that they can be iterated over. Let’s do that:
Then, you use this as before.
Now, let’s return to that first question — what algorithm do you actually want to use to do the lookup? The advantage of this iterative O(N) lookup is that it’s easy to program, and it doesn’t require any initialization of any data structures at run-time — you can just call it. However, as noted, it’s O(N). An alternate choice is to use a
std::mapobject; you can use a similar recursive definition to initialize it at runtime, and then use it. That might look something like this:Personally, I would probably combine this with your
GetTagByHash<>idea, and give the Hash_loop a “runtime-computed result” function as I described, as well as a “compile-time-computed result” function that takes a template argument rather than a function argument. But, in general, that’s the basic idea for doing runtime lookup — you put the values you want to look up into a set of templated classes that you can recursively iterate over at compile time, and then you use that recursive iteration to either define a lookup function or initialize a runtime structure that you can use for doing the lookup.