hyperloglog_rs/bitor_iter.rs
1use crate::precisions::{Precision, WordType};
2use crate::HyperLogLog;
3use core::hash::Hash;
4use core::ops::{BitOr, BitOrAssign};
5
6impl<
7 Item: Hash,
8 I: IntoIterator<Item = Item>,
9 P: Precision + WordType<BITS>,
10 const BITS: usize,
11 > BitOrAssign<I> for HyperLogLog<P, BITS>
12{
13 #[inline(always)]
14 /// Computes inplace union between an HLL counter and an iterator.
15 ///
16 /// ```rust
17 /// # use hyperloglog_rs::prelude::*;
18 /// # use core::ops::BitOrAssign;
19 ///
20 /// let mut hll = HyperLogLog::<Precision8, 6>::default();
21 ///
22 /// hll |= [1u8, 2u8];
23 ///
24 /// assert!(hll.estimate_cardinality() > 2.0 - 0.1, "The cardinality is {}, we were expecting 2.", hll.estimate_cardinality());
25 /// assert!(hll.estimate_cardinality() < 2.0 + 0.1, "The cardinality is {}, we were expecting 2.", hll.estimate_cardinality());
26 ///
27 /// hll |= [2u8, 3u8];
28 ///
29 /// assert!(hll.estimate_cardinality() > 3.0 - 0.1, "Expected a value equal to around 3, got {}", hll.estimate_cardinality());
30 /// assert!(hll.estimate_cardinality() < 3.0 + 0.1, "Expected a value equal to around 3, got {}", hll.estimate_cardinality());
31 /// ```
32 fn bitor_assign(&mut self, rhs: I) {
33 rhs.into_iter().for_each(|item| {
34 self.insert(item);
35 });
36 }
37}
38
39impl<
40 Item: Hash,
41 I: IntoIterator<Item = Item>,
42 P: Precision + WordType<BITS>,
43 const BITS: usize,
44 > BitOrAssign<I> for &mut HyperLogLog<P, BITS>
45{
46 #[inline(always)]
47 fn bitor_assign(&mut self, rhs: I) {
48 rhs.into_iter().for_each(|item| {
49 self.insert(item);
50 });
51 }
52}
53
54impl<Item: Hash, I: Iterator<Item = Item>, P: Precision + WordType<BITS>, const BITS: usize>
55 BitOr<I> for HyperLogLog<P, BITS>
56{
57 type Output = Self;
58
59 #[inline(always)]
60 /// Computes the union between an HLL counter and an iterator.
61 ///
62 fn bitor(mut self, rhs: I) -> Self {
63 self.bitor_assign(rhs);
64 self
65 }
66}