Skip to content

Commit f7d4114

Browse files
committed
ixdyn: Implement .remove_axis()
1 parent 2af8bac commit f7d4114

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

src/dimension/dynindeximpl.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::ops::{
66
DerefMut,
77
};
88
use imp_prelude::*;
9+
use dimension::DimPrivate;
910

1011
const CAP: usize = 4;
1112

@@ -124,8 +125,24 @@ unsafe impl Send for IxDynImpl {}
124125
unsafe impl Sync for IxDynImpl {}
125126

126127
impl IxDynImpl {
127-
pub fn remove(&mut self, i: usize) {
128-
unimplemented!()
128+
fn remove(&self, i: usize) -> Self {
129+
IxDynImpl(match self.0 {
130+
IxDynRepr::Inline(0, _) => IxDynRepr::Inline(0, [0; CAP]),
131+
IxDynRepr::Inline(1, _) => IxDynRepr::Inline(0, [0; CAP]),
132+
IxDynRepr::Inline(2, ref arr) => {
133+
let mut out = [0; CAP];
134+
out[0] = arr[1 - i];
135+
IxDynRepr::Inline(1, out)
136+
}
137+
ref ixdyn => {
138+
let len = ixdyn.len();
139+
let mut result = IxDynRepr::copy_from(&ixdyn[..len - 1]);
140+
for j in i..len - 1 {
141+
result[j] = ixdyn[j + 1]
142+
}
143+
result
144+
}
145+
})
129146
}
130147
}
131148

@@ -183,3 +200,11 @@ impl<'a> IntoIterator for &'a IxDynImpl {
183200
self[..].into_iter()
184201
}
185202
}
203+
204+
impl RemoveAxis for Dim<IxDynImpl> {
205+
type Smaller = Self;
206+
fn remove_axis(&self, axis: Axis) -> Self {
207+
debug_assert!(axis.index() < self.ndim());
208+
Dim::new(self.ix().remove(axis.index()))
209+
}
210+
}

src/dimension/remove_axis.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99

1010
use {Ix, Ix0, Ix1, Dimension, Dim, Axis};
11-
use IxDyn;
1211
use super::DimPrivate;
1312

1413
/// Array shape with a next smaller dimension.
@@ -67,12 +66,4 @@ macro_rules! impl_remove_axis_array(
6766
impl_remove_axis_array!(3, 4, 5, 6);
6867

6968

70-
impl RemoveAxis for IxDyn {
71-
type Smaller = Self;
72-
fn remove_axis(&self, axis: Axis) -> Self {
73-
let mut res = self.clone();
74-
res.ixm().remove(axis.index());
75-
res
76-
}
77-
}
7869

tests/dimension.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
extern crate ndarray;
2+
#[macro_use]
3+
extern crate defmac;
24

35
use ndarray::{
46
RcArray,
@@ -18,7 +20,7 @@ fn remove_axis()
1820
assert_eq!(Dim([1, 2]).remove_axis(Axis(0)), Dim([2]));
1921
assert_eq!(Dim([4, 5, 6]).remove_axis(Axis(1)), Dim([4, 6]));
2022

21-
assert_eq!(Dim(vec![1,2]).remove_axis(Axis(0)), Dim(vec![2]));
23+
assert_eq!(Dim(vec![1, 2]).remove_axis(Axis(0)), Dim(vec![2]));
2224
assert_eq!(Dim(vec![4, 5, 6]).remove_axis(Axis(1)), Dim(vec![4, 6]));
2325

2426
let a = RcArray::<f32, _>::zeros((4,5));
@@ -43,6 +45,25 @@ fn dyn_dimension()
4345
assert_eq!(z.shape(), &dim[..]);
4446
}
4547

48+
#[test]
49+
fn dyn_remove() {
50+
let mut v = vec![1, 2, 3, 4, 5, 6, 7];
51+
let mut dim = Dim(v.clone());
52+
defmac!(test_remove index => {
53+
dim = dim.remove_axis(Axis(index));
54+
v.remove(index);
55+
assert_eq!(dim.slice(), &v[..]);
56+
});
57+
58+
test_remove!(1);
59+
test_remove!(2);
60+
test_remove!(3);
61+
test_remove!(0);
62+
test_remove!(2);
63+
test_remove!(0);
64+
test_remove!(0);
65+
}
66+
4667
#[test]
4768
fn fastest_varying_order() {
4869
let strides = Dim([2, 8, 4, 1]);

0 commit comments

Comments
 (0)