@@ -74,66 +74,78 @@ where
74
74
///
75
75
/// [`ArrayBase`]: struct.ArrayBase.html
76
76
impl < A > Array < A , Ix2 > {
77
- /// Append a row to an array with row major memory layout.
77
+ /// Append a row to an array
78
78
///
79
- /// ***Errors*** with a layout error if the array is not in standard order or
80
- /// if it has holes, even exterior holes (from slicing). <br>
81
79
/// ***Errors*** with shape error if the length of the input row does not match
82
80
/// the length of the rows in the array. <br>
83
81
///
84
- /// The memory layout matters, since it determines in which direction the array can easily
85
- /// grow. Notice that an empty array is compatible both ways. The amortized average
86
- /// complexity of the append is O(m) where *m* is the length of the row.
82
+ /// The memory layout of the `self` array matters for ensuring that the append is efficient.
83
+ /// Appending automatically changes memory layout of the array so that it is appended to
84
+ /// along the "growing axis".
85
+ ///
86
+ /// Ensure appending is efficient by for example starting appending an empty array and
87
+ /// always appending along the same axis. For rows, ndarray's default layout is efficient for
88
+ /// appending.
89
+ ///
90
+ /// Notice that an empty array (where it has an axis of length zero) is the simplest starting
91
+ /// point. The amortized average complexity of the append is O(m) where *m* is the length of
92
+ /// the row.
87
93
///
88
94
/// ```rust
89
95
/// use ndarray::{Array, ArrayView, array};
90
96
///
91
97
/// // create an empty array and append
92
98
/// let mut a = Array::zeros((0, 4));
93
- /// a.try_append_row (ArrayView::from(&[ 1., 2., 3., 4.])).unwrap();
94
- /// a.try_append_row (ArrayView::from(&[-1., -2., -3., -4.])).unwrap();
99
+ /// a.append_row (ArrayView::from(&[ 1., 2., 3., 4.])).unwrap();
100
+ /// a.append_row (ArrayView::from(&[-1., -2., -3., -4.])).unwrap();
95
101
///
96
102
/// assert_eq!(
97
103
/// a,
98
104
/// array![[ 1., 2., 3., 4.],
99
105
/// [-1., -2., -3., -4.]]);
100
106
/// ```
101
- pub fn try_append_row ( & mut self , row : ArrayView < A , Ix1 > ) -> Result < ( ) , ShapeError >
107
+ pub fn append_row ( & mut self , row : ArrayView < A , Ix1 > ) -> Result < ( ) , ShapeError >
102
108
where
103
109
A : Clone ,
104
110
{
105
- self . try_append_array ( Axis ( 0 ) , row. insert_axis ( Axis ( 0 ) ) )
111
+ self . append ( Axis ( 0 ) , row. insert_axis ( Axis ( 0 ) ) )
106
112
}
107
113
108
- /// Append a column to an array with column major memory layout.
114
+ /// Append a column to an array
109
115
///
110
- /// ***Errors*** with a layout error if the array is not in column major order or
111
- /// if it has holes, even exterior holes (from slicing). <br>
112
116
/// ***Errors*** with shape error if the length of the input column does not match
113
117
/// the length of the columns in the array.<br>
114
118
///
115
- /// The memory layout matters, since it determines in which direction the array can easily
116
- /// grow. Notice that an empty array is compatible both ways. The amortized average
117
- /// complexity of the append is O(m) where *m* is the length of the column.
119
+ /// The memory layout of the `self` array matters for ensuring that the append is efficient.
120
+ /// Appending automatically changes memory layout of the array so that it is appended to
121
+ /// along the "growing axis".
122
+ ///
123
+ /// Ensure appending is efficient by for example starting appending an empty array and
124
+ /// always appending along the same axis. For columns, column major ("F") memory layout is
125
+ /// efficient for appending.
126
+ ///
127
+ /// Notice that an empty array (where it has an axis of length zero) is the simplest starting
128
+ /// point. The amortized average complexity of the append is O(m) where *m* is the length of
129
+ /// the row.
118
130
///
119
131
/// ```rust
120
132
/// use ndarray::{Array, ArrayView, array};
121
133
///
122
134
/// // create an empty array and append
123
135
/// let mut a = Array::zeros((2, 0));
124
- /// a.try_append_column (ArrayView::from(&[1., 2.])).unwrap();
125
- /// a.try_append_column (ArrayView::from(&[-1., -2.])).unwrap();
136
+ /// a.append_column (ArrayView::from(&[1., 2.])).unwrap();
137
+ /// a.append_column (ArrayView::from(&[-1., -2.])).unwrap();
126
138
///
127
139
/// assert_eq!(
128
140
/// a,
129
141
/// array![[1., -1.],
130
142
/// [2., -2.]]);
131
143
/// ```
132
- pub fn try_append_column ( & mut self , column : ArrayView < A , Ix1 > ) -> Result < ( ) , ShapeError >
144
+ pub fn append_column ( & mut self , column : ArrayView < A , Ix1 > ) -> Result < ( ) , ShapeError >
133
145
where
134
146
A : Clone ,
135
147
{
136
- self . try_append_array ( Axis ( 1 ) , column. insert_axis ( Axis ( 1 ) ) )
148
+ self . append ( Axis ( 1 ) , column. insert_axis ( Axis ( 1 ) ) )
137
149
}
138
150
}
139
151
@@ -311,17 +323,21 @@ impl<A, D> Array<A, D>
311
323
/// - If the array is empty (the axis or any other has length 0) or if `axis`
312
324
/// has length 1, then the array can always be appended.
313
325
///
314
- /// ***Errors*** with a layout error if the array is not in standard order or
315
- /// if it has holes, even exterior holes (from slicing). <br>
316
- /// ***Errors*** with shape error if the length of the input row does not match
317
- /// the length of the rows in the array. <br>
326
+ /// ***Errors*** with shape error if the shape of self does not match the array-to-append;
327
+ /// all axes *except* the axis along which it being appended matter for this check.
328
+ ///
329
+ /// The memory layout of the `self` array matters for ensuring that the append is efficient.
330
+ /// Appending automatically changes memory layout of the array so that it is appended to
331
+ /// along the "growing axis".
332
+ ///
333
+ /// Ensure appending is efficient by for example starting appending an empty array and
334
+ /// always appending along the same axis.
318
335
///
319
- /// The memory layout of the `self` array matters, since it determines in which direction the
320
- /// array can easily grow. Notice that an empty array is compatible both ways. The amortized
321
- /// average complexity of the append is O(m) where *m* is the number of elements in the
322
- /// array-to-append (equivalent to how `Vec::extend` works).
336
+ /// Notice that an empty array (where it has an axis of length zero) is the simplest starting
337
+ /// point. The amortized average complexity of the append is O(m) where *m* is the length of
338
+ /// the row.
323
339
///
324
- /// The memory layout of the argument `array` does not matter.
340
+ /// The memory layout of the argument `array` does not matter to the same extent .
325
341
///
326
342
/// ```rust
327
343
/// use ndarray::{Array, ArrayView, array, Axis};
@@ -330,9 +346,9 @@ impl<A, D> Array<A, D>
330
346
/// let mut a = Array::zeros((0, 4));
331
347
/// let ones = ArrayView::from(&[1.; 8]).into_shape((2, 4)).unwrap();
332
348
/// let zeros = ArrayView::from(&[0.; 8]).into_shape((2, 4)).unwrap();
333
- /// a.try_append_array (Axis(0), ones).unwrap();
334
- /// a.try_append_array (Axis(0), zeros).unwrap();
335
- /// a.try_append_array (Axis(0), ones).unwrap();
349
+ /// a.append (Axis(0), ones).unwrap();
350
+ /// a.append (Axis(0), zeros).unwrap();
351
+ /// a.append (Axis(0), ones).unwrap();
336
352
///
337
353
/// assert_eq!(
338
354
/// a,
@@ -343,7 +359,7 @@ impl<A, D> Array<A, D>
343
359
/// [1., 1., 1., 1.],
344
360
/// [1., 1., 1., 1.]]);
345
361
/// ```
346
- pub fn try_append_array ( & mut self , axis : Axis , mut array : ArrayView < A , D > )
362
+ pub fn append ( & mut self , axis : Axis , mut array : ArrayView < A , D > )
347
363
-> Result < ( ) , ShapeError >
348
364
where
349
365
A : Clone ,
0 commit comments