Skip to content

Commit

Permalink
Reduced trait requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaCappelletti94 committed Dec 2, 2023
1 parent a819de8 commit 764d483
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 93 deletions.
10 changes: 5 additions & 5 deletions src/estimated_union_cardinalities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<F: MaxMin + One + Add<F, Output = F> + Sub<F, Output = F> + Div<F, Output =
///
/// ```
pub fn get_left_difference_cardinality(&self) -> F {
self.left_cardinality - self.get_intersection_cardinality()
self.union_cardinality - self.right_cardinality
}

#[inline(always)]
Expand All @@ -139,7 +139,7 @@ impl<F: MaxMin + One + Add<F, Output = F> + Sub<F, Output = F> + Div<F, Output =
///
/// ```
pub fn get_right_difference_cardinality(&self) -> F {
self.right_cardinality - self.get_intersection_cardinality()
self.union_cardinality - self.left_cardinality
}

#[inline(always)]
Expand All @@ -158,9 +158,9 @@ impl<F: MaxMin + One + Add<F, Output = F> + Sub<F, Output = F> + Div<F, Output =
///
/// ```
pub fn get_symmetric_difference_cardinality(&self) -> 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)]
Expand Down
8 changes: 4 additions & 4 deletions src/exact_hyper_spheres_sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashSet;

impl<I, C> SetLike<C> for HashSet<I>
where
C: Primitive<usize>,
C: Primitive<f32>,
I: Eq + Hash,
{
fn get_estimated_union_cardinality(
Expand All @@ -15,14 +15,14 @@ where
other: &Self,
other_cardinality: C,
) -> EstimatedUnionCardinalities<C> {
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<C: Eq + Hash, I: Primitive<usize> + One> HyperSpheresSketch<I> for HashSet<C> {}
impl<C: Eq + Hash, I: Primitive<f32> + One> HyperSpheresSketch<I> for HashSet<C> {}
84 changes: 0 additions & 84 deletions src/hyper_spheres_sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<const L: usize, const R: usize>(
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<I> = 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<PRECISION: Precision + WordType<BITS>, const BITS: usize, I: Primitive<f32>>
Expand Down
1 change: 1 addition & 0 deletions src/joint_estimation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 764d483

Please sign in to comment.