icentral_mindexed_map/
maybe_indexed_map_iterator.rs1crate::ix!();
2
3
4pub struct NodeIdEnumerate<I: Iterator>(pub std::iter::Enumerate<I>);
6
7impl<I: Iterator> Iterator for NodeIdEnumerate<I> {
8
9 type Item = (NodeId, <I as Iterator>::Item);
10
11 fn next(&mut self) -> Option<(NodeId, <I as Iterator>::Item)> {
12
13 let (i,a) = self.0.next()?;
14
15 Some((nodeid!(i), a))
16 }
17}
18
19pub struct IndexedIterator<'m, T>(NodeIdEnumerate<std::slice::Iter<'m,T>>);
21
22impl<'m,T> IndexedIterator<'m,T> {
23
24 pub fn new(x: std::slice::Iter<'m,T>) -> Self {
25 IndexedIterator(NodeIdEnumerate(x.enumerate()))
26 }
27}
28
29impl<'m,T> Iterator for IndexedIterator<'m,T> {
30
31 type Item = (NodeId, &'m T);
32
33 fn next(&mut self) -> Option<(NodeId, &'m T)> {
34 self.0.next()
35 }
36}
37
38pub struct MappedIterator<'m, T> {
40 inner: std::collections::hash_map::Iter<'m, NodeId, T>,
41}
42
43impl<'m,T> MappedIterator<'m,T> {
44
45 pub fn new(x: std::collections::hash_map::Iter<'m,NodeId,T>) -> Self {
46 MappedIterator {
47 inner: x,
48 }
49 }
50}
51
52impl<'m,T> Iterator for MappedIterator<'m,T> {
53
54 type Item = (NodeId, &'m T);
55
56 fn next(&mut self) -> Option<(NodeId, &'m T)> {
57
58 let (i,a) = self.inner.next()?;
59
60 Some((i.clone(),a))
61 }
62}
63
64pub enum MaybeIndexedMapIterator<'m, T> {
66 Indexed(IndexedIterator<'m,T>),
67 Mapped(MappedIterator<'m,T>),
68}
69
70impl<'m, T: Clone> MaybeIndexedMapIterator<'m, T> {
71
72 pub fn new_indexed(x: std::slice::Iter<'m,T>) -> Self {
73 let it = IndexedIterator::new(x);
74 MaybeIndexedMapIterator::Indexed(it)
75 }
76
77 pub fn new_mapped(x: std::collections::hash_map::Iter<'m,NodeId,T>) -> Self {
78 let it = MappedIterator::new(x);
79 MaybeIndexedMapIterator::Mapped(it)
80 }
81}
82
83impl<'m, T: Clone> Iterator for MaybeIndexedMapIterator<'m, T> {
84
85 type Item = (NodeId, &'m T);
86
87 fn next(&mut self) -> Option<(NodeId, &'m T)> {
88 match self {
89 MaybeIndexedMapIterator::Indexed(it) => {
90 it.next()
91 }
92 MaybeIndexedMapIterator::Mapped(it) => {
93 it.next()
94 }
95 }
96 }
97}