notfilippo commented on code in PR #53:
URL: https://github.com/apache/datasketches-rust/pull/53#discussion_r2655447014


##########
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)
+    /// ```
+    pub fn contains<T: Hash>(&self, item: &T) -> bool {
+        if self.is_empty() {
+            return false;
+        }
+
+        let (h0, h1) = self.compute_hash(item);
+        self.check_bits(h0, h1)
+    }
+
+    /// Tests and inserts an item in a single operation.
+    ///
+    /// Returns whether the item was possibly already in the set before 
insertion.
+    /// This is more efficient than calling `contains()` then `insert()` 
separately.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # use datasketches::bloom::BloomFilterBuilder;
+    /// let mut filter = BloomFilterBuilder::with_accuracy(100, 0.01).build();
+    ///
+    /// let was_present = filter.contains_and_insert(&"apple");
+    /// assert!(!was_present); // First insertion
+    ///
+    /// let was_present = filter.contains_and_insert(&"apple");
+    /// assert!(was_present); // Now it's in the set
+    /// ```
+    pub fn contains_and_insert<T: Hash>(&mut self, item: &T) -> bool {
+        let (h0, h1) = self.compute_hash(item);
+        let was_present = self.check_bits(h0, h1);
+        self.set_bits(h0, h1);
+        was_present
+    }
+
+    /// Inserts an item into the filter.
+    ///
+    /// After insertion, `contains(item)` will always return `true`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # use datasketches::bloom::BloomFilterBuilder;
+    /// let mut filter = BloomFilterBuilder::with_accuracy(100, 0.01).build();
+    ///
+    /// filter.insert("apple");
+    /// filter.insert(42_u64);
+    /// filter.insert(&[1, 2, 3]);
+    ///
+    /// assert!(filter.contains(&"apple"));
+    /// ```
+    pub fn insert<T: Hash>(&mut self, item: T) {
+        let (h0, h1) = self.compute_hash(&item);
+        self.set_bits(h0, h1);
+    }
+
+    /// Resets the filter to its initial empty state.
+    ///
+    /// Clears all bits while preserving capacity and configuration.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # use datasketches::bloom::BloomFilterBuilder;
+    /// let mut filter = BloomFilterBuilder::with_accuracy(100, 0.01).build();
+    /// filter.insert("apple");
+    /// assert!(!filter.is_empty());
+    ///
+    /// filter.reset();
+    /// assert!(filter.is_empty());
+    /// assert!(!filter.contains(&"apple"));
+    /// ```
+    pub fn reset(&mut self) {
+        for word in &mut self.bit_array {
+            *word = 0;
+        }

Review Comment:
   Always appreciate learning about new rust API. Thanks!



-- 
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]

Reply via email to