array_object/
storage.rs

1use crate::misc::Product;
2
3/// The type of the elements.
4#[derive(Debug, Clone, PartialEq)]
5pub enum DataType {
6    UnsignedInteger,
7    SignedInteger,
8    Real,
9    Complex,
10    String,
11}
12
13/// The main array storage with type abstraction.
14/// 
15/// Data is stored uncompressed. Compression is only available in binary form, i.e. when `.pack()` is called.
16#[derive(Debug, Clone, PartialEq)]
17pub struct ArrayObject {
18    pub(crate) data: Vec<u8>,
19    pub(crate) shape: Vec<u64>,
20    pub(crate) datatype: DataType,
21}
22
23impl ArrayObject {
24    /// Returens the minimal size of type, in bits, required to restore the array.
25    pub fn bits(&self) -> Option<usize> {
26        match self.datatype {
27            DataType::UnsignedInteger | DataType::SignedInteger | DataType::Real => {
28                Some(8 * self.data.len() / self.shape.product() as usize)
29            }
30            DataType::Complex => Some(8 * self.data.len() / self.shape.product() as usize / 2),
31            DataType::String => None,
32        }
33    }
34    /// Returns the total number of elements in the array.
35    pub fn len(&self) -> usize {
36        self.shape.product() as usize
37    }
38    /// Returns the shape of the array.
39    pub fn shape(&self) -> Vec<usize> {
40        self.shape.iter().map(|&x| x as usize).collect()
41    }
42    /// Returns the datasize of the array.
43    pub fn datasize(&self) -> usize {
44        self.data.len()
45    }
46    /// Returns the data type.
47    pub fn datatype(&self) -> DataType {
48        self.datatype.clone()
49    }
50    /// Returns the dimension of the array.
51    pub fn dimension(&self) -> usize {
52        self.shape.len()
53    }
54}