Skip to main content

apache_datasketches/theta/
a_not_b.rs

1use super::input::ThetaInput;
2use super::CompactThetaSketch;
3use apache_datasketches_sys::theta_a_not_b::ffi as sys;
4use apache_datasketches_sys::theta_input::ThetaInputRef;
5use cxx::UniquePtr;
6
7/// Computes the set difference ("A not B": items in `a` but not `b`) of two
8/// theta sketches via [`Self::compute`]. Stateless between calls — unlike
9/// [`super::ThetaUnion`]/[`super::ThetaIntersection`], there is no
10/// accumulation across repeated calls.
11pub struct ThetaAnotB {
12    inner: UniquePtr<sys::ThetaAnotBShim>,
13}
14
15unsafe impl Send for ThetaAnotB {}
16
17impl Default for ThetaAnotB {
18    fn default() -> Self {
19        Self::new()
20    }
21}
22
23impl ThetaAnotB {
24    /// Creates a new, reusable a-not-b calculator.
25    pub fn new() -> Self {
26        Self {
27            inner: sys::new_theta_a_not_b(),
28        }
29    }
30
31    /// Computes the set difference `a - b` (items in `a` that are not in
32    /// `b`) as a [`CompactThetaSketch`]. `a` and `b` may independently be
33    /// any of [`super::ThetaSketch`], [`CompactThetaSketch`], or
34    /// [`super::WrappedCompactThetaSketch`]. If `ordered` is `true`, the
35    /// result's entries are sorted by hash value.
36    pub fn compute(
37        &self,
38        a: &impl ThetaInput,
39        b: &impl ThetaInput,
40        ordered: bool,
41    ) -> CompactThetaSketch {
42        let inner = match (a.as_theta_input(), b.as_theta_input()) {
43            (ThetaInputRef::Sketch(a), ThetaInputRef::Sketch(b)) => {
44                self.inner.compute_sketch_sketch(a, b, ordered)
45            }
46            (ThetaInputRef::Sketch(a), ThetaInputRef::Compact(b)) => {
47                self.inner.compute_sketch_compact(a, b, ordered)
48            }
49            (ThetaInputRef::Sketch(a), ThetaInputRef::Wrapped(b)) => {
50                self.inner.compute_sketch_wrapped(a, b, ordered)
51            }
52            (ThetaInputRef::Compact(a), ThetaInputRef::Sketch(b)) => {
53                self.inner.compute_compact_sketch(a, b, ordered)
54            }
55            (ThetaInputRef::Compact(a), ThetaInputRef::Compact(b)) => {
56                self.inner.compute_compact_compact(a, b, ordered)
57            }
58            (ThetaInputRef::Compact(a), ThetaInputRef::Wrapped(b)) => {
59                self.inner.compute_compact_wrapped(a, b, ordered)
60            }
61            (ThetaInputRef::Wrapped(a), ThetaInputRef::Sketch(b)) => {
62                self.inner.compute_wrapped_sketch(a, b, ordered)
63            }
64            (ThetaInputRef::Wrapped(a), ThetaInputRef::Compact(b)) => {
65                self.inner.compute_wrapped_compact(a, b, ordered)
66            }
67            (ThetaInputRef::Wrapped(a), ThetaInputRef::Wrapped(b)) => {
68                self.inner.compute_wrapped_wrapped(a, b, ordered)
69            }
70        };
71        CompactThetaSketch::from_shim(inner)
72    }
73}