This is an automated email from the ASF dual-hosted git repository.

tisonkun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datasketches-rust.git


The following commit(s) were added to refs/heads/main by this push:
     new e10a9c4  refactor: flatten collect_retained_entries and tidy 
sort_niche_by_hash (#356)
e10a9c4 is described below

commit e10a9c42c83bfd6736f80e168e35cdbff1915922
Author: tison <[email protected]>
AuthorDate: Thu Nov 12 16:49:58 2026 +0800

    refactor: flatten collect_retained_entries and tidy sort_niche_by_hash 
(#356)
    
    Signed-off-by: tison <[email protected]>
---
 .../src/thetafamily/common/sketch_state.rs         | 25 ++++++++++++-----
 datasketches/src/thetafamily/common/union.rs       | 31 ++++++++++++----------
 2 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/datasketches/src/thetafamily/common/sketch_state.rs 
b/datasketches/src/thetafamily/common/sketch_state.rs
index 0619379..a99b76d 100644
--- a/datasketches/src/thetafamily/common/sketch_state.rs
+++ b/datasketches/src/thetafamily/common/sketch_state.rs
@@ -254,22 +254,33 @@ pub fn sort_copy_by_hash<E: SketchEntry + Copy>(entries: 
&mut Vec<E>) {
 }
 
 #[cfg(feature = "tuple")]
-pub fn sort_niche_by_hash<E: SketchEntry>(entries: &mut Vec<E>) {
+use crate::tuple::TupleEntry;
+
+#[cfg(feature = "tuple")]
+pub fn sort_niche_by_hash<S>(entries: &mut Vec<TupleEntry<S>>) {
     const {
-        assert!(
-            size_of::<Option<E>>() == size_of::<E>() && 
align_of::<Option<E>>() == align_of::<E>(),
-            "niche bucket sort requires Option<E> to match E in size and 
alignment",
-        );
+        if size_of::<TupleEntry<S>>() != size_of::<Option<TupleEntry<S>>>() {
+            panic!("niche sort requires Option<TupleEntry> to match TupleEntry 
in size");
+        }
+        if align_of::<TupleEntry<S>>() != align_of::<Option<TupleEntry<S>>>() {
+            panic!("niche sort requires Option<TupleEntry> to match TupleEntry 
in alignment");
+        }
     }
+
     with_bucket_starts(entries, |entries, starts, shift, mask| {
         let len = entries.len();
-        let mut sorted: Vec<Option<E>> = std::iter::repeat_with(|| 
None).take(len).collect();
+        let mut sorted = std::iter::repeat_with(|| None)
+            .take(len)
+            .collect::<Vec<_>>();
         for entry in std::mem::take(entries) {
             let next = &mut starts[(entry.hash() >> shift) as usize & mask];
             sorted[*next as usize] = Some(entry);
             *next += 1;
         }
-        let mut sorted: Vec<E> = 
sorted.into_iter().map(Option::unwrap).collect();
+        let mut sorted = sorted
+            .into_iter()
+            .map(|ent| ent.unwrap())
+            .collect::<Vec<_>>();
         for i in 1..len {
             let mut j = i;
             while j > 0 && sorted[j - 1].hash() > sorted[j].hash() {
diff --git a/datasketches/src/thetafamily/common/union.rs 
b/datasketches/src/thetafamily/common/union.rs
index 5e2e478..43d77a4 100644
--- a/datasketches/src/thetafamily/common/union.rs
+++ b/datasketches/src/thetafamily/common/union.rs
@@ -118,7 +118,17 @@ where
         let Some(theta) = self.result_theta else {
             return CompactSketchState::empty(self.table.seed_hash());
         };
-        let entries = self.collect_retained_entries(theta, Clone::clone);
+
+        let entries = if theta >= self.table.retention_theta() {
+            self.table.iter_entries().cloned().collect()
+        } else {
+            self.table
+                .iter_entries()
+                .filter(|&ent| ent.hash() < theta)
+                .cloned()
+                .collect()
+        };
+
         self.finish_compaction(entries, theta, ordered)
     }
 
@@ -146,24 +156,17 @@ where
         let Some(theta) = self.result_theta else {
             return CompactSketchState::empty(self.table.seed_hash());
         };
-        let entries = self.collect_retained_entries(theta, |entry| entry);
-        self.finish_compaction(entries, theta, ordered)
-    }
 
-    fn collect_retained_entries<'a, T>(
-        &'a self,
-        theta: u64,
-        map: impl FnMut(&'a E) -> T,
-    ) -> Vec<T> {
-        if theta >= self.table.retention_theta() {
-            self.table.iter_entries().map(map).collect()
+        let entries = if theta >= self.table.retention_theta() {
+            self.table.iter_entries().collect()
         } else {
             self.table
                 .iter_entries()
-                .filter(|entry| entry.hash() < theta)
-                .map(map)
+                .filter(|ent| ent.hash() < theta)
                 .collect()
-        }
+        };
+
+        self.finish_compaction(entries, theta, ordered)
     }
 
     fn finish_compaction<T: SketchEntry>(


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to