Skip to content

Commit 54359a9

Browse files
committed
append: Use try_append_array for append row/column
1 parent 29896d8 commit 54359a9

File tree

1 file changed

+2
-71
lines changed

1 file changed

+2
-71
lines changed

src/impl_owned_array.rs

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -99,73 +99,7 @@ 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.ptr = self.data.reserve(row_len); // because we are standard order
119-
120-
// recompute strides - if the array was previously empty, it could have
121-
// zeros in strides.
122-
let strides = res_dim.default_strides();
123-
124-
// copy elements from view to the array now
125-
//
126-
// make a raw view with the new row
127-
// safe because the data was "full"
128-
let tail_ptr = self.data.as_end_nonnull();
129-
let tail_view = RawArrayViewMut::new(tail_ptr, Ix1(row_len), Ix1(1));
130-
131-
struct SetLenOnDrop<'a, A: 'a> {
132-
len: usize,
133-
data: &'a mut OwnedRepr<A>,
134-
}
135-
136-
let mut length_guard = SetLenOnDrop {
137-
len: self.data.len(),
138-
data: &mut self.data,
139-
};
140-
141-
impl<A> Drop for SetLenOnDrop<'_, A> {
142-
fn drop(&mut self) {
143-
unsafe {
144-
self.data.set_len(self.len);
145-
}
146-
}
147-
}
148-
149-
// assign the new elements
150-
Zip::from(tail_view).and(row)
151-
.for_each(|to, from| {
152-
to.write(from.clone());
153-
length_guard.len += 1;
154-
});
155-
156-
drop(length_guard);
157-
158-
// update array dimension
159-
self.strides = strides;
160-
self.dim[0] += 1;
161-
162-
}
163-
// multiple assertions after pointer & dimension update
164-
debug_assert_eq!(self.data.len(), self.len());
165-
debug_assert_eq!(self.len(), new_len);
166-
debug_assert!(self.is_standard_layout());
167-
168-
Ok(())
102+
self.try_append_array(Axis(0), row.insert_axis(Axis(0)))
169103
}
170104

171105
/// Append a column to an array with column major memory layout.
@@ -196,10 +130,7 @@ impl<A> Array<A, Ix2> {
196130
where
197131
A: Clone,
198132
{
199-
self.swap_axes(0, 1);
200-
let ret = self.try_append_row(column);
201-
self.swap_axes(0, 1);
202-
ret
133+
self.try_append_array(Axis(1), column.insert_axis(Axis(1)))
203134
}
204135
}
205136

0 commit comments

Comments
 (0)