Skip to content

Commit

Permalink
Merge pull request #2 from LucaCappelletti94/intersection_mle
Browse files Browse the repository at this point in the history
Resolved code smells
  • Loading branch information
LucaCappelletti94 authored Oct 27, 2023
2 parents 3611ba9 + 452d189 commit 32c01b3
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ jobs:
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
run: cargo test --features=std
- name: Run clippy
run: cargo clippy -- -Dclippy::all -Dclippy::cargo
6 changes: 3 additions & 3 deletions src/hyperloglog_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ impl<
"in the HyperLogLogArray."
));
let mut array = [HyperLogLog::new(); N];
for (i, item) in items.into_iter().enumerate() {
for item in item.into_iter() {
array[i].insert(&item);
for (i, item) in items.iter().enumerate() {
for item in item.iter() {
array[i].insert(item);
}
}
Self { counters: array }
Expand Down
74 changes: 0 additions & 74 deletions src/hyperloglog_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,80 +192,6 @@ pub trait HyperLogLogTrait<PRECISION: Precision + WordType<BITS>, const BITS: us
Self::adjust_estimate(raw_estimate)
}

fn second_order_cardinality_adjustment(&self) -> f32 {
assert!(PRECISION::EXPONENT == 8);
assert!(BITS == 6);
let approximation = self.estimate_cardinality();
let number_of_zeros = self.get_number_of_zero_registers() as f32;
let number_of_zeros_rate =
self.get_number_of_zero_registers() as f32 / PRECISION::NUMBER_OF_REGISTERS as f32;

let weights = [0.9228072, 0.0052479184, 0.16169931];
let bias = 13.130121;

([approximation, number_of_zeros, number_of_zeros_rate]
.iter()
.zip(weights.iter())
.map(|(x, w)| x * w)
.sum::<f32>()
+ bias)
.max(0.0)
}

fn second_order_union_adjustment(&self, other: &Self) -> EstimatedUnionCardinalities<f32> {
assert!(PRECISION::EXPONENT == 8);
assert!(BITS == 6);
let euc: EstimatedUnionCardinalities<f32> = self.estimate_union_and_sets_cardinality(other);
let approximation_left_cardinality = euc.get_left_cardinality();
let approximation_right_cardinality = euc.get_right_cardinality();
let approximation_union_cardinality = euc.get_union_cardinality();
let approximation_intersection_cardinality = euc.get_intersection_cardinality();
let approximation_left_difference = euc.get_left_difference_cardinality();
let approximation_right_difference = euc.get_right_difference_cardinality();
let approximation_symmetric_difference = euc.get_symmetric_difference_cardinality();
let left_number_of_zeros = self.get_number_of_zero_registers();
let right_number_of_zeros = other.get_number_of_zero_registers();

let input = [
approximation_left_cardinality as f32,
approximation_right_cardinality as f32,
approximation_union_cardinality as f32,
approximation_left_difference as f32,
approximation_right_difference as f32,
approximation_symmetric_difference as f32,
approximation_intersection_cardinality as f32,
left_number_of_zeros as f32,
right_number_of_zeros as f32,
];

let weights = [
-0.4552027,
0.87494165,
0.41451707,
1.0906912,
-0.23572926,
-0.13723965,
0.08993981,
-3.9901836,
-1.0626647,
];
let bias = 12.55702;

let adjusted_union = (input
.iter()
.zip(weights.iter())
.map(|(x, w)| x * w)
.sum::<f32>()
+ bias)
.max(0.0);

EstimatedUnionCardinalities::from((
approximation_left_cardinality,
approximation_right_cardinality,
adjusted_union,
))
}

#[inline(always)]
/// Returns an estimate of the cardinality of the union of two HyperLogLog counters.
///
Expand Down

0 comments on commit 32c01b3

Please sign in to comment.