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::array_slots;
10use vortex_array::dtype::PType;
11use vortex_array::scalar::Scalar;
12use vortex_error::VortexResult;
13use vortex_error::vortex_ensure;
14
15pub mod for_compress;
16pub mod for_decompress;
17
18#[array_slots(crate::FoR)]
19pub struct FoRSlots {
20    /// The encoded array with the frame-of-reference (minimum value) subtracted.
21    pub encoded: ArrayRef,
22}
23
24/// Frame of Reference (FoR) encoded array.
25///
26/// This encoding stores values as offsets from a reference value, which can significantly reduce
27/// storage requirements when values are clustered around a specific point.
28#[derive(Clone, Debug)]
29pub struct FoRData {
30    pub(super) reference: Scalar,
31}
32
33pub trait FoRArrayExt: FoRArraySlotsExt {
34    fn reference_scalar(&self) -> &Scalar {
35        &self.reference
36    }
37}
38
39impl<T: TypedArrayRef<crate::FoR>> FoRArrayExt for T {}
40
41impl Display for FoRData {
42    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
43        write!(f, "reference: {}", self.reference)
44    }
45}
46
47impl FoRData {
48    pub(crate) fn try_new(reference: Scalar) -> VortexResult<Self> {
49        vortex_ensure!(!reference.is_null(), "Reference value cannot be null");
50        vortex_ensure!(
51            reference.dtype().is_int(),
52            "FoR requires an integer reference dtype, got {}",
53            reference.dtype()
54        );
55        Ok(Self { reference })
56    }
57
58    #[inline]
59    pub fn ptype(&self) -> PType {
60        self.reference.dtype().as_ptype()
61    }
62}