Skip to main content

vortex_array/arrays/piecewise_sequence/
array.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use smallvec::smallvec;
5use vortex_error::VortexResult;
6
7use crate::ArrayRef;
8use crate::array::Array;
9use crate::array::ArrayParts;
10use crate::array::EmptyArrayData;
11use crate::array::TypedArrayRef;
12use crate::array_slots;
13use crate::arrays::PiecewiseSequence;
14use crate::dtype::PType;
15
16#[array_slots(PiecewiseSequence)]
17pub struct PiecewiseSequenceSlots {
18    /// The inclusive start index of each sequential piece.
19    #[slot(0)]
20    pub starts: ArrayRef,
21    /// The length of each sequential piece.
22    #[slot(1)]
23    pub lengths: ArrayRef,
24    /// The distance between consecutive indices in each piece.
25    #[slot(2)]
26    pub multipliers: ArrayRef,
27}
28
29/// Extension methods for [`Array`] values using the [`PiecewiseSequence`] encoding.
30pub trait PiecewiseSequenceArrayExt: TypedArrayRef<PiecewiseSequence> {}
31impl<T: TypedArrayRef<PiecewiseSequence>> PiecewiseSequenceArrayExt for T {}
32
33impl Array<PiecewiseSequence> {
34    /// Constructs a new `PiecewiseSequenceArray` from start, length, and multiplier arrays.
35    ///
36    /// This validates only structural invariants: all children must be non-nullable unsigned
37    /// integer arrays with matching lengths, and the outer array length is the declared expanded
38    /// index length. Individual ranges are checked when the index array is executed or consumed by
39    /// a take implementation.
40    pub fn try_new(
41        starts: ArrayRef,
42        lengths: ArrayRef,
43        multipliers: ArrayRef,
44        len: usize,
45    ) -> VortexResult<Self> {
46        Array::try_from_parts(ArrayParts::new(
47            PiecewiseSequence,
48            PType::U64.into(),
49            len,
50            EmptyArrayData,
51            smallvec![Some(starts), Some(lengths), Some(multipliers)],
52        ))
53    }
54
55    /// Constructs a new `PiecewiseSequenceArray` without validation.
56    ///
57    /// # Safety
58    ///
59    /// The caller must guarantee the same structural invariants as [`Self::try_new`].
60    pub unsafe fn new_unchecked(
61        starts: ArrayRef,
62        lengths: ArrayRef,
63        multipliers: ArrayRef,
64        len: usize,
65    ) -> Self {
66        unsafe {
67            Array::from_parts_unchecked(ArrayParts::new(
68                PiecewiseSequence,
69                PType::U64.into(),
70                len,
71                EmptyArrayData,
72                smallvec![Some(starts), Some(lengths), Some(multipliers)],
73            ))
74        }
75    }
76}