|
20 | 20 | #define LIBBITCOIN_SYSTEM_FILTER_SIEVE_HPP |
21 | 21 |
|
22 | 22 | #include <bitcoin/system/define.hpp> |
| 23 | +#include <bitcoin/system/math/math.hpp> |
23 | 24 |
|
24 | 25 | namespace libbitcoin { |
25 | 26 | namespace system { |
26 | 27 |
|
| 28 | +/// Sieve is limited to integral types. |
| 29 | +/// There are no false negatives, with the goal of minimizing false positives. |
| 30 | +template <size_t SieveBits, size_t SelectBits, |
| 31 | + if_not_greater<SelectBits, SieveBits> = true, |
| 32 | + if_not_greater<SieveBits, bits<uint64_t>> = true> |
| 33 | +class sieve |
| 34 | +{ |
| 35 | +public: |
| 36 | + /// This produces size_t when disabled. |
| 37 | + using type = unsigned_type<to_ceilinged_bytes(SieveBits)>; |
| 38 | + |
| 39 | + /// Sieve is bypassed. |
| 40 | + static constexpr bool disabled = is_zero(SieveBits) || is_zero(SelectBits); |
| 41 | + |
| 42 | + /// Is sieve saturated did fingerprint collide with it. |
| 43 | + static constexpr bool is_collision(type previous, type next) NOEXCEPT; |
| 44 | + |
| 45 | + /// Is potential duplicate (saturated or screened). |
| 46 | + static constexpr bool is_screened(type value, uint64_t entropy) NOEXCEPT; |
| 47 | + |
| 48 | + /// Add fingerprint to sieve. Changes determined by return/value. |
| 49 | + /// Only "value added to sieve" implies a negative result. |
| 50 | + /// All other results imply a positive result (potential existence). |
| 51 | + /// return == value && return == saturated: sieve was saturated. |
| 52 | + /// return != value && return == saturated: sieve now saturated |
| 53 | + /// return == value && return != saturated: value was screened. |
| 54 | + /// return != value && return != saturated: value added to sieve. |
| 55 | + static constexpr type screen(type value, uint64_t entropy) NOEXCEPT; |
| 56 | + |
| 57 | +protected: |
| 58 | + static constexpr auto screen_bits = SieveBits - SelectBits; |
| 59 | + static constexpr auto screens = power2(SelectBits); |
| 60 | + static constexpr auto limit = sub1(screens); |
| 61 | + static constexpr auto sentinel = sub1(screen_bits); |
| 62 | + static constexpr auto empty = unmask_right<type>(SieveBits); |
| 63 | + static constexpr auto saturated = mask_right(empty, sentinel); |
| 64 | + static constexpr auto first_mask = unmask_right<type>(screen_bits); |
| 65 | + static constexpr auto select_mask = first_mask; |
| 66 | + static constexpr auto mask_count = to_half(safe_multiply(screens, |
| 67 | + add1(screens))); |
| 68 | + |
| 69 | + using masks_t = std_array<type, mask_count>; |
| 70 | + using offsets_t = std_array<type, screens>; |
| 71 | + |
| 72 | + /// Generate compression offsets at compile time. |
| 73 | + static CONSTEVAL offsets_t generate_offsets() NOEXCEPT; |
| 74 | + |
| 75 | + /// Generate compressed mask table at compile time. |
| 76 | + static CONSTEVAL masks_t generate_masks() NOEXCEPT; |
| 77 | + |
| 78 | + /// Read member compressed mask array as if it was a two-dimesional array. |
| 79 | + static constexpr type masks(size_t row, size_t column) NOEXCEPT; |
| 80 | + |
| 81 | + /// Is sentinel value for empty filter. |
| 82 | + static constexpr bool is_empty(type value) NOEXCEPT; |
| 83 | + |
| 84 | + /// Is sentinel value for saturated filter. |
| 85 | + static constexpr bool is_saturated(type value) NOEXCEPT; |
| 86 | +}; |
| 87 | + |
27 | 88 | } // namespace system |
28 | 89 | } // namespace libbitcoin |
29 | 90 |
|
| 91 | +#define TEMPLATE template <size_t SieveBits, size_t SelectBits, \ |
| 92 | + if_not_greater<SelectBits, SieveBits> If1, \ |
| 93 | + if_not_greater<SieveBits, bits<uint64_t>> If2> |
| 94 | + |
| 95 | +#define CLASS sieve<SieveBits, SelectBits, If1, If2> |
| 96 | + |
30 | 97 | #include <bitcoin/system/impl/filter/sieve.ipp> |
31 | 98 |
|
| 99 | +#undef CLASS |
| 100 | +#undef TEMPLATE |
| 101 | + |
32 | 102 | #endif |
0 commit comments