|
|
- #pragma once
- #include <stdint.h>
- #include <atomic>
- #include <gp/array.hpp>
- #include <gp/algorithm/tmp_manip.hpp>
-
-
- namespace gp {
- template<typename hash_type = uint64_t, uint8_t magnitude = 19, bool threading = false>
- class bloomfilter {
- constexpr static size_t phys_size = (1 << magnitude) / 32;
- gp::array<
- typename gp::either< threading,
- std::atomic_uint32_t,
- uint32_t
- >::type,
- phys_size
- > data;
-
- template<typename T>
- static void set_bit(T* value, const int v_pos) {
- *value |= (1 << v_pos);
- }
-
- template<typename T>
- static bool get_bit(T* value, const int v_pos) {
- return (*value >> v_pos) & 1;
- }
-
- public:
- void set_hash(hash_type v)
- {
- const size_t modulo = v & ((1 << magnitude)-1);
- const size_t p_pos = modulo / 32;
- const size_t v_pos = modulo % 32;
-
- set_bit(&data[p_pos], v_pos);
- }
-
- bool test_hash(hash_type v)
- {
- const size_t modulo = v & ((1 << magnitude)-1);
- const size_t p_pos = modulo / 32;
- const size_t v_pos = modulo % 32;
-
- return get_bit(&data[p_pos], v_pos);
- }
- };
- }
|