Skip to content

Commit 1347d05

Browse files
committed
Rename slice_inplace to slice_collapse
1 parent b71f5cc commit 1347d05

File tree

7 files changed

+48
-38
lines changed

7 files changed

+48
-38
lines changed

examples/axis_ops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn main() {
4747
}
4848
a.swap_axes(0, 1);
4949
a.swap_axes(0, 2);
50-
a.slice_inplace(s![.., ..;-1, ..]);
50+
a.slice_collapse(s![.., ..;-1, ..]);
5151
regularize(&mut a).ok();
5252

5353
let mut b = Array::<u8, _>::zeros((2, 3, 4));
@@ -64,6 +64,6 @@ fn main() {
6464
for (i, elt) in (0..).zip(&mut a) {
6565
*elt = i;
6666
}
67-
a.slice_inplace(s![..;-1, ..;2, ..]);
67+
a.slice_collapse(s![..;-1, ..;2, ..]);
6868
regularize(&mut a).ok();
6969
}

serialization-tests/tests/serialize.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn serial_many_dim()
5353
{
5454
// Test a sliced array.
5555
let mut a = RcArray::linspace(0., 31., 32).reshape((2, 2, 2, 4));
56-
a.slice_inplace(s![..;-1, .., .., ..2]);
56+
a.slice_collapse(s![..;-1, .., .., ..2]);
5757
let serial = json::encode(&a).unwrap();
5858
println!("Encode {:?} => {:?}", a, serial);
5959
let res = json::decode::<RcArray<f32, _>>(&serial);
@@ -114,7 +114,7 @@ fn serial_many_dim_serde()
114114
{
115115
// Test a sliced array.
116116
let mut a = RcArray::linspace(0., 31., 32).reshape((2, 2, 2, 4));
117-
a.slice_inplace(s![..;-1, .., .., ..2]);
117+
a.slice_collapse(s![..;-1, .., .., ..2]);
118118
let serial = serde_json::to_string(&a).unwrap();
119119
println!("Encode {:?} => {:?}", a, serial);
120120
let res = serde_json::from_str::<RcArray<f32, _>>(&serial);
@@ -221,7 +221,7 @@ fn serial_many_dim_serde_msgpack()
221221
{
222222
// Test a sliced array.
223223
let mut a = RcArray::linspace(0., 31., 32).reshape((2, 2, 2, 4));
224-
a.slice_inplace(s![..;-1, .., .., ..2]);
224+
a.slice_collapse(s![..;-1, .., .., ..2]);
225225

226226
let mut buf = Vec::new();
227227
serde::Serialize::serialize(&a, &mut rmp_serde::Serializer::new(&mut buf)).ok().unwrap();
@@ -273,7 +273,7 @@ fn serial_many_dim_ron()
273273
{
274274
// Test a sliced array.
275275
let mut a = RcArray::linspace(0., 31., 32).reshape((2, 2, 2, 4));
276-
a.slice_inplace(s![..;-1, .., .., ..2]);
276+
a.slice_collapse(s![..;-1, .., .., ..2]);
277277

278278
let a_s = ron_serialize(&a).unwrap();
279279

src/doc/ndarray_for_numpy_users/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@
229229
//! Only the non-mutable methods that take the array by reference are listed in
230230
//! this table. For example, [`.slice()`][.slice()] also has corresponding
231231
//! methods [`.slice_mut()`][.slice_mut()], [`.slice_move()`][.slice_move()], and
232-
//! [`.slice_inplace()`][.slice_inplace()].
232+
//! [`.slice_collapse()`][.slice_collapse()].
233233
//!
234234
//! * The behavior of slicing is slightly different from NumPy for slices with
235235
//! `step < -1`. See the docs for the [`s![]` macro][s!] for more details.
@@ -618,7 +618,7 @@
618618
//! [.sum()]: ../../struct.ArrayBase.html#method.sum
619619
//! [.slice()]: ../../struct.ArrayBase.html#method.slice
620620
//! [.slice_axis()]: ../../struct.ArrayBase.html#method.slice_axis
621-
//! [.slice_inplace()]: ../../struct.ArrayBase.html#method.slice_inplace
621+
//! [.slice_collapse()]: ../../struct.ArrayBase.html#method.slice_collapse
622622
//! [.slice_move()]: ../../struct.ArrayBase.html#method.slice_move
623623
//! [.slice_mut()]: ../../struct.ArrayBase.html#method.slice_mut
624624
//! [.shape()]: ../../struct.ArrayBase.html#method.shape

src/impl_methods.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
311311
Do: Dimension,
312312
{
313313
// Slice and collapse in-place without changing the number of dimensions.
314-
self.slice_inplace(&*info);
314+
self.slice_collapse(&*info);
315315

316316
let indices: &[SliceOrIndex] = (**info).as_ref();
317317

@@ -352,7 +352,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
352352
///
353353
/// **Panics** if an index is out of bounds or step size is zero.<br>
354354
/// (**Panics** if `D` is `IxDyn` and `indices` does not match the number of array axes.)
355-
pub fn slice_inplace(&mut self, indices: &D::SliceArg) {
355+
pub fn slice_collapse(&mut self, indices: &D::SliceArg) {
356356
let indices: &[SliceOrIndex] = indices.as_ref();
357357
assert_eq!(indices.len(), self.ndim());
358358
indices
@@ -369,6 +369,15 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
369369
});
370370
}
371371

372+
/// Slice the array in place without changing the number of dimensions.
373+
///
374+
/// **Panics** if an index is out of bounds or step size is zero.<br>
375+
/// (**Panics** if `D` is `IxDyn` and `indices` does not match the number of array axes.)
376+
#[deprecated(note="renamed to `slice_collapse`", since="0.12.1")]
377+
pub fn slice_inplace(&mut self, indices: &D::SliceArg) {
378+
self.slice_collapse(indices)
379+
}
380+
372381
/// Return a view of the array, sliced along the specified axis.
373382
///
374383
/// **Panics** if an index is out of bounds or step size is zero.<br>

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ pub type Ixs = isize;
444444
///
445445
/// You can use slicing to create a view of a subset of the data in
446446
/// the array. Slicing methods include [`.slice()`], [`.slice_mut()`],
447-
/// [`.slice_move()`], and [`.slice_inplace()`].
447+
/// [`.slice_move()`], and [`.slice_collapse()`].
448448
///
449449
/// The slicing argument can be passed using the macro [`s![]`](macro.s!.html),
450450
/// which will be used in all examples. (The explicit form is an instance of
@@ -455,13 +455,13 @@ pub type Ixs = isize;
455455
/// If a range is used, the axis is preserved. If an index is used, that index
456456
/// is selected and the axis is removed; this selects a subview. See
457457
/// [*Subviews*](#subviews) for more information about subviews. Note that
458-
/// [`.slice_inplace()`] behaves like [`.collapse_axis()`] by preserving the
459-
/// number of dimensions.
458+
/// [`.slice_collapse()`] behaves like [`.collapse_axis()`] by preserving
459+
/// the number of dimensions.
460460
///
461461
/// [`.slice()`]: #method.slice
462462
/// [`.slice_mut()`]: #method.slice_mut
463463
/// [`.slice_move()`]: #method.slice_move
464-
/// [`.slice_inplace()`]: #method.slice_inplace
464+
/// [`.slice_collapse()`]: #method.slice_collapse
465465
///
466466
/// ```
467467
/// // import the s![] macro

src/slice.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ impl_slicenextdim_larger!((), Slice);
466466
/// ] ]* `]`, where *axis-slice-or-index* is any of the following:
467467
///
468468
/// * *index*: an index to use for taking a subview with respect to that axis.
469-
/// (The index is selected and the axis is removed.)
469+
/// (The index is selected. The axis is removed except with
470+
/// [`.slice_collapse()`].)
470471
/// * *range*: a range with step size 1 to use for slicing that axis.
471472
/// * *range* `;` *step*: a range with step size *step* to use for slicing that axis.
472473
/// * *slice*: a [`Slice`] instance to use for slicing that axis.
@@ -487,12 +488,12 @@ impl_slicenextdim_larger!((), Slice);
487488
/// the third axis for 1..5 with default step size 1. The input array must have
488489
/// 3 dimensions. The resulting slice would have shape `[2, 4]` for
489490
/// [`.slice()`], [`.slice_mut()`], and [`.slice_move()`], and shape
490-
/// `[2, 1, 4]` for [`.slice_inplace()`].
491+
/// `[2, 1, 4]` for [`.slice_collapse()`].
491492
///
492493
/// [`.slice()`]: struct.ArrayBase.html#method.slice
493494
/// [`.slice_mut()`]: struct.ArrayBase.html#method.slice_mut
494495
/// [`.slice_move()`]: struct.ArrayBase.html#method.slice_move
495-
/// [`.slice_inplace()`]: struct.ArrayBase.html#method.slice_inplace
496+
/// [`.slice_collapse()`]: struct.ArrayBase.html#method.slice_collapse
496497
///
497498
/// See also [*Slicing*](struct.ArrayBase.html#slicing).
498499
///

tests/array.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ fn test_slice_array_fixed() {
173173
arr.slice(info);
174174
arr.slice_mut(info);
175175
arr.view().slice_move(info);
176-
arr.view().slice_inplace(info);
176+
arr.view().slice_collapse(info);
177177
}
178178

179179
#[test]
@@ -183,7 +183,7 @@ fn test_slice_dyninput_array_fixed() {
183183
arr.slice(info);
184184
arr.slice_mut(info);
185185
arr.view().slice_move(info);
186-
arr.view().slice_inplace(info.as_ref());
186+
arr.view().slice_collapse(info.as_ref());
187187
}
188188

189189
#[test]
@@ -197,7 +197,7 @@ fn test_slice_array_dyn() {
197197
arr.slice(info);
198198
arr.slice_mut(info);
199199
arr.view().slice_move(info);
200-
arr.view().slice_inplace(info);
200+
arr.view().slice_collapse(info);
201201
}
202202

203203
#[test]
@@ -211,7 +211,7 @@ fn test_slice_dyninput_array_dyn() {
211211
arr.slice(info);
212212
arr.slice_mut(info);
213213
arr.view().slice_move(info);
214-
arr.view().slice_inplace(info.as_ref());
214+
arr.view().slice_collapse(info.as_ref());
215215
}
216216

217217
#[test]
@@ -225,7 +225,7 @@ fn test_slice_dyninput_vec_fixed() {
225225
arr.slice(info.as_ref());
226226
arr.slice_mut(info.as_ref());
227227
arr.view().slice_move(info.as_ref());
228-
arr.view().slice_inplace(info.as_ref());
228+
arr.view().slice_collapse(info.as_ref());
229229
}
230230

231231
#[test]
@@ -239,7 +239,7 @@ fn test_slice_dyninput_vec_dyn() {
239239
arr.slice(info.as_ref());
240240
arr.slice_mut(info.as_ref());
241241
arr.view().slice_move(info.as_ref());
242-
arr.view().slice_inplace(info.as_ref());
242+
arr.view().slice_collapse(info.as_ref());
243243
}
244244

245245
#[test]
@@ -276,15 +276,15 @@ fn test_slice_with_subview() {
276276
}
277277

278278
#[test]
279-
fn test_slice_inplace_with_indices() {
279+
fn test_slice_collapse_with_indices() {
280280
let mut arr = RcArray::<usize, _>::zeros((3, 5, 4));
281281
for (i, elt) in arr.iter_mut().enumerate() {
282282
*elt = i;
283283
}
284284

285285
{
286286
let mut vi = arr.view();
287-
vi.slice_inplace(s![1.., 2, ..;2]);
287+
vi.slice_collapse(s![1.., 2, ..;2]);
288288
assert_eq!(vi.shape(), &[2, 1, 2]);
289289
assert!(
290290
vi.iter()
@@ -293,7 +293,7 @@ fn test_slice_inplace_with_indices() {
293293
);
294294

295295
let mut vi = arr.view();
296-
vi.slice_inplace(s![1, 2, ..;2]);
296+
vi.slice_collapse(s![1, 2, ..;2]);
297297
assert_eq!(vi.shape(), &[1, 1, 2]);
298298
assert!(
299299
vi.iter()
@@ -302,15 +302,15 @@ fn test_slice_inplace_with_indices() {
302302
);
303303

304304
let mut vi = arr.view();
305-
vi.slice_inplace(s![1, 2, 3]);
305+
vi.slice_collapse(s![1, 2, 3]);
306306
assert_eq!(vi.shape(), &[1, 1, 1]);
307307
assert_eq!(vi, Array3::from_elem((1, 1, 1), arr[(1, 2, 3)]));
308308
}
309309

310310
// Do it to the RcArray itself
311311
let elem = arr[(1, 2, 3)];
312312
let mut vi = arr;
313-
vi.slice_inplace(s![1, 2, 3]);
313+
vi.slice_collapse(s![1, 2, 3]);
314314
assert_eq!(vi.shape(), &[1, 1, 1]);
315315
assert_eq!(vi, Array3::from_elem((1, 1, 1), elem));
316316
}
@@ -458,7 +458,7 @@ fn test_cow()
458458
assert_eq!(n[[0, 1]], 0);
459459
assert_eq!(n.get((0, 1)), Some(&0));
460460
let mut rev = mat.reshape(4);
461-
rev.slice_inplace(s![..;-1]);
461+
rev.slice_collapse(s![..;-1]);
462462
assert_eq!(rev[0], 4);
463463
assert_eq!(rev[1], 3);
464464
assert_eq!(rev[2], 2);
@@ -483,7 +483,7 @@ fn test_cow_shrink()
483483
// mutation shrinks the array and gives it different strides
484484
//
485485
let mut mat = RcArray::zeros((2, 3));
486-
//mat.slice_inplace(s![.., ..;2]);
486+
//mat.slice_collapse(s![.., ..;2]);
487487
mat[[0, 0]] = 1;
488488
let n = mat.clone();
489489
mat[[0, 1]] = 2;
@@ -498,7 +498,7 @@ fn test_cow_shrink()
498498
assert_eq!(n.get((0, 1)), Some(&0));
499499
// small has non-C strides this way
500500
let mut small = mat.reshape(6);
501-
small.slice_inplace(s![4..;-1]);
501+
small.slice_collapse(s![4..;-1]);
502502
assert_eq!(small[0], 6);
503503
assert_eq!(small[1], 5);
504504
let before = small.clone();
@@ -678,7 +678,7 @@ fn assign()
678678
let mut a = arr2(&[[1, 2], [3, 4]]);
679679
{
680680
let mut v = a.view_mut();
681-
v.slice_inplace(s![..1, ..]);
681+
v.slice_collapse(s![..1, ..]);
682682
v.fill(0);
683683
}
684684
assert_eq!(a, arr2(&[[0, 0], [3, 4]]));
@@ -995,7 +995,7 @@ fn owned_array_discontiguous_drop() {
995995
let v: Vec<_> = (0..12).map(|x| InsertOnDrop(set.clone(), Some(x))).collect();
996996
let mut a = Array::from_shape_vec((2, 6), v).unwrap();
997997
// discontiguous and non-zero offset
998-
a.slice_inplace(s![.., 1..]);
998+
a.slice_collapse(s![.., 1..]);
999999
}
10001000
// each item was dropped exactly once
10011001
itertools::assert_equal(set.borrow().iter().cloned(), 0..12);
@@ -1561,7 +1561,7 @@ fn to_owned_memory_order() {
15611561
fn to_owned_neg_stride() {
15621562
let mut c = arr2(&[[1, 2, 3],
15631563
[4, 5, 6]]);
1564-
c.slice_inplace(s![.., ..;-1]);
1564+
c.slice_collapse(s![.., ..;-1]);
15651565
let co = c.to_owned();
15661566
assert_eq!(c, co);
15671567
}
@@ -1570,7 +1570,7 @@ fn to_owned_neg_stride() {
15701570
fn discontiguous_owned_to_owned() {
15711571
let mut c = arr2(&[[1, 2, 3],
15721572
[4, 5, 6]]);
1573-
c.slice_inplace(s![.., ..;2]);
1573+
c.slice_collapse(s![.., ..;2]);
15741574

15751575
let co = c.to_owned();
15761576
assert_eq!(c.strides(), &[3, 2]);
@@ -1738,10 +1738,10 @@ fn test_to_vec() {
17381738
[7, 8, 9],
17391739
[10,11,12]]);
17401740

1741-
a.slice_inplace(s![..;-1, ..]);
1741+
a.slice_collapse(s![..;-1, ..]);
17421742
assert_eq!(a.row(3).to_vec(), vec![1, 2, 3]);
17431743
assert_eq!(a.column(2).to_vec(), vec![12, 9, 6, 3]);
1744-
a.slice_inplace(s![.., ..;-1]);
1744+
a.slice_collapse(s![.., ..;-1]);
17451745
assert_eq!(a.row(3).to_vec(), vec![3, 2, 1]);
17461746
}
17471747

@@ -1757,7 +1757,7 @@ fn test_array_clone_unalias() {
17571757
#[test]
17581758
fn test_array_clone_same_view() {
17591759
let mut a = Array::from_iter(0..9).into_shape((3, 3)).unwrap();
1760-
a.slice_inplace(s![..;-1, ..;-1]);
1760+
a.slice_collapse(s![..;-1, ..;-1]);
17611761
let b = a.clone();
17621762
assert_eq!(a, b);
17631763
}

0 commit comments

Comments
 (0)