### Example: Union of Bloom Filters Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Illustrates the union operation between two Bloom filters. Elements from both filters are combined into the first filter. Assumes identical filter configurations. ```rust use fastbloom::BloomFilter; let mut bloom = BloomFilter::with_num_bits(4096).seed(&1).hashes(4); let mut other = BloomFilter::with_num_bits(4096).seed(&1).hashes(4); for x in 0..=1000 { bloom.insert(&x); } for x in 500..=1500 { other.insert(&x); } bloom.union(&other); for x in 0..=2000 { assert_eq!(bloom.contains(&x), bloom.contains(&x) || other.contains(&x)); } ``` -------------------------------- ### Create a Builder with False Positive Rate Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithFalsePositiveRate.html Initializes a BuilderWithFalsePositiveRate to construct a BloomFilter with a target false positive rate. This is the starting point for configuring a Bloom filter. ```rust use fastbloom::BloomFilter; let builder = BloomFilter::with_false_pos(0.01); ``` -------------------------------- ### AtomicBloomFilter Builder with False Positive Rate Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBloomFilter.html Example of creating an AtomicBloomFilter builder using `with_false_pos` and then specifying the expected number of items. This method panics if the false positive rate is 0. ```rust use fastbloom::AtomicBloomFilter; let filter = AtomicBloomFilter::with_false_pos(0.001).expected_items(1000); ``` -------------------------------- ### Builder for BloomFilter with False Positive Rate Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BloomFilter.html Example of creating a BloomFilter builder configured with a specific false positive rate and expected number of items. ```rust use fastbloom::BloomFilter; let filter = BloomFilter::with_false_pos(0.001).expected_items(1000); ``` -------------------------------- ### Checking Membership in BloomFilter Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BloomFilter.html Example of checking if an element is possibly present in a BloomFilter after it has been populated. ```rust use fastbloom::BloomFilter; let bloom = BloomFilter::with_num_bits(1024).items([1, 2, 3].iter()); assert!(bloom.contains(&1)); ``` -------------------------------- ### Initialize BuilderWithBits Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithBits.html Demonstrates creating a BuilderWithBits instance with a specified number of bits or from a vector. ```rust use fastbloom::BloomFilter; let builder = BloomFilter::with_num_bits(1024); let builder = BloomFilter::from_vec(vec![0; 8]); ``` -------------------------------- ### AtomicBloomFilter Contains Check Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBloomFilter.html Example of checking for the possible presence of an item in an AtomicBloomFilter. The filter is initialized with a specific number of bits and populated with items. ```rust use fastbloom::AtomicBloomFilter; let bloom = AtomicBloomFilter::with_num_bits(1024).items([1, 2, 3].iter()); assert!(bloom.contains(&1)); ``` -------------------------------- ### Basic BloomFilter Usage Source: https://docs.rs/fastbloom/0.17.0/fastbloom Demonstrates the basic instantiation and insertion of items into a BloomFilter. ```rust use fastbloom::BloomFilter; let mut filter = BloomFilter::with_num_bits(1024).expected_items(2); filter.insert("42"); filter.insert("🦀"); ``` -------------------------------- ### Creating an AtomicBloomFilter Builder Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Demonstrates how to initialize an AtomicBuilderWithBits with a specified number of bits or from a vector. ```rust use fastbloom::AtomicBloomFilter; let builder = AtomicBloomFilter::with_num_bits(1024); let builder = AtomicBloomFilter::from_vec(vec![0; 8]); ``` -------------------------------- ### Basic BloomFilter Usage Source: https://docs.rs/fastbloom/0.17.0/fastbloom/index.html 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("🦀"); ``` -------------------------------- ### Basic Bloom Filter Usage Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BloomFilter.html Demonstrates the fundamental usage of BloomFilter, including initialization, inserting an element, and checking for its presence. This is useful for basic element tracking. ```rust use fastbloom::BloomFilter; let mut bloom = BloomFilter::with_num_bits(1024).hashes(4); bloom.insert(&2); assert!(bloom.contains(&2)); ``` -------------------------------- ### Bloom Filter Usage Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Demonstrates the basic usage of a Bloom filter, including instantiation with a specified number of bits and expected items, followed by inserting elements. ```APIDOC ## Bloom Filter Basic Usage ### Description This example shows how to create a Bloom filter with a specific number of bits and expected items, and then insert elements into it. ### Method Instantiation and Insertion ### Endpoint N/A (SDK Usage) ### Parameters N/A ### Request Example ```rust use fastbloom::BloomFilter; // Assuming BloomFilter is the type let mut filter = BloomFilter::with_num_bits(1024).expected_items(2); filter.insert("42"); filter.insert("🦀"); ``` ### Response N/A (SDK Usage) ``` -------------------------------- ### BitVec Methods Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/bit_vector.rs.html Provides methods for manipulating a non-atomic bit vector. These include getting the length, number of bits, checking bit status, setting bits, clearing the vector, and performing union and intersection operations. ```APIDOC ## BitVec ### Description A bit vector partitioned into `u64` blocks. ### Methods - `len() -> usize` Returns the number of `u64` blocks in the bit vector. - `num_bits() -> usize` Returns the total number of bits in the bit vector. - `as_slice() -> &[$bits]` Returns a slice view of the underlying `u64` blocks. - `iter() -> impl Iterator + '_` Returns an iterator over the `u64` blocks. - `check(index: usize) -> bool` Checks if the bit at the given index is set. - `set(&mut self, index: usize) -> bool` Sets the bit at the given index and returns `true` if it was previously unset. - `clear(&mut self)` Clears all bits in the bit vector, setting them to 0. - `union(&mut self, other: &BitVec)` Performs a bitwise union operation with another `BitVec`. Both vectors must have the same length. - `intersect(&mut self, other: &BitVec)` Performs a bitwise intersection operation with another `BitVec`. Both vectors must have the same length. ``` -------------------------------- ### TryInto for BuilderWithFalsePositiveRate Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithFalsePositiveRate.html Enables attempting to convert a BuilderWithFalsePositiveRate into another type `U`, with error handling for potential conversion failures. ```APIDOC ## try_into(self) -> Result>::Error> ### Description Performs the conversion from `BuilderWithFalsePositiveRate` to type `U`. Returns a `Result` which is `Ok` on success or `Err` if the conversion fails. ### Method N/A (Associated function) ### Parameters - **self**: BuilderWithFalsePositiveRate - The instance to attempt to convert. ``` -------------------------------- ### Bloom Filter with Custom Hasher Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Shows how to instantiate a Bloom filter and provide a custom hasher implementation for hashing elements. ```APIDOC ## Bloom Filter with Custom Hasher ### Description This example demonstrates how to create a Bloom filter and specify a custom hasher to be used for hashing elements. ### Method Instantiation with Custom Hasher ### Endpoint N/A (SDK Usage) ### Parameters N/A ### Request Example ```rust use fastbloom::BloomFilter; // Assuming BloomFilter is the type use foldhash::fast::RandomState; let filter = BloomFilter::with_num_bits(1024) .hasher(RandomState::default()) .items(["42", "🦀"].iter()); ``` ### Response N/A (SDK Usage) ``` -------------------------------- ### Creating an AtomicBloomFilter with Initial Items Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Consumes the builder and constructs an AtomicBloomFilter containing all provided items. The number of hashes is optimized based on the number of items to maximize accuracy. ```rust use fastbloom::AtomicBloomFilter; let bloom = AtomicBloomFilter::with_num_bits(1024).items([1, 2, 3].iter()); ``` -------------------------------- ### AtomicBloomFilter Initialization from Vec Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBloomFilter.html Shows how to create an AtomicBloomFilter from an existing vector of u64, preserving its state. It requires specifying the seed and number of hashes. Panics if the input vector is empty. ```rust use fastbloom::AtomicBloomFilter; let orig = AtomicBloomFilter::with_false_pos(0.001).seed(&42).items([1, 2].iter()); let num_hashes = orig.num_hashes(); let new = AtomicBloomFilter::from_vec(orig.iter().collect()).seed(&42).hashes(num_hashes); assert!(new.contains(&1)); assert!(new.contains(&2)); ``` -------------------------------- ### Creating and Inserting into AtomicBloomFilter Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBloomFilter.html Demonstrates how to initialize an AtomicBloomFilter and insert an element. This snippet shows the basic usage for adding items to the filter. ```rust use fastbloom::AtomicBloomFilter; let bloom = AtomicBloomFilter::with_num_bits(1024).hashes(4); bloom.insert(&2); assert!(bloom.contains(&2)); ``` -------------------------------- ### TryFrom for BuilderWithFalsePositiveRate Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithFalsePositiveRate.html Allows attempting to convert a type `U` into a BuilderWithFalsePositiveRate, with error handling for potential conversion failures. ```APIDOC ## try_from(value: U) -> Result>::Error> ### Description Performs the conversion from type `U` to `BuilderWithFalsePositiveRate`. Returns a `Result` which is `Ok` on success or `Err` if the conversion fails. ### Method N/A (Associated function) ### Parameters - **value**: U - The value to attempt to convert. ``` -------------------------------- ### into Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Calls `U::from(self)`. This is a standard conversion trait. ```APIDOC ## into ### Description Calls `U::from(self)`. This conversion is determined by the `From for U` implementation. ### Method `fn` ### Returns - U - The result of the conversion. ``` -------------------------------- ### Basic Bloom Filter Usage Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Demonstrates the basic insertion and checking of items in a Bloom filter created with a specified number of bits. ```rust #[doc = concat!("use fastbloom::", stringify!($name), ");"] #[doc = concat!("let ", $ismut, "filter = ", stringify!($name), "::with_num_bits(1024).expected_items(2);")] filter.insert("42"); filter.insert("🦀"); ``` -------------------------------- ### Add fastbloom to Cargo.toml Source: https://docs.rs/fastbloom/0.17.0/fastbloom To use the fastbloom crate, add it as a dependency in your Cargo.toml file. ```toml # Cargo.toml [dependencies] fastbloom = "0.17.0" ``` -------------------------------- ### Setting Expected Items Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/builder.rs.html Consumes the builder and creates a Bloom filter, optimizing the number of hash functions based on the expected number of items to be inserted. This aims to minimize the false positive rate. ```APIDOC ## Setting Expected Items ### Description Constructs a Bloom filter by calculating the optimal number of hash functions based on the provided `expected_items` and the filter's capacity. This method aims to achieve the best possible accuracy for the given constraints. If `expected_items` is 0, it defaults to 1. ### Method `expected_items` ### Endpoint N/A ### Parameters - **expected_items** (usize) - Required - The estimated number of items that will be inserted into the Bloom filter. ### Request Example ```rust use fastbloom::Bloom; let bloom = Bloom::with_num_bits(1024).expected_items(500); ``` ### Response N/A ``` -------------------------------- ### clone_to_uninit Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Performs copy-assignment from `self` to `dest`. This is a nightly-only experimental API. ```APIDOC ## clone_to_uninit ### Description Performs copy-assignment from `self` to `dest`. ### Method `unsafe fn` ### Parameters - **dest** (*mut u8) - Description: The destination pointer. ### Note This is a nightly-only experimental API. ``` -------------------------------- ### Construct BloomFilter with Initial Items Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithBits.html Consumes the builder and populates a BloomFilter with provided items. The number of hashes is optimized based on the item count for accuracy. ```rust use fastbloom::BloomFilter; let bloom = BloomFilter::with_num_bits(1024).items([1, 2, 3].iter()); ``` -------------------------------- ### AtomicBloomFilter with Target False Positive Rate Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBloomFilter.html Shows how to create an AtomicBloomFilter with a specific target false positive rate and initialize it with items. Asserts that existing items are contained. ```rust use fastbloom::AtomicBloomFilter; let filter = AtomicBloomFilter::with_false_pos(0.001).items(["42", "🦀"].iter()); assert!(filter.contains("42")); assert!(filter.contains("🦀")); ``` -------------------------------- ### AtomicBloomFilter Builder with Number of Bits Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBloomFilter.html Demonstrates creating an AtomicBloomFilter builder using `with_num_bits` and then specifying the number of hashes. This method panics if the number of bits is 0. ```rust use fastbloom::AtomicBloomFilter; let filter = AtomicBloomFilter::with_num_bits(1024).hashes(4); ``` -------------------------------- ### Builder Methods Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Methods for creating a builder instance to construct a Bloom filter with specific configurations like false positive rate, number of bits, or from an existing bit vector. ```APIDOC ## Builder Methods ### `with_false_pos(fp: f64)` Creates a builder instance to construct a Bloom filter with a specified false positive rate. #### Parameters * **fp** (`f64`) - The desired false positive rate. ### `with_num_bits(num_bits: usize)` Creates a builder instance to construct a Bloom filter with a specified number of bits. #### Parameters * **num_bits** (`usize`) - The number of bits for tracking item membership. Must not be 0. ### `from_vec(bit_vec: Vec)` Creates a builder instance to construct a Bloom filter initialized with a given bit vector. #### Parameters * **bit_vec** (`Vec`) - The initial bit vector. Must not be empty. ### Examples ```rust // Example for with_false_pos let filter_fp = fastbloom::BloomFilter::with_false_pos(0.001).expected_items(1000); // Example for with_num_bits let filter_bits = fastbloom::BloomFilter::with_num_bits(1024).hashes(4); // Example for from_vec let orig = fastbloom::BloomFilter::with_false_pos(0.001).seed(&42).items([1, 2].iter()); let num_hashes = orig.num_hashes(); let new_filter = fastbloom::BloomFilter::from_vec(orig.iter().collect()).seed(&42).hashes(num_hashes); assert!(new_filter.contains(&1)); assert!(new_filter.contains(&2)); ``` ``` -------------------------------- ### BloomFilter with Custom Hasher Source: https://docs.rs/fastbloom/0.17.0/fastbloom Shows how to configure a BloomFilter to use a custom hasher, such as RandomState from the foldhash crate. ```rust use fastbloom::BloomFilter; use foldhash::fast::RandomState; let filter = BloomFilter::with_num_bits(1024) .hasher(RandomState::default()) .items(["42", "🦀"].iter()); ``` -------------------------------- ### VZip for BuilderWithFalsePositiveRate Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithFalsePositiveRate.html Provides a `vzip` method for BuilderWithFalsePositiveRate when it implements the `MultiLane` trait for `V`. ```APIDOC ## vzip(self) -> V ### Description Zips the lanes of the `BuilderWithFalsePositiveRate` instance into a `V` type, assuming `V` implements `MultiLane`. ### Method N/A (Associated function) ### Parameters - **self**: BuilderWithFalsePositiveRate - The instance to zip. ``` -------------------------------- ### Creating BloomFilter from Vec Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BloomFilter.html Shows how to construct a BloomFilter initialized with a pre-existing bit vector, preserving configuration like seed and number of hashes. ```rust use fastbloom::BloomFilter; let orig = BloomFilter::with_false_pos(0.001).seed(&42).items([1, 2].iter()); let num_hashes = orig.num_hashes(); let new = BloomFilter::from_vec(orig.iter().collect()).seed(&42).hashes(num_hashes); assert!(new.contains(&1)); assert!(new.contains(&2)); ``` -------------------------------- ### Bloom Filter with False Positive Rate Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Shows how to instantiate a Bloom filter with a target false positive rate and populate it with items from an iterator. ```rust #[doc = concat!("use fastbloom::", stringify!($name), ");"] let filter = stringify!($name), "::with_false_pos(0.001).items(["42", "🦀"].iter()); assert!(filter.contains("42")); assert!(filter.contains("🦀")); ``` -------------------------------- ### Test Constructor Equality (num_bits) Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Asserts that constructing a filter using `with_num_bits` is equivalent to using `new_builder` with the same number of bits and hashes. ```rust assert_eq!( $name::with_num_bits(4).hashes(4), $name::new_builder(4).hashes(4), ); ``` -------------------------------- ### Create AtomicBloomFilter with False Positive Rate Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithFalsePositiveRate.html Initialize an AtomicBloomFilter builder with a specified false positive rate. ```rust use fastbloom::AtomicBloomFilter; let builder = AtomicBloomFilter::with_false_pos(0.01); ``` -------------------------------- ### Creating an AtomicBloomFilter with Expected Items Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Consumes the builder to create an empty AtomicBloomFilter, optimizing the number of hashes based on the expected number of items to maximize accuracy. If 0 is provided for expected items, it's internally set to 1. ```rust use fastbloom::AtomicBloomFilter; let bloom = AtomicBloomFilter::with_num_bits(1024).expected_items(500); ``` -------------------------------- ### Construct Bloom Filter with Initial Items Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithFalsePositiveRate.html Populate an AtomicBloomFilter with initial items. The builder optimizes hash count and memory based on the number of provided items to meet the false positive rate. ```rust use fastbloom::AtomicBloomFilter; let bloom = AtomicBloomFilter::with_false_pos(0.001).items([1, 2, 3].iter()); ``` -------------------------------- ### try_from Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Performs the conversion. This is a standard fallible conversion trait. ```APIDOC ## try_from ### Description Performs the conversion. ### Method `fn` ### Parameters - **value** (U) - Description: The value to convert. ### Returns - Result>::Error> - The result of the conversion, which may be an error. ``` -------------------------------- ### Bloom Filter with False Positive Rate Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Illustrates how to create a Bloom filter configured with a target false positive rate and populate it with an iterator of items. ```APIDOC ## Bloom Filter with False Positive Rate ### Description This example demonstrates creating a Bloom filter using a desired false positive rate and initializing it with a collection of items. ### Method Instantiation with False Positive Rate and Item Population ### Endpoint N/A (SDK Usage) ### Parameters N/A ### Request Example ```rust use fastbloom::BloomFilter; // Assuming BloomFilter is the type let filter = BloomFilter::with_false_pos(0.001).items(["42", "🦀"].iter()); assert!(filter.contains("42")); assert!(filter.contains("🦀")); ``` ### Response N/A (SDK Usage) ``` -------------------------------- ### from Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Returns the argument unchanged. This is a standard conversion trait. ```APIDOC ## from ### Description Returns the argument unchanged. ### Method `fn` ### Parameters - **t** (T) - Description: The value to return. ### Returns - T - The argument `t`. ``` -------------------------------- ### Builder for BloomFilter with Number of Bits Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BloomFilter.html Demonstrates creating a BloomFilter builder configured with a specific number of bits and number of hashes. ```rust use fastbloom::BloomFilter; let filter = BloomFilter::with_num_bits(1024).hashes(4); ``` -------------------------------- ### Test Constructor Equality (false_pos) Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Asserts that constructing a filter using `with_false_pos` is equivalent to using `new_with_false_pos` with the same false positive rate. ```rust assert_eq!( $name::with_false_pos(0.4), $name::new_with_false_pos(0.4), ); ``` -------------------------------- ### Create Bloom Filter with Expected Items Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithFalsePositiveRate.html Constructs a BloomFilter by specifying the expected number of items to be inserted. The builder optimizes the filter's size and hash count based on this value to meet the desired false positive rate. Note that if 0 is provided, it defaults to 1. ```rust use fastbloom::BloomFilter; let bloom = BloomFilter::with_false_pos(0.001).expected_items(500); ``` -------------------------------- ### AtomicBloomFilter with Custom Hasher Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBloomFilter.html Illustrates using a custom hasher, such as RandomState from the foldhash crate, when creating an AtomicBloomFilter. Initializes the filter with items. ```rust use fastbloom::AtomicBloomFilter; use foldhash::fast::RandomState; let filter = AtomicBloomFilter::with_num_bits(1024) .hasher(RandomState::default()) .items(["42", "🦀"].iter()); ``` -------------------------------- ### Create Bloom Filter with Specific Size and Hashes Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/builder.rs.html Initializes a Bloom filter with a predefined number of bits and hash functions. Useful for testing or when exact control over filter parameters is needed. ```rust use crate::BloomFilter; let size_bits = 512 * 1000; let bloom = BloomFilter::with_num_bits(size_bits).hashes(4); ``` -------------------------------- ### Into for BuilderWithFalsePositiveRate Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithFalsePositiveRate.html Provides a way to convert a BuilderWithFalsePositiveRate into another type `U` by leveraging the `From for U` trait implementation. ```APIDOC ## into(self) -> U ### Description Calls `U::from(self)`. This conversion is determined by the implementation of `From for U`. ### Method N/A (Associated function) ### Parameters - **self**: BuilderWithFalsePositiveRate - The instance to convert. ``` -------------------------------- ### Construct BloomFilter with Expected Items Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithBits.html Consumes the builder to create an empty BloomFilter, optimizing the number of hash functions based on the expected number of items to maximize accuracy. ```rust use fastbloom::BloomFilter; let bloom = BloomFilter::with_num_bits(1024).expected_items(500); ``` -------------------------------- ### Test Initial State of Bloom Filter Insertion Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Verifies that inserting an item into a newly created bloom filter (with specified capacity) returns `false`, indicating the item was not previously present. ```Rust #[test] fn first_insert_false() { let mut filter = $name::with_num_bits(1202).expected_items(4); assert!(!filter.insert(&5)); } ``` -------------------------------- ### BuilderWithBits::items Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithBits.html Consumes this builder and constructs a `BloomFilter` containing all values in `items`. The number of hashes per item is optimized based on `items.len()` to maximize Bloom filter accuracy. ```APIDOC ## BuilderWithBits::items ### Description Consumes this builder and constructs a `BloomFilter` containing all values in `items`. The number of hashes per item is optimized based on `items.len()` to maximize Bloom filter accuracy (minimize false positives chance on `BloomFilter::contains`). ### Method ```rust pub fn items<'a, H: Hash + 'a, I: IntoIterator>>( self, items: I, ) -> BloomFilter ``` ### Parameters * **items** (`I: IntoIterator>`) - Description: An iterator yielding items to be inserted into the Bloom filter. ### Returns `BloomFilter` - A Bloom filter containing all values from the provided iterator. ``` -------------------------------- ### Creating an AtomicBloomFilter with a Specific Number of Hashes Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Consumes the builder to create an empty AtomicBloomFilter with a specified number of hashes per item. If 0 is provided, it's treated as 1. ```rust use fastbloom::AtomicBloomFilter; let bloom = AtomicBloomFilter::with_num_bits(1024).hashes(4); ``` -------------------------------- ### try_into Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Performs the conversion. This is a standard fallible conversion trait. ```APIDOC ## try_into ### Description Performs the conversion. ### Method `fn` ### Returns - Result>::Error> - The result of the conversion, which may be an error. ``` -------------------------------- ### clone_into Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Uses borrowed data to replace owned data, usually by cloning. ```APIDOC ## clone_into ### Description Uses borrowed data to replace owned data, usually by cloning. ### Method `fn` ### Parameters - **target** (&mut T) - Description: A mutable reference to the owned data to be replaced. ``` -------------------------------- ### Setting the Number of Hashes Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/builder.rs.html Consumes the builder and creates a Bloom filter with a specified number of hash functions. The number of hashes is crucial for Bloom filter performance and accuracy. ```APIDOC ## Setting the Number of Hashes ### Description Finalizes the builder and constructs a Bloom filter using the specified number of hash functions. If `num_hashes` is 0, it defaults to 1. ### Method `hashes` ### Endpoint N/A ### Parameters - **num_hashes** (u32) - Required - The number of hash functions to use for the Bloom filter. Must be greater than 0. ### Request Example ```rust use fastbloom::Bloom; let bloom = Bloom::with_num_bits(1024).hashes(4); ``` ### Response N/A ``` -------------------------------- ### BloomFilter::with_num_bits Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BloomFilter.html Creates a builder instance to construct a BloomFilter with a specified number of bits for tracking item membership. Panics if the number of bits is 0. ```APIDOC ## BloomFilter::with_num_bits ### Description Creates a builder instance to construct a `Self` with `num_bits` number of bits for tracking item membership. ### Panics Panics if the number of bits, `num_bits`, is 0. ### Examples ```rust use fastbloom::BloomFilter; let filter = BloomFilter::with_num_bits(1024).hashes(4); ``` ``` -------------------------------- ### Create Bloom Filter with Specific Number of Bits Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Instantiate a Bloom filter builder by specifying the exact number of bits to be used. This allows for precise control over the filter's memory footprint. ```Rust let filter = BloomFilter::with_num_bits(1024).hashes(4); ``` -------------------------------- ### Test Seeded Filter Construction Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Verifies that constructing a filter with the same seed and items results in an equal filter. It also checks that incrementing the seed produces a different filter. ```rust let num_bits = 1 << 10; let sample_vals = member_nums(1000).collect::>(); for x in 0u8..32 { let seed = x as u128; assert_eq!( $name::new_builder(num_bits) .seed(&seed) .items(sample_vals.iter()), $name::new_builder(num_bits) .seed(&seed) .items(sample_vals.iter()) ); assert!( !($name::new_builder(num_bits) .seed(&(seed + 1)) .items(sample_vals.iter()) == $name::new_builder(num_bits) .seed(&seed) .items(sample_vals.iter())) ); } ``` -------------------------------- ### BloomFilter::with_false_pos Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BloomFilter.html Creates a new builder instance to construct a BloomFilter with a target false positive rate. The memory size depends on the false positive rate and the expected number of items. Panics if the false positive rate is 0. ```APIDOC ## BloomFilter::with_false_pos ### Description Creates a new builder instance to construct a `Self` with a target false positive rate of `fp`. The memory size of the underlying bit vector is dependent on the false positive rate and the expected number of items. ### Panics Panics if the false positive rate, `fp`, is 0. ### Examples ```rust use fastbloom::BloomFilter; let filter = BloomFilter::with_false_pos(0.001).expected_items(1000); ``` ``` -------------------------------- ### Setting the Hasher Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/builder.rs.html Allows specifying a custom hasher for the Bloom filter builder. This provides flexibility in choosing the hashing algorithm. ```APIDOC ## Setting the Hasher ### Description Replaces the default hasher with a custom `BuildHasher`. This allows for advanced control over the hashing process used by the Bloom filter. ### Method `hasher` ### Endpoint N/A ### Parameters - **hasher** (H: BuildHasher) - Required - An instance of a type implementing `BuildHasher`. ### Request Example ```rust use fastbloom::Bloom; use foldhash::fast::RandomState; let bloom = Bloom::with_num_bits(1024).hasher(RandomState::default()).hashes(4); ``` ### Response N/A ``` -------------------------------- ### VZip for T Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBloomFilter.html Implementation of the VZip trait for T, enabling zipped iteration over multiple lanes. ```APIDOC ## fn vzip(self) -> V ``` -------------------------------- ### ToOwned for BuilderWithFalsePositiveRate Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithFalsePositiveRate.html Enables creating an owned version of BuilderWithFalsePositiveRate, typically by cloning. ```APIDOC ## to_owned(&self) -> T ### Description Creates owned data from borrowed data, usually by cloning. ### Method N/A (Associated function) ### Parameters - **&self**: BuilderWithFalsePositiveRate - A reference to the borrowed data. ``` ```APIDOC ## clone_into(&self, target: &mut T) ### Description Uses borrowed data to replace owned data, usually by cloning. ### Method N/A (Associated function) ### Parameters - **&self**: BuilderWithFalsePositiveRate - A reference to the borrowed data. - **target**: &mut T - A mutable reference to the owned data to be replaced. ``` -------------------------------- ### Construct BloomFilter with Number of Hashes Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithBits.html Consumes the builder to create an empty BloomFilter using a specified number of hash functions. If 0 is provided, it's treated as 1. ```rust use fastbloom::BloomFilter; let bloom = BloomFilter::with_num_bits(1024).hashes(4); ``` -------------------------------- ### pow function implementation Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/math.rs.html Provides the power function (base to the exponent). Uses the standard library's `powf` when the 'std' feature is enabled, otherwise falls back to `libm::pow`. ```Rust #[cfg(feature = "std")] #[inline] pub(crate) fn pow(b: f64, p: f64) -> f64 { b.powf(p) } #[cfg(not(feature = "std"))] #[inline] pub(crate) fn pow(b: f64, p: f64) -> f64 { libm::pow(b, p) } ``` -------------------------------- ### Calculate Optimal Hashes for Bloom Filter Source: https://docs.rs/fastbloom/0.17.0/fastbloom/fn.optimal_hashes.html Use this function to find the ideal number of hash functions for a bloom filter given the total number of bits and the expected number of items. This helps in optimizing the false positive rate. ```rust pub fn optimal_hashes(num_bits: usize, num_items: usize) -> u32 ``` -------------------------------- ### Setting a Custom Hasher for AtomicBloomFilter Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Allows setting a custom hasher for the builder, which will be used by the Bloom filter for inserting and checking items. ```rust use fastbloom::AtomicBloomFilter; use foldhash::fast::RandomState; let bloom = AtomicBloomFilter::with_num_bits(1024).hasher(RandomState::default()).hashes(4); ``` -------------------------------- ### BuilderWithBits::hashes Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithBits.html Consumes this builder, using the provided `num_hashes` to return an empty `BloomFilter`. Note: if `num_hashes` is 0, it is treated as 1. ```APIDOC ## BuilderWithBits::hashes ### Description Consumes this builder, using the provided `num_hashes` to return an empty `BloomFilter`. Note: if `num_hashes` is 0, it is treated as 1. Bloom filters with 0 hashes per item are practically useless, and disallowing this case enables further optimizations. ### Method ```rust pub fn hashes(self, num_hashes: u32) -> BloomFilter ``` ### Parameters * **num_hashes** (`u32`) - Description: The number of hashes to use per item. If 0, it is treated as 1. ### Returns `BloomFilter` - An empty Bloom filter constructed with the specified number of hashes. ``` -------------------------------- ### Construct Bloom Filter with Expected Items Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithFalsePositiveRate.html Create an empty AtomicBloomFilter by specifying the expected number of items. The builder optimizes hash count and memory for accuracy. ```rust use fastbloom::AtomicBloomFilter; let bloom = AtomicBloomFilter::with_false_pos(0.001).expected_items(500); ``` -------------------------------- ### Handle Zero Hashes Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/builder.rs.html Tests the behavior when zero hashes are specified. The library defaults to one hash function in this case for both `BloomFilter` and `AtomicBloomFilter`. ```Rust let bloom = BloomFilter::with_num_bits(512).hashes(0); assert_eq!(bloom.num_hashes(), 1); let bloom = AtomicBloomFilter::with_num_bits(512).hashes(0); assert_eq!(bloom.num_hashes(), 1); ``` -------------------------------- ### BloomFilter::from_vec Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BloomFilter.html Creates a builder instance to construct a BloomFilter initialized with a given bit vector. Panics if the bit vector is empty. ```APIDOC ## BloomFilter::from_vec ### Description Creates a builder instance to construct a `Self` initialized with bit vector `bit_vec`. ### Panics Panics if the bit vector, `bit_vec`, is empty. ### Examples ```rust use fastbloom::BloomFilter; let orig = BloomFilter::with_false_pos(0.001).seed(&42).items([1, 2].iter()); let num_hashes = orig.num_hashes(); let new = BloomFilter::from_vec(orig.iter().collect()).seed(&42).hashes(num_hashes); assert!(new.contains(&1)); assert!(new.contains(&2)); ``` ``` -------------------------------- ### ceil function implementation Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/math.rs.html Provides the ceiling function. Uses the standard library's `ceil` when the 'std' feature is enabled, otherwise falls back to `libm::ceil`. ```Rust #[cfg(feature = "std")] #[inline] pub(crate) fn ceil(x: f64) -> f64 { x.ceil() } #[cfg(not(feature = "std"))] #[inline] pub(crate) fn ceil(x: f64) -> f64 { libm::ceil(x) } ``` -------------------------------- ### Seeded Hasher Implementation Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Provides a way to create a `DefaultHasher` with a specific seed, useful for reproducible hashing in tests. ```Rust impl Seeded for DefaultHasher { fn seeded(seed: &[u8; 16]) -> Self { Self::seeded(seed) } } ``` -------------------------------- ### Test Clone and Equality Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Verifies that a filter can be cloned and that the clone is initially equal to the original. It also checks that modifying the clone results in inequality. ```rust let filter = $name::with_num_bits(4).hashes(4); let mut cloned = filter.clone(); assert_eq!(filter, cloned); cloned.insert(&42); assert!(filter != cloned); ``` -------------------------------- ### AtomicBuilderWithBits::items Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Consumes the builder and constructs an AtomicBloomFilter containing all provided items, optimizing the number of hashes based on the number of items. ```APIDOC ## AtomicBuilderWithBits::items ### Description Consumes this builder and constructs a `AtomicBloomFilter` containing all values in `items`. The number of hashes per item is optimized based on `items.len()` to maximize Bloom filter accuracy (minimize false positives chance on `AtomicBloomFilter::contains`). ### Method `items` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use fastbloom::AtomicBloomFilter; let bloom = AtomicBloomFilter::with_num_bits(1024).items([1, 2, 3].iter()); ``` ### Response #### Success Response Returns a `AtomicBloomFilter` containing the provided items. #### Response Example None (returns `AtomicBloomFilter`) ``` -------------------------------- ### AtomicBloomFilter::with_false_pos Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBloomFilter.html Creates a new builder instance to construct an AtomicBloomFilter with a target false positive rate. The memory size of the underlying bit vector is dependent on the false positive rate and the expected number of items. ```APIDOC ## AtomicBloomFilter::with_false_pos ### Description Creates a new builder instance to construct a `Self` with a target false positive rate of `fp`. The memory size of the underlying bit vector is dependent on the false positive rate and the expected number of items. ### Panics Panics if the false positive rate, `fp`, is 0. ### Examples ```rust use fastbloom::AtomicBloomFilter; let filter = AtomicBloomFilter::with_false_pos(0.001).expected_items(1000); ``` ``` -------------------------------- ### DoubleHasher Initialization Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/hasher.rs.html Initializes a DoubleHasher with a seed value and calculates the second hash (h2) using a specific multiplication constant. ```rust pub(crate) fn new(h1: u64) -> Self { // 0xffff_ffff_ffff_ffff / 0x517c_c1b7_2722_0a95 = π let h2 = h1.wrapping_mul(0x51_7c_c1_b7_27_22_0a_95); Self { h1, h2 } } ``` -------------------------------- ### BuilderWithBits::expected_items Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BuilderWithBits.html Consumes this builder, using the provided `expected_items` to return an empty `BloomFilter`. The number of hashes is optimized based on `expected_items` to maximize Bloom filter accuracy. ```APIDOC ## BuilderWithBits::expected_items ### Description Consumes this builder, using the provided `expected_items` to return an empty `BloomFilter`. The number of hashes is optimized based on `expected_items` to maximize Bloom filter accuracy (minimize false positives chance on `BloomFilter::contains`). More or less than `expected_items` may be inserted into Bloom filter. Note: `expected_items` will internally be set to 1 if 0 is specified. ### Method ```rust pub fn expected_items(self, expected_items: usize) -> BloomFilter ``` ### Parameters * **expected_items** (`usize`) - Description: The expected number of items to be inserted into the Bloom filter. If 0, it is treated as 1. ### Returns `BloomFilter` - An empty Bloom filter constructed with optimized hash count based on expected items. ``` -------------------------------- ### AtomicBloomFilter::with_num_bits Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBloomFilter.html Creates a builder instance to construct an AtomicBloomFilter with a specified number of bits for tracking item membership. ```APIDOC ## AtomicBloomFilter::with_num_bits ### Description Creates a builder instance to construct a `Self` with `num_bits` number of bits for tracking item membership. ### Panics Panics if the number of bits, `num_bits`, is 0. ### Examples ```rust use fastbloom::AtomicBloomFilter; let filter = AtomicBloomFilter::with_num_bits(1024).hashes(4); ``` ``` -------------------------------- ### Calculate Expected Density Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/builder.rs.html Computes the probability of a '1' bit being set in the Bloom filter based on the number of hashes, total bits, and expected items. ```rust pub fn expected_density(hashes: u32, bits: usize, items: usize) -> f64 { let total_sets = (items * hashes as usize) as f64; let bits = bits as f64; let prob_set = 1.0 / bits; let prob_not_set = 1.0 - prob_set; let prob_all_not_set = crate::math::pow(prob_not_set, total_sets); 1.0 - prob_all_not_set } ``` -------------------------------- ### Query Methods Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/lib.rs.html Methods for checking the presence of elements within the Bloom filter and retrieving filter properties. ```APIDOC ## Query Methods ### `contains(&self, val: &(impl Hash + ?Sized)) -> bool` Checks if an element is possibly in the Bloom filter. #### Returns `true` if the item is possibly in the Bloom filter, `false` otherwise. #### Parameters * **val** - The value to check for. Must implement `Hash` and `?Sized`. ### `contains_hash(&self, hash: u64) -> bool` Checks if the hash of an element is possibly in the Bloom filter. The element is pre-hashed, and all subsequent hashes are derived from this source hash. #### Returns `true` if the item is possibly in the Bloom filter, `false` otherwise. #### Parameters * **hash** (`u64`) - The pre-computed hash of the element. ### `num_hashes(&self) -> u32` Returns the number of hashes per item used by this Bloom filter. ### `num_bits(&self) -> usize` Returns the total number of bits used by this Bloom filter. ### Examples ```rust use fastbloom::BloomFilter; let bloom = BloomFilter::with_num_bits(1024).items([1, 2, 3].iter()); assert!(bloom.contains(&1)); assert!(!bloom.contains(&4)); let hash_val = bloom.source_hash(&1); assert!(bloom.contains_hash(hash_val)); println!("Number of hashes: {}", bloom.num_hashes()); println!("Number of bits: {}", bloom.num_bits()); ``` ``` -------------------------------- ### BitVec and AtomicBitVec Core Functionality Source: https://docs.rs/fastbloom/0.17.0/src/fastbloom/bit_vector.rs.html Defines common methods for BitVec and AtomicBitVec, including length, number of bits, slice access, iteration, and checking individual bits. ```rust macro_rules! impl_bitvec { ($name:ident, $bits:ty) => { impl $name { #[inline(always)] pub(crate) const fn len(&self) -> usize { self.bits.len() } #[inline(always)] pub(crate) const fn num_bits(&self) -> usize { self.len() * u64::BITS as usize } #[inline(always)] pub(crate) fn as_slice(&self) -> &[$bits] { &self.bits } #[inline(always)] pub(crate) fn iter(&self) -> impl Iterator + '_ { self.bits.iter().map(Self::fetch) } #[inline(always)] pub(crate) fn check(&self, index: usize) -> bool { let (index, bit) = coord(index); Self::fetch(&self.bits[index]) & bit > 0 } } impl FromIterator for $name { fn from_iter>(iter: I) -> Self { let mut bits = iter.into_iter().map(Self::new).collect::>(); bits.shrink_to_fit(); Self { bits: bits.into() } } } impl PartialEq for $name { fn eq(&self, other: &Self) -> bool { if self.len() != other.len() { return false; } core::iter::zip(self.iter(), other.iter()).all(|(l, r)| l == r) } } impl Eq for $name {} }; } impl_bitvec!(BitVec, u64); impl_bitvec!(AtomicBitVec, AtomicU64); ``` -------------------------------- ### AtomicBloomFilter for Concurrency Source: https://docs.rs/fastbloom/0.17.0/fastbloom Demonstrates the usage of AtomicBloomFilter for thread-safe operations. All methods take `&self`, allowing for concurrent access without explicit locking. ```rust use fastbloom::AtomicBloomFilter; let filter = AtomicBloomFilter::with_num_bits(1024).expected_items(2); filter.insert("42"); filter.insert("🦀"); ``` -------------------------------- ### impl TryFrom for T Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BloomFilter.html Enables attempting to convert one type into another, returning a `Result`. The `try_from` method attempts the conversion, returning the converted type or an `Infallible` error. ```APIDOC ## impl TryFrom for T ### Description Enables attempting to convert one type into another, returning a `Result`. The `try_from` method attempts the conversion, returning the converted type or an `Infallible` error. ### Type Alias #### type Error = Infallible - **Description**: The type returned in the event of a conversion error. ### Methods #### fn try_from(value: U) -> Result>::Error> - **Description**: Performs the conversion. ``` -------------------------------- ### to_owned Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.AtomicBuilderWithBits.html Creates owned data from borrowed data, usually by cloning. ```APIDOC ## to_owned ### Description Creates owned data from borrowed data, usually by cloning. ### Method `fn` ### Returns - T - The owned data. ``` -------------------------------- ### impl TryInto for T Source: https://docs.rs/fastbloom/0.17.0/fastbloom/struct.BloomFilter.html Provides a fallible conversion from one type to another. The `try_into` method attempts the conversion, returning a `Result` with the converted type or an error. ```APIDOC ## impl TryInto for T ### Description Provides a fallible conversion from one type to another. The `try_into` method attempts the conversion, returning a `Result` with the converted type or an error. ### Type Alias #### type Error = >::Error - **Description**: The type returned in the event of a conversion error. ### Methods #### fn try_into(self) -> Result>::Error> - **Description**: Performs the conversion. ```