apache_datasketches/hll/
union.rs1use crate::error::SketchError;
3use crate::hll::sketch::{HllSketch, TargetHllType};
4use apache_datasketches_sys::hll::ffi as sys;
5use cxx::UniquePtr;
6
7pub struct HllUnion {
8 inner: UniquePtr<sys::HllUnionShim>,
9}
10
11unsafe impl Send for HllUnion {}
12
13impl HllUnion {
14 pub fn new(lg_max_k: u8) -> Result<Self, SketchError> {
15 let inner = sys::new_hll_union(lg_max_k)
16 .map_err(|e| SketchError::InvalidConfig(e.what().to_string()))?;
17 Ok(Self { inner })
18 }
19
20 pub fn update_sketch(&mut self, sketch: &HllSketch) {
21 self.inner.pin_mut().update_sketch(&sketch.inner);
22 }
23
24 pub fn update_u64(&mut self, value: u64) {
25 self.inner.pin_mut().update_u64(value);
26 }
27
28 pub fn update_i64(&mut self, value: i64) {
29 self.inner.pin_mut().update_i64(value);
30 }
31
32 pub fn update_f64(&mut self, value: f64) {
33 self.inner.pin_mut().update_f64(value);
34 }
35
36 pub fn update_str(&mut self, value: &str) {
37 self.inner.pin_mut().update_str(value);
38 }
39
40 pub fn update_bytes(&mut self, value: &[u8]) {
41 self.inner.pin_mut().update_bytes(value);
42 }
43
44 pub fn get_result(&self, tgt_type: TargetHllType) -> HllSketch {
45 let inner = self.inner.get_result(tgt_type.into());
46 HllSketch { inner }
47 }
48
49 pub fn serialize_compact(&self, tgt_type: TargetHllType) -> Vec<u8> {
55 self.inner.serialize_compact(tgt_type.into())
56 }
57
58 pub fn serialize_updatable(&self, tgt_type: TargetHllType) -> Vec<u8> {
61 self.inner.serialize_updatable(tgt_type.into())
62 }
63
64 pub fn get_estimate(&self) -> f64 {
65 self.inner.get_estimate()
66 }
67
68 pub fn get_lower_bound(&self, num_std_dev: u8) -> Result<f64, SketchError> {
69 self.inner
70 .get_lower_bound(num_std_dev)
71 .map_err(|e| SketchError::InvalidConfig(e.what().to_string()))
72 }
73
74 pub fn get_upper_bound(&self, num_std_dev: u8) -> Result<f64, SketchError> {
75 self.inner
76 .get_upper_bound(num_std_dev)
77 .map_err(|e| SketchError::InvalidConfig(e.what().to_string()))
78 }
79
80 pub fn is_empty(&self) -> bool {
81 self.inner.is_empty()
82 }
83
84 pub fn reset(&mut self) {
85 self.inner.pin_mut().reset();
86 }
87}