<p>Yes, you can build a minimal perfect hash function (MPHF) at runtime. There are multiple algorithms you can use, but most of them are a bit complex to implement so I can't give you working sample code. Many are implemented in the <a href="http://cmph.sourceforge.net/" rel="nofollow noreferrer">cmph project</a>.</p>
<p>The most simple one is probably BDZ. On a high level, lookup requires calculating 3 hash functions, and 3 memory accesses. If memory isn't an issue, you only need 2. It supports millions of keys. This algorithm requires a lookup table that is about 1.23 times the number of entries, when using 3 hash functions, and with 2 bits per entry.</p>
<p>There are other algorithms, one I invented myself, <a href="https://www.slideshare.net/ThomasMueller12/recsplit-minimal-perfect-hashing" rel="nofollow noreferrer">the RecSplit algorithm</a> (there's even a <a href="https://arxiv.org/abs/1910.06416" rel="nofollow noreferrer">research paper</a> now), and there is a <a href="https://github.com/vigna/sux/blob/master/sux/function/RecSplit.hpp" rel="nofollow noreferrer">C++ implementation</a>, and <a href="https://github.com/thomasmueller/minperf/blob/master/src/main/java/org/minperf/hem/recsplit/FastGenerator.java" rel="nofollow noreferrer">Java</a> right now. Basically, the algorithms finds a way to split the set into subsets (recursively), until the subset size is 1. You need to remember how you split. The most simple solution is in fact using a lookup table for &quot;how you split&quot;, but the table is really small, possibly only 5 integers for 64 keys. The first one to divide into 4 subsets of 16, and 4 to map each subset to a number 0..15.</p>
<p>(I added a second answer if you don't strictly need a <em>minimal</em> perfect hash function, just a <em>perfect</em> hash function. Construction is simpler and lookup is a lot faster, but requires a larger array.)</p>
Is it possible to create a Minimal Perfect Hash function without a separate lookup table for a small (<64) set of keys?
<p>I recently read this article <a href="http://stevehanov.ca/blog/?id=119" rel="nofollow noreferrer">Throw away the keys: Easy, Minimal Perfect Hashing</a> about generating a minimal perfect hash table for a known set of keys.</p>

<p>The article seems to assume that you need an intermediate table. Is there any other, simpler way to generate such a function if we assume that the set of keys is small (i.e. &lt; 64).</p>

<p>In my case, I want to map a set of thread ID:s to a unique block of data within an array. The threads are started before the hash function is generated and stay constant during the running time of the program. The exact number of threads vary but stays fixed during the runtime of the program:</p>

<pre><code>unsigned int thread_ids*;
unsigned int thread_count;
struct {
    /* Some thread specific data */
}* ThreadData;

int start_threads () {
    /* Code which starts the threads and allocates the threaddata. */
}

int f(thread_id) {
    /* return unique index into threadData */
}

int main() {
    thread_count = 64; /* This number will be small, e.g. &lt; 64 */
    start_threads();
    ThreadData[f(thread_ids[0])]
}
</code></pre>