micromap/set/
iterators.rs

1// SPDX-FileCopyrightText: Copyright (c) 2023-2025 Yegor Bugayenko
2// SPDX-License-Identifier: MIT
3
4use super::Set;
5use core::iter::FusedIterator;
6
7/// Iterator over the [`Set`].
8#[repr(transparent)]
9#[must_use = "iterators are lazy and do nothing unless consumed"]
10pub struct Iter<'a, T> {
11    iter: crate::map::keys::Keys<'a, T, ()>,
12}
13
14/// Into-iterator over the [`Set`].
15#[repr(transparent)]
16#[must_use = "iterators are lazy and do nothing unless consumed"]
17pub struct IntoIter<T, const N: usize> {
18    iter: crate::map::keys::IntoKeys<T, (), N>,
19}
20
21impl<T, const N: usize> Set<T, N> {
22    /// An iterator visiting all elements in arbitrary order. The iterator
23    /// element type is `&'a T`.
24    ///
25    /// # Examples
26    /// ```
27    /// use micromap::Set;
28    /// let mut set: Set<_, 3> = Set::new();
29    /// set.insert("a");
30    /// set.insert("b");
31    /// // Will print in an arbitrary order.
32    /// for x in set.iter() {
33    ///     println!("{x}");
34    /// }
35    /// ```
36    ///
37    /// # Performance
38    /// In the current implementation, iterating over set takes O(len) time.
39    #[inline]
40    pub fn iter(&self) -> Iter<'_, T> {
41        Iter {
42            iter: self.map.keys(),
43        }
44    }
45}
46
47impl<T> Clone for Iter<'_, T> {
48    #[inline]
49    fn clone(&self) -> Self {
50        Iter {
51            iter: self.iter.clone(),
52        }
53    }
54}
55
56impl<'a, T: 'a> Iterator for Iter<'a, T> {
57    type Item = &'a T;
58
59    #[inline]
60    fn next(&mut self) -> Option<Self::Item> {
61        self.iter.next()
62    }
63
64    #[inline]
65    fn size_hint(&self) -> (usize, Option<usize>) {
66        self.iter.size_hint()
67    }
68}
69
70impl<T, const N: usize> Iterator for IntoIter<T, N> {
71    type Item = T;
72
73    #[inline]
74    fn next(&mut self) -> Option<Self::Item> {
75        self.iter.next()
76    }
77
78    #[inline]
79    fn size_hint(&self) -> (usize, Option<usize>) {
80        self.iter.size_hint()
81    }
82}
83
84impl<'a, T, const N: usize> IntoIterator for &'a Set<T, N> {
85    type Item = &'a T;
86    type IntoIter = Iter<'a, T>;
87
88    #[inline]
89    fn into_iter(self) -> Self::IntoIter {
90        self.iter()
91    }
92}
93
94impl<T, const N: usize> IntoIterator for Set<T, N> {
95    type Item = T;
96    type IntoIter = IntoIter<T, N>;
97
98    #[inline]
99    fn into_iter(self) -> Self::IntoIter {
100        IntoIter {
101            iter: self.map.into_keys(),
102        }
103    }
104}
105
106impl<'a, T: 'a> ExactSizeIterator for Iter<'a, T> {
107    #[inline]
108    fn len(&self) -> usize {
109        self.iter.len()
110    }
111}
112
113impl<T, const N: usize> ExactSizeIterator for IntoIter<T, N> {
114    #[inline]
115    fn len(&self) -> usize {
116        self.iter.len()
117    }
118}
119
120impl<'a, T: 'a> FusedIterator for Iter<'a, T> {}
121
122impl<T, const N: usize> FusedIterator for IntoIter<T, N> {}