Skip to content

Commit 508c9dd

Browse files
committed
append: Name methods .append(axis, array), .append_row/column()
Now without the tricky memory layout failure mode, these methods can just have names without try_ prefix. It's expected that they work successfully as long as the user matches up array sizes correctly.
1 parent e7a6c14 commit 508c9dd

File tree

3 files changed

+93
-81
lines changed

3 files changed

+93
-81
lines changed

src/impl_owned_array.rs

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,66 +77,78 @@ where
7777
///
7878
/// [`ArrayBase`]: struct.ArrayBase.html
7979
impl<A> Array<A, Ix2> {
80-
/// Append a row to an array with row major memory layout.
80+
/// Append a row to an array
8181
///
82-
/// ***Errors*** with a layout error if the array is not in standard order or
83-
/// if it has holes, even exterior holes (from slicing). <br>
84-
/// ***Errors*** with shape error if the length of the input row does not match
85-
/// the length of the rows in the array. <br>
82+
/// ***Errors*** with a shape error if the length of the row does not match the length of the
83+
/// rows in the array. <br>
8684
///
87-
/// The memory layout matters, since it determines in which direction the array can easily
88-
/// grow. Notice that an empty array is compatible both ways. The amortized average
89-
/// complexity of the append is O(m) where *m* is the length of the row.
85+
/// The memory layout of the `self` array matters for ensuring that the append is efficient.
86+
/// Appending automatically changes memory layout of the array so that it is appended to
87+
/// along the "growing axis".
88+
///
89+
/// Ensure appending is efficient by for example starting appending an empty array and
90+
/// always appending along the same axis. For rows, ndarray's default layout is efficient for
91+
/// appending.
92+
///
93+
/// Notice that an empty array (where it has an axis of length zero) is the simplest starting
94+
/// point. The amortized average complexity of the append is O(m) where *m* is the length of
95+
/// the row.
9096
///
9197
/// ```rust
9298
/// use ndarray::{Array, ArrayView, array};
9399
///
94100
/// // create an empty array and append
95101
/// let mut a = Array::zeros((0, 4));
96-
/// a.try_append_row(ArrayView::from(&[ 1., 2., 3., 4.])).unwrap();
97-
/// a.try_append_row(ArrayView::from(&[-1., -2., -3., -4.])).unwrap();
102+
/// a.append_row(ArrayView::from(&[ 1., 2., 3., 4.])).unwrap();
103+
/// a.append_row(ArrayView::from(&[-1., -2., -3., -4.])).unwrap();
98104
///
99105
/// assert_eq!(
100106
/// a,
101107
/// array![[ 1., 2., 3., 4.],
102108
/// [-1., -2., -3., -4.]]);
103109
/// ```
104-
pub fn try_append_row(&mut self, row: ArrayView<A, Ix1>) -> Result<(), ShapeError>
110+
pub fn append_row(&mut self, row: ArrayView<A, Ix1>) -> Result<(), ShapeError>
105111
where
106112
A: Clone,
107113
{
108-
self.try_append_array(Axis(0), row.insert_axis(Axis(0)))
114+
self.append(Axis(0), row.insert_axis(Axis(0)))
109115
}
110116

111-
/// Append a column to an array with column major memory layout.
117+
/// Append a column to an array
118+
///
119+
/// ***Errors*** with a shape error if the length of the column does not match the length of
120+
/// the columns in the array.<br>
121+
///
122+
/// The memory layout of the `self` array matters for ensuring that the append is efficient.
123+
/// Appending automatically changes memory layout of the array so that it is appended to
124+
/// along the "growing axis".
112125
///
113-
/// ***Errors*** with a layout error if the array is not in column major order or
114-
/// if it has holes, even exterior holes (from slicing). <br>
115-
/// ***Errors*** with shape error if the length of the input column does not match
116-
/// the length of the columns in the array.<br>
126+
/// Ensure appending is efficient by for example starting appending an empty array and
127+
/// always appending along the same axis. For columns, column major ("F") memory layout is
128+
/// efficient for appending.
117129
///
118-
/// The memory layout matters, since it determines in which direction the array can easily
119-
/// grow. Notice that an empty array is compatible both ways. The amortized average
120-
/// complexity of the append is O(m) where *m* is the length of the column.
130+
/// Notice that an empty array (where it has an axis of length zero) is the simplest starting
131+
/// point. The amortized average complexity of the append is O(m) where *m* is the length of
132+
/// the row.
121133
///
122134
/// ```rust
123135
/// use ndarray::{Array, ArrayView, array};
124136
///
125137
/// // create an empty array and append
126138
/// let mut a = Array::zeros((2, 0));
127-
/// a.try_append_column(ArrayView::from(&[1., 2.])).unwrap();
128-
/// a.try_append_column(ArrayView::from(&[-1., -2.])).unwrap();
139+
/// a.append_column(ArrayView::from(&[1., 2.])).unwrap();
140+
/// a.append_column(ArrayView::from(&[-1., -2.])).unwrap();
129141
///
130142
/// assert_eq!(
131143
/// a,
132144
/// array![[1., -1.],
133145
/// [2., -2.]]);
134146
/// ```
135-
pub fn try_append_column(&mut self, column: ArrayView<A, Ix1>) -> Result<(), ShapeError>
147+
pub fn append_column(&mut self, column: ArrayView<A, Ix1>) -> Result<(), ShapeError>
136148
where
137149
A: Clone,
138150
{
139-
self.try_append_array(Axis(1), column.insert_axis(Axis(1)))
151+
self.append(Axis(1), column.insert_axis(Axis(1)))
140152
}
141153
}
142154

