computation_types/run/
matrix.rs

1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2pub struct Matrix<V> {
3    shape: (usize, usize),
4    inner: V,
5}
6
7impl<A> Matrix<Vec<A>> {
8    pub fn from_vec(shape: (usize, usize), vec: Vec<A>) -> Option<Self> {
9        if shape.0 * shape.1 == vec.len() {
10            Some(Matrix { shape, inner: vec })
11        } else {
12            None
13        }
14    }
15}
16
17impl<I> Matrix<I> {
18    pub fn from_iter(shape: (usize, usize), it: I) -> Option<Self>
19    where
20        I: ExactSizeIterator,
21    {
22        if shape.0 * shape.1 == it.len() {
23            Some(Matrix { shape, inner: it })
24        } else {
25            None
26        }
27    }
28}
29
30impl<V> Matrix<V> {
31    /// # Safety
32    ///
33    /// This function is safe
34    /// if `inner` implements `IntoIterator`
35    /// and produces a number of items
36    /// equal to `shape.0 * shape.1`.
37    pub unsafe fn new_unchecked(shape: (usize, usize), inner: V) -> Self {
38        Matrix { shape, inner }
39    }
40
41    pub fn shape(&self) -> (usize, usize) {
42        self.shape
43    }
44
45    pub fn inner(&self) -> &V {
46        &self.inner
47    }
48
49    pub fn into_inner(self) -> V {
50        self.inner
51    }
52}
53
54impl<V> IntoIterator for Matrix<V>
55where
56    V: IntoIterator,
57{
58    type Item = V::Item;
59
60    type IntoIter = V::IntoIter;
61
62    fn into_iter(self) -> Self::IntoIter {
63        self.inner.into_iter()
64    }
65}