orx_iterable/transformations/
stepped_by.rs

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
use crate::{Collection, CollectionMut, Iterable};
use core::marker::PhantomData;
use orx_self_or::SoM;

/// Wraps an `Iterable` and creates a new `Iterable` which yields elements of
/// the original iterable by stepping by a given step size.
pub struct SteppedBy<I>
where
    I: Iterable,
{
    pub(crate) it: I,
    pub(crate) step: usize,
}

impl<I> Iterable for SteppedBy<I>
where
    I: Iterable,
{
    type Item = I::Item;

    type Iter = core::iter::StepBy<I::Iter>;

    fn iter(&self) -> Self::Iter {
        self.it.iter().step_by(self.step)
    }
}

// col

/// Wraps an `Collection` and creates a new `Collection` which yields elements of
/// the original iterable by stepping by a given step size.
pub struct SteppedByCol<I, E>
where
    I: Collection,
    E: SoM<I>,
{
    pub(crate) it: E,
    pub(crate) step: usize,
    pub(crate) phantom: PhantomData<I>,
}

impl<'a, I, E> Iterable for &'a SteppedByCol<I, E>
where
    I: Collection,
    E: SoM<I>,
{
    type Item = &'a I::Item;

    type Iter = core::iter::StepBy<<I::Iterable<'a> as Iterable>::Iter>;

    fn iter(&self) -> Self::Iter {
        self.it.get_ref().iter().step_by(self.step)
    }
}

impl<I, E> Collection for SteppedByCol<I, E>
where
    I: Collection,
    E: SoM<I>,
{
    type Item = I::Item;

    type Iterable<'i>
        = &'i Self
    where
        Self: 'i;

    fn as_iterable(&self) -> Self::Iterable<'_> {
        self
    }
}

impl<I, E> CollectionMut for SteppedByCol<I, E>
where
    I: CollectionMut,
    E: SoM<I>,
{
    type IterMut<'i>
        = core::iter::StepBy<I::IterMut<'i>>
    where
        Self: 'i;

    fn iter_mut(&mut self) -> Self::IterMut<'_> {
        self.it.get_mut().iter_mut().step_by(self.step)
    }
}