tisonkun commented on code in PR #271:
URL: https://github.com/apache/datasketches-rust/pull/271#discussion_r3920180109
##########
datasketches/src/bloom/sketch.rs:
##########
@@ -613,6 +622,114 @@ impl BloomFilter {
}
}
+/// A read-only inverted view of a [`BloomFilter`].
+///
+/// An inverted view is created by calling [`BloomFilter::invert()`].
+/// Modifications (such as inserting new elements or merging) are disallowed
+/// on an inverted view to avoid corrupting set membership invariants.
+///
+/// Set membership queries can still be executed via
[`contains()`](Self::contains),
+/// and the view can be reinverted back into an updatable [`BloomFilter`].
+#[derive(Debug, Clone, PartialEq)]
+pub struct BloomFilterInvertedView {
+ inner: BloomFilter,
+}
+
+impl BloomFilterInvertedView {
+ /// Returns `true` if an item is possibly in the inverted set.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use datasketches::bloom::BloomFilterBuilder;
+ ///
+ /// let mut filter = BloomFilterBuilder::with_accuracy(100, 0.01)
+ /// .build()
+ /// .unwrap();
+ /// filter.insert("apple");
+ ///
+ /// let inverted = filter.invert();
+ /// assert!(!inverted.contains(&"apple"));
+ /// ```
+ pub fn contains<T: Hash>(&self, item: &T) -> bool {
+ self.inner.contains(item)
+ }
+
+ /// Re-inverts the view back into an updatable [`BloomFilter`].
+ ///
+ /// Inverting twice restores the original bit state and filter guarantees.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use datasketches::bloom::BloomFilterBuilder;
+ ///
+ /// let mut filter = BloomFilterBuilder::with_accuracy(100, 0.01)
+ /// .build()
+ /// .unwrap();
+ /// filter.insert("apple");
+ ///
+ /// let inverted = filter.invert();
+ /// let restored = inverted.invert();
+ /// assert!(restored.contains(&"apple"));
+ /// ```
+ pub fn invert(self) -> BloomFilter {
+ self.into_filter()
+ }
+
+ /// Converts this inverted view back into an updatable [`BloomFilter`] by
+ /// inverting the bits again.
+ ///
+ /// Equivalent to [`invert()`](Self::invert).
+ pub fn into_filter(mut self) -> BloomFilter {
+ for word in &mut self.inner.bit_array {
+ *word = !*word;
+ }
+ self.inner.num_bits_set = self.inner.capacity() as u64 -
self.inner.num_bits_set;
+ self.inner
+ }
+
+ /// Returns a reference to the underlying [`BloomFilter`] representation.
+ pub fn as_filter(&self) -> &BloomFilter {
+ &self.inner
+ }
Review Comment:
Why do we have this method?
--
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]