Skip to content

Commit 51bccf9

Browse files
committed
Revert "Reduce code duplication"
This reverts commit d5d7c72.
1 parent d5d7c72 commit 51bccf9

File tree

4 files changed

+96
-58
lines changed

4 files changed

+96
-58
lines changed

src/impl_methods.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ use rawpointer::PointerExt;
1414

1515
use crate::imp_prelude::*;
1616

17-
use crate::iterators::AxisWindow;
18-
use crate::iterators::GeneralWindow;
1917
use crate::{arraytraits, DimMax};
2018
use crate::argument_traits::AssignElem;
2119
use crate::dimension;
@@ -36,7 +34,7 @@ use crate::zip::{IntoNdProducer, Zip};
3634

3735
use crate::iter::{
3836
AxisChunksIter, AxisChunksIterMut, AxisIter, AxisIterMut, ExactChunks, ExactChunksMut,
39-
IndexedIter, IndexedIterMut, Iter, IterMut, Lanes, LanesMut, Windows
37+
IndexedIter, IndexedIterMut, Iter, IterMut, Lanes, LanesMut, Windows, AxisWindows
4038
};
4139
use crate::slice::{MultiSliceArg, SliceArg};
4240
use crate::stacking::concatenate;
@@ -1421,12 +1419,12 @@ where
14211419
/// that fit into the array's shape.
14221420
///
14231421
/// This is essentially equivalent to [`.windows_with_stride()`] with unit stride.
1424-
pub fn windows<E>(&self, window_size: E) -> Windows<'_, A, D, GeneralWindow>
1422+
pub fn windows<E>(&self, window_size: E) -> Windows<'_, A, D>
14251423
where
14261424
E: IntoDimension<Dim = D>,
14271425
S: Data,
14281426
{
1429-
Windows::new(self.view(), window_size, GeneralWindow)
1427+
Windows::new(self.view(), window_size)
14301428
}
14311429

14321430
/// Return a window producer and iterable.
@@ -1473,12 +1471,12 @@ where
14731471
/// ┃ a₂₀ ┃ a₂₁ ┃ │ │ │ │ ┃ a₂₂ ┃ a₂₃ ┃
14741472
/// ┗━━━━━┻━━━━━┹─────┴─────┘ └─────┴─────┺━━━━━┻━━━━━┛
14751473
/// ```
1476-
pub fn windows_with_stride<E>(&self, window_size: E, stride: E) -> Windows<'_, A, D, GeneralWindow>
1474+
pub fn windows_with_stride<E>(&self, window_size: E, stride: E) -> Windows<'_, A, D>
14771475
where
14781476
E: IntoDimension<Dim = D>,
14791477
S: Data,
14801478
{
1481-
Windows::new_with_stride(self.view(), window_size, stride, GeneralWindow)
1479+
Windows::new_with_stride(self.view(), window_size, stride)
14821480
}
14831481

14841482
/// Returns a producer which traverses over all windows of a given length along an axis.
@@ -1502,7 +1500,7 @@ where
15021500
/// assert_eq!(window.shape(), &[4, 3, 2]);
15031501
/// }
15041502
/// ```
1505-
pub fn axis_windows(&self, axis: Axis, window_size: usize) -> Windows<'_, A, D, AxisWindow>
1503+
pub fn axis_windows(&self, axis: Axis, window_size: usize) -> AxisWindows<'_, A, D>
15061504
where
15071505
S: Data,
15081506
{
@@ -1519,10 +1517,7 @@ where
15191517
self.shape()
15201518
);
15211519

1522-
let mut size = self.raw_dim();
1523-
size[axis_index] = window_size;
1524-
1525-
Windows::new(self.view(), size, AxisWindow{index: axis_index})
1520+
AxisWindows::new(self.view(), axis, window_size)
15261521
}
15271522

15281523
// Return (length, stride) for diagonal

src/iterators/iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ pub use crate::indexes::{Indices, IndicesIter};
1111
pub use crate::iterators::{
1212
AxisChunksIter, AxisChunksIterMut, AxisIter, AxisIterMut, ExactChunks, ExactChunksIter,
1313
ExactChunksIterMut, ExactChunksMut, IndexedIter, IndexedIterMut, Iter, IterMut, Lanes,
14-
LanesIter, LanesIterMut, LanesMut, Windows
14+
LanesIter, LanesIterMut, LanesMut, Windows, AxisWindows
1515
};

