1 #include <cmph.h> 2 #include <stdio.h> 3 #include <string.h> 4 // Create minimal perfect hash function from in-disk keys using BDZ algorithm main(int argc,char ** argv)5int main(int argc, char **argv) 6 { 7 //Open file with newline separated list of keys 8 FILE * keys_fd = fopen("keys.txt", "r"); 9 cmph_t *hash = NULL; 10 if (keys_fd == NULL) 11 { 12 fprintf(stderr, "File \"keys.txt\" not found\n"); 13 exit(1); 14 } 15 // Source of keys 16 cmph_io_adapter_t *source = cmph_io_nlfile_adapter(keys_fd); 17 18 cmph_config_t *config = cmph_config_new(source); 19 cmph_config_set_algo(config, CMPH_BDZ); 20 hash = cmph_new(config); 21 cmph_config_destroy(config); 22 23 //Find key 24 const char *key = "jjjjjjjjjj"; 25 unsigned int id = cmph_search(hash, key, (cmph_uint32)strlen(key)); 26 fprintf(stderr, "Id:%u\n", id); 27 //Destroy hash 28 cmph_destroy(hash); 29 cmph_io_nlfile_adapter_destroy(source); 30 fclose(keys_fd); 31 return 0; 32 } 33