Skip to content

Commit 5d01d9e

Browse files
committed
FIX: Let .try_append_row reuse .try_append_array()
1 parent 6d56153 commit 5d01d9e

File tree

1 file changed

+2
-68
lines changed

1 file changed

+2
-68
lines changed

src/impl_owned_array.rs

+2-68
Original file line numberDiff line numberDiff line change
@@ -99,74 +99,8 @@ impl<A> Array<A, Ix2> {
9999
where
100100
A: Clone,
101101
{
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(())
102+
let row2 = row.insert_axis(Axis(0));
103+
self.try_append_array(Axis(0), row2)
170104
}
171105

172106
/// Append a column to an array with column major memory layout.

0 commit comments

Comments
 (0)