diff --git a/src/estimated_union_cardinalities.rs b/src/estimated_union_cardinalities.rs index 36ff55a..881094e 100644 --- a/src/estimated_union_cardinalities.rs +++ b/src/estimated_union_cardinalities.rs @@ -120,7 +120,7 @@ impl + Sub + Div F { - self.left_cardinality - self.get_intersection_cardinality() + self.union_cardinality - self.right_cardinality } #[inline(always)] @@ -139,7 +139,7 @@ impl + Sub + Div F { - self.right_cardinality - self.get_intersection_cardinality() + self.union_cardinality - self.left_cardinality } #[inline(always)] @@ -158,9 +158,9 @@ impl + Sub + Div F { - self.left_cardinality + self.right_cardinality - - self.get_intersection_cardinality() - - self.get_intersection_cardinality() + self.union_cardinality + self.union_cardinality + - self.left_cardinality + - self.right_cardinality } #[inline(always)] diff --git a/src/exact_hyper_spheres_sketch.rs b/src/exact_hyper_spheres_sketch.rs index 672b19e..e9f7c90 100644 --- a/src/exact_hyper_spheres_sketch.rs +++ b/src/exact_hyper_spheres_sketch.rs @@ -6,7 +6,7 @@ use std::collections::HashSet; impl SetLike for HashSet where - C: Primitive, + C: Primitive, I: Eq + Hash, { fn get_estimated_union_cardinality( @@ -15,14 +15,14 @@ where other: &Self, other_cardinality: C, ) -> EstimatedUnionCardinalities { - let union_cardinality = C::reverse(self.union(other).count()); + let union_cardinality = C::reverse(self.union(other).count() as f32); EstimatedUnionCardinalities::from((self_cardinality, other_cardinality, union_cardinality)) } fn get_cardinality(&self) -> C { - C::reverse(self.len()) + C::reverse(self.len() as f32) } } -impl + One> HyperSpheresSketch for HashSet {} +impl + One> HyperSpheresSketch for HashSet {} diff --git a/src/hyper_spheres_sketch.rs b/src/hyper_spheres_sketch.rs index 4117606..db5fa09 100644 --- a/src/hyper_spheres_sketch.rs +++ b/src/hyper_spheres_sketch.rs @@ -184,90 +184,6 @@ where right_difference_cardinality_vector, ) } - - /// Returns the overlap cardinality matrices and outer difference shells cardinality of two lists of sets. - /// - /// # Arguments - /// * `left` - The first list of sets. - /// * `right` - The second list of sets. - /// - /// # Returns - /// * `overlap_cardinality_matrix` - Matrix of estimated overlapping cardinalities between the elements of the left and right arrays. - /// * `left_difference_cardinality_vector` - Vector of estimated difference cardinalities between the elements of the left array and the last element of the right array. - /// * `right_difference_cardinality_vector` - Vector of estimated difference cardinalities between the elements of the right array and the last element of the left array. - /// - /// # Implementative details - /// We expect the elements of the left and right arrays to be increasingly contained in the next one. - /// - /// # Examples - /// In the following illustration, we show that for two vectors left and right of three elements, - /// we expect to compute the exclusively overlap matrix $A_{ij}$ and the exclusively differences vectors $B_i$. - /// - /// ![Illustration of overlaps](https://github.com/LucaCappelletti94/hyperloglog-rs/blob/main/triple_overlap.png?raw=true) - /// - /// Very similarly, for the case of vectors of two elements: - /// - /// ![Illustration of overlaps](https://github.com/LucaCappelletti94/hyperloglog-rs/blob/main/tuple_overlap.png?raw=true) - /// - fn overlap_matrix_and_outer_difference_shells_cardinality( - left: &[Self; L], - right: &[Self; R], - ) -> ([[I; R]; L], I, I) { - // Initialize overlap and differences cardinality matrices/vectors. - let mut last_row = [I::default(); R]; - let mut differential_overlap_cardinality_matrix = [[I::default(); R]; L]; - let mut left_difference_outer_shell = I::default(); - let mut right_difference_outer_shell = I::default(); - let mut right_cardinalities = [I::default(); R]; - - right.iter().zip(right_cardinalities.iter_mut()).for_each( - |(right_hll, right_cardinality)| { - *right_cardinality = right_hll.get_cardinality(); - }, - ); - - let mut euc: EstimatedUnionCardinalities = EstimatedUnionCardinalities::default(); - let mut last_left_difference: I = I::default(); - - // Populate the overlap cardinality matrix. - for (i, left_hll) in left.iter().enumerate() { - let mut last_right_difference: I = I::default(); - let left_cardinality = left_hll.get_cardinality(); - let mut comulative_row = I::default(); - for (j, (right_hll, right_cardinality)) in - right.iter().zip(right_cardinalities).enumerate() - { - euc = left_hll.get_estimated_union_cardinality( - left_cardinality, - right_hll, - right_cardinality, - ); - let delta = last_row[j] + comulative_row; - differential_overlap_cardinality_matrix[i][j] = - (euc.get_intersection_cardinality() - delta).get_max(I::default()); - last_row[j] = euc.get_intersection_cardinality().get_max(delta); - comulative_row += differential_overlap_cardinality_matrix[i][j]; - - // We always set the value of the right difference so that the - // last time we write this will necessarily be with the last - // and largest left set. - let this_difference = euc.get_right_difference_cardinality(); - right_difference_outer_shell = - (this_difference - last_right_difference).get_max(I::default()); - last_right_difference = this_difference; - } - let this_difference = euc.get_left_difference_cardinality(); - left_difference_outer_shell = - (this_difference - last_left_difference).get_max(I::default()); - last_left_difference = this_difference; - } - - ( - differential_overlap_cardinality_matrix, - left_difference_outer_shell, - right_difference_outer_shell, - ) - } } impl, const BITS: usize, I: Primitive> diff --git a/src/joint_estimation.rs b/src/joint_estimation.rs index 75f32d5..af6439a 100644 --- a/src/joint_estimation.rs +++ b/src/joint_estimation.rs @@ -217,6 +217,7 @@ where PRECISION::NUMBER_OF_REGISTERS as f32 * x } + #[inline] /// Returns estimated intersection cardinality object based on MLE joint cardinality estimation. /// /// # References