1use core::slice;
2
3use tinyvec::ArrayVecIterator;
4
5use super::Node;
6
7#[repr(transparent)]
9pub struct Iter<'a, K, V> {
10 inner: slice::Iter<'a, Option<Node<K, V>>>,
11}
12
13impl<'a, K, V> Iter<'a, K, V> {
14 #[inline]
15 pub(crate) fn new(inner: slice::Iter<'a, Option<Node<K, V>>>) -> Self {
16 Self { inner }
17 }
18}
19
20impl<'a, K, V> Iterator for Iter<'a, K, V> {
21 type Item = (&'a K, &'a V);
22
23 #[inline]
24 fn next(&mut self) -> Option<(&'a K, &'a V)> {
25 self.inner.find_map(|node| match node.as_ref() {
26 None => None,
27 Some(Node {
28 kv: (ref k, ref v), ..
29 }) => Some((k, v)),
30 })
31 }
32}
33
34#[repr(transparent)]
36pub struct IterMut<'a, K, V> {
37 inner: slice::IterMut<'a, Option<Node<K, V>>>,
38}
39
40impl<'a, K, V> IterMut<'a, K, V> {
41 #[inline]
42 pub(crate) fn new(inner: slice::IterMut<'a, Option<Node<K, V>>>) -> Self {
43 Self { inner }
44 }
45}
46
47impl<'a, K, V> Iterator for IterMut<'a, K, V> {
48 type Item = (&'a K, &'a mut V);
49
50 #[inline]
51 fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
52 self.inner.find_map(|node| match node.as_mut() {
53 None => None,
54 Some(Node {
55 kv: (ref k, ref mut v),
56 ..
57 }) => Some((k, v)),
58 })
59 }
60}
61
62#[repr(transparent)]
64pub struct IntoIter<K, V, const N: usize> {
65 inner: ArrayVecIterator<[Option<Node<K, V>>; N]>,
66}
67
68impl<K, V, const N: usize> IntoIter<K, V, N> {
69 #[inline]
70 pub(crate) fn new(inner: ArrayVecIterator<[Option<Node<K, V>>; N]>) -> Self {
71 Self { inner }
72 }
73}
74
75impl<K, V, const N: usize> Iterator for IntoIter<K, V, N> {
76 type Item = (K, V);
77
78 fn next(&mut self) -> Option<Self::Item> {
79 self.inner.find_map(|node| match node {
80 None => None,
81 Some(node) => Some(node.kv),
82 })
83 }
84
85 #[inline]
86 #[must_use]
87 fn size_hint(&self) -> (usize, Option<usize>) {
88 (0, self.inner.size_hint().1)
89 }
90}
91
92#[test]
93fn test_remove() {
94 use super::ArrayMap;
95
96 let mut map: ArrayMap<u32, u32, 25> = ArrayMap::new();
97 for i in 0..25 {
98 map.insert(i, i);
99 }
100
101 for j in 10..15 {
102 map.remove(&j);
103 }
104
105 let _test = map.get(&16).unwrap();
106}
107
108pub struct Drain<'a, K: PartialOrd, V> {
110 inner: slice::IterMut<'a, Option<Node<K, V>>>,
111}
112
113impl<'a, K: PartialOrd, V> Drain<'a, K, V> {
114 #[inline]
115 pub(crate) fn new(inner: slice::IterMut<'a, Option<Node<K, V>>>) -> Self {
116 Self { inner }
117 }
118}
119
120impl<'a, K: PartialOrd, V> Iterator for Drain<'a, K, V> {
121 type Item = (K, V);
122
123 #[inline]
124 fn next(&mut self) -> Option<(K, V)> {
125 self.inner.find_map(|m| m.take().map(|r| r.kv))
126 }
127
128 #[inline]
129 fn size_hint(&self) -> (usize, Option<usize>) {
130 (0, None)
131 }
132}
133
134impl<'a, K: PartialOrd, V> DoubleEndedIterator for Drain<'a, K, V> {
135 #[inline]
136 fn next_back(&mut self) -> Option<(K, V)> {
137 self.inner
138 .rfind(|o| Option::is_some(o))
139 .map(|r| r.take().unwrap().kv)
140 }
141}
142
143#[repr(transparent)]
145pub struct Keys<'a, K, V> {
146 inner: Iter<'a, K, V>,
147}
148
149impl<'a, K, V> Keys<'a, K, V> {
150 #[inline]
151 pub(crate) fn new(inner: Iter<'a, K, V>) -> Self {
152 Self { inner }
153 }
154}
155
156impl<'a, K, V> Iterator for Keys<'a, K, V> {
157 type Item = &'a K;
158
159 #[inline]
160 fn next(&mut self) -> Option<&'a K> {
161 match self.inner.next() {
162 Some((k, _v)) => Some(k),
163 None => None,
164 }
165 }
166}
167
168#[repr(transparent)]
170pub struct Values<'a, K, V> {
171 inner: Iter<'a, K, V>,
172}
173
174impl<'a, K, V> Values<'a, K, V> {
175 #[inline]
176 pub(crate) fn new(inner: Iter<'a, K, V>) -> Self {
177 Self { inner }
178 }
179}
180
181impl<'a, K, V> Iterator for Values<'a, K, V> {
182 type Item = &'a V;
183
184 #[inline]
185 fn next(&mut self) -> Option<&'a V> {
186 match self.inner.next() {
187 Some((_k, v)) => Some(v),
188 None => None,
189 }
190 }
191}
192
193#[repr(transparent)]
195pub struct ValuesMut<'a, K, V> {
196 inner: IterMut<'a, K, V>,
197}
198
199impl<'a, K, V> ValuesMut<'a, K, V> {
200 #[inline]
201 pub(crate) fn new(inner: IterMut<'a, K, V>) -> Self {
202 Self { inner }
203 }
204}
205
206impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
207 type Item = &'a mut V;
208
209 #[inline]
210 fn next(&mut self) -> Option<&'a mut V> {
211 match self.inner.next() {
212 Some((_k, v)) => Some(v),
213 None => None,
214 }
215 }
216}