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


##########
src/hll/hash_set.rs:
##########
@@ -0,0 +1,196 @@
+// 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.
+
+//! Hash set for storing unique coupons with linear probing
+//!
+//! Uses open addressing with a custom stride function to handle collisions.
+//! Provides better performance than List when many coupons are stored.
+
+use crate::error::{SerdeError, SerdeResult};
+use crate::hll::container::{COUPON_EMPTY, Container};
+use crate::hll::serialization::*;
+use crate::hll::{HllType, KEY_MASK_26};
+
+/// Hash set for efficient coupon storage with collision handling
+#[derive(Debug, Clone, PartialEq)]
+pub struct HashSet {
+    container: Container,
+}
+
+impl Default for HashSet {
+    fn default() -> Self {
+        const LG_INIT_SET_SIZE: usize = 5;
+        Self::new(LG_INIT_SET_SIZE)
+    }
+}
+
+impl HashSet {
+    pub fn new(lg_size: usize) -> Self {
+        Self {
+            container: Container::new(lg_size),
+        }
+    }
+
+    /// Insert coupon into hash set, ignoring duplicates
+    pub fn update(&mut self, coupon: u32) {
+        let mask = (1 << self.container.lg_size()) - 1;
+
+        // Initial probe position from low bits of coupon
+        let mut probe = coupon & mask;
+        let starting_position = probe;
+
+        loop {
+            let value = &mut self.container.coupons[probe as usize];
+            if value == &COUPON_EMPTY {
+                // Found empty slot, insert new coupon
+                *value = coupon;
+                self.container.len += 1;
+                break;
+            } else if value == &coupon {
+                // Duplicate found, nothing to do
+                break;
+            }
+
+            // Collision: compute stride and probe next position
+            // Stride is always odd to ensure all slots are visited
+            let stride = ((coupon & KEY_MASK_26) >> self.container.lg_size()) 
| 1;
+            probe = (probe + stride) & mask;
+            if probe == starting_position {
+                panic!("HashSet full; no empty slots");

Review Comment:
   Added a comment on top of the `panic!` (now `unreachable!`) macro calls.



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