src/iterators/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ use super::{Dimension, Ix, Ixs};
2626

2727
pub use self::chunks::{ExactChunks, ExactChunksIter, ExactChunksIterMut, ExactChunksMut};
2828
pub use self::lanes::{Lanes, LanesMut};
29-
pub use self::windows::Windows;
30-
pub(crate) use self::windows::{GeneralWindow, AxisWindow};
29+
pub use self::windows::{Windows, AxisWindows};
3130
pub use self::into_iter::IntoIter;
3231

3332
use std::slice::{self, Iter as SliceIter, IterMut as SliceIterMut};

src/iterators/windows.rs

+87-43
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,18 @@ use crate::Layout;
55
use crate::NdProducer;
66
use crate::Slice;
77

8-
#[derive(Clone)]
9-
pub struct GeneralWindow;
10-
#[derive(Clone)]
11-
pub struct AxisWindow{pub(crate) index: usize}
12-
138
/// Window producer and iterable
149
///
1510
/// See [`.windows()`](ArrayBase::windows) for more
1611
/// information.
17-
pub struct Windows<'a, A, D, V> {
12+
pub struct Windows<'a, A, D> {
1813
base: ArrayView<'a, A, D>,
1914
window: D,
2015
strides: D,
21-
variant: V,
2216
}
2317

