|
1 | 1 |
|
2 | 2 | use alloc::vec::Vec;
|
| 3 | + |
3 | 4 | use crate::imp_prelude::*;
|
| 5 | +use crate::dimension; |
| 6 | +use crate::error::{ErrorKind, ShapeError}; |
| 7 | +use crate::OwnedRepr; |
| 8 | +use crate::Zip; |
4 | 9 |
|
5 | 10 | /// Methods specific to `Array0`.
|
6 | 11 | ///
|
@@ -59,3 +64,143 @@ where
|
59 | 64 | self.data.into_vec()
|
60 | 65 | }
|
61 | 66 | }
|
| 67 | + |
| 68 | +/// Methods specific to `Array2`. |
| 69 | +/// |
| 70 | +/// ***See also all methods for [`ArrayBase`]*** |
| 71 | +/// |
| 72 | +/// [`ArrayBase`]: struct.ArrayBase.html |
| 73 | +impl<A> Array<A, Ix2> { |
| 74 | + /// Append a row to an array with row major memory layout. |
| 75 | + /// |
| 76 | + /// ***Errors*** with a layout error if the array is not in standard order or |
| 77 | + /// if it has holes, even exterior holes (from slicing). <br> |
| 78 | + /// ***Errors*** with shape error if the length of the input row does not match |
| 79 | + /// the length of the rows in the array. <br> |
| 80 | + /// |
| 81 | + /// The memory layout matters, since it determines in which direction the array can easily |
| 82 | + /// grow. Notice that an empty array is compatible both ways. The amortized average |
| 83 | + /// complexity of the append is O(m) where *m* is the length of the row. |
| 84 | + /// |
| 85 | + /// ```rust |
| 86 | + /// use ndarray::{Array, ArrayView, array}; |
| 87 | + /// |
| 88 | + /// // create an empty array and append |
| 89 | + /// let mut a = Array::zeros((0, 4)); |
| 90 | + /// a.try_append_row(ArrayView::from(&[1., 2., 3., 4.])).unwrap(); |
| 91 | + /// a.try_append_row(ArrayView::from(&[0., -2., -3., -4.])).unwrap(); |
| 92 | + /// |
| 93 | + /// assert_eq!( |
| 94 | + /// a, |
| 95 | + /// array![[1., 2., 3., 4.], |
| 96 | + /// [0., -2., -3., -4.]]); |
| 97 | + /// ``` |
| 98 | + pub fn try_append_row(&mut self, row: ArrayView<A, Ix1>) -> Result<(), ShapeError> |
| 99 | + where |
| 100 | + A: Clone, |
| 101 | + { |
| 102 | + let row_len = row.len(); |
| 103 | + if row_len != self.len_of(Axis(1)) { |
| 104 | + return Err(ShapeError::from_kind(ErrorKind::IncompatibleShape)); |
| 105 | + } |
| 106 | + let mut res_dim = self.raw_dim(); |
| 107 | + res_dim[0] += 1; |
| 108 | + let new_len = dimension::size_of_shape_checked(&res_dim)?; |
| 109 | + |
| 110 | + // array must be c-contiguous and be "full" (have no exterior holes) |
| 111 | + if !self.is_standard_layout() || self.len() != self.data.len() { |
| 112 | + return Err(ShapeError::from_kind(ErrorKind::IncompatibleLayout)); |
| 113 | + } |
| 114 | + |
| 115 | + unsafe { |
| 116 | + // grow backing storage and update head ptr |
| 117 | + debug_assert_eq!(self.data.as_ptr(), self.as_ptr()); |
| 118 | + self.data.reserve(row_len); |
| 119 | + self.ptr = self.data.as_nonnull_mut(); // because we are standard order |
| 120 | + |
| 121 | + // recompute strides - if the array was previously empty, it could have |
| 122 | + // zeros in strides. |
| 123 | + let strides = res_dim.default_strides(); |
| 124 | + |
| 125 | + // copy elements from view to the array now |
| 126 | + // |
| 127 | + // make a raw view with the new row |
| 128 | + // safe because the data was "full" |
| 129 | + let tail_ptr = self.data.as_end_nonnull(); |
| 130 | + let tail_view = RawArrayViewMut::new(tail_ptr, Ix1(row_len), Ix1(1)); |
| 131 | + |
| 132 | + struct SetLenOnDrop<'a, A: 'a> { |
| 133 | + len: usize, |
| 134 | + data: &'a mut OwnedRepr<A>, |
| 135 | + } |
| 136 | + |
| 137 | + let mut length_guard = SetLenOnDrop { |
| 138 | + len: self.data.len(), |
| 139 | + data: &mut self.data, |
| 140 | + }; |
| 141 | + |
| 142 | + impl<A> Drop for SetLenOnDrop<'_, A> { |
| 143 | + fn drop(&mut self) { |
| 144 | + unsafe { |
| 145 | + self.data.set_len(self.len); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // assign the new elements |
| 151 | + Zip::from(tail_view).and(row) |
| 152 | + .for_each(|to, from| { |
| 153 | + to.write(from.clone()); |
| 154 | + length_guard.len += 1; |
| 155 | + }); |
| 156 | + |
| 157 | + drop(length_guard); |
| 158 | + |
| 159 | + // update array dimension |
| 160 | + self.strides = strides; |
| 161 | + self.dim[0] += 1; |
| 162 | + |
| 163 | + } |
| 164 | + // multiple assertions after pointer & dimension update |
| 165 | + debug_assert_eq!(self.data.len(), self.len()); |
| 166 | + debug_assert_eq!(self.len(), new_len); |
| 167 | + debug_assert!(self.is_standard_layout()); |
| 168 | + |
| 169 | + Ok(()) |
| 170 | + } |
| 171 | + |
| 172 | + /// Append a column to and array with column major memory layout. |
| 173 | + /// |
| 174 | + /// ***Errors*** with a layout error if the array is not in column major order or |
| 175 | + /// if it has holes, even exterior holes (from slicing). <br> |
| 176 | + /// ***Errors*** with shape error if the length of the input column does not match |
| 177 | + /// the length of the columns in the array.<br> |
| 178 | + /// |
| 179 | + /// The memory layout matters, since it determines in which direction the array can easily |
| 180 | + /// grow. Notice that an empty array is compatible both ways. The amortized average |
| 181 | + /// complexity of the append is O(m) where *m* is the length of the column. |
| 182 | + /// |
| 183 | + /// ```rust |
| 184 | + /// use ndarray::{Array, ArrayView, array}; |
| 185 | + /// |
| 186 | + /// // create an empty array and append |
| 187 | + /// let mut a = Array::zeros((2, 0)); |
| 188 | + /// a.try_append_column(ArrayView::from(&[1., 2.])).unwrap(); |
| 189 | + /// a.try_append_column(ArrayView::from(&[0., -2.])).unwrap(); |
| 190 | + /// |
| 191 | + /// assert_eq!( |
| 192 | + /// a, |
| 193 | + /// array![[1., 0.], |
| 194 | + /// [2., -2.]]); |
| 195 | + /// ``` |
| 196 | + pub fn try_append_column(&mut self, column: ArrayView<A, Ix1>) -> Result<(), ShapeError> |
| 197 | + where |
| 198 | + A: Clone, |
| 199 | + { |
| 200 | + self.swap_axes(0, 1); |
| 201 | + let ret = self.try_append_row(column); |
| 202 | + self.swap_axes(0, 1); |
| 203 | + ret |
| 204 | + } |
| 205 | +} |
| 206 | + |
0 commit comments