1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use std::iter::FusedIterator;
use std::ops::Range;
use std::ptr::{slice_from_raw_parts, slice_from_raw_parts_mut};
use imgref::Img;
use crate::iter::{IterPtr, IterPtrMut};
use crate::{slice_ptr_len, slice_ptr_len_mut};
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct IterWindowsPtr<T>(*const [T], usize, usize, Range<usize>);
unsafe impl<T: Sync> Send for IterWindowsPtr<T> {}
unsafe impl<T> Sync for IterWindowsPtr<T> {}
impl<T> IterWindowsPtr<T> {
/// Creates a new [`IterWindowsPtr`]:
///
/// - `slice` is the slice that will be returned by the first iteration;
/// - `slice_stride` is the stride of `slice`;
/// - `iter_stride` is how far the slice will move each iteration;
/// - `len` is how many iterations
///
/// For example, for an iterator over rows, this would be:
///
/// - the first row of the image;
/// - `1`;
/// - the stride of the image;
/// - the height of the image
///
/// For an iterator over cols:
///
/// - the first column of the image;
/// - the stride of the image;
/// - `1`;
/// - the width of the image
///
/// # Safety
///
/// The provided slice must be valid for the lifetime of the returned
/// [`IterWindowsPtr`]. There must be at least `iter_stride` elements
/// available past the end of the provided slice.
///
/// # Panics
///
/// Panics if the slice does not start and end on an element.
#[inline]
pub unsafe fn new(slice: *const [T], slice_stride: usize, iter_stride: usize, len: usize) -> Self {
assert!(IterPtr::is_slice_perfect(slice_ptr_len(slice), slice_stride));
Self::new_unchecked(slice, slice_stride, iter_stride, len)
}
/// Same as `new`, but does not verify the slice length.
///
/// # Safety
///
/// All safety invariants of `new` must be upheld, and the slice must start
/// and end on an element.
#[inline]
pub unsafe fn new_unchecked(slice: *const [T], slice_stride: usize, iter_stride: usize, len: usize) -> Self {
Self(slice, slice_stride, iter_stride, 0..len)
}
/// Creates a new [`IterWindowsPtr`] over the rows of an [`Img`].
///
/// # Safety
///
/// The buffer must be valid for the lifetime of the returned iterator.
///
/// # Panics
///
/// Panics if the provided buffer has a width and height too large to fit in
/// its backing store.
#[inline]
pub unsafe fn rows<S: AsRef<[T]>>(buf: &Img<S>) -> Self {
let (width, height, stride) = (buf.width(), buf.height(), buf.stride());
let buf = buf.buf().as_ref() as *const [T];
Self::rows_ptr(Img::new_stride(buf, width, height, stride))
}
/// Creates a new [`IterWindowsPtr`] over the rows of an [`Img`].
///
/// # Safety
///
/// The buffer must be valid for the lifetime of the returned iterator.
///
/// # Panics
///
/// Panics if the provided buffer has a width and height too large to fit in
/// its backing store.
#[inline]
pub unsafe fn rows_ptr(buf: Img<*const [T]>) -> Self {
IterPtr::assert_slice_enough(buf);
let (width, height, stride) = (buf.width(), buf.height(), buf.stride());
let first_row = slice_from_raw_parts(buf.buf().cast::<T>(), width);
Self::new_unchecked(first_row, 1, stride, height)
}
/// Creates a new [`IterWindowsPtr`] over the cols of an [`Img`].
///
/// # Safety
///
/// The buffer must be valid for the lifetime of the returned iterator.
///
/// # Panics
///
/// Panics if the provided buffer has a width and height too large to fit in
/// its backing store.
#[inline]
pub unsafe fn cols<S: AsRef<[T]>>(buf: &Img<S>) -> Self {
let (width, height, stride) = (buf.width(), buf.height(), buf.stride());
let buf = buf.buf().as_ref() as *const [T];
Self::cols_ptr(Img::new_stride(buf, width, height, stride))
}
/// Creates a new [`IterWindowsPtr`] over the cols of an [`Img`].
///
/// # Safety
///
/// The buffer must be valid for the lifetime of the returned iterator.
///
/// # Panics
///
/// Panics if the provided buffer has a width and height too large to fit in
/// its backing store.
#[inline]
pub unsafe fn cols_ptr(buf: Img<*const [T]>) -> Self {
IterPtr::assert_slice_enough(buf);
let (width, height, stride) = (buf.width(), buf.height(), buf.stride());
let first_col = slice_from_raw_parts(buf.buf().cast::<T>(), stride * (height - 1) + 1);
Self::new_unchecked(first_col, buf.stride(), 1, width)
}
#[inline]
unsafe fn window(&self, offset: usize) -> *const [T] {
let data = self.0.cast::<T>().add(offset);
let len = slice_ptr_len(self.0);
slice_from_raw_parts(data, len)
}
}
impl<T> Iterator for IterWindowsPtr<T> {
type Item = IterPtr<T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.3.next().map(|index| unsafe { IterPtr::new(self.window(index * self.2), self.1) })
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}
impl<T> DoubleEndedIterator for IterWindowsPtr<T> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.3.next_back().map(|index| unsafe { IterPtr::new(self.window(index * self.2), self.1) })
}
}
impl<T> ExactSizeIterator for IterWindowsPtr<T> {
#[inline]
fn len(&self) -> usize {
self.3.len()
}
}
impl<T> FusedIterator for IterWindowsPtr<T> {}
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct IterWindowsPtrMut<T>(*mut [T], usize, usize, Range<usize>);
unsafe impl<T: Send> Send for IterWindowsPtrMut<T> {}
unsafe impl<T> Sync for IterWindowsPtrMut<T> {}
impl<T> IterWindowsPtrMut<T> {
/// Creates a new [`IterWindowsPtrMut`]:
///
/// - `slice` is the slice that will be returned by the first iteration;
/// - `slice_stride` is the stride of `slice`;
/// - `iter_stride` is how far the slice will move each iteration;
/// - `len` is how many iterations
///
/// For example, for an iterator over rows, this would be:
///
/// - the first row of the image;
/// - `1`;
/// - the stride of the image;
/// - the height of the image
///
/// For an iterator over cols:
///
/// - the first column of the image;
/// - the stride of the image;
/// - `1`;
/// - the width of the image
///
/// # Safety
///
/// The provided slice must be valid for the lifetime of the returned
/// [`IterWindowsPtrMut`]. There must be at least `iter_stride` elements
/// available past the end of the provided slice.
///
/// # Panics
///
/// Panics if the slice does not start and end on an element.
#[inline]
pub unsafe fn new(slice: *mut [T], slice_stride: usize, iter_stride: usize, len: usize) -> Self {
assert!(IterPtr::is_slice_perfect(slice_ptr_len_mut(slice), slice_stride));
Self::new_unchecked(slice, slice_stride, iter_stride, len)
}
/// Same as `new`, but does not verify the slice length.
///
/// # Safety
///
/// All safety invariants of `new` must be upheld, and the slice must start
/// and end on an element.
#[inline]
pub unsafe fn new_unchecked(slice: *mut [T], slice_stride: usize, iter_stride: usize, len: usize) -> Self {
Self(slice, slice_stride, iter_stride, 0..len)
}
/// Creates a new [`IterWindowsPtrMut`] over the rows of an [`Img`].
///
/// # Safety
///
/// The buffer must be valid for the lifetime of the returned iterator.
///
/// # Panics
///
/// Panics if the provided buffer has a width and height too large to fit in
/// its backing store.
#[inline]
pub unsafe fn rows<S: AsMut<[T]>>(buf: &mut Img<S>) -> Self {
let (width, height, stride) = (buf.width(), buf.height(), buf.stride());
let buf = buf.buf_mut().as_mut() as *mut [T];
Self::rows_ptr(Img::new_stride(buf, width, height, stride))
}
/// Creates a new [`IterWindowsPtrMut`] over the rows of an [`Img`].
///
/// # Safety
///
/// The buffer must be valid for the lifetime of the returned iterator.
///
/// # Panics
///
/// Panics if the provided buffer has a width and height too large to fit in
/// its backing store.
#[inline]
pub unsafe fn rows_ptr(buf: Img<*mut [T]>) -> Self {
IterPtrMut::assert_slice_enough(buf);
let (width, height, stride) = (buf.width(), buf.height(), buf.stride());
let first_row = slice_from_raw_parts_mut(buf.buf().cast::<T>(), width);
Self::new_unchecked(first_row, 1, stride, height)
}
/// Creates a new [`IterWindowsPtrMut`] over the cols of an [`Img`].
///
/// # Safety
///
/// The buffer must be valid for the lifetime of the returned iterator.
///
/// # Panics
///
/// Panics if the provided buffer has a width and height too large to fit in
/// its backing store.
#[inline]
pub unsafe fn cols<S: AsMut<[T]>>(buf: &mut Img<S>) -> Self {
let (width, height, stride) = (buf.width(), buf.height(), buf.stride());
let buf = buf.buf_mut().as_mut() as *mut [T];
Self::cols_ptr(Img::new_stride(buf, width, height, stride))
}
/// Creates a new [`IterWindowsPtrMut`] over the cols of an [`Img`].
///
/// # Safety
///
/// The buffer must be valid for the lifetime of the returned iterator.
///
/// # Panics
///
/// Panics if the provided buffer has a width and height too large to fit in
/// its backing store.
#[inline]
pub unsafe fn cols_ptr(buf: Img<*mut [T]>) -> Self {
IterPtrMut::assert_slice_enough(buf);
let (width, height, stride) = (buf.width(), buf.height(), buf.stride());
let first_col = slice_from_raw_parts_mut(buf.buf().cast::<T>(), stride * (height - 1) + 1);
Self::new_unchecked(first_col, buf.stride(), 1, width)
}
#[inline]
unsafe fn window(&self, offset: usize) -> *mut [T] {
let data = self.0.cast::<T>().add(offset);
let len = slice_ptr_len_mut(self.0);
slice_from_raw_parts_mut(data, len)
}
}
impl<T> Iterator for IterWindowsPtrMut<T> {
type Item = IterPtrMut<T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.3.next().map(|index| unsafe { IterPtrMut::new(self.window(index * self.2), self.1) })
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}
impl<T> DoubleEndedIterator for IterWindowsPtrMut<T> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.3.next_back().map(|index| unsafe { IterPtrMut::new(self.window(index * self.2), self.1) })
}
}
impl<T> ExactSizeIterator for IterWindowsPtrMut<T> {
#[inline]
fn len(&self) -> usize {
self.3.len()
}
}
impl<T> FusedIterator for IterWindowsPtrMut<T> {}