use vortex_error::VortexResult;
use vortex_error::vortex_err;
use crate::ExecutionCtx;
use crate::ProbeState;
use crate::arrays::runend::RunEnd;
use crate::arrays::runend::RunEndArrayExt;
use crate::arrays::runend::RunEndSlots;
use crate::scalar::Scalar;
pub(crate) fn scalar_at(
state: &mut ProbeState<'_, RunEnd>,
index: usize,
ctx: &mut ExecutionCtx,
) -> VortexResult<Scalar> {
let array = state.array();
let logical_index = array
.offset()
.checked_add(index)
.ok_or_else(|| vortex_err!("RunEnd logical index overflow"))?;
let mut ends = state
.slot(RunEndSlots::ENDS)?
.ok_or_else(|| vortex_err!("RunEnd ends slot is missing"))?;
let mut left = 0;
let mut right = ends.array().len();
while left < right {
let mid = left + (right - left) / 2;
let end = usize::try_from(&ends.execute_scalar(mid, ctx)?)?;
if end <= logical_index {
left = mid + 1;
} else {
right = mid;
}
}
state
.slot(RunEndSlots::VALUES)?
.ok_or_else(|| vortex_err!("RunEnd values slot is missing"))?
.execute_scalar(left, ctx)
}
#[cfg(test)]
mod tests;