Skip to main content

ordinal_map/map/total/
iter.rs

1use std::array;
2use std::fmt;
3use std::fmt::Debug;
4use std::fmt::Formatter;
5use std::slice;
6use std::vec;
7
8use crate::map::enumerate::OrdinalEnumerate;
9use crate::Ordinal;
10
11/// Iterator created from
12/// [`OrdinalTotalMap`](crate::map::total::OrdinalTotalMap) and
13/// [`OrdinalTotalArrayMap`](crate::map::total::OrdinalTotalArrayMap).
14pub struct Iter<'a, K, V> {
15    iter: OrdinalEnumerate<K, slice::Iter<'a, V>>,
16}
17
18impl<'a, K, V> Iter<'a, K, V> {
19    pub(crate) fn new(iter: slice::Iter<'a, V>, next: usize) -> Self {
20        Iter {
21            iter: OrdinalEnumerate::new(iter, next),
22        }
23    }
24}
25
26impl<'a, K: Ordinal, V> Iterator for Iter<'a, K, V> {
27    type Item = (K, &'a V);
28
29    #[inline]
30    fn next(&mut self) -> Option<Self::Item> {
31        self.iter.next()
32    }
33
34    fn size_hint(&self) -> (usize, Option<usize>) {
35        self.iter.size_hint()
36    }
37}
38
39impl<'a, K: Ordinal, V> ExactSizeIterator for Iter<'a, K, V> {
40    fn len(&self) -> usize {
41        self.iter.len()
42    }
43}
44
45impl<'a, K: Ordinal, V> DoubleEndedIterator for Iter<'a, K, V> {
46    #[inline]
47    fn next_back(&mut self) -> Option<Self::Item> {
48        self.iter.next_back()
49    }
50}
51
52impl<'a, K, V> Clone for Iter<'a, K, V> {
53    fn clone(&self) -> Self {
54        Iter {
55            iter: self.iter.clone(),
56        }
57    }
58}
59
60impl<'a, K: Ordinal + Debug, V: Debug> Debug for Iter<'a, K, V> {
61    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
62        f.debug_list().entries(self.clone()).finish()
63    }
64}
65
66/// Mutable iterator created from
67/// [`OrdinalTotalMap`](crate::map::total::OrdinalTotalMap) and
68/// [`OrdinalTotalArrayMap`](crate::map::total::OrdinalTotalArrayMap).
69pub struct IterMut<'a, K, V> {
70    iter: OrdinalEnumerate<K, slice::IterMut<'a, V>>,
71}
72
73impl<'a, K: Ordinal, V> IterMut<'a, K, V> {
74    #[inline]
75    pub(crate) fn new(iter: slice::IterMut<'a, V>) -> Self {
76        IterMut {
77            iter: OrdinalEnumerate::new(iter, 0),
78        }
79    }
80
81    pub(crate) fn iter(&self) -> Iter<'_, K, V> {
82        Iter::new(self.iter.iter.as_slice().iter(), self.iter.next)
83    }
84}
85
86impl<'a, K: Ordinal, V> Iterator for IterMut<'a, K, V> {
87    type Item = (K, &'a mut V);
88
89    fn next(&mut self) -> Option<Self::Item> {
90        self.iter.next()
91    }
92
93    fn size_hint(&self) -> (usize, Option<usize>) {
94        self.iter.size_hint()
95    }
96}
97
98impl<'a, K: Ordinal, V> ExactSizeIterator for IterMut<'a, K, V> {
99    fn len(&self) -> usize {
100        self.iter.len()
101    }
102}
103
104impl<'a, K: Ordinal, V> DoubleEndedIterator for IterMut<'a, K, V> {
105    fn next_back(&mut self) -> Option<Self::Item> {
106        self.iter.next_back()
107    }
108}
109
110impl<'a, K: Ordinal + Debug, V: Debug> Debug for IterMut<'a, K, V> {
111    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
112        f.debug_list().entries(self.iter()).finish()
113    }
114}
115
116/// Iterator created from [`TotalArrayMap`](crate::map::total::OrdinalTotalArrayMap).
117pub struct IntoIterArray<K, V, const S: usize> {
118    iter: OrdinalEnumerate<K, array::IntoIter<V, S>>,
119}
120
121impl<K, V, const S: usize> IntoIterArray<K, V, S> {
122    #[inline]
123    pub(crate) fn new(iter: array::IntoIter<V, S>) -> Self {
124        IntoIterArray {
125            iter: OrdinalEnumerate::new(iter, 0),
126        }
127    }
128
129    pub(crate) fn iter(&self) -> Iter<'_, K, V> {
130        Iter::new(self.iter.iter.as_slice().iter(), self.iter.next)
131    }
132}
133
134impl<K: Ordinal, V, const S: usize> Iterator for IntoIterArray<K, V, S> {
135    type Item = (K, V);
136
137    #[inline]
138    fn next(&mut self) -> Option<Self::Item> {
139        self.iter.next()
140    }
141}
142
143impl<K: Ordinal, V, const S: usize> ExactSizeIterator for IntoIterArray<K, V, S> {
144    fn len(&self) -> usize {
145        self.iter.len()
146    }
147}
148
149impl<K: Ordinal, V, const S: usize> DoubleEndedIterator for IntoIterArray<K, V, S> {
150    #[inline]
151    fn next_back(&mut self) -> Option<Self::Item> {
152        self.iter.next_back()
153    }
154}
155
156impl<K, V: Clone, const S: usize> Clone for IntoIterArray<K, V, S> {
157    fn clone(&self) -> Self {
158        IntoIterArray {
159            iter: self.iter.clone(),
160        }
161    }
162}
163
164impl<K: Ordinal + Debug, V: Debug, const S: usize> Debug for IntoIterArray<K, V, S> {
165    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
166        f.debug_list().entries(self.iter()).finish()
167    }
168}
169
170/// Iterator created from [`OrdinalTotalMap`](crate::map::total::OrdinalTotalMap).
171pub struct IntoIter<K, V> {
172    iter: OrdinalEnumerate<K, vec::IntoIter<V>>,
173}
174
175impl<K, V> IntoIter<K, V> {
176    #[inline]
177    pub(crate) fn new(iter: vec::IntoIter<V>) -> Self {
178        IntoIter {
179            iter: OrdinalEnumerate::new(iter, 0),
180        }
181    }
182
183    pub(crate) fn iter(&self) -> Iter<'_, K, V> {
184        Iter::new(self.iter.iter.as_slice().iter(), self.iter.next)
185    }
186}
187
188impl<K: Ordinal, V> Iterator for IntoIter<K, V> {
189    type Item = (K, V);
190
191    #[inline]
192    fn next(&mut self) -> Option<Self::Item> {
193        self.iter.next()
194    }
195
196    #[inline]
197    fn size_hint(&self) -> (usize, Option<usize>) {
198        self.iter.size_hint()
199    }
200}
201
202impl<K: Ordinal, V> ExactSizeIterator for IntoIter<K, V> {
203    #[inline]
204    fn len(&self) -> usize {
205        self.iter.len()
206    }
207}
208
209impl<K: Ordinal, V> DoubleEndedIterator for IntoIter<K, V> {
210    #[inline]
211    fn next_back(&mut self) -> Option<Self::Item> {
212        self.iter.next_back()
213    }
214}
215
216impl<K: Ordinal + Debug, V: Debug> Debug for IntoIter<K, V> {
217    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
218        f.debug_list().entries(self.iter()).finish()
219    }
220}
221
222impl<K, V: Clone> Clone for IntoIter<K, V> {
223    fn clone(&self) -> Self {
224        IntoIter {
225            iter: self.iter.clone(),
226        }
227    }
228}