General Purpose library for Freestanding C++ and POSIX systems
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.0 KiB

  1. #pragma once
  2. #include <gp/algorithm/tmp_manip.hpp>
  3. #include <gp/array.hpp>
  4. #include <atomic>
  5. #include <stdint.h>
  6. namespace gp {
  7. template<typename hash_type = uint64_t, uint8_t magnitude = 19, bool threading = false>
  8. class bloomfilter {
  9. constexpr static size_t phys_size = (1 << magnitude) / 32;
  10. gp::array<
  11. typename gp::either< threading,
  12. std::atomic_uint32_t,
  13. uint32_t
  14. >::type,
  15. phys_size
  16. > data;
  17. template<typename T>
  18. static void set_bit(T* value, const int v_pos) {
  19. *value |= (1 << v_pos);
  20. }
  21. template<typename T>
  22. static bool get_bit(T* value, const int v_pos) {
  23. return (*value >> v_pos) & 1;
  24. }
  25. public:
  26. void set_hash(hash_type v)
  27. {
  28. const size_t modulo = v & ((1 << magnitude)-1);
  29. const size_t p_pos = modulo / 32;
  30. const size_t v_pos = modulo % 32;
  31. set_bit(&data[p_pos], v_pos);
  32. }
  33. bool test_hash(hash_type v)
  34. {
  35. const size_t modulo = v & ((1 << magnitude)-1);
  36. const size_t p_pos = modulo / 32;
  37. const size_t v_pos = modulo % 32;
  38. return get_bit(&data[p_pos], v_pos);
  39. }
  40. };
  41. }