Skip to content

Commit f21c668

Browse files
committed
append: Update docs for push_row/column, push, append
A few things stuck around in the doc that came from the earlier versions of these methods. Correct the doc so that it's easier to understand and remove the part about where it can fail when it can not.
1 parent 864cccf commit f21c668

File tree

1 file changed

+66
-44
lines changed

1 file changed

+66
-44
lines changed

src/impl_owned_array.rs

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,29 @@ where
8080
impl<A> Array<A, Ix2> {
8181
/// Append a row to an array
8282
///
83+
/// The elements from `row` are cloned and added as a new row in the array.
84+
///
8385
/// ***Errors*** with a shape error if the length of the row does not match the length of the
84-
/// rows in the array. <br>
86+
/// rows in the array.
8587
///
8688
/// The memory layout of the `self` array matters for ensuring that the append is efficient.
8789
/// Appending automatically changes memory layout of the array so that it is appended to
88-
/// along the "growing axis".
90+
/// along the "growing axis". However, if the memory layout needs adjusting, the array must
91+
/// reallocate and move memory.
92+
///
93+
/// The operation leaves the existing data in place and is most efficent if one of these is
94+
/// true:
8995
///
90-
/// Ensure appending is efficient by, for example, appending to an empty array and then
91-
/// always appending along the same axis. For rows, ndarray's default layout is efficient for
92-
/// appending.
96+
/// - The axis being appended to is the longest stride axis, i.e the array is in row major
97+
/// ("C") layout.
98+
/// - The array has 0 or 1 rows (It is converted to row major)
9399
///
94-
/// Notice that an empty array (where it has an axis of length zero) is the simplest starting
95-
/// point. When repeatedly appending to a single axis, the amortized average complexity of each append is O(m), where *m* is the length of
96-
/// the row.
100+
/// Ensure appending is efficient by, for example, appending to an empty array and then always
101+
/// pushing/appending along the same axis. For pushing rows, ndarray's default layout (C order)
102+
/// is efficient.
103+
///
104+
/// When repeatedly appending to a single axis, the amortized average complexity of each
105+
/// append is O(m), where *m* is the length of the row.
97106
///
98107
/// ```rust
99108
/// use ndarray::{Array, ArrayView, array};
@@ -117,20 +126,29 @@ impl<A> Array<A, Ix2> {
117126

118127
/// Append a column to an array
119128
///
129+
/// The elements from `column` are cloned and added as a new row in the array.
130+
///
120131
/// ***Errors*** with a shape error if the length of the column does not match the length of
121-
/// the columns in the array.<br>
132+
/// the columns in the array.
122133
///
123134
/// The memory layout of the `self` array matters for ensuring that the append is efficient.
124135
/// Appending automatically changes memory layout of the array so that it is appended to
125-
/// along the "growing axis".
136+
/// along the "growing axis". However, if the memory layout needs adjusting, the array must
137+
/// reallocate and move memory.
126138
///
127-
/// Ensure appending is efficient by, for example, appending to an empty array and then
128-
/// always appending along the same axis. For columns, column major ("F") memory layout is
129-
/// efficient for appending.
139+
/// The operation leaves the existing data in place and is most efficent if one of these is
140+
/// true:
130141
///
131-
/// Notice that an empty array (where it has an axis of length zero) is the simplest starting
132-
/// point. When repeatedly appending to a single axis, the amortized average complexity of each append is O(m), where *m* is the length of
133-
/// the row.
142+
/// - The axis being appended to is the longest stride axis, i.e the array is in column major
143+
/// ("F") layout.
144+
/// - The array has 0 or 1 columns (It is converted to column major)
145+
///
146+
/// Ensure appending is efficient by, for example, appending to an empty array and then always
147+
/// pushing/appending along the same axis. For pushing columns, column major layout (F order)
148+
/// is efficient.
149+
///
150+
/// When repeatedly appending to a single axis, the amortized average complexity of each append
151+
/// is O(m), where *m* is the length of the column.
134152
///
135153
/// ```rust
136154
/// use ndarray::{Array, ArrayView, array};
@@ -339,28 +357,30 @@ impl<A, D> Array<A, D>
339357
}
340358
}
341359

