1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use vortex::compute::unary::{scalar_at, ScalarAtFn};
use vortex::compute::{slice, ArrayCompute, SliceFn};
use vortex::{Array, IntoArray};
use vortex_dtype::PType;
use vortex_error::VortexResult;
use vortex_scalar::{PrimitiveScalar, Scalar};
use zigzag::ZigZag as ExternalZigZag;

use crate::ZigZagArray;

impl ArrayCompute for ZigZagArray {
    fn scalar_at(&self) -> Option<&dyn ScalarAtFn> {
        Some(self)
    }

    fn slice(&self) -> Option<&dyn SliceFn> {
        Some(self)
    }
}

impl ScalarAtFn for ZigZagArray {
    fn scalar_at(&self, index: usize) -> VortexResult<Scalar> {
        let scalar = scalar_at(&self.encoded(), index)?;
        let pscalar = PrimitiveScalar::try_from(&scalar)?;
        match pscalar.ptype() {
            PType::U8 => Ok(i8::decode(pscalar.typed_value::<u8>().unwrap()).into()),
            PType::U16 => Ok(i16::decode(pscalar.typed_value::<u16>().unwrap()).into()),
            PType::U32 => Ok(i32::decode(pscalar.typed_value::<u32>().unwrap()).into()),
            PType::U64 => Ok(i64::decode(pscalar.typed_value::<u64>().unwrap()).into()),
            _ => unreachable!(),
        }
    }
}

impl SliceFn for ZigZagArray {
    fn slice(&self, start: usize, stop: usize) -> VortexResult<Array> {
        Ok(Self::try_new(slice(&self.encoded(), start, stop)?)?.into_array())
    }
}