Skip to main content

vortex_array/arrays/runend/
iter.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::cmp::min;
5
6use vortex_error::vortex_panic;
7
8use crate::dtype::IntegerPType;
9
10/// Yields exclusive run ends relative to a logical slice.
11///
12/// Subtracts `offset` from each end and clamps it to `length`. One end is yielded per input run,
13/// including zero-length runs at the slice boundaries, so indices still match the values child.
14///
15/// # Panics
16///
17/// Panics if `offset` or `length` cannot be represented by `E`, or an input end is before `offset`.
18pub fn slice_relative_ends<E: IntegerPType>(
19    run_ends: &[E],
20    offset: usize,
21    length: usize,
22) -> impl Iterator<Item = usize> + use<'_, E> {
23    let offset_e = E::from_usize(offset).unwrap_or_else(|| {
24        vortex_panic!(
25            "offset {} cannot be converted to {}",
26            offset,
27            std::any::type_name::<E>()
28        )
29    });
30    let length_e = E::from_usize(length).unwrap_or_else(|| {
31        vortex_panic!(
32            "length {} cannot be converted to {}",
33            length,
34            std::any::type_name::<E>()
35        )
36    });
37    run_ends
38        .iter()
39        .copied()
40        .map(move |v| {
41            if v < offset_e {
42                vortex_panic!("run end {v} must be >= offset {offset}");
43            }
44            v - offset_e
45        })
46        .map(move |v| min(v, length_e))
47        .map(|v| v.as_())
48}