theultraman20

Untitled

Jul 27th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.61 KB | None | 0 0
  1.     fn bloomfilter() {
  2.  
  3.         let mut bloom_filter = BloomFilter::<&str, 1024>::new(5);
  4.         //they really are that terrible
  5.         let test_sentence = "AJR are a god awful fucking band";
  6.        
  7.         for word in test_sentence.split_whitespace() {
  8.             bloom_filter.insert(&word);
  9.         }
  10.  
  11.         for word in test_sentence.split_whitespace() {
  12.             assert!(bloom_filter.must_contain(&word));
  13.         }
  14.  
  15.         let mut more_hashes = BloomFilter::<&str, 1024>::new(50);
  16.         let mut less_hashes = BloomFilter::<&str, 1024>::new(5);
  17.  
  18.         let test_sentence = "I'm listening to \"Terminal Z\" by Skee Mask and it's
  19.        honestly stunning. It's a very futuristic spacey sounding atmospheric ambient,
  20.        with lush synths and detuned wacky sounding effects. Touching music truly is
  21.        one of the most precious things in life.";
  22.  
  23.  
  24.         for word in test_sentence.split_whitespace() {
  25.             more_hashes.insert(&word);
  26.             less_hashes.insert(&word);
  27.         }
  28.  
  29.         let mut false_positives = (0, 0);
  30.  
  31.         for _ in 0..10000 {
  32.             let word = &rand_string()[..];
  33.  
  34.             if less_hashes.must_contain(&word) {
  35.                 false_positives.0 = false_positives.0 + 1
  36.             }
  37.  
  38.             if more_hashes.must_contain(&word) {
  39.                 false_positives.1 = false_positives.1 + 1
  40.             }
  41.         }
  42.  
  43.         println!("5 hash bloom filter false positives: {}", false_positives.0);
  44.         println!("50 hash false positives: {}", false_positives.1);
  45.         assert!(false_positives.0 >= false_positives.1);
  46.  
  47.     }
Add Comment
Please, Sign In to add comment