diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..9a9d230 --- /dev/null +++ b/.clang-format @@ -0,0 +1,5 @@ +BasedOnStyle: LLVM +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +ContinuationIndentWidth: 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cba7efc --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +a.out diff --git a/.gitingore b/.gitingore new file mode 100644 index 0000000..e69de29 diff --git a/DEV.md b/DEV.md new file mode 100644 index 0000000..a972a7a --- /dev/null +++ b/DEV.md @@ -0,0 +1,8 @@ +# Dev Docs + +I've scaffolded but it I feel it makes more sense to just have a one file impl first in some .cpp file +and take it from there. So off we go to top level main.cpp + +There is an original paper but I'm reading off from wiki for now https://en.wikipedia.org/wiki/Bloom_filter + + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d7792d9 --- /dev/null +++ b/main.cpp @@ -0,0 +1,69 @@ +#include +#include +#include + +/* + * Bloom filters + * - n bit array + * - k hash functions + * - add_elem: + * - get the set of {h_i(elem) mod n} and mark them 1 + * - check elem: + * - x is elem + * - generate same positions set, if any are false this does not belong + * for sure + * - no guarantees on presence + */ + +const int N = 1024; +const int K = 5; +std::vector bf(N); + +int hash_i(int i, int x) { + switch (i) { + case 1: + return x; // h1(i) = i is perfectly ok, std::hash will do the same + case 2: + // here we need something better + // the | 1 makes it co-rime to N always + return std::hash{}(std::to_string(x)) | 1; + default: + return hash_i(1, x) + i * hash_i(2, x); + } +} + +void add(int elem){ + for(int i = 1;i<=K;i++){ + int hash = hash_i(i, elem) % N; + bf[hash] = 1; + } +} + +bool missing(int elem){ + for(int i = 1;i<=K;i++){ + int hash = hash_i(i, elem) % N; + if(!bf[hash]) return true; // actually missing + } + return false; // may be present +} + +int main() { + srand(time(nullptr)); + std::set actually_present{}; + const int LIM = 500; + for(int i = 1; i< LIM;i++){ + if(rand()&1){ + actually_present.insert(i); + add(i); + } + } + + int wrong = 0; + for(int i=1;i