Skip to main content

vortex_fastlanes/for/array/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::fmt::Display;
5use std::fmt::Formatter;
6
7use vortex_array::ArrayRef;
8use vortex_array::TypedArrayRef;
9use vortex_array::dtype::PType;
10use vortex_array::scalar::Scalar;
11use vortex_error::VortexExpect as _;
12use vortex_error::VortexResult;
13use vortex_error::vortex_ensure;
14
15pub mod for_compress;
16pub mod for_decompress;
17
18/// The encoded array with the frame-of-reference (minimum value) subtracted.
19pub(super) const ENCODED_SLOT: usize = 0;
20pub(super) const NUM_SLOTS: usize = 1;
21pub(super) const SLOT_NAMES: [&str; NUM_SLOTS] = ["encoded"];
22
23/// Frame of Reference (FoR) encoded array.
24///
25/// This encoding stores values as offsets from a reference value, which can significantly reduce
26/// storage requirements when values are clustered around a specific point.
27#[derive(Clone, Debug)]
28pub struct FoRData {
29    pub(super) reference: Scalar,
30}
31
32pub trait FoRArrayExt: TypedArrayRef<crate::FoR> {
33    fn encoded(&self) -> &ArrayRef {
34        self.as_ref().slots()[ENCODED_SLOT]
35            .as_ref()
36            .vortex_expect("FoRArray encoded slot")
37    }
38
39    fn reference_scalar(&self) -> &Scalar {
40        &self.reference
41    }
42}
43
44impl<T: TypedArrayRef<crate::FoR>> FoRArrayExt for T {}
45
46impl Display for FoRData {
47    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48        write!(f, "reference: {}", self.reference)
49    }
50}
51
52impl FoRData {
53    pub(crate) fn try_new(reference: Scalar) -> VortexResult<Self> {
54        vortex_ensure!(!reference.is_null(), "Reference value cannot be null");
55        vortex_ensure!(
56            reference.dtype().is_int(),
57            "FoR requires an integer reference dtype, got {}",
58            reference.dtype()
59        );
60        Ok(Self { reference })
61    }
62
63    #[inline]
64    pub fn ptype(&self) -> PType {
65        self.reference.dtype().as_ptype()
66    }
67}