我花了几天时间试图让图书馆在我的系统上工作。该库有几种生成 MPHF 的算法。我对最小散列函数的理解是,当我使用 MPHF 散列两个不同的键时,它们将返回两个不同的 id。我生成的 200 万个键似乎不是这种情况(整数,由算法读取为字符串)。我已经尝试了该库实现的几种算法,但所有这些算法都会导致很多键出现重复的“ID”。
这是我写的:
#include <cmph.h>
#include <iostream>
#include <fstream>
#include <bitset>
#include <string>
#include <sstream>
#include <limits.h>
using namespace std;
int main(int argc, char** argv){
FILE *fp = fopen("keys.txt", "r");
FILE *read = fopen("keys2.txt", "r");
ofstream ids("ids2.txt");
if(!fp || !read || !ids.is_open()){
cerr<<"Failed to open the file\n";
exit(1);
}
cmph_t* hash = NULL;
// source of keys
cmph_io_adapter_t *source = cmph_io_nlfile_adapter(fp);
cmph_config_t *config = cmph_config_new(source);
cmph_config_set_algo(config, CMPH_BDZ);
hash = cmph_new(config);
cmph_config_destroy(config);
char *k = (char *)malloc(sizeof(12));
while(fgets(k, INT_MAX, read) != NULL){
string key = k;
unsigned int id = cmph_search(hash, k, (cmph_uint32)key.length());
ids<<id<<"\n";
}
cmph_destroy(hash);
cmph_io_nlfile_adapter_destroy(source);
fclose(fp);
fclose(read);
ids.close();
}
如果算法声称生成最小完美散列函数,那么每个不同键的 ID 不应该是唯一的吗?有 2048383 个键。对于我的项目,我需要将 id 从 0 映射到 2048382,因为我计划使用最小的完美哈希函数。我不确定我的理解哪里出了问题。请帮忙。