hawkingrei commented on code in PR #142: URL: https://github.com/apache/datasketches-rust/pull/142#discussion_r3698268669
########## datasketches/src/thetacommon/jaccard_similarity.rs: ########## @@ -0,0 +1,202 @@ +// 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 crate::common::ResizeFactor; +use crate::error::Error; +use crate::thetacommon::RetainedEntry; +use crate::thetacommon::ThetaFamilySketchView; +use crate::thetacommon::bounds_binomial_proportions; +use crate::thetacommon::constants::MAX_LG_K; +use crate::thetacommon::constants::MAX_THETA; +use crate::thetacommon::constants::MIN_LG_K; +use crate::thetacommon::intersection::IntersectionMergePolicy; +use crate::thetacommon::intersection::IntersectionState; +use crate::thetacommon::union::UnionMergePolicy; +use crate::thetacommon::union::UnionState; + +const NUM_STD_DEVS: f64 = 2.0; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) struct RawThetaJaccardSimilarity { + pub(crate) lower_bound: f64, + pub(crate) estimate: f64, + pub(crate) upper_bound: f64, +} + +#[derive(Debug)] +struct NoopMergePolicy; + +impl<E: RetainedEntry> UnionMergePolicy<E> for NoopMergePolicy { + fn merge(&self, _existing: &mut E, _incoming: E) {} +} + +impl<E: RetainedEntry> IntersectionMergePolicy<E> for NoopMergePolicy { + fn merge(&self, _existing: &mut E, _incoming: E) {} +} + +struct CompactSketchView<E> { + entries: Vec<E>, + theta: u64, + seed_hash: u16, + ordered: bool, + empty: bool, +} + +impl<E: RetainedEntry + Clone> ThetaFamilySketchView for CompactSketchView<E> { + type Entry = E; + + fn seed_hash(&self) -> u16 { + self.seed_hash + } + + fn theta64(&self) -> u64 { + self.theta + } + + fn is_empty(&self) -> bool { + self.empty + } + + fn is_ordered(&self) -> bool { + self.ordered + } + + fn iter(&self) -> impl Iterator<Item = E> + '_ { + self.entries.iter().cloned() + } + + fn num_retained(&self) -> usize { + self.entries.len() + } +} + +impl RawThetaJaccardSimilarity { + pub(crate) fn compute<A, B>(sketch_a: &A, sketch_b: &B, seed: u64) -> Result<Self, Error> + where + A: ThetaFamilySketchView, + B: ThetaFamilySketchView<Entry = A::Entry>, + A::Entry: Clone, + { + if sketch_a.is_empty() && sketch_b.is_empty() { + return Ok(Self::exact(1.0)); + } + if sketch_a.is_empty() || sketch_b.is_empty() { + return Ok(Self::exact(0.0)); + } + + let mut union = UnionState::new( + union_lg_k(sketch_a.num_retained(), sketch_b.num_retained()), + ResizeFactor::X8, + 1.0, + seed, + NoopMergePolicy, + ); + union.update(sketch_a)?; + union.update(sketch_b)?; + let union = union.to_compact_parts(false); + + if union.entries.len() == sketch_a.num_retained() + && union.entries.len() == sketch_b.num_retained() + && union.theta == sketch_a.theta64() + && union.theta == sketch_b.theta64() Review Comment: Fixed in aeec2fd. The exact-equality fast path now requires a non-empty retained union, so logically non-empty sketches with zero retained entries fall through to the conservative [0.0, 0.5, 1.0] result. Added deterministic regressions for both Theta and Tuple sketches. ########## datasketches/tests/theta_test/jaccard_similarity.rs: ########## @@ -0,0 +1,145 @@ +// 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. + +#![cfg(feature = "theta")] Review Comment: Removed the redundant feature gate in aeec2fd. -- 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]
