CMinimalPerfectHashingLibrary(CMPH)是一个可移植的基于LGPL协议发布的C语言的哈希函数库。该库封装了最新而且最高效的算法,易用,稳定、快速。
示例代码:
#include<cmph.h>#include<string.h>//Createminimalperfecthashfunctionfromin-memoryvectorintmain(intargc,char**argv){//Creatingafilledvectorunsignedinti=0;constchar*vector[]={"aaaaaaaaaa","bbbbbbbbbb","cccccccccc","dddddddddd","eeeeeeeeee","ffffffffff","gggggggggg","hhhhhhhhhh","iiiiiiiiii","jjjjjjjjjj"};unsignedintnkeys=10;FILE*mphf_fd=fopen("temp.mph","w");//Sourceofkeyscmph_io_adapter_t*source=cmph_io_vector_adapter((char**)vector,nkeys);//Createminimalperfecthashfunctionusingthebrzalgorithm.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);cmph_dump(hash,mphf_fd);cmph_destroy(hash);fclose(mphf_fd);//Findkeymphf_fd=fopen("temp.mph","r");hash=cmph_load(mphf_fd);while(i<nkeys){constchar*key=vector[i];unsignedintid=cmph_search(hash,key,(cmph_uint32)strlen(key));fprintf(stderr,"key:%s--hash:%u\n",key,id);i++;}//Destroyhashcmph_destroy(hash);cmph_io_vector_adapter_destroy(source);fclose(mphf_fd);return0;}
评论