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

/// Wraps an `Iterable` and creates a new `Iterable` which skips the elements
/// of the original iterable that satisfy a given predicate and yields the
/// remaining.
pub struct SkippedWhile<I, P>
where
    I: Iterable,
    P: Fn(&I::Item) -> bool + Copy,
{
    pub(crate) it: I,
    pub(crate) skip_while: P,
}

impl<I, P> Iterable for SkippedWhile<I, P>
where
    I: Iterable,
    P: Fn(&I::Item) -> bool + Copy,
{
    type Item = I::Item;

    type Iter = core::iter::SkipWhile<I::Iter, P>;

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

// col

/// Wraps an `Collection` and creates a new `Collection` which skips the elements
/// of the original iterable that satisfy a given predicate and yields the
/// remaining.
pub struct SkippedWhileCol<I, E, P>
where
    I: Collection,
    E: SoM<I>,
    P: Fn(&I::Item) -> bool + Copy,
{
    pub(crate) it: E,
    pub(crate) skip_while: P,
    pub(crate) phantom: PhantomData<I>,
}

impl<'a, I, E, P> Iterable for &'a SkippedWhileCol<I, E, P>
where
    I: Collection,
    E: SoM<I>,
    P: Fn(&I::Item) -> bool + Copy,
{
    type Item = &'a I::Item;

    type Iter = SkippedWhileColIter<'a, I, P>;

    fn iter(&self) -> Self::Iter {
        let iter = self.it.get_ref().iter();
        SkippedWhileColIter {
            iter,
            skip_while: self.skip_while,
            skipped: false,
        }
    }
}

impl<I, E, P> Collection for SkippedWhileCol<I, E, P>
where
    I: Collection,
    E: SoM<I>,
    P: Fn(&I::Item) -> bool + Copy,
{
    type Item = I::Item;

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

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

impl<I, E, P> CollectionMut for SkippedWhileCol<I, E, P>
where
    I: CollectionMut,
    E: SoM<I>,
    P: Fn(&I::Item) -> bool + Copy,
{
    type IterMut<'i>
        = SkippedWhileColIterMut<'i, I, P>
    where
        Self: 'i;

    fn iter_mut(&mut self) -> Self::IterMut<'_> {
        let iter = self.it.get_mut().iter_mut();
        SkippedWhileColIterMut {
            iter,
            skip_while: self.skip_while,
            skipped: false,
        }
    }
}

// col - iters

/// Immutable iterator for skipped while iterable collection.
pub struct SkippedWhileColIter<'a, I, P>
where
    I: Collection + 'a,
    P: Fn(&I::Item) -> bool + Copy,
{
    pub(crate) iter: <I::Iterable<'a> as Iterable>::Iter,
    pub(crate) skip_while: P,
    pub(crate) skipped: bool,
}

impl<'a, I, P> Iterator for SkippedWhileColIter<'a, I, P>
where
    I: Collection,
    P: Fn(&I::Item) -> bool + Copy,
{
    type Item = <I::Iterable<'a> as Iterable>::Item;

    fn next(&mut self) -> Option<Self::Item> {
        match self.skipped {
            true => self.iter.next(),
            false => loop {
                match self.iter.next() {
                    Some(x) => match (self.skip_while)(x) {
                        true => {}
                        false => {
                            self.skipped = true;
                            return Some(x);
                        }
                    },
                    None => {
                        self.skipped = true;
                        return None;
                    }
                }
            },
        }
    }
}

/// Mutable iterator for skipped while iterable collection.
pub struct SkippedWhileColIterMut<'a, I, P>
where
    I: CollectionMut + 'a,
    P: Fn(&I::Item) -> bool + Copy,
{
    pub(crate) iter: I::IterMut<'a>,
    pub(crate) skip_while: P,
    pub(crate) skipped: bool,
}

impl<'a, I, P> Iterator for SkippedWhileColIterMut<'a, I, P>
where
    I: CollectionMut,
    P: Fn(&I::Item) -> bool + Copy,
{
    type Item = <I::IterMut<'a> as Iterator>::Item;

    fn next(&mut self) -> Option<Self::Item> {
        match self.skipped {
            true => self.iter.next(),
            false => loop {
                match self.iter.next() {
                    Some(x) => match (self.skip_while)(x) {
                        true => {}
                        false => {
                            self.skipped = true;
                            return Some(x);
                        }
                    },
                    None => {
                        self.skipped = true;
                        return None;
                    }
                }
            },
        }
    }
}