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
use super::{HashMap, HashMapInt};
use core::hash::{BuildHasher, Hash};
use std::iter::{FromIterator, IntoIterator};

/// Iterator over the key value pairs of a Halfbrown map
#[derive(Debug)]
pub struct Iter<'a, K, V>(IterInt<'a, K, V>);

/// Manual implementation so that `Clone` isn't required for `K` nor `V`
impl<'a, K, V> Clone for Iter<'a, K, V> {
    fn clone(&self) -> Self {
        Iter(self.0.clone())
    }
}

impl<'a, K, V> From<IterInt<'a, K, V>> for Iter<'a, K, V> {
    fn from(i: IterInt<'a, K, V>) -> Self {
        Self(i)
    }
}
#[derive(Debug)]
pub(crate) enum IterInt<'a, K, V> {
    Map(hashbrown::hash_map::Iter<'a, K, V>),
    Vec(std::slice::Iter<'a, (K, V)>),
}

/// Manual implementation so that `Clone` isn't required for `K` nor `V`
impl<'a, K, V> Clone for IterInt<'a, K, V> {
    fn clone(&self) -> Self {
        match self {
            IterInt::Map(i) => IterInt::Map(i.clone()),
            IterInt::Vec(i) => IterInt::Vec(i.clone()),
        }
    }
}

impl<'a, K, V> Iterator for Iter<'a, K, V> {
    type Item = (&'a K, &'a V);
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.0 {
            IterInt::Map(m) => m.next(),
            IterInt::Vec(m) => {
                if let Some((k, v)) = m.next() {
                    Some((k, v))
                } else {
                    None
                }
            }
        }
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        match &self.0 {
            IterInt::Map(m) => m.size_hint(),
            IterInt::Vec(m) => m.size_hint(),
        }
    }
}

/// Into iterator for a Halfbrown map
pub struct IntoIter<K, V>(IntoIterInt<K, V>);
enum IntoIterInt<K, V> {
    Map(hashbrown::hash_map::IntoIter<K, V>),
    Vec(std::vec::IntoIter<(K, V)>),
}
impl<K, V> IntoIter<K, V> {
    /// The length of this iterator
    #[must_use]
    pub fn len(&self) -> usize {
        match &self.0 {
            IntoIterInt::Map(i) => i.len(),
            IntoIterInt::Vec(i) => i.len(),
        }
    }
    /// If this iteratoris empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<K, V> Iterator for IntoIter<K, V> {
    type Item = (K, V);
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.0 {
            IntoIterInt::Map(m) => m.next(),
            IntoIterInt::Vec(m) => m.next(),
        }
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        match &self.0 {
            IntoIterInt::Map(m) => m.size_hint(),
            IntoIterInt::Vec(m) => m.size_hint(),
        }
    }
}

impl<K, V, S> IntoIterator for HashMap<K, V, S> {
    type Item = (K, V);
    type IntoIter = IntoIter<K, V>;

    #[inline]
    fn into_iter(self) -> IntoIter<K, V> {
        match self.0 {
            HashMapInt::Map(m) => IntoIter(IntoIterInt::Map(m.into_iter())),
            HashMapInt::Vec(m) => IntoIter(IntoIterInt::Vec(m.into_iter())),
            HashMapInt::None => unreachable!(),
        }
    }
}

impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> {
    type Item = (&'a K, &'a V);
    type IntoIter = Iter<'a, K, V>;

    #[inline]
    fn into_iter(self) -> Iter<'a, K, V> {
        self.iter()
    }
}

impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
where
    K: Eq + Hash,
    S: BuildHasher + Default,
{
    #[inline]
    fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
        let iter = iter.into_iter();
        let mut map = Self::with_capacity_and_hasher(iter.size_hint().0, S::default());
        iter.for_each(|(k, v)| {
            map.insert(k, v);
        });
        map
    }
}

/// Mutable iterator over the key value pairs
pub struct IterMut<'a, K, V>(IterMutInt<'a, K, V>);

impl<'a, K, V> From<IterMutInt<'a, K, V>> for IterMut<'a, K, V> {
    fn from(i: IterMutInt<'a, K, V>) -> Self {
        Self(i)
    }
}

pub(crate) enum IterMutInt<'a, K, V> {
    Map(hashbrown::hash_map::IterMut<'a, K, V>),
    Vec(std::slice::IterMut<'a, (K, V)>),
}

impl<'a, K, V> Iterator for IterMut<'a, K, V> {
    type Item = (&'a K, &'a mut V);

    #[inline]
    fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
        match &mut self.0 {
            IterMutInt::Map(m) => m.next(),
            IterMutInt::Vec(m) => m.next().map(|(k, v)| (k as &K, v)),
        }
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        match &self.0 {
            IterMutInt::Map(m) => m.size_hint(),
            IterMutInt::Vec(m) => m.size_hint(),
        }
    }
}