Skip to main content

apache_datasketches/tuple/generic/
jaccard.rs

1use super::{TupleInput, TupleSummary};
2use crate::tuple::JaccardBounds;
3use apache_datasketches_sys::tuple_generic_input::TupleGenericInputRef;
4use apache_datasketches_sys::tuple_generic_jaccard::ffi as sys;
5
6impl From<sys::TupleGenericJaccardBoundsFfi> for JaccardBounds {
7    fn from(ffi: sys::TupleGenericJaccardBoundsFfi) -> Self {
8        Self {
9            lower_bound: ffi.lower_bound,
10            estimate: ffi.estimate,
11            upper_bound: ffi.upper_bound,
12        }
13    }
14}
15
16/// Estimates the Jaccard index (intersection-over-union) of two generic
17/// Tuple sketches.
18///
19/// Only the keys affect the result — per-entry summaries do not, and no
20/// summary callback influences the returned bounds.
21pub fn tuple_jaccard_similarity<S: TupleSummary>(
22    a: &impl TupleInput<S>,
23    b: &impl TupleInput<S>,
24) -> JaccardBounds {
25    let ffi = match (a.as_input(), b.as_input()) {
26        (TupleGenericInputRef::Sketch(a), TupleGenericInputRef::Sketch(b)) => {
27            sys::tuple_generic_jaccard_sketch_sketch(a, b)
28        }
29        (TupleGenericInputRef::Sketch(a), TupleGenericInputRef::Compact(b)) => {
30            sys::tuple_generic_jaccard_sketch_compact(a, b)
31        }
32        (TupleGenericInputRef::Compact(a), TupleGenericInputRef::Sketch(b)) => {
33            sys::tuple_generic_jaccard_compact_sketch(a, b)
34        }
35        (TupleGenericInputRef::Compact(a), TupleGenericInputRef::Compact(b)) => {
36            sys::tuple_generic_jaccard_compact_compact(a, b)
37        }
38    };
39    ffi.into()
40}