use std::ops::Range;
use num_traits::AsPrimitive;
use num_traits::ToPrimitive;
use vortex_error::VortexExpect;
use vortex_mask::Mask;
use vortex_mask::MaskValues;
use crate::dtype::NativePType;
use crate::dtype::UnsignedPType;
pub(super) fn add_unsigned_run<T: AsPrimitive<u64>>(sum: u64, value: T, len: usize) -> Option<u64> {
value
.as_()
.checked_mul(len as u64)
.and_then(|product| sum.checked_add(product))
}
pub(super) fn add_signed_run<T: AsPrimitive<i64>>(sum: i64, value: T, len: usize) -> Option<i64> {
let product = i128::from(value.as_()) * len as i128;
i64::try_from(i128::from(sum) + product).ok()
}
pub(super) fn add_float_run<T: NativePType>(
sum: f64,
value: T,
len: usize,
skip_nans: bool,
) -> Option<f64> {
if skip_nans && value.is_nan() {
return Some(sum);
}
let value = ToPrimitive::to_f64(&value).vortex_expect("Float values fit in f64");
Some(value.mul_add(len as f64, sum))
}
pub(super) fn sum_range<E: UnsignedPType, T: NativePType, A: NativePType>(
ends: &[E],
values: &[T],
validity: &Mask,
range: Range<usize>,
add_run: impl Fn(A, T, usize) -> Option<A>,
) -> (Option<A>, bool) {
if range.is_empty() {
return (Some(A::default()), true);
}
match validity {
Mask::AllTrue(_) => sum_all_valid(ends, values, range, add_run),
Mask::AllFalse(_) => (Some(A::default()), true),
Mask::Values(validity) => sum_partially_valid(ends, values, validity, range, add_run),
}
}
fn sum_all_valid<E: UnsignedPType, T: NativePType, A: NativePType>(
ends: &[E],
values: &[T],
range: Range<usize>,
add_run: impl Fn(A, T, usize) -> Option<A>,
) -> (Option<A>, bool) {
let mut sum = A::default();
let first = ends.partition_point(|end| end.as_() <= range.start);
let mut start = range.start;
for (&end, &value) in ends[first..].iter().zip(&values[first..]) {
let end = end.as_();
if end >= range.end {
return (add_run(sum, value, range.end - start), false);
}
let Some(next) = add_run(sum, value, end - start) else {
return (None, false);
};
sum = next;
start = end;
}
(Some(sum), false)
}
fn sum_partially_valid<E: UnsignedPType, T: NativePType, A: NativePType>(
ends: &[E],
values: &[T],
validity: &MaskValues,
range: Range<usize>,
add_run: impl Fn(A, T, usize) -> Option<A>,
) -> (Option<A>, bool) {
if let Some(indices) = validity.cached_indices() {
let start = indices.partition_point(|&index| ends[index].as_() <= range.start);
return sum_indexed_runs(
ends,
values,
indices[start..].iter().copied(),
range,
add_run,
);
}
let first = ends.partition_point(|end| end.as_() <= range.start);
let last = ends.partition_point(|end| end.as_() < range.end);
let bits = validity.bit_buffer().slice(first..=last);
let indices = bits.set_indices().map(|index| first + index);
sum_indexed_runs(ends, values, indices, range, add_run)
}
fn sum_indexed_runs<E: UnsignedPType, T: NativePType, A: NativePType>(
ends: &[E],
values: &[T],
indices: impl Iterator<Item = usize>,
range: Range<usize>,
add_run: impl Fn(A, T, usize) -> Option<A>,
) -> (Option<A>, bool) {
let mut sum = A::default();
let mut is_empty = true;
for index in indices {
let end = ends[index].as_();
let run_start = if index == 0 { 0 } else { ends[index - 1].as_() };
let start = run_start.max(range.start);
if start >= range.end {
break;
}
let overlap_len = end.min(range.end) - start;
is_empty = false;
let Some(next) = add_run(sum, values[index], overlap_len) else {
return (None, false);
};
sum = next;
if end >= range.end {
break;
}
}
(Some(sum), is_empty)
}