Show More
@@ -26,15 +26,6 b' pub struct AncestorsIterator<G: Graph> {' | |||
|
26 | 26 | stoprev: Revision, |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | /// Lazy ancestors set, backed by AncestorsIterator | |
|
30 | pub struct LazyAncestors<G: Graph + Clone> { | |
|
31 | graph: G, | |
|
32 | containsiter: AncestorsIterator<G>, | |
|
33 | initrevs: Vec<Revision>, | |
|
34 | stoprev: Revision, | |
|
35 | inclusive: bool, | |
|
36 | } | |
|
37 | ||
|
38 | 29 | pub struct MissingAncestors<G: Graph> { |
|
39 | 30 | graph: G, |
|
40 | 31 | bases: HashSet<Revision>, |
@@ -165,49 +156,6 b' impl<G: Graph> Iterator for AncestorsIte' | |||
|
165 | 156 | } |
|
166 | 157 | } |
|
167 | 158 | |
|
168 | impl<G: Graph + Clone> LazyAncestors<G> { | |
|
169 | pub fn new( | |
|
170 | graph: G, | |
|
171 | initrevs: impl IntoIterator<Item = Revision>, | |
|
172 | stoprev: Revision, | |
|
173 | inclusive: bool, | |
|
174 | ) -> Result<Self, GraphError> { | |
|
175 | let v: Vec<Revision> = initrevs.into_iter().collect(); | |
|
176 | Ok(LazyAncestors { | |
|
177 | graph: graph.clone(), | |
|
178 | containsiter: AncestorsIterator::new( | |
|
179 | graph, | |
|
180 | v.iter().cloned(), | |
|
181 | stoprev, | |
|
182 | inclusive, | |
|
183 | )?, | |
|
184 | initrevs: v, | |
|
185 | stoprev, | |
|
186 | inclusive, | |
|
187 | }) | |
|
188 | } | |
|
189 | ||
|
190 | pub fn contains(&mut self, rev: Revision) -> Result<bool, GraphError> { | |
|
191 | self.containsiter.contains(rev) | |
|
192 | } | |
|
193 | ||
|
194 | pub fn is_empty(&self) -> bool { | |
|
195 | self.containsiter.is_empty() | |
|
196 | } | |
|
197 | ||
|
198 | pub fn iter(&self) -> AncestorsIterator<G> { | |
|
199 | // the arguments being the same as for self.containsiter, we know | |
|
200 | // for sure that AncestorsIterator constructor can't fail | |
|
201 | AncestorsIterator::new( | |
|
202 | self.graph.clone(), | |
|
203 | self.initrevs.iter().cloned(), | |
|
204 | self.stoprev, | |
|
205 | self.inclusive, | |
|
206 | ) | |
|
207 | .unwrap() | |
|
208 | } | |
|
209 | } | |
|
210 | ||
|
211 | 159 | impl<G: Graph> MissingAncestors<G> { |
|
212 | 160 | pub fn new(graph: G, bases: impl IntoIterator<Item = Revision>) -> Self { |
|
213 | 161 | let mut created = MissingAncestors { |
@@ -550,39 +498,6 b' mod tests {' | |||
|
550 | 498 | } |
|
551 | 499 | |
|
552 | 500 | #[test] |
|
553 | fn test_lazy_iter_contains() { | |
|
554 | let mut lazy = | |
|
555 | LazyAncestors::new(SampleGraph, vec![11, 13], 0, false).unwrap(); | |
|
556 | ||
|
557 | let revs: Vec<Revision> = lazy.iter().map(|r| r.unwrap()).collect(); | |
|
558 | // compare with iterator tests on the same initial revisions | |
|
559 | assert_eq!(revs, vec![8, 7, 4, 3, 2, 1, 0]); | |
|
560 | ||
|
561 | // contains() results are correct, unaffected by the fact that | |
|
562 | // we consumed entirely an iterator out of lazy | |
|
563 | assert_eq!(lazy.contains(2), Ok(true)); | |
|
564 | assert_eq!(lazy.contains(9), Ok(false)); | |
|
565 | } | |
|
566 | ||
|
567 | #[test] | |
|
568 | fn test_lazy_contains_iter() { | |
|
569 | let mut lazy = | |
|
570 | LazyAncestors::new(SampleGraph, vec![11, 13], 0, false).unwrap(); // reminder: [8, 7, 4, 3, 2, 1, 0] | |
|
571 | ||
|
572 | assert_eq!(lazy.contains(2), Ok(true)); | |
|
573 | assert_eq!(lazy.contains(6), Ok(false)); | |
|
574 | ||
|
575 | // after consumption of 2 by the inner iterator, results stay | |
|
576 | // consistent | |
|
577 | assert_eq!(lazy.contains(2), Ok(true)); | |
|
578 | assert_eq!(lazy.contains(5), Ok(false)); | |
|
579 | ||
|
580 | // iter() still gives us a fresh iterator | |
|
581 | let revs: Vec<Revision> = lazy.iter().map(|r| r.unwrap()).collect(); | |
|
582 | assert_eq!(revs, vec![8, 7, 4, 3, 2, 1, 0]); | |
|
583 | } | |
|
584 | ||
|
585 | #[test] | |
|
586 | 501 | /// Test constructor, add/get bases and heads |
|
587 | 502 | fn test_missing_bases() -> Result<(), GraphError> { |
|
588 | 503 | let mut missing_ancestors = |
General Comments 0
You need to be logged in to leave comments.
Login now