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.
 
 

57 lines
1.2 KiB

#pragma once
#include <gp/algorithms/foreach.hpp>
#include <gp/algorithms/tmp_manip.hpp>
#include <gp/containers/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 = (1ull << magnitude) / 32ull;
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 |= (1ull << v_pos);
}
template<typename T>
static bool get_bit(T* value, const int v_pos) {
return (*value >> v_pos) & 1ull;
}
public:
bloomfilter()
{
fill(data, 0);
}
void set_hash(hash_type v)
{
const size_t modulo = v & ((1ull << magnitude)-1ull);
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 & ((1ull << magnitude)-1ull);
const size_t p_pos = modulo / 32;
const size_t v_pos = modulo % 32;
return get_bit(&data[p_pos], v_pos);
}
};
}