notfilippo commented on code in PR #53: URL: https://github.com/apache/datasketches-rust/pull/53#discussion_r2655393255
########## datasketches/src/bloom/sketch.rs: ########## @@ -0,0 +1,732 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::hash::Hash; +use std::hash::Hasher; + +use crate::codec::SketchBytes; +use crate::codec::SketchSlice; +use crate::error::Error; +use crate::hash::XxHash64; + +// Serialization constants +const PREAMBLE_LONGS_EMPTY: u8 = 3; +const PREAMBLE_LONGS_STANDARD: u8 = 4; +const FAMILY_ID: u8 = 21; // Bloom filter family ID +const SERIAL_VERSION: u8 = 1; +const EMPTY_FLAG_MASK: u8 = 1 << 2; + +/// A Bloom filter for probabilistic set membership testing. +/// +/// Provides fast membership queries with: +/// - No false negatives (inserted items always return `true`) +/// - Tunable false positive rate +/// - Constant space usage +/// +/// Use [`super::BloomFilterBuilder`] to construct instances. +#[derive(Debug, Clone, PartialEq)] +pub struct BloomFilter { + /// Hash seed for all hash functions + pub(super) seed: u64, + /// Number of hash functions to use (k) + pub(super) num_hashes: u16, + /// Total number of bits in the filter (m) + pub(super) capacity_bits: u64, + /// Count of bits set to 1 (for statistics) + pub(super) num_bits_set: u64, + /// Bit array packed into u64 words + /// Length = ceil(capacity_bits / 64) + pub(super) bit_array: Vec<u64>, +} + +impl BloomFilter { + /// Tests whether an item is possibly in the set. + /// + /// Returns: + /// - `true`: Item was **possibly** inserted (or false positive) + /// - `false`: Item was **definitely not** inserted + /// + /// # Examples + /// + /// ``` + /// # use datasketches::bloom::BloomFilterBuilder; + /// let mut filter = BloomFilterBuilder::with_accuracy(100, 0.01).build(); + /// filter.insert("apple"); + /// + /// assert!(filter.contains(&"apple")); // true - was inserted + /// assert!(!filter.contains(&"grape")); // false - never inserted (probably) Review Comment: Typo. Good catch! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
