turbonss/deps/cmph/examples/file_adapter_ex2.c

33 lines
874 B
C
Raw Permalink Normal View History

2005-07-26 00:26:17 +03:00
#include <cmph.h>
#include <stdio.h>
#include <string.h>
2009-06-13 03:49:26 +03:00
// Create minimal perfect hash function from in-disk keys using BDZ algorithm
2005-07-26 00:26:17 +03:00
int main(int argc, char **argv)
{
//Open file with newline separated list of keys
FILE * keys_fd = fopen("keys.txt", "r");
cmph_t *hash = NULL;
if (keys_fd == NULL)
{
fprintf(stderr, "File \"keys.txt\" not found\n");
exit(1);
}
// Source of keys
cmph_io_adapter_t *source = cmph_io_nlfile_adapter(keys_fd);
cmph_config_t *config = cmph_config_new(source);
2009-06-13 03:49:26 +03:00
cmph_config_set_algo(config, CMPH_BDZ);
2005-07-26 00:26:17 +03:00
hash = cmph_new(config);
cmph_config_destroy(config);
//Find key
const char *key = "jjjjjjjjjj";
2008-04-12 09:17:21 +03:00
unsigned int id = cmph_search(hash, key, (cmph_uint32)strlen(key));
2005-07-26 00:26:17 +03:00
fprintf(stderr, "Id:%u\n", id);
//Destroy hash
cmph_destroy(hash);
2005-10-10 20:43:21 +03:00
cmph_io_nlfile_adapter_destroy(source);
2005-07-26 00:26:17 +03:00
fclose(keys_fd);
return 0;
}