]>
git.proxmox.com Git - rustc.git/blob - vendor/rayon/src/slice/rchunks.rs
1 use crate::iter
::plumbing
::*;
3 use crate::math
::div_round_up
;
5 /// Parallel iterator over immutable non-overlapping chunks of a slice, starting at the end.
7 pub struct RChunks
<'data
, T
: Sync
> {
12 impl<'data
, T
: Sync
> RChunks
<'data
, T
> {
13 pub(super) fn new(chunk_size
: usize, slice
: &'data
[T
]) -> Self {
14 Self { chunk_size, slice }
18 impl<'data
, T
: Sync
> Clone
for RChunks
<'data
, T
> {
19 fn clone(&self) -> Self {
24 impl<'data
, T
: Sync
+ 'data
> ParallelIterator
for RChunks
<'data
, T
> {
25 type Item
= &'data
[T
];
27 fn drive_unindexed
<C
>(self, consumer
: C
) -> C
::Result
29 C
: UnindexedConsumer
<Self::Item
>,
31 bridge(self, consumer
)
34 fn opt_len(&self) -> Option
<usize> {
39 impl<'data
, T
: Sync
+ 'data
> IndexedParallelIterator
for RChunks
<'data
, T
> {
40 fn drive
<C
>(self, consumer
: C
) -> C
::Result
42 C
: Consumer
<Self::Item
>,
44 bridge(self, consumer
)
47 fn len(&self) -> usize {
48 div_round_up(self.slice
.len(), self.chunk_size
)
51 fn with_producer
<CB
>(self, callback
: CB
) -> CB
::Output
53 CB
: ProducerCallback
<Self::Item
>,
55 callback
.callback(RChunksProducer
{
56 chunk_size
: self.chunk_size
,
62 struct RChunksProducer
<'data
, T
: Sync
> {
67 impl<'data
, T
: 'data
+ Sync
> Producer
for RChunksProducer
<'data
, T
> {
68 type Item
= &'data
[T
];
69 type IntoIter
= ::std
::slice
::RChunks
<'data
, T
>;
71 fn into_iter(self) -> Self::IntoIter
{
72 self.slice
.rchunks(self.chunk_size
)
75 fn split_at(self, index
: usize) -> (Self, Self) {
76 let elem_index
= self.slice
.len().saturating_sub(index
* self.chunk_size
);
77 let (left
, right
) = self.slice
.split_at(elem_index
);
80 chunk_size
: self.chunk_size
,
84 chunk_size
: self.chunk_size
,
91 /// Parallel iterator over immutable non-overlapping chunks of a slice, starting at the end.
93 pub struct RChunksExact
<'data
, T
: Sync
> {
99 impl<'data
, T
: Sync
> RChunksExact
<'data
, T
> {
100 pub(super) fn new(chunk_size
: usize, slice
: &'data
[T
]) -> Self {
101 let rem_len
= slice
.len() % chunk_size
;
102 let (rem
, slice
) = slice
.split_at(rem_len
);
110 /// Return the remainder of the original slice that is not going to be
111 /// returned by the iterator. The returned slice has at most `chunk_size-1`
113 pub fn remainder(&self) -> &'data
[T
] {
118 impl<'data
, T
: Sync
> Clone
for RChunksExact
<'data
, T
> {
119 fn clone(&self) -> Self {
120 RChunksExact { ..*self }
124 impl<'data
, T
: Sync
+ 'data
> ParallelIterator
for RChunksExact
<'data
, T
> {
125 type Item
= &'data
[T
];
127 fn drive_unindexed
<C
>(self, consumer
: C
) -> C
::Result
129 C
: UnindexedConsumer
<Self::Item
>,
131 bridge(self, consumer
)
134 fn opt_len(&self) -> Option
<usize> {
139 impl<'data
, T
: Sync
+ 'data
> IndexedParallelIterator
for RChunksExact
<'data
, T
> {
140 fn drive
<C
>(self, consumer
: C
) -> C
::Result
142 C
: Consumer
<Self::Item
>,
144 bridge(self, consumer
)
147 fn len(&self) -> usize {
148 self.slice
.len() / self.chunk_size
151 fn with_producer
<CB
>(self, callback
: CB
) -> CB
::Output
153 CB
: ProducerCallback
<Self::Item
>,
155 callback
.callback(RChunksExactProducer
{
156 chunk_size
: self.chunk_size
,
162 struct RChunksExactProducer
<'data
, T
: Sync
> {
167 impl<'data
, T
: 'data
+ Sync
> Producer
for RChunksExactProducer
<'data
, T
> {
168 type Item
= &'data
[T
];
169 type IntoIter
= ::std
::slice
::RChunksExact
<'data
, T
>;
171 fn into_iter(self) -> Self::IntoIter
{
172 self.slice
.rchunks_exact(self.chunk_size
)
175 fn split_at(self, index
: usize) -> (Self, Self) {
176 let elem_index
= self.slice
.len() - index
* self.chunk_size
;
177 let (left
, right
) = self.slice
.split_at(elem_index
);
179 RChunksExactProducer
{
180 chunk_size
: self.chunk_size
,
183 RChunksExactProducer
{
184 chunk_size
: self.chunk_size
,
191 /// Parallel iterator over mutable non-overlapping chunks of a slice, starting at the end.
193 pub struct RChunksMut
<'data
, T
: Send
> {
195 slice
: &'data
mut [T
],
198 impl<'data
, T
: Send
> RChunksMut
<'data
, T
> {
199 pub(super) fn new(chunk_size
: usize, slice
: &'data
mut [T
]) -> Self {
200 Self { chunk_size, slice }
204 impl<'data
, T
: Send
+ 'data
> ParallelIterator
for RChunksMut
<'data
, T
> {
205 type Item
= &'data
mut [T
];
207 fn drive_unindexed
<C
>(self, consumer
: C
) -> C
::Result
209 C
: UnindexedConsumer
<Self::Item
>,
211 bridge(self, consumer
)
214 fn opt_len(&self) -> Option
<usize> {
219 impl<'data
, T
: Send
+ 'data
> IndexedParallelIterator
for RChunksMut
<'data
, T
> {
220 fn drive
<C
>(self, consumer
: C
) -> C
::Result
222 C
: Consumer
<Self::Item
>,
224 bridge(self, consumer
)
227 fn len(&self) -> usize {
228 div_round_up(self.slice
.len(), self.chunk_size
)
231 fn with_producer
<CB
>(self, callback
: CB
) -> CB
::Output
233 CB
: ProducerCallback
<Self::Item
>,
235 callback
.callback(RChunksMutProducer
{
236 chunk_size
: self.chunk_size
,
242 struct RChunksMutProducer
<'data
, T
: Send
> {
244 slice
: &'data
mut [T
],
247 impl<'data
, T
: 'data
+ Send
> Producer
for RChunksMutProducer
<'data
, T
> {
248 type Item
= &'data
mut [T
];
249 type IntoIter
= ::std
::slice
::RChunksMut
<'data
, T
>;
251 fn into_iter(self) -> Self::IntoIter
{
252 self.slice
.rchunks_mut(self.chunk_size
)
255 fn split_at(self, index
: usize) -> (Self, Self) {
256 let elem_index
= self.slice
.len().saturating_sub(index
* self.chunk_size
);
257 let (left
, right
) = self.slice
.split_at_mut(elem_index
);
260 chunk_size
: self.chunk_size
,
264 chunk_size
: self.chunk_size
,
271 /// Parallel iterator over mutable non-overlapping chunks of a slice, starting at the end.
273 pub struct RChunksExactMut
<'data
, T
: Send
> {
275 slice
: &'data
mut [T
],
279 impl<'data
, T
: Send
> RChunksExactMut
<'data
, T
> {
280 pub(super) fn new(chunk_size
: usize, slice
: &'data
mut [T
]) -> Self {
281 let rem_len
= slice
.len() % chunk_size
;
282 let (rem
, slice
) = slice
.split_at_mut(rem_len
);
290 /// Return the remainder of the original slice that is not going to be
291 /// returned by the iterator. The returned slice has at most `chunk_size-1`
294 /// Note that this has to consume `self` to return the original lifetime of
295 /// the data, which prevents this from actually being used as a parallel
296 /// iterator since that also consumes. This method is provided for parity
297 /// with `std::iter::RChunksExactMut`, but consider calling `remainder()` or
298 /// `take_remainder()` as alternatives.
299 pub fn into_remainder(self) -> &'data
mut [T
] {
303 /// Return the remainder of the original slice that is not going to be
304 /// returned by the iterator. The returned slice has at most `chunk_size-1`
307 /// Consider `take_remainder()` if you need access to the data with its
308 /// original lifetime, rather than borrowing through `&mut self` here.
309 pub fn remainder(&mut self) -> &mut [T
] {
313 /// Return the remainder of the original slice that is not going to be
314 /// returned by the iterator. The returned slice has at most `chunk_size-1`
315 /// elements. Subsequent calls will return an empty slice.
316 pub fn take_remainder(&mut self) -> &'data
mut [T
] {
317 std
::mem
::replace(&mut self.rem
, &mut [])
321 impl<'data
, T
: Send
+ 'data
> ParallelIterator
for RChunksExactMut
<'data
, T
> {
322 type Item
= &'data
mut [T
];
324 fn drive_unindexed
<C
>(self, consumer
: C
) -> C
::Result
326 C
: UnindexedConsumer
<Self::Item
>,
328 bridge(self, consumer
)
331 fn opt_len(&self) -> Option
<usize> {
336 impl<'data
, T
: Send
+ 'data
> IndexedParallelIterator
for RChunksExactMut
<'data
, T
> {
337 fn drive
<C
>(self, consumer
: C
) -> C
::Result
339 C
: Consumer
<Self::Item
>,
341 bridge(self, consumer
)
344 fn len(&self) -> usize {
345 self.slice
.len() / self.chunk_size
348 fn with_producer
<CB
>(self, callback
: CB
) -> CB
::Output
350 CB
: ProducerCallback
<Self::Item
>,
352 callback
.callback(RChunksExactMutProducer
{
353 chunk_size
: self.chunk_size
,
359 struct RChunksExactMutProducer
<'data
, T
: Send
> {
361 slice
: &'data
mut [T
],
364 impl<'data
, T
: 'data
+ Send
> Producer
for RChunksExactMutProducer
<'data
, T
> {
365 type Item
= &'data
mut [T
];
366 type IntoIter
= ::std
::slice
::RChunksExactMut
<'data
, T
>;
368 fn into_iter(self) -> Self::IntoIter
{
369 self.slice
.rchunks_exact_mut(self.chunk_size
)
372 fn split_at(self, index
: usize) -> (Self, Self) {
373 let elem_index
= self.slice
.len() - index
* self.chunk_size
;
374 let (left
, right
) = self.slice
.split_at_mut(elem_index
);
376 RChunksExactMutProducer
{
377 chunk_size
: self.chunk_size
,
380 RChunksExactMutProducer
{
381 chunk_size
: self.chunk_size
,