342-
/// Append an array to the array along an axis
343-
///
344-
/// Where the item to push to the array has one dimension less than the `self` array. This
345-
/// method is equivalent to `self.append(axis, array.insert_axis(axis))`.
360+
/// Append an array to the array along an axis.
346361
///
347-
/// The axis-to-append-to `axis` must be the array's "growing axis" for this operation
348-
/// to succeed. The growing axis is the outermost or last-visited when elements are visited in
349-
/// memory order:
362+
/// The elements of `array` are cloned and extend the axis `axis` in the present array;
363+
/// `self` will grow in size by 1 along `axis`.
350364
///
351-
/// `axis` must be the growing axis of the current array, an axis with length 0 or 1.
352-
///
353-
/// - This is the 0th axis for standard layout arrays
354-
/// - This is the *n*-1 th axis for fortran layout arrays
355-
/// - If the array is empty (the axis or any other has length 0) or if `axis`
356-
/// has length 1, then the array can always be appended.
365+
/// Append to the array, where the array being pushed to the array has one dimension less than
366+
/// the `self` array. This method is equivalent to [append](ArrayBase::append) in this way:
367+
/// `self.append(axis, array.insert_axis(axis))`.
357368
///
358369
/// ***Errors*** with a shape error if the shape of self does not match the array-to-append;
359-
/// all axes *except* the axis along which it being appended matter for this check.
370+
/// all axes *except* the axis along which it being appended matter for this check:
371+
/// the shape of `self` with `axis` removed must be the same as the shape of `array`.
360372
///
361373
/// The memory layout of the `self` array matters for ensuring that the append is efficient.
362374
/// Appending automatically changes memory layout of the array so that it is appended to
363-
/// along the "growing axis".
375+
/// along the "growing axis". However, if the memory layout needs adjusting, the array must
376+
/// reallocate and move memory.
377+
///
378+
/// The operation leaves the existing data in place and is most efficent if `axis` is a
379+
/// "growing axis" for the array, i.e. one of these is true:
380+
///
381+
/// - The axis is the longest stride axis, for example the 0th axis in a C-layout or the
382+
/// *n-1*th axis in an F-layout array.
383+
/// - The axis has length 0 or 1 (It is converted to the new growing axis)
364384
///
365385
/// Ensure appending is efficient by for example starting from an empty array and/or always
366386
/// appending to an array along the same axis.
@@ -398,25 +418,27 @@ impl<A, D> Array<A, D>
398418
}
399419

400420

401-
/// Append an array to the array along an axis
402-
///
403-
/// The axis-to-append-to `axis` must be the array's "growing axis" for this operation
404-
/// to succeed. The growing axis is the outermost or last-visited when elements are visited in
405-
/// memory order:
421+
/// Append an array to the array along an axis.
406422
///
407-
/// `axis` must be the growing axis of the current array, an axis with length 0 or 1.
408-
///
409-
/// - This is the 0th axis for standard layout arrays
410-
/// - This is the *n*-1 th axis for fortran layout arrays
411-
/// - If the array is empty (the axis or any other has length 0) or if `axis`
412-
/// has length 1, then the array can always be appended.
423+
/// The elements of `array` are cloned and extend the axis `axis` in the present array;
424+
/// `self` will grow in size by `array.len_of(axis)` along `axis`.
413425
///
414426
/// ***Errors*** with a shape error if the shape of self does not match the array-to-append;
415-
/// all axes *except* the axis along which it being appended matter for this check.
427+
/// all axes *except* the axis along which it being appended matter for this check:
428+
/// the shape of `self` with `axis` removed must be the same as the shape of `array` with
429+
/// `axis` removed.
416430
///
417431
/// The memory layout of the `self` array matters for ensuring that the append is efficient.
418432
/// Appending automatically changes memory layout of the array so that it is appended to
419-
/// along the "growing axis".
433+
/// along the "growing axis". However, if the memory layout needs adjusting, the array must
434+
/// reallocate and move memory.
435+
///
436+
/// The operation leaves the existing data in place and is most efficent if `axis` is a
437+
/// "growing axis" for the array, i.e. one of these is true:
438+
///
439+
/// - The axis is the longest stride axis, for example the 0th axis in a C-layout or the
440+
/// *n-1*th axis in an F-layout array.
441+
/// - The axis has length 0 or 1 (It is converted to the new growing axis)
420442
///
421443
/// Ensure appending is efficient by for example starting from an empty array and/or always
422444
/// appending to an array along the same axis.

0 commit comments

Comments
 (0)