Skip to main content

vortex_array/arrays/slice/
array.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::ops::Range;
5
6use vortex_error::VortexExpect;
7use vortex_error::VortexResult;
8use vortex_error::vortex_panic;
9
10use crate::ArrayRef;
11use crate::stats::ArrayStats;
12
13#[derive(Clone, Debug)]
14pub struct SliceArray {
15    pub(super) child: ArrayRef,
16    pub(super) range: Range<usize>,
17    pub(super) stats: ArrayStats,
18}
19
20pub struct SliceArrayParts {
21    pub child: ArrayRef,
22    pub range: Range<usize>,
23}
24
25impl SliceArray {
26    pub fn try_new(child: ArrayRef, range: Range<usize>) -> VortexResult<Self> {
27        if range.end > child.len() {
28            vortex_panic!(
29                "SliceArray range out of bounds: range {:?} exceeds child array length {}",
30                range,
31                child.len()
32            );
33        }
34        Ok(Self {
35            child,
36            range,
37            stats: ArrayStats::default(),
38        })
39    }
40
41    pub fn new(child: ArrayRef, range: Range<usize>) -> Self {
42        Self::try_new(child, range).vortex_expect("failed")
43    }
44
45    /// The range used to slice the child array.
46    pub fn slice_range(&self) -> &Range<usize> {
47        &self.range
48    }
49
50    /// The child array being sliced.
51    pub fn child(&self) -> &ArrayRef {
52        &self.child
53    }
54
55    /// Consume the slice array and return its components.
56    pub fn into_parts(self) -> SliceArrayParts {
57        SliceArrayParts {
58            child: self.child,
59            range: self.range,
60        }
61    }
62}