Skip to main content

ariadnetor_tensor/dense/
tensor_data.rs

1//! Convenience constructors and accessors for `DenseTensorData<T>`.
2
3use ariadnetor_core::backend::MemoryOrder;
4
5use crate::{DenseLayout, DenseStorage, TensorData};
6
7/// Backend-less Dense tensor bundle = `TensorData<DenseStorage<T>, DenseLayout>`.
8pub type DenseTensorData<T = f64> = TensorData<DenseStorage<T>, DenseLayout>;
9
10impl<T> DenseTensorData<T> {
11    /// Construct from flat data, shape, and the memory order the
12    /// data is laid out in.
13    ///
14    /// # Panics
15    ///
16    /// Panics if `data.len()` does not equal `shape.iter().product()`.
17    pub fn from_raw_parts(data: Vec<T>, shape: Vec<usize>, order: MemoryOrder) -> Self
18    where
19        T: Clone,
20    {
21        let storage = DenseStorage::new(data);
22        let layout = DenseLayout::new(shape, order);
23        Self::new(storage, layout)
24    }
25
26    /// Reference to the flat data buffer.
27    pub fn data(&self) -> &[T] {
28        self.storage().data()
29    }
30
31    /// Logical shape.
32    pub fn shape(&self) -> &[usize] {
33        self.layout().shape()
34    }
35
36    /// Memory order the flat data is laid out in.
37    pub fn order(&self) -> MemoryOrder {
38        self.layout().order()
39    }
40
41    /// Rank (number of dimensions).
42    pub fn rank(&self) -> usize {
43        self.layout().rank()
44    }
45
46    /// Total number of logical elements (`shape().iter().product()`).
47    pub fn len(&self) -> usize {
48        self.shape().iter().product()
49    }
50
51    /// Whether the tensor has zero logical elements.
52    pub fn is_empty(&self) -> bool {
53        self.len() == 0
54    }
55}