|
1 | | -// Copyright 2014-2016 bluss and ndarray developers. |
| 1 | +// Copyright 2014-2020 bluss and ndarray developers. |
2 | 2 | // |
3 | 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
4 | 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
|
8 | 8 |
|
9 | 9 | use crate::error::{from_kind, ErrorKind, ShapeError}; |
10 | 10 | use crate::imp_prelude::*; |
| 11 | +use crate::traversal_utils::assign_to; |
11 | 12 |
|
12 | 13 | /// Stack arrays along the new axis. |
13 | 14 | /// |
@@ -88,25 +89,23 @@ where |
88 | 89 | let stacked_dim = arrays.iter().fold(0, |acc, a| acc + a.len_of(axis)); |
89 | 90 | res_dim.set_axis(axis, stacked_dim); |
90 | 91 |
|
91 | | - // we can safely use uninitialized values here because they are Copy |
92 | | - // and we will only ever write to them |
93 | | - let size = res_dim.size(); |
94 | | - let mut v = Vec::with_capacity(size); |
95 | | - unsafe { |
96 | | - v.set_len(size); |
97 | | - } |
98 | | - let mut res = Array::from_shape_vec(res_dim, v)?; |
| 92 | + // we can safely use uninitialized values here because we will |
| 93 | + // overwrite every one of them. |
| 94 | + let mut res = Array::maybe_uninit(res_dim); |
99 | 95 |
|
100 | 96 | { |
101 | 97 | let mut assign_view = res.view_mut(); |
102 | 98 | for array in arrays { |
103 | 99 | let len = array.len_of(axis); |
104 | | - let (mut front, rest) = assign_view.split_at(axis, len); |
105 | | - front.assign(array); |
| 100 | + let (front, rest) = assign_view.split_at(axis, len); |
| 101 | + assign_to(array, front); |
106 | 102 | assign_view = rest; |
107 | 103 | } |
| 104 | + debug_assert_eq!(assign_view.len(), 0); |
| 105 | + } |
| 106 | + unsafe { |
| 107 | + Ok(res.assume_init()) |
108 | 108 | } |
109 | | - Ok(res) |
110 | 109 | } |
111 | 110 |
|
112 | 111 | /// Stack arrays along the new axis. |
@@ -158,22 +157,24 @@ where |
158 | 157 |
|
159 | 158 | res_dim.set_axis(axis, arrays.len()); |
160 | 159 |
|
161 | | - // we can safely use uninitialized values here because they are Copy |
162 | | - // and we will only ever write to them |
163 | | - let size = res_dim.size(); |
164 | | - let mut v = Vec::with_capacity(size); |
165 | | - unsafe { |
166 | | - v.set_len(size); |
167 | | - } |
168 | | - let mut res = Array::from_shape_vec(res_dim, v)?; |
| 160 | + // we can safely use uninitialized values here because we will |
| 161 | + // overwrite every one of them. |
| 162 | + let mut res = Array::maybe_uninit(res_dim); |
169 | 163 |
|
170 | 164 | res.axis_iter_mut(axis) |
171 | 165 | .zip(arrays.iter()) |
172 | | - .for_each(|(mut assign_view, array)| { |
173 | | - assign_view.assign(&array); |
| 166 | + .for_each(|(assign_view, array)| { |
| 167 | + // assign_view is D::Larger::Smaller which is usually == D |
| 168 | + // (but if D is Ix6, we have IxD != Ix6 here; differing types |
| 169 | + // but same number of axes). |
| 170 | + let assign_view = assign_view.into_dimensionality::<D>() |
| 171 | + .expect("same-dimensionality cast"); |
| 172 | + assign_to(array, assign_view); |
174 | 173 | }); |
175 | 174 |
|
176 | | - Ok(res) |
| 175 | + unsafe { |
| 176 | + Ok(res.assume_init()) |
| 177 | + } |
177 | 178 | } |
178 | 179 |
|
179 | 180 | /// Stack arrays along the new axis. |
|
0 commit comments