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
use super::{Entry, Key, MultiStash};
use alloc::vec;
use core::iter::{Enumerate, FusedIterator};
use core::slice;

/// Immutable [`MultiStash`] iterator.
///
/// This struct is created by [`MultiStash::iter`].
#[derive(Debug)]
pub struct Iter<'a, T> {
    /// The amount of remaining `Entry::Occupied` entries.
    remaining: usize,
    /// Iterator over the entries of the `MultiStash`.
    iter: Enumerate<slice::Iter<'a, Entry<T>>>,
}

impl<'a, T> Iter<'a, T> {
    /// Creates a new [`Iter`] for the [`MultiStash`].
    pub(crate) fn new(stash: &'a MultiStash<T>) -> Self {
        Self {
            remaining: stash.len_occupied,
            iter: stash.entries.iter().enumerate(),
        }
    }
}

impl<'a, T> Iterator for Iter<'a, T> {
    type Item = (Key, usize, &'a T);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.iter.next() {
                None => return None,
                Some((_, Entry::Vacant(_))) => continue,
                Some((index, Entry::Occupied(entry))) => {
                    self.remaining -= 1;
                    return Some((Key(index), entry.remaining.get(), &entry.item));
                }
            }
        }
    }
}

impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        loop {
            match self.iter.next_back() {
                None => return None,
                Some((_, Entry::Vacant(_))) => continue,
                Some((index, Entry::Occupied(entry))) => {
                    self.remaining -= 1;
                    return Some((Key(index), entry.remaining.get(), &entry.item));
                }
            }
        }
    }
}

impl<'a, T> ExactSizeIterator for Iter<'a, T> {
    fn len(&self) -> usize {
        self.remaining
    }
}

impl<'a, T> FusedIterator for Iter<'a, T> {}

/// Mutable [`MultiStash`] iterator.
///
/// This struct is created by [`MultiStash::iter_mut`].
#[derive(Debug)]
pub struct IterMut<'a, T> {
    /// The amount of remaining `Entry::Occupied` entries.
    remaining: usize,
    /// Iterator over the entries of the `MultiStash`.
    iter: Enumerate<slice::IterMut<'a, Entry<T>>>,
}

impl<'a, T> IterMut<'a, T> {
    /// Creates a new [`IterMut`] for the [`MultiStash`].
    pub(crate) fn new(stash: &'a mut MultiStash<T>) -> Self {
        Self {
            remaining: stash.len_occupied,
            iter: stash.entries.iter_mut().enumerate(),
        }
    }
}

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = (Key, usize, &'a mut T);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.iter.next() {
                None => return None,
                Some((_, Entry::Vacant(_))) => continue,
                Some((index, Entry::Occupied(entry))) => {
                    self.remaining -= 1;
                    return Some((Key(index), entry.remaining.get(), &mut entry.item));
                }
            }
        }
    }
}

impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        loop {
            match self.iter.next_back() {
                None => return None,
                Some((_, Entry::Vacant(_))) => continue,
                Some((index, Entry::Occupied(entry))) => {
                    self.remaining -= 1;
                    return Some((Key(index), entry.remaining.get(), &mut entry.item));
                }
            }
        }
    }
}

impl<'a, T> ExactSizeIterator for IterMut<'a, T> {
    fn len(&self) -> usize {
        self.remaining
    }
}

impl<'a, T> FusedIterator for IterMut<'a, T> {}

/// An iterator that moves out of a [`MultiStash`].
///
/// This `struct` is created by the `into_iter` method on [`MultiStash`]
/// (provided by the [`IntoIterator`] trait).
#[derive(Debug)]
pub struct IntoIter<T> {
    /// The amount of remaining `Entry::Occupied` entries.
    remaining: usize,
    /// Iterator over the entries of the `MultiStash`.
    iter: Enumerate<vec::IntoIter<Entry<T>>>,
}

impl<T> IntoIter<T> {
    /// Creates a new [`IntoIter`] for the [`MultiStash`].
    pub(crate) fn new(stash: MultiStash<T>) -> Self {
        Self {
            remaining: stash.len_occupied,
            iter: stash.entries.into_iter().enumerate(),
        }
    }
}

impl<T> Iterator for IntoIter<T> {
    type Item = (Key, usize, T);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.iter.next() {
                None => return None,
                Some((_, Entry::Vacant(_))) => continue,
                Some((index, Entry::Occupied(entry))) => {
                    self.remaining -= 1;
                    return Some((Key(index), entry.remaining.get(), entry.item));
                }
            }
        }
    }
}

impl<T> DoubleEndedIterator for IntoIter<T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        loop {
            match self.iter.next_back() {
                None => return None,
                Some((_, Entry::Vacant(_))) => continue,
                Some((index, Entry::Occupied(entry))) => {
                    self.remaining -= 1;
                    return Some((Key(index), entry.remaining.get(), entry.item));
                }
            }
        }
    }
}

impl<T> ExactSizeIterator for IntoIter<T> {
    fn len(&self) -> usize {
        self.remaining
    }
}

impl<T> FusedIterator for IntoIter<T> {}