deepmesa_collections/lhmap/
iter.rs

1/*
2   LinkedHashMap: A fast and flexible linked HashMap that allows for
3   O(1) inserts and removes with a predictable iteration order.
4
5   Copyright 2021 "Rahul Singh <rsingh@arrsingh.com>"
6
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10
11       http://www.apache.org/licenses/LICENSE-2.0
12
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18*/
19use crate::lhmap::entry::Entry;
20use crate::lhmap::lhmap::LinkedHashMap;
21use core::hash::Hash;
22
23pub struct Iter<'a, K, V> {
24    ll_iter: crate::linkedlist::iter::Iter<'a, Entry<K, V>>,
25}
26
27impl<'a, K, V> Iter<'a, K, V>
28where
29    K: Hash + Eq,
30{
31    pub(crate) fn new(lhmap: &'a LinkedHashMap<K, V>) -> Iter<K, V> {
32        Iter {
33            ll_iter: lhmap.ll.iter(),
34        }
35    }
36
37    pub fn reverse(self) -> Iter<'a, K, V> {
38        return Iter {
39            ll_iter: self.ll_iter.reverse(),
40        };
41    }
42}
43
44impl<'a, K, V> Iterator for Iter<'a, K, V> {
45    type Item = (&'a K, &'a V);
46    fn next(&mut self) -> Option<(&'a K, &'a V)> {
47        match self.ll_iter.next() {
48            Some(entry) => return Some((&entry.key, &entry.val)),
49            None => {
50                return None;
51            }
52        }
53    }
54}
55
56pub struct IterMut<'a, K, V> {
57    ll_iter: crate::linkedlist::iter::IterMut<'a, Entry<K, V>>,
58}
59
60impl<'a, K, V> IterMut<'a, K, V>
61where
62    K: Hash + Eq,
63{
64    pub(crate) fn new(lhmap: &'a mut LinkedHashMap<K, V>) -> IterMut<K, V> {
65        IterMut {
66            ll_iter: lhmap.ll.iter_mut(),
67        }
68    }
69
70    pub fn reverse(self) -> IterMut<'a, K, V> {
71        return IterMut {
72            ll_iter: self.ll_iter.reverse(),
73        };
74    }
75}
76
77impl<'a, K, V> Iterator for IterMut<'a, K, V> {
78    type Item = (&'a K, &'a mut V);
79    fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
80        match self.ll_iter.next() {
81            Some(entry) => return Some((&entry.key, &mut entry.val)),
82            None => {
83                return None;
84            }
85        }
86    }
87}
88
89pub struct Keys<'a, K, V> {
90    ll_iter: crate::linkedlist::iter::Iter<'a, Entry<K, V>>,
91}
92
93impl<'a, K, V> Keys<'a, K, V>
94where
95    K: Hash + Eq,
96{
97    pub(crate) fn new(lhmap: &'a LinkedHashMap<K, V>) -> Keys<'a, K, V> {
98        Keys {
99            ll_iter: lhmap.ll.iter(),
100        }
101    }
102
103    pub fn reverse(self) -> Keys<'a, K, V> {
104        return Keys {
105            ll_iter: self.ll_iter.reverse(),
106        };
107    }
108}
109
110impl<'a, K, V> Iterator for Keys<'a, K, V> {
111    type Item = &'a K;
112    fn next(&mut self) -> Option<&'a K> {
113        match self.ll_iter.next() {
114            Some(entry) => return Some(&entry.key),
115            None => {
116                return None;
117            }
118        }
119    }
120}
121
122pub struct Values<'a, K, V> {
123    ll_iter: crate::linkedlist::iter::Iter<'a, Entry<K, V>>,
124}
125
126impl<'a, K, V> Values<'a, K, V>
127where
128    K: Hash + Eq,
129{
130    pub(crate) fn new(lhmap: &'a LinkedHashMap<K, V>) -> Values<'a, K, V> {
131        Values {
132            ll_iter: lhmap.ll.iter(),
133        }
134    }
135
136    pub fn reverse(self) -> Values<'a, K, V> {
137        return Values {
138            ll_iter: self.ll_iter.reverse(),
139        };
140    }
141}
142
143impl<'a, K, V> Iterator for Values<'a, K, V> {
144    type Item = &'a V;
145    fn next(&mut self) -> Option<&'a V> {
146        match self.ll_iter.next() {
147            Some(entry) => return Some(&entry.val),
148            None => {
149                return None;
150            }
151        }
152    }
153}
154
155//
156pub struct ValuesMut<'a, K, V> {
157    ll_iter: crate::linkedlist::iter::IterMut<'a, Entry<K, V>>,
158}
159
160impl<'a, K, V> ValuesMut<'a, K, V>
161where
162    K: Hash + Eq,
163{
164    pub(crate) fn new(lhmap: &'a mut LinkedHashMap<K, V>) -> ValuesMut<'a, K, V> {
165        ValuesMut {
166            ll_iter: lhmap.ll.iter_mut(),
167        }
168    }
169
170    pub fn reverse(self) -> ValuesMut<'a, K, V> {
171        return ValuesMut {
172            ll_iter: self.ll_iter.reverse(),
173        };
174    }
175}
176
177impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
178    type Item = &'a mut V;
179    fn next(&mut self) -> Option<&'a mut V> {
180        match self.ll_iter.next() {
181            Some(entry) => return Some(&mut entry.val),
182            None => {
183                return None;
184            }
185        }
186    }
187}