我正在尝试使用MurmurHash3哈希函数对Bloom过滤器的C ++实现进行编码。我的实现基于以下站点:http://blog.michaelschmatz.com/2016/04/11/how-to-write-a-bloom-filter-cpp/
以某种方式,在我的BloomFilter头文件中,哈希函数引发了一个不完整的类型错误,而且,当我在add函数内部使用哈希函数时,我得到了erroe-对`MurmurHash3_x64_128(void const *,int ,unsigned int,void *)'。我想知道如何解决此问题。此外,我熟悉C ++,但我想知道如何将文件中的数据作为参数传递给此对象。我应该读取数组中文件存储的每一行,并将数组传递给object memebers参数吗?
我的代码是:
#ifndef BLOOM_FILTER_H
#define BLOOM_FILTER_H
#include "MurmurHash3.h"
#include <vector>
#include <array>
//basic structure of a bloom filter object
struct BloomFilter {
BloomFilter(size_t size, uint8_t numHashes);
void add(const uint8_t *data, std::size_t len);
bool possiblyContains(const uint8_t *data, std::size_t len) const;
private:
uint8_t m_numHashes;
std::vector<bool> m_bits;
};
//Bloom filter constructor
BloomFilter::BloomFilter(size_t size, uint8_t numHashes)
: m_bits(size),
m_numHashes(numHashes) {}
//Hash array created using the MurmurHash3 code
static std::array<uint64_t, 2> myhash(const uint8_t *data, std::size_t len)
{
std::array<uint64_t, 2> hashValue;
MurmurHash3_x64_128(data, len, 0, hashValue.data());
return hashValue;
}
//Hash array created using the MurmurHash3 code
inline size_t nthHash(int n,
uint64_t hashA,
uint64_t hashB,
size_t filterSize) {
return (hashA + n * hashB) % filterSize; // <- not sure if that is OK,
perhaps it is.
}
//Adds an element to the array
void BloomFilter::add(const uint8_t *data, std::size_t len) {
auto hashValues = myhash(data, len);
for (int n = 0; n < m_numHashes; n++)
{
m_bits[nthHash(n, hashValues[0], hashValues[1], m_bits.size())] = true;
}
}
//Returns true or false based on a probabilistic assesment of the array
using MurmurHash3
bool BloomFilter::possiblyContains(const uint8_t *data, std::size_t len)
const {
auto hashValues = myhash(data, len);
for (int n = 0; n < m_numHashes; n++)
{
if (!m_bits[nthHash(n, hashValues[0], hashValues[1], m_bits.size())])
{
return false;
}
}
return true;
}
#endif