Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better to check at compile time. (If at all) #16

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/BloomFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static vector<BlockType> readVectorFromStream(BinaryInputStream &in);
// Implementation

BloomFilter::BloomFilter(size_t maxItems, double targetProbability) {
static_assert(CHAR_BIT == BITS_PER_BLOCK, "Unsupported architecture: char is not 8 bit");
checkArchitecture();
bitCount = (size_t) ceil((maxItems * log(targetProbability)) / log(1.0 / (pow(2.0, log(2.0)))));
auto blocks = (size_t) ceil(bitCount / (double) BITS_PER_BLOCK);
Expand All @@ -50,21 +51,16 @@ BloomFilter::BloomFilter(size_t maxItems, double targetProbability) {
}

BloomFilter::BloomFilter(const string &importFilePath, size_t bitCount, size_t maxItems) : bitCount(bitCount) {
checkArchitecture();
bloomVector = readVectorFromFile(importFilePath);
hashRounds = calculateHashRounds(bitCount, maxItems);
}

BloomFilter::BloomFilter(BinaryInputStream &in, size_t bitCount, size_t maxItems) : bitCount(bitCount) {
checkArchitecture();
bloomVector = readVectorFromStream(in);
hashRounds = calculateHashRounds(bitCount, maxItems);
}

static void checkArchitecture() {
if (CHAR_BIT != BITS_PER_BLOCK) {
throw std::runtime_error("Unsupported architecture: char is not 8 bit");
}
}

static size_t calculateHashRounds(size_t size, size_t maxItems) {
Expand Down Expand Up @@ -151,4 +147,4 @@ static vector<BlockType> readVectorFromStream(BinaryInputStream &in) {

size_t BloomFilter::getBitCount() const {
return bitCount;
}
}