Skip to content

Commit 13c1999

Browse files
authored
Merge pull request #915 from jturner314/make-axisdescription-non-tuple
Make AxisDescription a non-tuple struct
2 parents a6fe82f + c264ecf commit 13c1999

File tree

4 files changed

+45
-34
lines changed

4 files changed

+45
-34
lines changed

examples/axis_ops.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ where
1414
{
1515
println!("Regularize:\n{:?}", a);
1616
// reverse all neg axes
17-
while let Some(ax) = a.axes().find(|ax| ax.stride() <= 0) {
18-
if ax.stride() == 0 {
17+
while let Some(ax) = a.axes().find(|ax| ax.stride <= 0) {
18+
if ax.stride == 0 {
1919
return Err(());
2020
}
2121
// reverse ax
22-
println!("Reverse {:?}", ax.axis());
23-
a.invert_axis(ax.axis());
22+
println!("Reverse {:?}", ax.axis);
23+
a.invert_axis(ax.axis);
2424
}
2525

2626
// sort by least stride
2727
let mut i = 0;
2828
let n = a.ndim();
29-
while let Some(ax) = a.axes().rev().skip(i).min_by_key(|ax| ax.stride().abs()) {
30-
a.swap_axes(n - 1 - i, ax.axis().index());
31-
println!("Swap {:?} <=> {}", ax.axis(), n - 1 - i);
29+
while let Some(ax) = a.axes().rev().skip(i).min_by_key(|ax| ax.stride.abs()) {
30+
a.swap_axes(n - 1 - i, ax.axis.index());
31+
println!("Swap {:?} <=> {}", ax.axis, n - 1 - i);
3232
i += 1;
3333
}
3434

src/dimension/axes.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ pub struct Axes<'a, D> {
4242

4343
/// Description of the axis, its length and its stride.
4444
#[derive(Debug)]
45-
pub struct AxisDescription(pub Axis, pub Ix, pub Ixs);
45+
pub struct AxisDescription {
46+
pub axis: Axis,
47+
pub len: usize,
48+
pub stride: isize,
49+
}
4650

4751
copy_and_clone!(AxisDescription);
4852

@@ -51,19 +55,22 @@ copy_and_clone!(AxisDescription);
5155
#[allow(clippy::len_without_is_empty)]
5256
impl AxisDescription {
5357
/// Return axis
58+
#[deprecated(note = "Use .axis field instead", since = "0.15.0")]
5459
#[inline(always)]
5560
pub fn axis(self) -> Axis {
56-
self.0
61+
self.axis
5762
}
5863
/// Return length
64+
#[deprecated(note = "Use .len field instead", since = "0.15.0")]
5965
#[inline(always)]
6066
pub fn len(self) -> Ix {
61-
self.1
67+
self.len
6268
}
6369
/// Return stride
70+
#[deprecated(note = "Use .stride field instead", since = "0.15.0")]
6471
#[inline(always)]
6572
pub fn stride(self) -> Ixs {
66-
self.2
73+
self.stride
6774
}
6875
}
6976

@@ -79,11 +86,11 @@ where
7986
fn next(&mut self) -> Option<Self::Item> {
8087
if self.start < self.end {
8188
let i = self.start.post_inc();
82-
Some(AxisDescription(
83-
Axis(i),
84-
self.dim[i],
85-
self.strides[i] as Ixs,
86-
))
89+
Some(AxisDescription {
90+
axis: Axis(i),
91+
len: self.dim[i],
92+
stride: self.strides[i] as Ixs,
93+
})
8794
} else {
8895
None
8996
}
@@ -94,7 +101,11 @@ where
94101
F: FnMut(B, AxisDescription) -> B,
95102
{
96103
(self.start..self.end)
97-
.map(move |i| AxisDescription(Axis(i), self.dim[i], self.strides[i] as isize))
104+
.map(move |i| AxisDescription {
105+
axis: Axis(i),
106+
len: self.dim[i],
107+
stride: self.strides[i] as isize,
108+
})
98109
.fold(init, f)
99110
}
100111

@@ -111,11 +122,11 @@ where
111122
fn next_back(&mut self) -> Option<Self::Item> {
112123
if self.start < self.end {
113124
let i = self.end.pre_dec();
114-
Some(AxisDescription(
115-
Axis(i),
116-
self.dim[i],
117-
self.strides[i] as Ixs,
118-
))
125+
Some(AxisDescription {
126+
axis: Axis(i),
127+
len: self.dim[i],
128+
stride: self.strides[i] as Ixs,
129+
})
119130
} else {
120131
None
121132
}

src/dimension/dimension_trait.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ pub trait Dimension:
332332
};
333333
axes_of(self, strides)
334334
.rev()
335-
.min_by_key(|ax| ax.stride().abs())
336-
.map_or(Axis(n - 1), |ax| ax.axis())
335+
.min_by_key(|ax| ax.stride.abs())
336+
.map_or(Axis(n - 1), |ax| ax.axis)
337337
}
338338

339339
/// Compute the maximum stride axis (absolute value), under the constraint
@@ -346,9 +346,9 @@ pub trait Dimension:
346346
_ => {}
347347
}
348348
axes_of(self, strides)
349-
.filter(|ax| ax.len() > 1)
350-
.max_by_key(|ax| ax.stride().abs())
351-
.map_or(Axis(0), |ax| ax.axis())
349+
.filter(|ax| ax.len > 1)
350+
.max_by_key(|ax| ax.stride.abs())
351+
.map_or(Axis(0), |ax| ax.axis)
352352
}
353353

354354
/// Convert the dimensional into a dynamic dimensional (IxDyn).

src/impl_methods.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -557,16 +557,16 @@ where
557557
where
558558
F: FnMut(AxisDescription) -> Slice,
559559
{
560-
(0..self.ndim()).for_each(|ax| {
560+
for ax in 0..self.ndim() {
561561
self.slice_axis_inplace(
562562
Axis(ax),
563-
f(AxisDescription(
564-
Axis(ax),
565-
self.dim[ax],
566-
self.strides[ax] as isize,
567-
)),
563+
f(AxisDescription {
564+
axis: Axis(ax),
565+
len: self.dim[ax],
566+
stride: self.strides[ax] as isize,
567+
}),
568568
)
569-
})
569+
}
570570
}
571571

572572
/// Return a reference to the element at `index`, or return `None`

0 commit comments

Comments
 (0)