@@ -334,7 +346,7 @@ impl<A, D> Array<A, D>
334346
/// - If the array is empty (the axis or any other has length 0) or if `axis`
335347
/// has length 1, then the array can always be appended.
336348
///
337-
/// ***Errors*** with shape error if the shape of self does not match the array-to-append;
349+
/// ***Errors*** with a shape error if the shape of self does not match the array-to-append;
338350
/// all axes *except* the axis along which it being appended matter for this check.
339351
///
340352
/// The memory layout of the `self` array matters for ensuring that the append is efficient.
@@ -347,7 +359,7 @@ impl<A, D> Array<A, D>
347359
/// The amortized average complexity of the append, when appending along its growing axis, is
348360
/// O(*m*) where *m* is the length of the row.
349361
///
350-
/// The memory layout of the argument `array` does not matter.
362+
/// The memory layout of the argument `array` does not matter to the same extent.
351363
///
352364
/// ```rust
353365
/// use ndarray::{Array, ArrayView, array, Axis};
@@ -356,9 +368,9 @@ impl<A, D> Array<A, D>
356368
/// let mut a = Array::zeros((0, 4));
357369
/// let ones = ArrayView::from(&[1.; 8]).into_shape((2, 4)).unwrap();
358370
/// let zeros = ArrayView::from(&[0.; 8]).into_shape((2, 4)).unwrap();
359-
/// a.try_append_array(Axis(0), ones).unwrap();
360-
/// a.try_append_array(Axis(0), zeros).unwrap();
361-
/// a.try_append_array(Axis(0), ones).unwrap();
371+
/// a.append(Axis(0), ones).unwrap();
372+
/// a.append(Axis(0), zeros).unwrap();
373+
/// a.append(Axis(0), ones).unwrap();
362374
///
363375
/// assert_eq!(
364376
/// a,
@@ -369,7 +381,7 @@ impl<A, D> Array<A, D>
369381
/// [1., 1., 1., 1.],
370382
/// [1., 1., 1., 1.]]);
371383
/// ```
372-
pub fn try_append_array(&mut self, axis: Axis, mut array: ArrayView<A, D>)
384+
pub fn append(&mut self, axis: Axis, mut array: ArrayView<A, D>)
373385
-> Result<(), ShapeError>
374386
where
375387
A: Clone,

src/stacking.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ where
9494
let new_len = dimension::size_of_shape_checked(&res_dim)?;
9595

9696
// start with empty array with precomputed capacity
97-
// try_append_array's handling of empty arrays makes sure `axis` is ok for appending
97+
// append's handling of empty arrays makes sure `axis` is ok for appending
9898
res_dim.set_axis(axis, 0);
9999
let mut res = unsafe {
100100
// Safety: dimension is size 0 and vec is empty
101101
Array::from_shape_vec_unchecked(res_dim, Vec::with_capacity(new_len))
102102
};
103103

104104
for array in arrays {
105-
res.try_append_array(axis, array.clone())?;
105+
res.append(axis, array.clone())?;
106106
}
107107
debug_assert_eq!(res.len_of(axis), stacked_dim);
108108
Ok(res)
@@ -161,15 +161,15 @@ where
161161
let new_len = dimension::size_of_shape_checked(&res_dim)?;
162162

163163
// start with empty array with precomputed capacity
164-
// try_append_array's handling of empty arrays makes sure `axis` is ok for appending
164+
// append's handling of empty arrays makes sure `axis` is ok for appending
165165
res_dim.set_axis(axis, 0);
166166
let mut res = unsafe {
167167
// Safety: dimension is size 0 and vec is empty
168168
Array::from_shape_vec_unchecked(res_dim, Vec::with_capacity(new_len))
169169
};
170170

171171
for array in arrays {
172-
res.try_append_array(axis, array.clone().insert_axis(axis))?;
172+
res.append(axis, array.clone().insert_axis(axis))?;
173173
}
174174

175175
debug_assert_eq!(res.len_of(axis), arrays.len());

0 commit comments

Comments
 (0)