-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C++ Open Bloom Filter Library http://www.partow.net/programming/hashf…
- Loading branch information
1 parent
2bb464d
commit 404c742
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
## Description | ||
**C++ Bloom Filter Library**, has the following capabilities: | ||
|
||
+ Optimal parameter selection based on expected false positive rate. | ||
+ Union, intersection and difference operations between bloom filters. | ||
+ Compression of in-use table (increase in false positive probability vs space) | ||
+ Portable and efficient source code implementation. | ||
|
||
## Compatible Compilers | ||
+ GNU Compiler Collection (4.1+) | ||
+ Intel® C++ Compiler (9.x+) | ||
+ Clang/LLVM (1.1+) | ||
+ PGI C++ (10.x+) | ||
+ Microsoft Visual Studio C++ Compiler (8.1+) | ||
+ Comeau C++ Compiler (4.3+) | ||
|
||
For more information please visit: http://www.partow.net/programming/hashfunctions/index.html | ||
|
||
--- | ||
|
||
## Simple Bloom Filter Example | ||
```javascript | ||
#include <iostream> | ||
#include <string> | ||
|
||
#include "bloom_filter.hpp" | ||
|
||
int main() | ||
{ | ||
|
||
bloom_parameters parameters; | ||
|
||
// How many elements roughly do we expect to insert? | ||
parameters.projected_element_count = 1000; | ||
|
||
// Maximum tolerable false positive probability? (0,1) | ||
parameters.false_positive_probability = 0.0001; // 1 in 10000 | ||
|
||
// Simple randomizer (optional) | ||
parameters.random_seed = 0xA5A5A5A5; | ||
|
||
if (!parameters) | ||
{ | ||
std::cout << "Error - Invalid set of bloom filter parameters!" << std::endl; | ||
return 1; | ||
} | ||
|
||
parameters.compute_optimal_parameters(); | ||
|
||
//Instantiate Bloom Filter | ||
bloom_filter filter(parameters); | ||
|
||
std::string str_list[] = { "AbC", "iJk", "XYZ" }; | ||
|
||
// Insert into Bloom Filter | ||
{ | ||
// Insert some strings | ||
for (std::size_t i = 0; i < (sizeof(str_list) / sizeof(std::string)); ++i) | ||
{ | ||
filter.insert(str_list[i]); | ||
} | ||
|
||
// Insert some numbers | ||
for (std::size_t i = 0; i < 100; ++i) | ||
{ | ||
filter.insert(i); | ||
} | ||
} | ||
|
||
|
||
// Query Bloom Filter | ||
{ | ||
// Query the existence of strings | ||
for (std::size_t i = 0; i < (sizeof(str_list) / sizeof(std::string)); ++i) | ||
{ | ||
if (filter.contains(str_list[i])) | ||
{ | ||
std::cout << "BF contains: " << str_list[i] << std::endl; | ||
} | ||
} | ||
|
||
// Query the existence of numbers | ||
for (std::size_t i = 0; i < 100; ++i) | ||
{ | ||
if (filter.contains(i)) | ||
{ | ||
std::cout << "BF contains: " << i << std::endl; | ||
} | ||
} | ||
|
||
// Query the existence of invalid numbers | ||
for (std::size_t i = -1; i > -100; --i) | ||
{ | ||
if (filter.contains(i)) | ||
{ | ||
std::cout << "BF falsely contains: " << i << std::endl; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
``` | ||
|