pub struct CpcSketchBuilder { /* private fields */ }Expand description
Builder for crate::cpc::CpcSketch, mirroring upstream’s
cpc_sketch_alloc constructor. lg_k defaults to 11
(cpc_constants::DEFAULT_LG_K). The seed is never exposed — every
sketch built by this crate always uses upstream’s DEFAULT_SEED.
Implementations§
Source§impl CpcSketchBuilder
impl CpcSketchBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new builder with the default lg_k (11).
Examples found in repository?
examples/cpc.rs (line 13)
8fn main() {
9 // A sketch estimates the number of distinct items seen, using bounded
10 // memory regardless of how many items are added. `lg_k` (4..=26)
11 // trades memory for accuracy: higher values are more accurate but use
12 // more space.
13 let mut visitors_day1 = CpcSketchBuilder::new()
14 .lg_k(11)
15 .build()
16 .expect("valid lg_k");
17 for id in 0..10_000u64 {
18 visitors_day1.update_u64(id);
19 }
20
21 let mut visitors_day2 = CpcSketchBuilder::new()
22 .lg_k(11)
23 .build()
24 .expect("valid lg_k");
25 for id in 5_000..15_000u64 {
26 visitors_day2.update_u64(id);
27 }
28
29 println!(
30 "Day 1 unique visitors (estimate): {:.0}",
31 visitors_day1.get_estimate()
32 );
33 println!(
34 "Day 2 unique visitors (estimate): {:.0}",
35 visitors_day2.get_estimate()
36 );
37 println!(
38 "Day 1, 95% confidence interval: [{:.0}, {:.0}]",
39 visitors_day1.get_lower_bound(2).unwrap(),
40 visitors_day1.get_upper_bound(2).unwrap()
41 );
42
43 // CpcUnion merges multiple sketches into one, e.g. combining per-day
44 // counts into a total distinct count across both days.
45 let mut union = CpcUnionBuilder::new().lg_k(11).build().expect("valid lg_k");
46 union.update(&visitors_day1);
47 union.update(&visitors_day2);
48 let total_unique = union.get_result();
49 println!(
50 "Total unique visitors across both days (true count = 15000): {:.0}",
51 total_unique.get_estimate()
52 );
53
54 // Sketches can be serialized to bytes (e.g. to store or send over the
55 // network) and reconstructed later. CPC's serialized form is always
56 // compressed, so there's a single serialize()/deserialize() pair
57 // (unlike Theta, which has separate compressed/uncompressed formats).
58 let bytes = visitors_day1.serialize();
59 let restored = CpcSketch::deserialize(&bytes).expect("valid sketch bytes");
60 println!(
61 "serialized day-1 sketch to {} bytes and restored successfully (estimate {:.0})",
62 bytes.len(),
63 restored.get_estimate()
64 );
65}Sourcepub fn lg_k(self, lg_k: u8) -> Self
pub fn lg_k(self, lg_k: u8) -> Self
Sets the base-2 log of the number of bins in the sketch (4..=26).
Examples found in repository?
examples/cpc.rs (line 14)
8fn main() {
9 // A sketch estimates the number of distinct items seen, using bounded
10 // memory regardless of how many items are added. `lg_k` (4..=26)
11 // trades memory for accuracy: higher values are more accurate but use
12 // more space.
13 let mut visitors_day1 = CpcSketchBuilder::new()
14 .lg_k(11)
15 .build()
16 .expect("valid lg_k");
17 for id in 0..10_000u64 {
18 visitors_day1.update_u64(id);
19 }
20
21 let mut visitors_day2 = CpcSketchBuilder::new()
22 .lg_k(11)
23 .build()
24 .expect("valid lg_k");
25 for id in 5_000..15_000u64 {
26 visitors_day2.update_u64(id);
27 }
28
29 println!(
30 "Day 1 unique visitors (estimate): {:.0}",
31 visitors_day1.get_estimate()
32 );
33 println!(
34 "Day 2 unique visitors (estimate): {:.0}",
35 visitors_day2.get_estimate()
36 );
37 println!(
38 "Day 1, 95% confidence interval: [{:.0}, {:.0}]",
39 visitors_day1.get_lower_bound(2).unwrap(),
40 visitors_day1.get_upper_bound(2).unwrap()
41 );
42
43 // CpcUnion merges multiple sketches into one, e.g. combining per-day
44 // counts into a total distinct count across both days.
45 let mut union = CpcUnionBuilder::new().lg_k(11).build().expect("valid lg_k");
46 union.update(&visitors_day1);
47 union.update(&visitors_day2);
48 let total_unique = union.get_result();
49 println!(
50 "Total unique visitors across both days (true count = 15000): {:.0}",
51 total_unique.get_estimate()
52 );
53
54 // Sketches can be serialized to bytes (e.g. to store or send over the
55 // network) and reconstructed later. CPC's serialized form is always
56 // compressed, so there's a single serialize()/deserialize() pair
57 // (unlike Theta, which has separate compressed/uncompressed formats).
58 let bytes = visitors_day1.serialize();
59 let restored = CpcSketch::deserialize(&bytes).expect("valid sketch bytes");
60 println!(
61 "serialized day-1 sketch to {} bytes and restored successfully (estimate {:.0})",
62 bytes.len(),
63 restored.get_estimate()
64 );
65}Sourcepub fn build(self) -> Result<CpcSketch, SketchError>
pub fn build(self) -> Result<CpcSketch, SketchError>
Builds the sketch. Returns SketchError::InvalidConfig if lg_k
is out of range.
Examples found in repository?
examples/cpc.rs (line 15)
8fn main() {
9 // A sketch estimates the number of distinct items seen, using bounded
10 // memory regardless of how many items are added. `lg_k` (4..=26)
11 // trades memory for accuracy: higher values are more accurate but use
12 // more space.
13 let mut visitors_day1 = CpcSketchBuilder::new()
14 .lg_k(11)
15 .build()
16 .expect("valid lg_k");
17 for id in 0..10_000u64 {
18 visitors_day1.update_u64(id);
19 }
20
21 let mut visitors_day2 = CpcSketchBuilder::new()
22 .lg_k(11)
23 .build()
24 .expect("valid lg_k");
25 for id in 5_000..15_000u64 {
26 visitors_day2.update_u64(id);
27 }
28
29 println!(
30 "Day 1 unique visitors (estimate): {:.0}",
31 visitors_day1.get_estimate()
32 );
33 println!(
34 "Day 2 unique visitors (estimate): {:.0}",
35 visitors_day2.get_estimate()
36 );
37 println!(
38 "Day 1, 95% confidence interval: [{:.0}, {:.0}]",
39 visitors_day1.get_lower_bound(2).unwrap(),
40 visitors_day1.get_upper_bound(2).unwrap()
41 );
42
43 // CpcUnion merges multiple sketches into one, e.g. combining per-day
44 // counts into a total distinct count across both days.
45 let mut union = CpcUnionBuilder::new().lg_k(11).build().expect("valid lg_k");
46 union.update(&visitors_day1);
47 union.update(&visitors_day2);
48 let total_unique = union.get_result();
49 println!(
50 "Total unique visitors across both days (true count = 15000): {:.0}",
51 total_unique.get_estimate()
52 );
53
54 // Sketches can be serialized to bytes (e.g. to store or send over the
55 // network) and reconstructed later. CPC's serialized form is always
56 // compressed, so there's a single serialize()/deserialize() pair
57 // (unlike Theta, which has separate compressed/uncompressed formats).
58 let bytes = visitors_day1.serialize();
59 let restored = CpcSketch::deserialize(&bytes).expect("valid sketch bytes");
60 println!(
61 "serialized day-1 sketch to {} bytes and restored successfully (estimate {:.0})",
62 bytes.len(),
63 restored.get_estimate()
64 );
65}Trait Implementations§
Source§impl Clone for CpcSketchBuilder
impl Clone for CpcSketchBuilder
Source§fn clone(&self) -> CpcSketchBuilder
fn clone(&self) -> CpcSketchBuilder
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl Copy for CpcSketchBuilder
Source§impl Debug for CpcSketchBuilder
impl Debug for CpcSketchBuilder
Auto Trait Implementations§
impl Freeze for CpcSketchBuilder
impl RefUnwindSafe for CpcSketchBuilder
impl Send for CpcSketchBuilder
impl Sync for CpcSketchBuilder
impl Unpin for CpcSketchBuilder
impl UnsafeUnpin for CpcSketchBuilder
impl UnwindSafe for CpcSketchBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more