orx_iterable/transformations/
taken_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
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 as long as a predicate is satisfied.
pub struct TakenWhile<I, P>
where
    I: Iterable,
    P: Fn(&I::Item) -> bool + Copy,
{
    pub(crate) it: I,
    pub(crate) take_while: P,
}

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

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

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

// col

/// Wraps an `Collection` and creates a new `Collection` which yields elements of
/// the original iterable as long as a predicate is satisfied.
pub struct TakenWhileCol<I, E, P>
where
    I: Collection,
    E: SoM<I>,
    P: Fn(&I::Item) -> bool + Copy,
{
    pub(crate) it: E,
    pub(crate) take_while: P,
    pub(crate) phantom: PhantomData<I>,
}

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

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

    fn iter(&self) -> Self::Iter {
        let iter = self.it.get_ref().iter();
        TakenWhileColIter {
            iter,
            filter: self.take_while,
        }
    }
}

impl<I, E, P> Collection for TakenWhileCol<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 TakenWhileCol<I, E, P>
where
    I: CollectionMut,
    E: SoM<I>,
    P: Fn(&I::Item) -> bool + Copy,
{
    type IterMut<'i>
        = TakenWhileColIterMut<'i, I, P>
    where
        Self: 'i;

    fn iter_mut(&mut self) -> Self::IterMut<'_> {
        let iter = self.it.get_mut().iter_mut();
        TakenWhileColIterMut {
            iter,
            filter: self.take_while,
        }
    }
}

// col - iters

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

impl<'a, I, P> Iterator for TakenWhileColIter<'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> {
        let x = self.iter.next()?;
        (self.filter)(x).then_some(x)
    }
}

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

impl<'a, I, P> Iterator for TakenWhileColIterMut<'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> {
        let x = self.iter.next()?;
        (self.filter)(x).then_some(x)
    }
}