@@ -5,18 +5,24 @@ use crate::Layout;
5
5
use crate :: NdProducer ;
6
6
use crate :: Slice ;
7
7
8
+ #[ derive( Clone ) ]
9
+ pub struct GeneralWindow ;
10
+ #[ derive( Clone ) ]
11
+ pub struct AxisWindow { pub ( crate ) index : usize }
12
+
8
13
/// Window producer and iterable
9
14
///
10
15
/// See [`.windows()`](ArrayBase::windows) for more
11
16
/// information.
12
- pub struct Windows < ' a , A , D > {
17
+ pub struct Windows < ' a , A , D , V > {
13
18
base : ArrayView < ' a , A , D > ,
14
19
window : D ,
15
20
strides : D ,
21
+ variant : V ,
16
22
}
17
23
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
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
20
26
where
21
27
E : IntoDimension < Dim = D > ,
22
28
{
@@ -26,10 +32,15 @@ impl<'a, A, D: Dimension> Windows<'a, A, D> {
26
32
let mut unit_stride = D :: zeros ( ndim) ;
27
33
unit_stride. slice_mut ( ) . fill ( 1 ) ;
28
34
29
- Windows :: new_with_stride ( a, window, unit_stride)
35
+ Windows :: new_with_stride ( a, window, unit_stride, variant )
30
36
}
31
37
32
- pub ( crate ) fn new_with_stride < E > ( a : ArrayView < ' a , A , D > , window_size : E , axis_strides : E ) -> Self
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
33
44
where
34
45
E : IntoDimension < Dim = D > ,
35
46
{
@@ -77,6 +88,7 @@ impl<'a, A, D: Dimension> Windows<'a, A, D> {
77
88
base,
78
89
window,
79
90
strides : window_strides,
91
+ variant,
80
92
}
81
93
}
82
94
}
@@ -88,8 +100,9 @@ impl_ndproducer! {
88
100
base,
89
101
window,
90
102
strides,
103
+ variant,
91
104
}
92
- Windows <' a, A , D > {
105
+ Windows <' a, A , D , GeneralWindow > {
93
106
type Item = ArrayView <' a, A , D >;
94
107
type Dim = D ;
95
108
@@ -100,7 +113,7 @@ impl_ndproducer! {
100
113
}
101
114
}
102
115
103
- impl < ' a , A , D > IntoIterator for Windows < ' a , A , D >
116
+ impl < ' a , A , D , V > IntoIterator for Windows < ' a , A , D , V >
104
117
where
105
118
D : Dimension ,
106
119
A : ' a ,
@@ -148,55 +161,14 @@ impl_iterator! {
148
161
}
149
162
}
150
163
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 > {
164
+ impl < ' a , A , D : Dimension > NdProducer for Windows < ' a , A , D , AxisWindow > {
193
165
type Item = ArrayView < ' a , A , D > ;
194
166
type Dim = Ix1 ;
195
167
type Ptr = * mut A ;
196
168
type Stride = isize ;
197
169
198
170
fn raw_dim ( & self ) -> Ix1 {
199
- Ix1 ( self . base . raw_dim ( ) [ self . axis_idx ] )
171
+ Ix1 ( self . base . raw_dim ( ) [ self . variant . index ] )
200
172
}
201
173
202
174
fn layout ( & self ) -> Layout {
@@ -212,54 +184,38 @@ impl<'a, A, D: Dimension> NdProducer for AxisWindows<'a, A, D> {
212
184
}
213
185
214
186
unsafe fn as_ref ( & self , ptr : * mut A ) -> Self :: Item {
215
- ArrayView :: new_ ( ptr, self . window . clone ( ) ,
216
- self . strides . clone ( ) )
187
+ ArrayView :: new_ ( ptr, self . window . clone ( ) , self . strides . clone ( ) )
217
188
}
218
189
219
190
unsafe fn uget_ptr ( & self , i : & Self :: Dim ) -> * mut A {
220
191
let mut d = D :: zeros ( self . base . ndim ( ) ) ;
221
- d[ self . axis_idx ] = i[ 0 ] ;
192
+ d[ self . variant . index ] = i[ 0 ] ;
222
193
self . base . uget_ptr ( & d)
223
194
}
224
195
225
196
fn stride_of ( & self , axis : Axis ) -> isize {
226
197
assert_eq ! ( axis, Axis ( 0 ) ) ;
227
- self . base . stride_of ( Axis ( self . axis_idx ) )
198
+ self . base . stride_of ( Axis ( self . variant . index ) )
228
199
}
229
200
230
201
fn split_at ( self , axis : Axis , index : usize ) -> ( Self , Self ) {
231
202
assert_eq ! ( axis, Axis ( 0 ) ) ;
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
- }
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 ! { }
265
221
}
0 commit comments