General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

50 lines
1.0 KiB

#pragma once
#include <gp/algorithm/tmp_manip.hpp>
#include <gp/array.hpp>
#include <atomic>
#include <stdint.h>
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);
}
};
}