### Basic BloomFilter Usage in Rust Source: https://github.com/tomtomwombat/fastbloom/blob/main/README.md Demonstrates the basic instantiation and insertion of items into a BloomFilter. Initializes with a specified number of bits and expected items. ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(1024).expected_items(2); filter.insert("42"); filter.insert("🦀"); ``` -------------------------------- ### Enable Serde Serialization for Bloom Filters Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Demonstrates how to serialize and deserialize a BloomFilter using Serde when the 'serde' feature is enabled. Ensure 'serde_json' is also a dependency. ```toml # Cargo.toml [dependencies] fastbloom = { version = "0.17.0", features = ["serde"] } serde_json = "1" ``` ```rust // With feature = "serde" use fastbloom::BloomFilter; let mut filter = BloomFilter::with_false_pos(0.001).seed(&42).expected_items(1000); for i in 0u32..1000 { filter.insert(&i); } // Serialize to JSON (or any serde format) let json = serde_json::to_string(&filter).unwrap(); // Deserialize and verify let restored: BloomFilter = serde_json::from_str(&json).unwrap(); for i in 0u32..1000 { assert!(restored.contains(&i)); } println!("Serialized size: {} bytes", json.len()); ``` -------------------------------- ### Builder .hasher() Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Replaces the default SipHash-1-3 hasher with any type implementing BuildHasher. Useful for benchmarking, cross-language compatibility, or integration with an existing hashing infrastructure. ```APIDOC ## Builder `.hasher()` ### Description Replaces the default SipHash-1-3 hasher with any type implementing `BuildHasher`. Useful for benchmarking, cross-language compatibility, or integration with an existing hashing infrastructure. ### Usage ```rust use fastbloom::BloomFilter; use foldhash::fast::RandomState; // Use foldhash instead of SipHash-1-3 let mut filter = BloomFilter::with_false_pos(0.001) .hasher(RandomState::default()) .expected_items(5_000); for i in 0u32..5_000 { filter.insert(&i); } assert!(filter.contains(&0)); assert!(filter.contains(&4999)); ``` ``` -------------------------------- ### Create Deterministic Bloom Filters with Seed Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Use the `.seed()` builder method to set a fixed 128-bit seed, ensuring deterministic and reproducible filter results for identical configurations. ```rust use fastbloom::BloomFilter; let seed: u128 = 0xDEAD_BEEF_CAFE_BABE_1234_5678_9ABC_DEF0; let mut a = BloomFilter::with_num_bits(2048).seed(&seed).expected_items(100); let mut b = BloomFilter::with_num_bits(2048).seed(&seed).expected_items(100); a.insert(&"shared-item"); b.insert(&"shared-item"); // Identical seed → identical bit layouts → filters are equal assert_eq!(a, b); ``` -------------------------------- ### BloomFilter with Custom Hasher Source: https://github.com/tomtomwombat/fastbloom/blob/main/README.md Shows how to configure a BloomFilter to use a custom hasher, in this case, `RandomState` from the `foldhash` crate. Initializes with a specified number of bits and populates with items. ```rust use fastbloom::BloomFilter; use foldhash::fast::RandomState; let filter = BloomFilter::with_num_bits(1024) .hasher(RandomState::default()) .items(["42", "🦀"].iter()); ``` -------------------------------- ### Use Custom Hashers in Bloom Filters Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Replace the default SipHash-1-3 hasher by providing a `BuildHasher` implementation via the `.hasher()` method. This is useful for benchmarking or cross-language compatibility. ```rust use fastbloom::BloomFilter; use foldhash::fast::RandomState; // Use foldhash instead of SipHash-1-3 let mut filter = BloomFilter::with_false_pos(0.001) .hasher(RandomState::default()) .expected_items(5_000); for i in 0u32..5_000 { filter.insert(&i); } assert!(filter.contains(&0)); assert!(filter.contains(&4999)); ``` -------------------------------- ### Add fastbloom Dependency to Cargo.toml Source: https://github.com/tomtomwombat/fastbloom/blob/main/README.md Include the fastbloom crate in your project's dependencies by adding this to your Cargo.toml file. ```toml # Cargo.toml [dependencies] fastbloom = "0.17.0" ``` -------------------------------- ### Build Bloom Filter with Fixed Bit Count Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Use `with_num_bits` when memory is the primary constraint. Chain `.expected_items(n)` to auto-compute optimal hashes or `.hashes(k)` to set manually. ```rust use fastbloom::BloomFilter; // 8192-bit filter, optimal hashes for 1000 expected items let mut filter = BloomFilter::with_num_bits(8192).expected_items(1000); for word in &["apple", "banana", "cherry"] { filter.insert(*word); } assert!(filter.contains("apple")); assert!(filter.contains("banana")); assert!(!filter.contains("dragonfruit")); // probably false, may be true (false positive) println!("Bits allocated: {}", filter.num_bits()); // 8192 println!("Hashes per item: {}", filter.num_hashes()); // auto-computed optimal value ``` -------------------------------- ### Builder .seed() Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Sets a fixed 128-bit seed for the hasher. Two filters built with the same seed, bit count, and hash count will produce identical results and can be meaningfully compared, unioned, or intersected. ```APIDOC ## Builder `.seed()` ### Description Sets a fixed 128-bit seed for the hasher. Two filters built with the same seed, bit count, and hash count will produce identical results and can be meaningfully compared, unioned, or intersected. ### Usage ```rust use fastbloom::BloomFilter; let seed: u128 = 0xDEAD_BEEF_CAFE_BABE_1234_5678_9ABC_DEF0; let mut a = BloomFilter::with_num_bits(2048).seed(&seed).expected_items(100); let mut b = BloomFilter::with_num_bits(2048).seed(&seed).expected_items(100); a.insert(&"shared-item"); b.insert(&"shared-item"); // Identical seed → identical bit layouts → filters are equal assert_eq!(a, b); ``` ``` -------------------------------- ### Insert and check using pre-computed hashes Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Use `insert_hash` and `contains_hash` when you have already computed the hash of an item. This avoids redundant hashing, which is beneficial if the same hash is used multiple times for insertion and checking. ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(4096).expected_items(100); let key = "expensive-to-hash-string"; let h = filter.source_hash(key); // Insert using the pre-computed hash filter.insert_hash(h); // Check membership using the same pre-computed hash assert!(filter.contains_hash(h)); // Store the hash, send it over the network, etc. println!("Source hash: {h:#018x}"); ``` -------------------------------- ### Inspect Bloom Filter Parameters Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Use `num_bits` and `num_hashes` to retrieve the total bits and hash probes per item for a configured Bloom filter. ```rust use fastbloom::BloomFilter; let filter = BloomFilter::with_false_pos(0.001).expected_items(50_000); println!("Bits: {}", filter.num_bits()); // ~718,688 or similar println!("Hashes: {}", filter.num_hashes()); // optimal k for the configuration println!("Bytes: {}", filter.num_bits() / 8); ``` -------------------------------- ### Insert all items from an iterator Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Use `insert_all` for efficient bulk insertion of multiple items from an iterator. This is equivalent to calling `insert` repeatedly but can be more performant. ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(4096).expected_items(6); let items = vec!["cat", "dog", "fox", "owl", "bat", "ant"]; filter.insert_all(items.iter()); for item in &items { assert!(filter.contains(*item)); } ``` -------------------------------- ### BloomFilter::with_false_pos Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Creates a builder that automatically sizes the bit vector to meet a target false positive probability for a given number of expected insertions. Both bit count and hash count are derived from expected items and false positive rate. ```APIDOC ## BloomFilter::with_false_pos — Build a filter targeting a false positive rate ### Description Creates a builder that automatically sizes the underlying bit vector to meet a target false positive probability for a given expected number of insertions. The bit count and hash count are both derived from `(expected_items, fp_rate)` using optimal Bloom filter formulas. ### Method Rust function call ### Parameters - `fp_rate` (f64) - The target false positive rate (e.g., 0.001 for 0.1%). ### Usage Example ```rust use fastbloom::BloomFilter; // Target 0.1% false positive rate for 10,000 items let mut filter = BloomFilter::with_false_pos(0.001).expected_items(10_000); let urls: Vec = (0..10_000).map(|i| format!("https://example.com/page/{i}")).collect(); for url in &urls { filter.insert(url.as_str()); } // All inserted items must be found (no false negatives) for url in &urls { assert!(filter.contains(url.as_str())); } // Estimate false positive rate for 10,000 items let estimated_fp = filter.expected_false_pos(10_000); println!("Expected FP rate: {:.4}", estimated_fp); // ~0.001 // Check something not inserted — might return true (false positive) at ~0.1% rate let is_member = filter.contains("https://example.com/not-inserted"); println!("False positive check: {}", is_member); ``` ``` -------------------------------- ### Calculate Bloom Filter Accuracy Metrics Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Computes the probability of a bit being set and the expected false positive rate for a Bloom filter. Requires importing accuracy helper functions. ```rust use fastbloom::{expected_density, expected_false_pos, optimal_hashes, optimal_size}; let num_items = 50_000; let target_fp = 0.005; let bits = optimal_size(num_items, target_fp); let hashes = optimal_hashes(bits, num_items); let density = expected_density(hashes, bits, num_items); let fp = expected_false_pos(hashes, density); println!("Bits: {bits}"); println!("Hashes: {hashes}"); println!("Density: {density:.4}"); // ~0.5 at optimal hashes println!("FP rate: {fp:.6}"); // ~0.005 ``` -------------------------------- ### BloomFilter::num_bits and BloomFilter::num_hashes Source: https://context7.com/tomtomwombat/fastbloom/llms.txt These methods allow you to inspect the total number of bits in the underlying bit vector and the number of hash probes performed per item for a given Bloom filter configuration. ```APIDOC ## BloomFilter::num_bits / BloomFilter::num_hashes ### Description Returns the total number of bits in the underlying bit vector and the number of hash probes performed per item. ### Usage ```rust use fastbloom::BloomFilter; let filter = BloomFilter::with_false_pos(0.001).expected_items(50_000); println!("Bits: {}", filter.num_bits()); // ~718,688 or similar println!("Hashes: {}", filter.num_hashes()); // optimal k for the configuration println!("Bytes: {}", filter.num_bits() / 8); ``` ``` -------------------------------- ### optimal_hashes Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Free function that returns the optimal number of hash functions k for a Bloom filter given a fixed bit count and expected number of items, using the formula k = (m/n) * ln(2). ```APIDOC ## optimal_hashes ### Description Free function that returns the optimal number of hash functions `k` for a Bloom filter given a fixed bit count and expected number of items, using the formula `k = (m/n) * ln(2)`. ### Usage ```rust use fastbloom::optimal_hashes; let num_bits = 10_000 * 10; // 10 bits per item let num_items = 10_000; let k = optimal_hashes(num_bits, num_items); println!("Optimal hashes: {k}"); // 7 ``` ``` -------------------------------- ### Build Bloom Filter Targeting False Positive Rate Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Use `with_false_pos` to automatically size the bit vector for a target false positive rate and expected insertions. Bit and hash counts are derived from `(expected_items, fp_rate)`. ```rust use fastbloom::BloomFilter; // Target 0.1% false positive rate for 10,000 items let mut filter = BloomFilter::with_false_pos(0.001).expected_items(10_000); let urls: Vec = (0..10_000).map(|i| format!("https://example.com/page/{i}")).collect(); for url in &urls { filter.insert(url.as_str()); } // All inserted items must be found (no false negatives) for url in &urls { assert!(filter.contains(url.as_str())); } // Estimate false positive rate for 10,000 items let estimated_fp = filter.expected_false_pos(10_000); println!("Expected FP rate: {:.4}", estimated_fp); // ~0.001 // Check something not inserted — might return true (false positive) at ~0.1% rate let is_member = filter.contains("https://example.com/not-inserted"); println!("False positive check: {}", is_member); ``` -------------------------------- ### Reconstruct Bloom Filter from Raw Bits Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Use `from_vec` to restore a filter from a `Vec` bit vector. Provide the original number of hashes and the same seed/hasher for identical behavior. Useful for persistence and sharing. ```rust use fastbloom::BloomFilter; // Build and populate original filter let mut original = BloomFilter::with_false_pos(0.001).seed(&42).expected_items(500); for i in 0u32..500 { original.insert(&i); } let num_hashes = original.num_hashes(); // Serialize: extract raw u64 words let raw_bits: Vec = original.iter().collect(); // Reconstruct with identical seed and hash count let restored = BloomFilter::from_vec(raw_bits).seed(&42).hashes(num_hashes); // All originally inserted items are still found for i in 0u32..500 { assert!(restored.contains(&i)); } println!("Restored filter with {} bits, {} hashes", restored.num_bits(), restored.num_hashes()); ``` -------------------------------- ### Insert Item into Bloom Filter Source: https://context7.com/tomtomwombat/fastbloom/llms.txt The `insert` method adds an item to the filter. It returns `true` if the item might have already been present (possible false positive or duplicate), and `false` otherwise. Requires `&mut self`. ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(1024).hashes(4); let first = filter.insert(&"hello"); // false — definitely not previously inserted let second = filter.insert(&"hello"); // true — bits were already set let third = filter.insert(&"world"); // false — new item (probably) assert!(!first); assert!(second); assert!(filter.contains(&"hello")); assert!(filter.contains(&"world")); ``` -------------------------------- ### Compute Optimal Bit Count Source: https://context7.com/tomtomwombat/fastbloom/llms.txt The `optimal_size` function determines the minimum required bits (`m`) for a Bloom filter to achieve a target false positive rate (`fp`) for a given number of items (`n`), using the formula `m = -n * ln(fp) / (ln(2)^2)`. ```rust use fastbloom::optimal_size; let num_items = 100_000; let target_fp = 0.001; // 0.1% let bits = optimal_size(num_items, target_fp); let bytes = bits / 8; println!("Required bits: {bits}"); // ~1,437,760 println!("Required bytes: {bytes}"); // ~179,720 ``` -------------------------------- ### optimal_size Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Free function that returns the minimum number of bits required to achieve a target false positive rate for a given item count, using the formula m = -n * ln(fp) / (ln(2)^2). ```APIDOC ## optimal_size ### Description Free function that returns the minimum number of bits required to achieve a target false positive rate for a given item count, using the formula `m = -n * ln(fp) / (ln(2)^2)`. ### Usage ```rust use fastbloom::optimal_size; let num_items = 100_000; let target_fp = 0.001; // 0.1% let bits = optimal_size(num_items, target_fp); let bytes = bits / 8; println!("Required bits: {bits}"); // ~1,437,760 println!("Required bytes: {bytes}"); // ~179,720 ``` ``` -------------------------------- ### BloomFilter::with_num_bits Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Creates a builder for a BloomFilter with a fixed number of bits. This is useful when memory is a primary constraint. The number of hashes can be automatically computed based on expected items or set manually. ```APIDOC ## BloomFilter::with_num_bits — Build a filter with a fixed bit count ### Description Creates a builder for a `BloomFilter` with an exact number of bits allocated for the underlying bit vector. Use this when memory budget is the primary constraint. Chain `.expected_items(n)` to auto-compute the optimal number of hashes, or `.hashes(k)` to set it manually. ### Method Rust function call ### Parameters - `num_bits` (usize) - The exact number of bits to allocate for the filter. ### Usage Example ```rust use fastbloom::BloomFilter; // 8192-bit filter, optimal hashes for 1000 expected items let mut filter = BloomFilter::with_num_bits(8192).expected_items(1000); for word in &["apple", "banana", "cherry"] { filter.insert(*word); } assert!(filter.contains("apple")); assert!(filter.contains("banana")); assert!(!filter.contains("dragonfruit")); // probably false, may be true (false positive) println!("Bits allocated: {}", filter.num_bits()); // 8192 println!("Hashes per item: {}", filter.num_hashes()); // auto-computed optimal value ``` ``` -------------------------------- ### Concurrent BloomFilter Usage Source: https://github.com/tomtomwombat/fastbloom/blob/main/README.md Demonstrates the usage of `AtomicBloomFilter` for concurrent access. This filter is designed to be a drop-in replacement for locked Bloom filters, offering lock-free operations. ```rust use fastbloom::AtomicBloomFilter; let filter = AtomicBloomFilter::with_num_bits(1024).expected_items(2); filter.insert("42"); filter.insert("🦀"); ``` -------------------------------- ### Compute Optimal Hash Count Source: https://context7.com/tomtomwombat/fastbloom/llms.txt The `optimal_hashes` function calculates the ideal number of hash functions (`k`) for a Bloom filter given the total bits (`m`) and expected items (`n`), based on the formula `k = (m/n) * ln(2)`. ```rust use fastbloom::optimal_hashes; let num_bits = 10_000 * 10; // 10 bits per item let num_items = 10_000; let k = optimal_hashes(num_bits, num_items); println!("Optimal hashes: {k}"); // 7 ``` -------------------------------- ### BloomFilter with Target False Positive Rate Source: https://github.com/tomtomwombat/fastbloom/blob/main/README.md Instantiates a BloomFilter based on a desired false positive rate and populates it with an iterator of items. Includes assertions to check for item containment. ```rust use fastbloom::BloomFilter; let filter = BloomFilter::with_false_pos(0.001).items(["42", "🦀"].iter()); assert!(filter.contains("42")); assert!(filter.contains("🦀")); ``` -------------------------------- ### AtomicBloomFilter Source: https://context7.com/tomtomwombat/fastbloom/llms.txt AtomicBloomFilter is a drop-in concurrent alternative to BloomFilter. All methods take &self, enabling safe sharing across threads without a Mutex or RwLock. Internally uses atomic u64 operations. The API is identical to BloomFilter. ```APIDOC ## AtomicBloomFilter ### Description `AtomicBloomFilter` is a drop-in concurrent alternative to `BloomFilter`. All methods take `&self`, enabling safe sharing across threads without a `Mutex` or `RwLock`. Internally uses atomic `u64` operations. The API is identical to `BloomFilter`. ### Usage ```rust use fastbloom::AtomicBloomFilter; use std::sync::Arc; use std::thread; let filter = Arc::new( AtomicBloomFilter::with_false_pos(0.001).expected_items(10_000) ); // Spawn multiple writer threads concurrently let handles: Vec<_> = (0u32..4).map(|t| { let f = Arc::clone(&filter); thread::spawn(move || { for i in (t * 2500)..((t + 1) * 2500) { f.insert(&i); } }) }).collect(); for h in handles { h.join().unwrap(); } // All 10,000 items inserted across threads are found for i in 0u32..10_000 { assert!(filter.contains(&i)); } println!("Concurrent inserts verified. Bits: {}", filter.num_bits()); ``` ``` -------------------------------- ### Estimate false positive rate Source: https://context7.com/tomtomwombat/fastbloom/llms.txt The `expected_false_pos` method calculates the theoretical false positive rate for a given number of items, based on the filter's current bit count and hash function configuration. This is useful for understanding filter performance under different load conditions. ```rust use fastbloom::BloomFilter; let filter = BloomFilter::with_false_pos(0.001).expected_items(10_000); let fp_at_capacity = filter.expected_false_pos(10_000); let fp_half_full = filter.expected_false_pos(5_000); let fp_over_capacity = filter.expected_false_pos(20_000); println!("FP at capacity: {fp_at_capacity:.6}"); // ~0.001000 println!("FP at half capacity: {fp_half_full:.6}"); // much lower println!("FP at 2× capacity: {fp_over_capacity:.6}"); // higher ``` -------------------------------- ### BloomFilter::from_vec Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Restores a BloomFilter from a `Vec` bit vector. Requires the original number of hashes and the same seed/hasher for identical behavior. Useful for persistence and sharing. ```APIDOC ## BloomFilter::from_vec — Reconstruct a filter from raw bits ### Description Restores a `BloomFilter` from a previously serialized `Vec` bit vector. You must supply the original number of hashes (obtained via `num_hashes()`) and the same seed/hasher to get identical behavior. Useful for persisting filters to disk, sending over the network, or sharing between processes. ### Method Rust function call ### Parameters - `raw_bits` (Vec) - The raw bit vector representing the Bloom filter. - `seed` (impl SeedableHasher) - The seed for the hasher, must match the original filter. - `hashes` (usize) - The number of hash functions used, must match the original filter. ### Usage Example ```rust use fastbloom::BloomFilter; // Build and populate original filter let mut original = BloomFilter::with_false_pos(0.001).seed(&42).expected_items(500); for i in 0u32..500 { original.insert(&i); } let num_hashes = original.num_hashes(); // Serialize: extract raw u64 words let raw_bits: Vec = original.iter().collect(); // Reconstruct with identical seed and hash count let restored = BloomFilter::from_vec(raw_bits).seed(&42).hashes(num_hashes); // All originally inserted items are still found for i in 0u32..500 { assert!(restored.contains(&i)); } println!("Restored filter with {} bits, {} hashes", restored.num_bits(), restored.num_hashes()); ``` ``` -------------------------------- ### Merge two Bloom filters Source: https://context7.com/tomtomwombat/fastbloom/llms.txt The `union` method performs a bitwise OR operation, merging the bits of another filter into the current one. After the union, the filter represents the combined set of items from both original filters. Both filters must share the same configuration (number of bits, hashes, hasher, and seed). ```rust use fastbloom::BloomFilter; let mut a = BloomFilter::with_num_bits(4096).seed(&1).hashes(4); let mut b = BloomFilter::with_num_bits(4096).seed(&1).hashes(4); for x in 0u32..500 { a.insert(&x); } for x in 400u32..900 { b.insert(&x); } a.union(&b); // All items from both filters are now in `a` assert!(a.contains(&0)); assert!(a.contains(&499)); assert!(a.contains(&400)); assert!(a.contains(&898)); ``` -------------------------------- ### BloomFilter::insert_all Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Inserts every item yielded by an iterator into the filter. This is an efficient way to add multiple items at once, equivalent to calling `insert` in a loop. ```APIDOC ## BloomFilter::insert_all ### Description Inserts every item yielded by an iterator into the filter. Equivalent to calling `insert` in a loop. ### Method `insert_all` ### Parameters - `iterator`: An iterator yielding items to be inserted into the filter. ### Request Example ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(4096).expected_items(6); let items = vec!["cat", "dog", "fox", "owl", "bat", "ant"]; filter.insert_all(items.iter()); for item in &items { assert!(filter.contains(*item)); } ``` ``` -------------------------------- ### BloomFilter::iter and BloomFilter::as_slice Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Provides read access to the underlying bit vector as an iterator of u64 words or as a raw slice, enabling serialization, checksumming, or low-level manipulation. ```APIDOC ## BloomFilter::iter / BloomFilter::as_slice ### Description Provides read access to the underlying bit vector as an iterator of `u64` words or as a raw slice, enabling serialization, checksumming, or low-level manipulation. ### Usage ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(256).hashes(3); filter.insert(&"hello"); filter.insert(&"world"); // Collect raw u64 words for serialization let words: Vec = filter.iter().collect(); println!("Raw words ({} × u64): {:?}", words.len(), &words[..words.len().min(4)]); // Or access the underlying slice directly let slice: &[u64] = filter.as_slice(); println!("Slice length: {}", slice.len()); // num_bits / 64 ``` ``` -------------------------------- ### Intersect two Bloom filters Source: https://context7.com/tomtomwombat/fastbloom/llms.txt The `intersect` method performs a bitwise AND operation, keeping only the bits that are set in both filters. This approximates the intersection of the sets of items. Similar to `union`, both filters must have identical configurations. ```rust use fastbloom::BloomFilter; let mut a = BloomFilter::with_num_bits(4096).seed(&1).hashes(4); let mut b = BloomFilter::with_num_bits(4096).seed(&1).hashes(4); for x in 0u32..500 { a.insert(&x); } for x in 400u32..900 { b.insert(&x); } a.intersect(&b); // Items only in `a` are likely gone; items in both are likely present assert!(!a.contains(&0)); // only in `a`, should be absent assert!(a.contains(&450)); // in both — likely still present ``` -------------------------------- ### Concurrent Bloom Filter Operations Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Utilize `AtomicBloomFilter` for lock-free, thread-safe Bloom filter operations. It offers an identical API to `BloomFilter` but uses atomic operations for concurrent access. ```rust use fastbloom::AtomicBloomFilter; use std::sync::Arc; use std::thread; let filter = Arc::new( AtomicBloomFilter::with_false_pos(0.001).expected_items(10_000) ); // Spawn multiple writer threads concurrently let handles: Vec<_> = (0u32..4).map(|t| { let f = Arc::clone(&filter); thread::spawn(move || { for i in (t * 2500)..((t + 1) * 2500) { f.insert(&i); } }) }).collect(); for h in handles { h.join().unwrap(); } // All 10,000 items inserted across threads are found for i in 0u32..10_000 { assert!(filter.contains(&i)); } println!("Concurrent inserts verified. Bits: {}", filter.num_bits()); ``` -------------------------------- ### BloomFilter::expected_false_pos Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Calculates the theoretically expected false positive rate for the filter given a specific number of items. This is useful for understanding the filter's performance under different load conditions. ```APIDOC ## BloomFilter::expected_false_pos ### Description Returns the theoretically expected false positive rate of this filter if it contains `num_items` items, based on the current number of bits and hashes. ### Method `expected_false_pos` ### Parameters - `num_items`: The number of items assumed to be in the filter. ### Request Example ```rust use fastbloom::BloomFilter; let filter = BloomFilter::with_false_pos(0.001).expected_items(10_000); let fp_at_capacity = filter.expected_false_pos(10_000); let fp_half_full = filter.expected_false_pos(5_000); let fp_over_capacity = filter.expected_false_pos(20_000); println!("FP at capacity: {fp_at_capacity:.6}"); // ~0.001000 println!("FP at half capacity: {fp_half_full:.6}"); // much lower println!("FP at 2× capacity: {fp_over_capacity:.6}"); // higher ``` ``` -------------------------------- ### BloomFilter::insert Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Inserts an item into the filter by setting bits based on its hash. Returns true if the item might have already been present (possible false positive or duplicate), and false otherwise. Requires mutable access. ```APIDOC ## BloomFilter::insert — Insert an item ### Description Inserts an item into the filter by setting the appropriate bits derived from the item's hash. Returns `true` if the item appeared to already be in the filter before insertion (indicating a possible false positive or duplicate), and `false` on first-time insertions. Requires `&mut self`. ### Method Rust method call ### Parameters - `item` (&T) - The item to insert into the filter. Must implement `Hash` and `Borrow<[u8]>`. ### Return Value - `bool` - `true` if the item was likely already present, `false` otherwise. ### Usage Example ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(1024).hashes(4); let first = filter.insert(&"hello"); // false — definitely not previously inserted let second = filter.insert(&"hello"); // true — bits were already set let third = filter.insert(&"world"); // false — new item (probably) assert!(!first); assert!(second); assert!(filter.contains(&"hello")); assert!(filter.contains(&"world")); ``` ``` -------------------------------- ### Test item membership Source: https://context7.com/tomtomwombat/fastbloom/llms.txt The `contains` method checks if an item might be in the filter. It returns `true` if the item could be present (allowing for false positives) and `false` if it is definitely not present (no false negatives). ```rust use fastbloom::BloomFilter; let filter = BloomFilter::with_false_pos(0.01).items([10u32, 20, 30, 40, 50].iter()); assert!(filter.contains(&10)); assert!(filter.contains(&30)); assert!(filter.contains(&50)); // Items not inserted are definitely absent (false negatives impossible) // but a small fraction may return true (false positive) let absent = [11u32, 22, 33, 44, 55]; let fp_count = absent.iter().filter(|x| filter.contains(*x)).count(); println!("False positives: {fp_count} out of {}", absent.len()); ``` -------------------------------- ### Access Raw Bloom Filter Bit Data Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Access the underlying bit vector using `iter` for `u64` words or `as_slice` for a raw slice, useful for serialization or low-level manipulation. ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(256).hashes(3); filter.insert(&"hello"); filter.insert(&"world"); // Collect raw u64 words for serialization let words: Vec = filter.iter().collect(); println!("Raw words ({} × u64): {:?}", words.len(), &words[..words.len().min(4)]); // Or access the underlying slice directly let slice: &[u64] = filter.as_slice(); println!("Slice length: {}", slice.len()); // num_bits / 64 ``` -------------------------------- ### Clear all items from the filter Source: https://context7.com/tomtomwombat/fastbloom/llms.txt The `clear` method resets the filter by clearing all its bits, effectively removing all previously inserted items. The filter's memory is retained, allowing it to be reused immediately with the same capacity and hash configuration. ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(2048).hashes(4); for x in 0u32..100 { filter.insert(&x); } assert!(filter.contains(&42)); filter.clear(); // Nothing is found after clearing for x in 0u32..100 { assert!(!filter.contains(&x)); } ``` -------------------------------- ### BloomFilter::contains Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Checks whether an item is possibly in the filter. Returns `true` if the item may be present (subject to false positives) and `false` if it is definitely not present (no false negatives). ```APIDOC ## BloomFilter::contains ### Description Checks whether an item is possibly in the filter. Returns `true` if the item may be present (subject to false positives) and `false` if it is definitely not present (no false negatives). ### Method `contains` ### Parameters - `item`: A reference to the item to check for. ### Request Example ```rust use fastbloom::BloomFilter; let filter = BloomFilter::with_false_pos(0.01).items([10u32, 20, 30, 40, 50].iter()); assert!(filter.contains(&10)); assert!(filter.contains(&30)); assert!(filter.contains(&50)); // Items not inserted are definitely absent (false negatives impossible) // but a small fraction may return true (false positive) let absent = [11u32, 22, 33, 44, 55]; let fp_count = absent.iter().filter(|x| filter.contains(*x)).count(); println!("False positives: {fp_count} out of {}", absent.len()); ``` ``` -------------------------------- ### BloomFilter::insert_hash / BloomFilter::contains_hash Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Operations for inserting and checking membership using pre-computed hashes. This is useful for avoiding redundant hashing when the same hash value is used multiple times. ```APIDOC ## BloomFilter::insert_hash / BloomFilter::contains_hash ### Description Pre-compute a source hash with `source_hash()` and reuse it for multiple insert/contains calls. This avoids redundant hashing and is ideal when the same hash must be stored, transmitted, or checked repeatedly. ### Method `insert_hash`, `contains_hash` ### Parameters - `hash`: The pre-computed source hash value. ### Request Example ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(4096).expected_items(100); let key = "expensive-to-hash-string"; let h = filter.source_hash(key); // Insert using the pre-computed hash filter.insert_hash(h); // Check membership using the same pre-computed hash assert!(filter.contains_hash(h)); // Store the hash, send it over the network, etc. println!("Source hash: {h:#018x}"); ``` ``` -------------------------------- ### BloomFilter::clear Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Resets the Bloom filter by clearing all its bits, effectively removing all items. The filter can be reused immediately with the same configuration without reallocating memory. ```APIDOC ## BloomFilter::clear ### Description Clears all bits, effectively removing every item from the filter. Memory is not freed; the filter is ready for re-use with the same capacity and hash configuration. ### Method `clear` ### Request Example ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(2048).hashes(4); for x in 0u32..100 { filter.insert(&x); } assert!(filter.contains(&42)); filter.clear(); // Nothing is found after clearing for x in 0u32..100 { assert!(!filter.contains(&x)); } ``` ``` -------------------------------- ### BloomFilter::union Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Merges the bits of another Bloom filter into the current one. After the union, the filter contains all items that were present in either the original filter or the other filter. ```APIDOC ## BloomFilter::union ### Description Performs a bitwise OR of `other`'s bits into `self`. After the union, `self` contains all items from both filters. Both filters must have the same number of bits, the same number of hashes, and the same hasher/seed. ### Method `union` ### Parameters - `other`: A reference to another `BloomFilter` to merge with. ### Request Example ```rust use fastbloom::BloomFilter; let mut a = BloomFilter::with_num_bits(4096).seed(&1).hashes(4); let mut b = BloomFilter::with_num_bits(4096).seed(&1).hashes(4); for x in 0u32..500 { a.insert(&x); } for x in 400u32..900 { b.insert(&x); } a.union(&b); // All items from both filters are now in `a` assert!(a.contains(&0)); assert!(a.contains(&499)); assert!(a.contains(&400)); assert!(a.contains(&898)); ``` ``` -------------------------------- ### BloomFilter::intersect Source: https://context7.com/tomtomwombat/fastbloom/llms.txt Performs a bitwise AND operation with another Bloom filter, updating the current filter to only contain bits set in both filters. This approximates the set intersection of the items. ```APIDOC ## BloomFilter::intersect ### Description Performs a bitwise AND of `other`'s bits into `self`. After the intersect, `self` only preserves bits set in both filters, approximating the set intersection. Same size/hash/hasher requirements as `union`. ### Method `intersect` ### Parameters - `other`: A reference to another `BloomFilter` to intersect with. ### Request Example ```rust use fastbloom::BloomFilter; let mut a = BloomFilter::with_num_bits(4096).seed(&1).hashes(4); let mut b = BloomFilter::with_num_bits(4096).seed(&1).hashes(4); for x in 0u32..500 { a.insert(&x); } for x in 400u32..900 { b.insert(&x); } a.intersect(&b); // Items only in `a` are likely gone; items in both are likely present assert!(!a.contains(&0)); // only in `a`, should be absent assert!(a.contains(&450)); // in both — likely still present ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.