2005-07-26 00:26:17 +03:00
|
|
|
#include <cmph.h>
|
2006-04-27 20:30:19 +03:00
|
|
|
#include <string.h>
|
2005-07-26 00:26:17 +03:00
|
|
|
// Create minimal perfect hash function from in-memory vector
|
|
|
|
int main(int argc, char **argv)
|
2009-06-12 09:07:54 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
// Creating a filled vector
|
2008-03-30 02:23:41 +02:00
|
|
|
unsigned int i = 0;
|
2009-06-12 09:07:54 +03:00
|
|
|
const char *vector[] = {"aaaaaaaaaa", "bbbbbbbbbb", "cccccccccc", "dddddddddd", "eeeeeeeeee",
|
|
|
|
"ffffffffff", "gggggggggg", "hhhhhhhhhh", "iiiiiiiiii", "jjjjjjjjjj"};
|
|
|
|
unsigned int nkeys = 10;
|
2018-12-28 04:51:37 +02:00
|
|
|
FILE* mphf_fd = fopen("temp.mph", "wb");
|
2009-06-12 09:07:54 +03:00
|
|
|
// Source of keys
|
|
|
|
cmph_io_adapter_t *source = cmph_io_vector_adapter((char **)vector, nkeys);
|
2005-07-26 00:26:17 +03:00
|
|
|
|
2009-06-13 03:49:26 +03:00
|
|
|
//Create minimal perfect hash function using the brz algorithm.
|
2009-06-12 09:07:54 +03:00
|
|
|
cmph_config_t *config = cmph_config_new(source);
|
|
|
|
cmph_config_set_algo(config, CMPH_BRZ);
|
|
|
|
cmph_config_set_mphf_fd(config, mphf_fd);
|
|
|
|
cmph_t *hash = cmph_new(config);
|
|
|
|
cmph_config_destroy(config);
|
2008-03-30 02:23:41 +02:00
|
|
|
cmph_dump(hash, mphf_fd);
|
2009-06-12 09:07:54 +03:00
|
|
|
cmph_destroy(hash);
|
|
|
|
fclose(mphf_fd);
|
|
|
|
|
|
|
|
//Find key
|
2018-12-28 04:51:37 +02:00
|
|
|
mphf_fd = fopen("temp.mph", "rb");
|
2008-03-30 02:23:41 +02:00
|
|
|
hash = cmph_load(mphf_fd);
|
|
|
|
while (i < nkeys) {
|
2009-06-12 09:07:54 +03:00
|
|
|
const char *key = vector[i];
|
|
|
|
unsigned int id = cmph_search(hash, key, (cmph_uint32)strlen(key));
|
|
|
|
fprintf(stderr, "key:%s -- hash:%u\n", key, id);
|
|
|
|
i++;
|
2008-03-30 02:23:41 +02:00
|
|
|
}
|
2009-06-12 09:07:54 +03:00
|
|
|
|
|
|
|
//Destroy hash
|
|
|
|
cmph_destroy(hash);
|
|
|
|
cmph_io_vector_adapter_destroy(source);
|
2008-03-30 02:23:41 +02:00
|
|
|
fclose(mphf_fd);
|
2009-06-12 09:07:54 +03:00
|
|
|
return 0;
|
2005-07-26 00:26:17 +03:00
|
|
|
}
|