24-
impl<'a, A, D: Dimension, V> Windows<'a, A, D, V> {
25-
pub(crate) fn new<E>(a: ArrayView<'a, A, D>, window_size: E, variant: V) -> Self
18+
impl<'a, A, D: Dimension> Windows<'a, A, D> {
19+
pub(crate) fn new<E>(a: ArrayView<'a, A, D>, window_size: E) -> Self
2620
where
2721
E: IntoDimension<Dim = D>,
2822
{
@@ -32,15 +26,10 @@ impl<'a, A, D: Dimension, V> Windows<'a, A, D, V> {
3226
let mut unit_stride = D::zeros(ndim);
3327
unit_stride.slice_mut().fill(1);
3428

35-
Windows::new_with_stride(a, window, unit_stride, variant)
29+
Windows::new_with_stride(a, window, unit_stride)
3630
}
3731

38-
pub(crate) fn new_with_stride<E>(
39-
a: ArrayView<'a, A, D>,
40-
window_size: E,
41-
axis_strides: E,
42-
variant: V,
43-
) -> Self
32+
pub(crate) fn new_with_stride<E>(a: ArrayView<'a, A, D>, window_size: E, axis_strides: E) -> Self
4433
where
4534
E: IntoDimension<Dim = D>,
4635
{
@@ -88,7 +77,6 @@ impl<'a, A, D: Dimension, V> Windows<'a, A, D, V> {
8877
base,
8978
window,
9079
strides: window_strides,
91-
variant,
9280
}
9381
}
9482
}
@@ -100,9 +88,8 @@ impl_ndproducer! {
10088
base,
10189
window,
10290
strides,
103-
variant,
10491
}
105-
Windows<'a, A, D, GeneralWindow> {
92+
Windows<'a, A, D> {
10693
type Item = ArrayView<'a, A, D>;
10794
type Dim = D;
10895

@@ -113,7 +100,7 @@ impl_ndproducer! {
113100
}
114101
}
115102

116-
impl<'a, A, D, V> IntoIterator for Windows<'a, A, D, V>
103+
impl<'a, A, D> IntoIterator for Windows<'a, A, D>
117104
where
118105
D: Dimension,
119106
A: 'a,
@@ -161,14 +148,55 @@ impl_iterator! {
161148
}
162149
}
163150

164-
impl<'a, A, D: Dimension> NdProducer for Windows<'a, A, D, AxisWindow> {
151+
/// Window producer and iterable
152+
///
153+
/// See [`.axis_windows()`](ArrayBase::axis_windows) for more
154+
/// information.
155+
pub struct AxisWindows<'a, A, D>{
156+
base: ArrayView<'a, A, D>,
157+
axis_idx: usize,
158+
window: D,
159+
strides: D,
160+
}
161+
162+
impl<'a, A, D: Dimension> AxisWindows<'a, A, D> {
163+
pub(crate) fn new(a: ArrayView<'a, A, D>, axis: Axis, window_size: usize) -> Self
164+
{
165+
let strides = a.strides.clone();
166+
let mut base = a;
167+
let axis_idx = axis.index();
168+
let mut window = base.raw_dim();
169+
window[axis_idx] = window_size;
170+
171+
base.slice_each_axis_inplace(|ax_desc| {
172+
let len = ax_desc.len;
173+
let wsz = window[ax_desc.axis.index()];
174+
175+
if len < wsz {
176+
Slice::new(0, Some(0), 1)
177+
} else {
178+
Slice::new(0, Some((len - wsz + 1) as isize), 1)
179+
}
180+
});
181+
182+
AxisWindows {
183+
base,
184+
axis_idx,
185+
window,
186+
strides,
187+
}
188+
}
189+
}
190+
191+
192+
impl<'a, A, D: Dimension> NdProducer for AxisWindows<'a, A, D> {
165193
type Item = ArrayView<'a, A, D>;
166194
type Dim = Ix1;
167195
type Ptr = *mut A;
168196
type Stride = isize;
169197

170198
fn raw_dim(&self) -> Ix1 {
171-
Ix1(self.base.raw_dim()[self.variant.index])
199+
Ix1(self.base.raw_dim()[self.axis_idx])
172200
}
173201

174202
fn layout(&self) -> Layout {
@@ -184,38 +212,54 @@ impl<'a, A, D: Dimension> NdProducer for Windows<'a, A, D, AxisWindow> {
184212
}
185213

186214
unsafe fn as_ref(&self, ptr: *mut A) -> Self::Item {
187-
ArrayView::new_(ptr, self.window.clone(), self.strides.clone())
215+
ArrayView::new_(ptr, self.window.clone(),
216+
self.strides.clone())
188217
}
189218

190219
unsafe fn uget_ptr(&self, i: &Self::Dim) -> *mut A {
191220
let mut d = D::zeros(self.base.ndim());
192-
d[self.variant.index] = i[0];
221+
d[self.axis_idx] = i[0];
193222
self.base.uget_ptr(&d)
194223
}
195224

196225
fn stride_of(&self, axis: Axis) -> isize {
197226
assert_eq!(axis, Axis(0));
198-
self.base.stride_of(Axis(self.variant.index))
227+
self.base.stride_of(Axis(self.axis_idx))
199228
}
200229

201230
fn split_at(self, axis: Axis, index: usize) -> (Self, Self) {
202231
assert_eq!(axis, Axis(0));
203-
let (a, b) = self.base.split_at(Axis(self.variant.index), index);
204-
(
205-
Windows {
206-
base: a,
207-
window: self.window.clone(),
208-
strides: self.strides.clone(),
209-
variant: self.variant.clone(),
210-
},
211-
Windows {
212-
base: b,
213-
window: self.window,
214-
strides: self.strides,
215-
variant: self.variant,
216-
},
217-
)
218-
}
219-
220-
private_impl! {}
232+
let (a, b) = self.base.split_at(Axis(self.axis_idx), index);
233+
(AxisWindows {
234+
base: a,
235+
axis_idx: self.axis_idx,
236+
window: self.window.clone(),
237+
strides: self.strides.clone()
238+
239+
},
240+
AxisWindows {
241+
base: b,
242+
axis_idx: self.axis_idx,
243+
window: self.window,
244+
strides: self.strides,
245+
})
246+
}
247+
248+
private_impl!{}
249+
}
250+
251+
impl<'a, A, D> IntoIterator for AxisWindows<'a, A, D>
252+
where
253+
D: Dimension,
254+
A: 'a,
255+
{
256+
type Item = <Self::IntoIter as Iterator>::Item;
257+
type IntoIter = WindowsIter<'a, A, D>;
258+
fn into_iter(self) -> Self::IntoIter {
259+
WindowsIter {
260+
iter: self.base.into_elements_base(),
261+
window: self.window,
262+
strides: self.strides,
263+
}
264+
}
221265
}

0 commit comments

Comments
 (0)