File size: 7,203 Bytes
d5bfab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
use loda_rust_core::util::BigIntVec;
use num_bigint::BigInt;
pub trait WildcardChecker {
/// Beware that a bloomfilter is probabilistic, so false positives happens several times per second.
///
/// - If the bloomfilter contains the values, it always returns true.
/// - If the bloomfilter doesn't contains the values, it sporadic returns true.
/// - If the bloomfilter doesn't contains the values, it most of the time returns false.
fn bloomfilter_check(&self, bigint_vec_ref: &BigIntVec) -> bool;
/// Returns the magic value that indicates that it's a wildcard term.
fn bloomfilter_wildcard_magic_value(&self) -> &BigInt;
fn check_with_wildcards(&self, bigint_vec_ref: &BigIntVec, minimum_number_of_required_terms: usize) -> Option<usize> {
let mut bigint_vec: BigIntVec = bigint_vec_ref.clone();
self.mut_check_with_wildcards(&mut bigint_vec, minimum_number_of_required_terms)
}
/// Perform a fuzzy comparison.
/// Checks if the prefix is contained in the bloomfilter.
///
/// First it checks if all the values are contained in the bloomfilter.
/// If there isn't a match in the bloomfilter, then it repeats
/// replacing the last terms with the `WILDCARD_MAGIC_VALUE` until there is a match.
///
/// If there is a match, it returns the number of wildcard terms.
///
/// If there is no match, it returns `None`.
///
/// The bloomfilter is populated with data from the OEIS 'stripped' file.
/// The initial terms are always known.
/// Some sequences may only be 5 terms long where it's yet unknown what the 6th term may be.
/// Some sequences grows exponentially with so many digits that it makes sense to truncate.
/// Half of sequences are longer than 38 terms.
/// The wildcard handling is used for comparing sequences shorter than 40 terms.
fn mut_check_with_wildcards(&self, bigint_vec: &mut BigIntVec, minimum_number_of_required_terms: usize) -> Option<usize> {
let len = bigint_vec.len();
if len < minimum_number_of_required_terms {
return None;
}
if self.bloomfilter_check(&bigint_vec) {
return Some(0);
}
let number_of_wildcards: usize = len - minimum_number_of_required_terms + 1;
for i in 1..number_of_wildcards {
bigint_vec[len - i] = self.bloomfilter_wildcard_magic_value().clone();
if self.bloomfilter_check(&bigint_vec) {
return Some(i);
}
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use loda_rust_core::util::{BigIntVecToString, BigIntVecFromI64, IsBigIntVecEqual};
use num_traits::Zero;
struct MockCheckerImpl {
bigint_vec: BigIntVec,
bloomfilter_wildcard_magic_value: BigInt,
}
impl MockCheckerImpl {
fn new_with_wildcard_value(bigint_vec: BigIntVec, bloomfilter_wildcard_magic_value: BigInt) -> Self {
Self {
bigint_vec: bigint_vec,
bloomfilter_wildcard_magic_value: bloomfilter_wildcard_magic_value
}
}
fn new(bigint_vec: BigIntVec) -> Self {
Self::new_with_wildcard_value(bigint_vec, BigInt::zero())
}
}
impl WildcardChecker for MockCheckerImpl {
fn bloomfilter_check(&self, bigint_vec_ref: &BigIntVec) -> bool {
self.bigint_vec.is_bigintvec_equal(bigint_vec_ref)
}
fn bloomfilter_wildcard_magic_value(&self) -> &BigInt {
&self.bloomfilter_wildcard_magic_value
}
}
fn bigints(values: &[i64]) -> BigIntVec {
BigIntVec::from_i64array(values)
}
#[test]
fn test_10000_bloomfilter_check() {
let checker = MockCheckerImpl::new(bigints(&[1,2,3,4]));
assert_eq!(checker.bloomfilter_check(&bigints(&[1,2,3,4])), true);
assert_eq!(checker.bloomfilter_check(&bigints(&[4,3,2,1])), false);
}
#[test]
fn test_20000_check_with_wildcards_none() {
let checker = MockCheckerImpl::new(bigints(&[1,2,3,4]));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,3,4]), 0), Some(0));
assert_eq!(checker.check_with_wildcards(&bigints(&[4,3,2,1]), 0), None);
assert_eq!(checker.check_with_wildcards(&bigints(&[0,0,0,0]), 0), None);
}
#[test]
fn test_20001_check_with_wildcards_multiple() {
let checker = MockCheckerImpl::new(bigints(&[1,2,3,4,0,0,0]));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,3,4,0,0,0]), 0), Some(0));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,3,4,5,6,7]), 0), Some(3));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,3,4,5,6,9]), 0), Some(3));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,3,4,5,9,9]), 0), Some(3));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,3,4,9,9,9]), 0), Some(3));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,3,9,9,9,9]), 0), None);
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,9,9,9,9,9]), 0), None);
assert_eq!(checker.check_with_wildcards(&bigints(&[1,9,9,9,9,9,9]), 0), None);
assert_eq!(checker.check_with_wildcards(&bigints(&[9,9,9,9,9,9,9]), 0), None);
}
#[test]
fn test_20002_check_with_wildcards_fewer_than_minium_required_terms() {
let checker = MockCheckerImpl::new(bigints(&[1,2,3,4]));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,3,4]), 4), Some(0));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,3]), 4), None);
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2]), 4), None);
assert_eq!(checker.check_with_wildcards(&bigints(&[1]), 4), None);
assert_eq!(checker.check_with_wildcards(&bigints(&[]), 4), None);
}
#[test]
fn test_20003_check_with_wildcards_minium_required_terms1() {
let checker = MockCheckerImpl::new(bigints(&[1,1,1,1,0,0,0]));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,1,1,1,0,0,0]), 5), Some(0));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,1,1,1,0,0,9]), 5), Some(1));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,1,1,1,0,9,9]), 5), Some(2));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,1,1,1,9,9,9]), 5), None);
assert_eq!(checker.check_with_wildcards(&bigints(&[1,1,1,9,9,9,9]), 5), None);
}
#[test]
fn test_20004_check_with_wildcards_minium_required_terms2() {
let checker = MockCheckerImpl::new(bigints(&[1,2,3,4,5,6,7,9,10,0]));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,3,4,5,6,7,9,10,0]), 10), Some(0));
assert_eq!(checker.check_with_wildcards(&bigints(&[1,2,3,4,5,6,7,9,10,12]), 10), None);
}
#[test]
fn test_30000_mut_check_with_wildcards() {
// Arrange
let checker = MockCheckerImpl::new(bigints(&[2,3,5,7,0,0,0]));
let mut values: BigIntVec = bigints(&[2,3,5,7,11,13,17]);
// Act
assert_eq!(checker.mut_check_with_wildcards(&mut values, 0), Some(3));
// Assert
assert_eq!(values.to_compact_comma_string(), "2,3,5,7,0,0,0");
}
}
|