Skip to main content

ferray_core/array/
view_mut.rs

1// ferray-core: Mutable array view — ArrayViewMut<'a, T, D> (REQ-3)
2
3use crate::dimension::Dimension;
4use crate::dtype::Element;
5use crate::layout::MemoryLayout;
6
7use super::ArrayFlags;
8use super::owned::Array;
9
10/// A mutable, borrowed view into an existing array's data.
11///
12/// This is a zero-copy mutable slice. The lifetime `'a` ties this view
13/// to the source array, and Rust's borrow checker ensures exclusivity.
14pub struct ArrayViewMut<'a, T: Element, D: Dimension> {
15    pub(crate) inner: ndarray::ArrayViewMut<'a, T, D::NdarrayDim>,
16    pub(crate) dim: D,
17}
18
19impl<'a, T: Element, D: Dimension> ArrayViewMut<'a, T, D> {
20    /// Create from an ndarray mutable view. Crate-internal.
21    pub(crate) fn from_ndarray(inner: ndarray::ArrayViewMut<'a, T, D::NdarrayDim>) -> Self {
22        let dim = D::from_ndarray_dim(&inner.raw_dim());
23        Self { inner, dim }
24    }
25
26    /// Shape as a slice.
27    #[inline]
28    pub fn shape(&self) -> &[usize] {
29        self.inner.shape()
30    }
31
32    /// Number of dimensions.
33    #[inline]
34    pub fn ndim(&self) -> usize {
35        self.dim.ndim()
36    }
37
38    /// Total number of elements.
39    #[inline]
40    pub fn size(&self) -> usize {
41        self.inner.len()
42    }
43
44    /// Whether the view has zero elements.
45    #[inline]
46    pub fn is_empty(&self) -> bool {
47        self.inner.is_empty()
48    }
49
50    /// Strides as a slice.
51    #[inline]
52    pub fn strides(&self) -> &[isize] {
53        self.inner.strides()
54    }
55
56    /// Raw pointer to the first element.
57    #[inline]
58    pub fn as_ptr(&self) -> *const T {
59        self.inner.as_ptr()
60    }
61
62    /// Mutable raw pointer to the first element.
63    #[inline]
64    pub fn as_mut_ptr(&mut self) -> *mut T {
65        self.inner.as_mut_ptr()
66    }
67
68    /// Try to get a contiguous slice.
69    pub fn as_slice(&self) -> Option<&[T]> {
70        self.inner.as_slice()
71    }
72
73    /// Try to get a contiguous mutable slice.
74    pub fn as_slice_mut(&mut self) -> Option<&mut [T]> {
75        self.inner.as_slice_mut()
76    }
77
78    /// Memory layout.
79    pub fn layout(&self) -> MemoryLayout {
80        crate::layout::classify_layout(
81            self.inner.is_standard_layout(),
82            self.dim.as_slice(),
83            self.inner.strides(),
84        )
85    }
86
87    /// Return a reference to the internal dimension descriptor.
88    #[inline]
89    pub fn dim(&self) -> &D {
90        &self.dim
91    }
92
93    /// Convert to a flat `Vec<T>` in logical (row-major) order.
94    pub fn to_vec_flat(&self) -> Vec<T> {
95        self.inner.iter().cloned().collect()
96    }
97
98    /// Array flags for this mutable view.
99    pub fn flags(&self) -> ArrayFlags {
100        let layout = self.layout();
101        ArrayFlags {
102            c_contiguous: layout.is_c_contiguous(),
103            f_contiguous: layout.is_f_contiguous(),
104            owndata: false,
105            writeable: true,
106        }
107    }
108}
109
110// Create an ArrayViewMut from an owned Array
111impl<T: Element, D: Dimension> Array<T, D> {
112    /// Create a mutable view of this array.
113    pub fn view_mut(&mut self) -> ArrayViewMut<'_, T, D> {
114        ArrayViewMut::from_ndarray(self.inner.view_mut())
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121    use crate::dimension::Ix1;
122
123    #[test]
124    fn view_mut_from_owned() {
125        let mut arr = Array::<f64, Ix1>::from_vec(Ix1::new([3]), vec![1.0, 2.0, 3.0]).unwrap();
126        let v = arr.view_mut();
127        assert_eq!(v.shape(), &[3]);
128        assert!(v.flags().writeable);
129        assert!(!v.flags().owndata);
130    }
131
132    #[test]
133    fn view_mut_modify() {
134        let mut arr = Array::<f64, Ix1>::from_vec(Ix1::new([3]), vec![1.0, 2.0, 3.0]).unwrap();
135        {
136            let mut v = arr.view_mut();
137            if let Some(s) = v.as_slice_mut() {
138                s[0] = 99.0;
139            }
140        }
141        assert_eq!(arr.as_slice().unwrap()[0], 99.0);
142    }
143}