Skip to content

Commit

Permalink
fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
robert3005 committed May 7, 2024
1 parent aedfeb0 commit bfbfab1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 54 deletions.
5 changes: 3 additions & 2 deletions vortex-array/src/array/chunked/stats.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use vortex_error::VortexResult;

use crate::array::chunked::ChunkedArray;
use crate::stats::{ArrayStatisticsCompute, Stat, StatsSet};
use crate::stats::ArrayStatistics;
use crate::stats::{ArrayStatisticsCompute, Stat, StatsSet};

impl ArrayStatisticsCompute for ChunkedArray<'_> {
fn compute_statistics(&self, stat: Stat) -> VortexResult<StatsSet> {
Expand All @@ -17,6 +17,7 @@ impl ArrayStatisticsCompute for ChunkedArray<'_> {
.reduce(|mut acc, x| {
acc.merge(&x);
acc
}).unwrap_or_default())
})
.unwrap_or_default())
}
}
90 changes: 38 additions & 52 deletions vortex-array/src/stats/statsset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::HashMap;

use enum_iterator::all;
use itertools::Itertools;

use vortex_error::VortexError;
use vortex_scalar::{ListScalarVec, Scalar};

Expand Down Expand Up @@ -35,7 +34,7 @@ impl StatsSet {
self.values.get(&stat)
}

fn get_as<T: for<'a> TryFrom<&'a Scalar, Error=VortexError>>(&self, stat: Stat) -> Option<T> {
fn get_as<T: for<'a> TryFrom<&'a Scalar, Error = VortexError>>(&self, stat: Stat) -> Option<T> {
self.get(stat).map(|v| T::try_from(v).unwrap())
}

Expand Down Expand Up @@ -71,17 +70,14 @@ impl StatsSet {
}

fn merge_ordered<F: Fn(&Scalar, &Scalar) -> bool>(&mut self, stat: Stat, other: &Self, cmp: F) {
match self.values.entry(stat) {
Entry::Occupied(mut e) => {
if let Some(ov) = other.get(stat) {
if cmp(ov, e.get()) {
e.insert(ov.clone());
}
} else {
e.remove();
if let Entry::Occupied(mut e) = self.values.entry(stat) {
if let Some(ov) = other.get(stat) {
if cmp(ov, e.get()) {
e.insert(ov.clone());
}
} else {
e.remove();
}
_ => {}
}
}

Expand Down Expand Up @@ -129,16 +125,13 @@ impl StatsSet {
}

fn merge_scalar_stat(&mut self, other: &Self, stat: Stat) {
match self.values.entry(stat) {
Entry::Occupied(mut e) => {
if let Some(other_value) = other.get_as::<usize>(stat) {
let self_value: usize = e.get().try_into().unwrap();
e.insert((self_value + other_value).into());
} else {
e.remove();
}
if let Entry::Occupied(mut e) = self.values.entry(stat) {
if let Some(other_value) = other.get_as::<usize>(stat) {
let self_value: usize = e.get().try_into().unwrap();
e.insert((self_value + other_value).into());
} else {
e.remove();
}
_ => {}
}
}

Expand All @@ -151,49 +144,43 @@ impl StatsSet {
}

fn merge_freq_stat(&mut self, other: &Self, stat: Stat) {
match self.values.entry(stat) {
Entry::Occupied(mut e) => {
if let Some(other_value) = other.get_as::<ListScalarVec<u64>>(stat) {
// TODO(robert): Avoid the copy here. We could e.get_mut() but need to figure out casting
let self_value: ListScalarVec<u64> = e.get().try_into().unwrap();
e.insert(
ListScalarVec(
self_value
.0
.iter()
.zip_eq(other_value.0.iter())
.map(|(s, o)| *s + *o)
.collect::<Vec<_>>(),
)
.into(),
);
} else {
e.remove();
}
if let Entry::Occupied(mut e) = self.values.entry(stat) {
if let Some(other_value) = other.get_as::<ListScalarVec<u64>>(stat) {
// TODO(robert): Avoid the copy here. We could e.get_mut() but need to figure out casting
let self_value: ListScalarVec<u64> = e.get().try_into().unwrap();
e.insert(
ListScalarVec(
self_value
.0
.iter()
.zip_eq(other_value.0.iter())
.map(|(s, o)| *s + *o)
.collect::<Vec<_>>(),
)
.into(),
);
} else {
e.remove();
}
_ => {}
}
}

/// Merged run count is an upper bound where we assume run is interrupted at the boundary
fn merge_run_count(&mut self, other: &Self) {
match self.values.entry(Stat::RunCount) {
Entry::Occupied(mut e) => {
if let Some(other_value) = other.get_as::<usize>(Stat::RunCount) {
let self_value: usize = e.get().try_into().unwrap();
e.insert((self_value + other_value + 1).into());
} else {
e.remove();
}
if let Entry::Occupied(mut e) = self.values.entry(Stat::RunCount) {
if let Some(other_value) = other.get_as::<usize>(Stat::RunCount) {
let self_value: usize = e.get().try_into().unwrap();
e.insert((self_value + other_value + 1).into());
} else {
e.remove();
}
_ => {}
}
}
}

impl Extend<(Stat, Scalar)> for StatsSet {
#[inline]
fn extend<T: IntoIterator<Item=(Stat, Scalar)>>(&mut self, iter: T) {
fn extend<T: IntoIterator<Item = (Stat, Scalar)>>(&mut self, iter: T) {
self.values.extend(iter)
}
}
Expand Down Expand Up @@ -225,11 +212,10 @@ mod test {
assert_eq!(first.get(Stat::Min), None);
}


#[test]
fn merge_mins() {
let mut first = StatsSet::of(Stat::Min, 37.into());
first.merge(&StatsSet::of(Stat::Min, 42.into()));
assert_eq!(first.get(Stat::Min).cloned(), Some(37.into()));
}
}
}

0 comments on commit bfbfab1

Please sign in to comment.