Show More
@@ -1,783 +1,804 | |||||
1 | // ancestors.rs |
|
1 | // ancestors.rs | |
2 | // |
|
2 | // | |
3 | // Copyright 2018 Georges Racinet <gracinet@anybox.fr> |
|
3 | // Copyright 2018 Georges Racinet <gracinet@anybox.fr> | |
4 | // |
|
4 | // | |
5 | // This software may be used and distributed according to the terms of the |
|
5 | // This software may be used and distributed according to the terms of the | |
6 | // GNU General Public License version 2 or any later version. |
|
6 | // GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | //! Rust versions of generic DAG ancestors algorithms for Mercurial |
|
8 | //! Rust versions of generic DAG ancestors algorithms for Mercurial | |
9 |
|
9 | |||
10 | use super::{Graph, GraphError, Revision, NULL_REVISION}; |
|
10 | use super::{Graph, GraphError, Revision, NULL_REVISION}; | |
11 | use std::cmp::max; |
|
11 | use std::cmp::max; | |
12 | use std::collections::{BinaryHeap, HashSet}; |
|
12 | use std::collections::{BinaryHeap, HashSet}; | |
13 |
|
13 | |||
14 | /// Iterator over the ancestors of a given list of revisions |
|
14 | /// Iterator over the ancestors of a given list of revisions | |
15 | /// This is a generic type, defined and implemented for any Graph, so that |
|
15 | /// This is a generic type, defined and implemented for any Graph, so that | |
16 | /// it's easy to |
|
16 | /// it's easy to | |
17 | /// |
|
17 | /// | |
18 | /// - unit test in pure Rust |
|
18 | /// - unit test in pure Rust | |
19 | /// - bind to main Mercurial code, potentially in several ways and have these |
|
19 | /// - bind to main Mercurial code, potentially in several ways and have these | |
20 | /// bindings evolve over time |
|
20 | /// bindings evolve over time | |
21 | pub struct AncestorsIterator<G: Graph> { |
|
21 | pub struct AncestorsIterator<G: Graph> { | |
22 | graph: G, |
|
22 | graph: G, | |
23 | visit: BinaryHeap<Revision>, |
|
23 | visit: BinaryHeap<Revision>, | |
24 | seen: HashSet<Revision>, |
|
24 | seen: HashSet<Revision>, | |
25 | stoprev: Revision, |
|
25 | stoprev: Revision, | |
26 | } |
|
26 | } | |
27 |
|
27 | |||
28 | /// Lazy ancestors set, backed by AncestorsIterator |
|
28 | /// Lazy ancestors set, backed by AncestorsIterator | |
29 | pub struct LazyAncestors<G: Graph + Clone> { |
|
29 | pub struct LazyAncestors<G: Graph + Clone> { | |
30 | graph: G, |
|
30 | graph: G, | |
31 | containsiter: AncestorsIterator<G>, |
|
31 | containsiter: AncestorsIterator<G>, | |
32 | initrevs: Vec<Revision>, |
|
32 | initrevs: Vec<Revision>, | |
33 | stoprev: Revision, |
|
33 | stoprev: Revision, | |
34 | inclusive: bool, |
|
34 | inclusive: bool, | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | pub struct MissingAncestors<G: Graph> { |
|
37 | pub struct MissingAncestors<G: Graph> { | |
38 | graph: G, |
|
38 | graph: G, | |
39 | bases: HashSet<Revision>, |
|
39 | bases: HashSet<Revision>, | |
40 | } |
|
40 | } | |
41 |
|
41 | |||
42 | impl<G: Graph> AncestorsIterator<G> { |
|
42 | impl<G: Graph> AncestorsIterator<G> { | |
43 | /// Constructor. |
|
43 | /// Constructor. | |
44 | /// |
|
44 | /// | |
45 | /// if `inclusive` is true, then the init revisions are emitted in |
|
45 | /// if `inclusive` is true, then the init revisions are emitted in | |
46 | /// particular, otherwise iteration starts from their parents. |
|
46 | /// particular, otherwise iteration starts from their parents. | |
47 | pub fn new( |
|
47 | pub fn new( | |
48 | graph: G, |
|
48 | graph: G, | |
49 | initrevs: impl IntoIterator<Item = Revision>, |
|
49 | initrevs: impl IntoIterator<Item = Revision>, | |
50 | stoprev: Revision, |
|
50 | stoprev: Revision, | |
51 | inclusive: bool, |
|
51 | inclusive: bool, | |
52 | ) -> Result<Self, GraphError> { |
|
52 | ) -> Result<Self, GraphError> { | |
53 | let filtered_initrevs = initrevs.into_iter().filter(|&r| r >= stoprev); |
|
53 | let filtered_initrevs = initrevs.into_iter().filter(|&r| r >= stoprev); | |
54 | if inclusive { |
|
54 | if inclusive { | |
55 | let visit: BinaryHeap<Revision> = filtered_initrevs.collect(); |
|
55 | let visit: BinaryHeap<Revision> = filtered_initrevs.collect(); | |
56 | let seen = visit.iter().map(|&x| x).collect(); |
|
56 | let seen = visit.iter().map(|&x| x).collect(); | |
57 | return Ok(AncestorsIterator { |
|
57 | return Ok(AncestorsIterator { | |
58 | visit: visit, |
|
58 | visit: visit, | |
59 | seen: seen, |
|
59 | seen: seen, | |
60 | stoprev: stoprev, |
|
60 | stoprev: stoprev, | |
61 | graph: graph, |
|
61 | graph: graph, | |
62 | }); |
|
62 | }); | |
63 | } |
|
63 | } | |
64 | let mut this = AncestorsIterator { |
|
64 | let mut this = AncestorsIterator { | |
65 | visit: BinaryHeap::new(), |
|
65 | visit: BinaryHeap::new(), | |
66 | seen: HashSet::new(), |
|
66 | seen: HashSet::new(), | |
67 | stoprev: stoprev, |
|
67 | stoprev: stoprev, | |
68 | graph: graph, |
|
68 | graph: graph, | |
69 | }; |
|
69 | }; | |
70 | this.seen.insert(NULL_REVISION); |
|
70 | this.seen.insert(NULL_REVISION); | |
71 | for rev in filtered_initrevs { |
|
71 | for rev in filtered_initrevs { | |
72 | for parent in this.graph.parents(rev)?.iter().cloned() { |
|
72 | for parent in this.graph.parents(rev)?.iter().cloned() { | |
73 | this.conditionally_push_rev(parent); |
|
73 | this.conditionally_push_rev(parent); | |
74 | } |
|
74 | } | |
75 | } |
|
75 | } | |
76 | Ok(this) |
|
76 | Ok(this) | |
77 | } |
|
77 | } | |
78 |
|
78 | |||
79 | #[inline] |
|
79 | #[inline] | |
80 | fn conditionally_push_rev(&mut self, rev: Revision) { |
|
80 | fn conditionally_push_rev(&mut self, rev: Revision) { | |
81 | if self.stoprev <= rev && !self.seen.contains(&rev) { |
|
81 | if self.stoprev <= rev && !self.seen.contains(&rev) { | |
82 | self.seen.insert(rev); |
|
82 | self.seen.insert(rev); | |
83 | self.visit.push(rev); |
|
83 | self.visit.push(rev); | |
84 | } |
|
84 | } | |
85 | } |
|
85 | } | |
86 |
|
86 | |||
87 | /// Consumes partially the iterator to tell if the given target |
|
87 | /// Consumes partially the iterator to tell if the given target | |
88 | /// revision |
|
88 | /// revision | |
89 | /// is in the ancestors it emits. |
|
89 | /// is in the ancestors it emits. | |
90 | /// This is meant for iterators actually dedicated to that kind of |
|
90 | /// This is meant for iterators actually dedicated to that kind of | |
91 | /// purpose |
|
91 | /// purpose | |
92 | pub fn contains(&mut self, target: Revision) -> Result<bool, GraphError> { |
|
92 | pub fn contains(&mut self, target: Revision) -> Result<bool, GraphError> { | |
93 | if self.seen.contains(&target) && target != NULL_REVISION { |
|
93 | if self.seen.contains(&target) && target != NULL_REVISION { | |
94 | return Ok(true); |
|
94 | return Ok(true); | |
95 | } |
|
95 | } | |
96 | for item in self { |
|
96 | for item in self { | |
97 | let rev = item?; |
|
97 | let rev = item?; | |
98 | if rev == target { |
|
98 | if rev == target { | |
99 | return Ok(true); |
|
99 | return Ok(true); | |
100 | } |
|
100 | } | |
101 | if rev < target { |
|
101 | if rev < target { | |
102 | return Ok(false); |
|
102 | return Ok(false); | |
103 | } |
|
103 | } | |
104 | } |
|
104 | } | |
105 | Ok(false) |
|
105 | Ok(false) | |
106 | } |
|
106 | } | |
107 |
|
107 | |||
108 | pub fn peek(&self) -> Option<Revision> { |
|
108 | pub fn peek(&self) -> Option<Revision> { | |
109 | self.visit.peek().map(|&r| r) |
|
109 | self.visit.peek().map(|&r| r) | |
110 | } |
|
110 | } | |
111 |
|
111 | |||
112 | /// Tell if the iterator is about an empty set |
|
112 | /// Tell if the iterator is about an empty set | |
113 | /// |
|
113 | /// | |
114 | /// The result does not depend whether the iterator has been consumed |
|
114 | /// The result does not depend whether the iterator has been consumed | |
115 | /// or not. |
|
115 | /// or not. | |
116 | /// This is mostly meant for iterators backing a lazy ancestors set |
|
116 | /// This is mostly meant for iterators backing a lazy ancestors set | |
117 | pub fn is_empty(&self) -> bool { |
|
117 | pub fn is_empty(&self) -> bool { | |
118 | if self.visit.len() > 0 { |
|
118 | if self.visit.len() > 0 { | |
119 | return false; |
|
119 | return false; | |
120 | } |
|
120 | } | |
121 | if self.seen.len() > 1 { |
|
121 | if self.seen.len() > 1 { | |
122 | return false; |
|
122 | return false; | |
123 | } |
|
123 | } | |
124 | // at this point, the seen set is at most a singleton. |
|
124 | // at this point, the seen set is at most a singleton. | |
125 | // If not `self.inclusive`, it's still possible that it has only |
|
125 | // If not `self.inclusive`, it's still possible that it has only | |
126 | // the null revision |
|
126 | // the null revision | |
127 | self.seen.is_empty() || self.seen.contains(&NULL_REVISION) |
|
127 | self.seen.is_empty() || self.seen.contains(&NULL_REVISION) | |
128 | } |
|
128 | } | |
129 | } |
|
129 | } | |
130 |
|
130 | |||
131 | /// Main implementation for the iterator |
|
131 | /// Main implementation for the iterator | |
132 | /// |
|
132 | /// | |
133 | /// The algorithm is the same as in `_lazyancestorsiter()` from `ancestors.py` |
|
133 | /// The algorithm is the same as in `_lazyancestorsiter()` from `ancestors.py` | |
134 | /// with a few non crucial differences: |
|
134 | /// with a few non crucial differences: | |
135 | /// |
|
135 | /// | |
136 | /// - there's no filtering of invalid parent revisions. Actually, it should be |
|
136 | /// - there's no filtering of invalid parent revisions. Actually, it should be | |
137 | /// consistent and more efficient to filter them from the end caller. |
|
137 | /// consistent and more efficient to filter them from the end caller. | |
138 | /// - we don't have the optimization for adjacent revisions (i.e., the case |
|
138 | /// - we don't have the optimization for adjacent revisions (i.e., the case | |
139 | /// where `p1 == rev - 1`), because it amounts to update the first element of |
|
139 | /// where `p1 == rev - 1`), because it amounts to update the first element of | |
140 | /// the heap without sifting, which Rust's BinaryHeap doesn't let us do. |
|
140 | /// the heap without sifting, which Rust's BinaryHeap doesn't let us do. | |
141 | /// - we save a few pushes by comparing with `stoprev` before pushing |
|
141 | /// - we save a few pushes by comparing with `stoprev` before pushing | |
142 | impl<G: Graph> Iterator for AncestorsIterator<G> { |
|
142 | impl<G: Graph> Iterator for AncestorsIterator<G> { | |
143 | type Item = Result<Revision, GraphError>; |
|
143 | type Item = Result<Revision, GraphError>; | |
144 |
|
144 | |||
145 | fn next(&mut self) -> Option<Self::Item> { |
|
145 | fn next(&mut self) -> Option<Self::Item> { | |
146 | let current = match self.visit.peek() { |
|
146 | let current = match self.visit.peek() { | |
147 | None => { |
|
147 | None => { | |
148 | return None; |
|
148 | return None; | |
149 | } |
|
149 | } | |
150 | Some(c) => *c, |
|
150 | Some(c) => *c, | |
151 | }; |
|
151 | }; | |
152 | let [p1, p2] = match self.graph.parents(current) { |
|
152 | let [p1, p2] = match self.graph.parents(current) { | |
153 | Ok(ps) => ps, |
|
153 | Ok(ps) => ps, | |
154 | Err(e) => return Some(Err(e)), |
|
154 | Err(e) => return Some(Err(e)), | |
155 | }; |
|
155 | }; | |
156 | if p1 < self.stoprev || self.seen.contains(&p1) { |
|
156 | if p1 < self.stoprev || self.seen.contains(&p1) { | |
157 | self.visit.pop(); |
|
157 | self.visit.pop(); | |
158 | } else { |
|
158 | } else { | |
159 | *(self.visit.peek_mut().unwrap()) = p1; |
|
159 | *(self.visit.peek_mut().unwrap()) = p1; | |
160 | self.seen.insert(p1); |
|
160 | self.seen.insert(p1); | |
161 | }; |
|
161 | }; | |
162 |
|
162 | |||
163 | self.conditionally_push_rev(p2); |
|
163 | self.conditionally_push_rev(p2); | |
164 | Some(Ok(current)) |
|
164 | Some(Ok(current)) | |
165 | } |
|
165 | } | |
166 | } |
|
166 | } | |
167 |
|
167 | |||
168 | impl<G: Graph + Clone> LazyAncestors<G> { |
|
168 | impl<G: Graph + Clone> LazyAncestors<G> { | |
169 | pub fn new( |
|
169 | pub fn new( | |
170 | graph: G, |
|
170 | graph: G, | |
171 | initrevs: impl IntoIterator<Item = Revision>, |
|
171 | initrevs: impl IntoIterator<Item = Revision>, | |
172 | stoprev: Revision, |
|
172 | stoprev: Revision, | |
173 | inclusive: bool, |
|
173 | inclusive: bool, | |
174 | ) -> Result<Self, GraphError> { |
|
174 | ) -> Result<Self, GraphError> { | |
175 | let v: Vec<Revision> = initrevs.into_iter().collect(); |
|
175 | let v: Vec<Revision> = initrevs.into_iter().collect(); | |
176 | Ok(LazyAncestors { |
|
176 | Ok(LazyAncestors { | |
177 | graph: graph.clone(), |
|
177 | graph: graph.clone(), | |
178 | containsiter: AncestorsIterator::new( |
|
178 | containsiter: AncestorsIterator::new( | |
179 | graph, |
|
179 | graph, | |
180 | v.iter().cloned(), |
|
180 | v.iter().cloned(), | |
181 | stoprev, |
|
181 | stoprev, | |
182 | inclusive, |
|
182 | inclusive, | |
183 | )?, |
|
183 | )?, | |
184 | initrevs: v, |
|
184 | initrevs: v, | |
185 | stoprev: stoprev, |
|
185 | stoprev: stoprev, | |
186 | inclusive: inclusive, |
|
186 | inclusive: inclusive, | |
187 | }) |
|
187 | }) | |
188 | } |
|
188 | } | |
189 |
|
189 | |||
190 | pub fn contains(&mut self, rev: Revision) -> Result<bool, GraphError> { |
|
190 | pub fn contains(&mut self, rev: Revision) -> Result<bool, GraphError> { | |
191 | self.containsiter.contains(rev) |
|
191 | self.containsiter.contains(rev) | |
192 | } |
|
192 | } | |
193 |
|
193 | |||
194 | pub fn is_empty(&self) -> bool { |
|
194 | pub fn is_empty(&self) -> bool { | |
195 | self.containsiter.is_empty() |
|
195 | self.containsiter.is_empty() | |
196 | } |
|
196 | } | |
197 |
|
197 | |||
198 | pub fn iter(&self) -> AncestorsIterator<G> { |
|
198 | pub fn iter(&self) -> AncestorsIterator<G> { | |
199 | // the arguments being the same as for self.containsiter, we know |
|
199 | // the arguments being the same as for self.containsiter, we know | |
200 | // for sure that AncestorsIterator constructor can't fail |
|
200 | // for sure that AncestorsIterator constructor can't fail | |
201 | AncestorsIterator::new( |
|
201 | AncestorsIterator::new( | |
202 | self.graph.clone(), |
|
202 | self.graph.clone(), | |
203 | self.initrevs.iter().cloned(), |
|
203 | self.initrevs.iter().cloned(), | |
204 | self.stoprev, |
|
204 | self.stoprev, | |
205 | self.inclusive, |
|
205 | self.inclusive, | |
206 | ) |
|
206 | ) | |
207 | .unwrap() |
|
207 | .unwrap() | |
208 | } |
|
208 | } | |
209 | } |
|
209 | } | |
210 |
|
210 | |||
211 | impl<G: Graph> MissingAncestors<G> { |
|
211 | impl<G: Graph> MissingAncestors<G> { | |
212 | pub fn new(graph: G, bases: impl IntoIterator<Item = Revision>) -> Self { |
|
212 | pub fn new(graph: G, bases: impl IntoIterator<Item = Revision>) -> Self { | |
213 | let mut bases: HashSet<Revision> = bases.into_iter().collect(); |
|
213 | let mut bases: HashSet<Revision> = bases.into_iter().collect(); | |
214 | if bases.is_empty() { |
|
214 | if bases.is_empty() { | |
215 | bases.insert(NULL_REVISION); |
|
215 | bases.insert(NULL_REVISION); | |
216 | } |
|
216 | } | |
217 | MissingAncestors { graph, bases } |
|
217 | MissingAncestors { graph, bases } | |
218 | } |
|
218 | } | |
219 |
|
219 | |||
220 | pub fn has_bases(&self) -> bool { |
|
220 | pub fn has_bases(&self) -> bool { | |
221 | self.bases.iter().any(|&b| b != NULL_REVISION) |
|
221 | self.bases.iter().any(|&b| b != NULL_REVISION) | |
222 | } |
|
222 | } | |
223 |
|
223 | |||
224 | /// Return a reference to current bases. |
|
224 | /// Return a reference to current bases. | |
225 | /// |
|
225 | /// | |
226 | /// This is useful in unit tests, but also setdiscovery.py does |
|
226 | /// This is useful in unit tests, but also setdiscovery.py does | |
227 | /// read the bases attribute of a ancestor.missingancestors instance. |
|
227 | /// read the bases attribute of a ancestor.missingancestors instance. | |
228 | pub fn get_bases<'a>(&'a self) -> &'a HashSet<Revision> { |
|
228 | pub fn get_bases<'a>(&'a self) -> &'a HashSet<Revision> { | |
229 | &self.bases |
|
229 | &self.bases | |
230 | } |
|
230 | } | |
231 |
|
231 | |||
232 | pub fn add_bases( |
|
232 | pub fn add_bases( | |
233 | &mut self, |
|
233 | &mut self, | |
234 | new_bases: impl IntoIterator<Item = Revision>, |
|
234 | new_bases: impl IntoIterator<Item = Revision>, | |
235 | ) { |
|
235 | ) { | |
236 | self.bases.extend(new_bases); |
|
236 | self.bases.extend(new_bases); | |
237 | } |
|
237 | } | |
238 |
|
238 | |||
239 | /// Remove all ancestors of self.bases from the revs set (in place) |
|
239 | /// Remove all ancestors of self.bases from the revs set (in place) | |
240 | pub fn remove_ancestors_from( |
|
240 | pub fn remove_ancestors_from( | |
241 | &mut self, |
|
241 | &mut self, | |
242 | revs: &mut HashSet<Revision>, |
|
242 | revs: &mut HashSet<Revision>, | |
243 | ) -> Result<(), GraphError> { |
|
243 | ) -> Result<(), GraphError> { | |
244 | revs.retain(|r| !self.bases.contains(r)); |
|
244 | revs.retain(|r| !self.bases.contains(r)); | |
245 | // the null revision is always an ancestor |
|
245 | // the null revision is always an ancestor | |
246 | revs.remove(&NULL_REVISION); |
|
246 | revs.remove(&NULL_REVISION); | |
247 | if revs.is_empty() { |
|
247 | if revs.is_empty() { | |
248 | return Ok(()); |
|
248 | return Ok(()); | |
249 | } |
|
249 | } | |
250 | // anything in revs > start is definitely not an ancestor of bases |
|
250 | // anything in revs > start is definitely not an ancestor of bases | |
251 | // revs <= start need to be investigated |
|
251 | // revs <= start need to be investigated | |
252 | // TODO optim: if a missingancestors is to be used several times, |
|
252 | // TODO optim: if a missingancestors is to be used several times, | |
253 | // we shouldn't need to iterate each time on bases |
|
253 | // we shouldn't need to iterate each time on bases | |
254 | let start = match self.bases.iter().cloned().max() { |
|
254 | let start = match self.bases.iter().cloned().max() { | |
255 | Some(m) => m, |
|
255 | Some(m) => m, | |
256 | None => { |
|
256 | None => { | |
257 | // bases is empty (shouldn't happen, but let's be safe) |
|
257 | // bases is empty (shouldn't happen, but let's be safe) | |
258 | return Ok(()); |
|
258 | return Ok(()); | |
259 | } |
|
259 | } | |
260 | }; |
|
260 | }; | |
261 | // whatever happens, we'll keep at least keepcount of them |
|
261 | // whatever happens, we'll keep at least keepcount of them | |
262 | // knowing this gives us a earlier stop condition than |
|
262 | // knowing this gives us a earlier stop condition than | |
263 | // going all the way to the root |
|
263 | // going all the way to the root | |
264 | let keepcount = revs.iter().filter(|r| **r > start).count(); |
|
264 | let keepcount = revs.iter().filter(|r| **r > start).count(); | |
265 |
|
265 | |||
266 | let mut curr = start; |
|
266 | let mut curr = start; | |
267 | while curr != NULL_REVISION && revs.len() > keepcount { |
|
267 | while curr != NULL_REVISION && revs.len() > keepcount { | |
268 | if self.bases.contains(&curr) { |
|
268 | if self.bases.contains(&curr) { | |
269 | revs.remove(&curr); |
|
269 | revs.remove(&curr); | |
270 | self.add_parents(curr)?; |
|
270 | self.add_parents(curr)?; | |
271 | } |
|
271 | } | |
272 | curr -= 1; |
|
272 | curr -= 1; | |
273 | } |
|
273 | } | |
274 | Ok(()) |
|
274 | Ok(()) | |
275 | } |
|
275 | } | |
276 |
|
276 | |||
277 | /// Add rev's parents to self.bases |
|
277 | /// Add rev's parents to self.bases | |
278 | #[inline] |
|
278 | #[inline] | |
279 | fn add_parents(&mut self, rev: Revision) -> Result<(), GraphError> { |
|
279 | fn add_parents(&mut self, rev: Revision) -> Result<(), GraphError> { | |
280 | // No need to bother the set with inserting NULL_REVISION over and |
|
280 | // No need to bother the set with inserting NULL_REVISION over and | |
281 | // over |
|
281 | // over | |
282 | for p in self.graph.parents(rev)?.iter().cloned() { |
|
282 | for p in self.graph.parents(rev)?.iter().cloned() { | |
283 | if p != NULL_REVISION { |
|
283 | if p != NULL_REVISION { | |
284 | self.bases.insert(p); |
|
284 | self.bases.insert(p); | |
285 | } |
|
285 | } | |
286 | } |
|
286 | } | |
287 | Ok(()) |
|
287 | Ok(()) | |
288 | } |
|
288 | } | |
289 |
|
289 | |||
290 | /// Return all the ancestors of revs that are not ancestors of self.bases |
|
290 | /// Return all the ancestors of revs that are not ancestors of self.bases | |
291 | /// |
|
291 | /// | |
292 | /// This may include elements from revs. |
|
292 | /// This may include elements from revs. | |
293 | /// |
|
293 | /// | |
294 | /// Equivalent to the revset (::revs - ::self.bases). Revs are returned in |
|
294 | /// Equivalent to the revset (::revs - ::self.bases). Revs are returned in | |
295 | /// revision number order, which is a topological order. |
|
295 | /// revision number order, which is a topological order. | |
296 | pub fn missing_ancestors( |
|
296 | pub fn missing_ancestors( | |
297 | &mut self, |
|
297 | &mut self, | |
298 | revs: impl IntoIterator<Item = Revision>, |
|
298 | revs: impl IntoIterator<Item = Revision>, | |
299 | ) -> Result<Vec<Revision>, GraphError> { |
|
299 | ) -> Result<Vec<Revision>, GraphError> { | |
300 | // just for convenience and comparison with Python version |
|
300 | // just for convenience and comparison with Python version | |
301 | let bases_visit = &mut self.bases; |
|
301 | let bases_visit = &mut self.bases; | |
302 | let mut revs: HashSet<Revision> = revs |
|
302 | let mut revs: HashSet<Revision> = revs | |
303 | .into_iter() |
|
303 | .into_iter() | |
304 | .filter(|r| !bases_visit.contains(r)) |
|
304 | .filter(|r| !bases_visit.contains(r)) | |
305 | .collect(); |
|
305 | .collect(); | |
306 | let revs_visit = &mut revs; |
|
306 | let revs_visit = &mut revs; | |
307 | let mut both_visit: HashSet<Revision> = |
|
307 | let mut both_visit: HashSet<Revision> = | |
308 | revs_visit.intersection(&bases_visit).cloned().collect(); |
|
308 | revs_visit.intersection(&bases_visit).cloned().collect(); | |
309 | if revs_visit.is_empty() { |
|
309 | if revs_visit.is_empty() { | |
310 | return Ok(Vec::new()); |
|
310 | return Ok(Vec::new()); | |
311 | } |
|
311 | } | |
312 |
|
312 | |||
313 | let max_bases = |
|
313 | let max_bases = | |
314 | bases_visit.iter().cloned().max().unwrap_or(NULL_REVISION); |
|
314 | bases_visit.iter().cloned().max().unwrap_or(NULL_REVISION); | |
315 | let max_revs = |
|
315 | let max_revs = | |
316 | revs_visit.iter().cloned().max().unwrap_or(NULL_REVISION); |
|
316 | revs_visit.iter().cloned().max().unwrap_or(NULL_REVISION); | |
317 | let start = max(max_bases, max_revs); |
|
317 | let start = max(max_bases, max_revs); | |
318 |
|
318 | |||
319 | // TODO heuristics for with_capacity()? |
|
319 | // TODO heuristics for with_capacity()? | |
320 | let mut missing: Vec<Revision> = Vec::new(); |
|
320 | let mut missing: Vec<Revision> = Vec::new(); | |
321 | for curr in (0..=start).rev() { |
|
321 | for curr in (0..=start).rev() { | |
322 | if revs_visit.is_empty() { |
|
322 | if revs_visit.is_empty() { | |
323 | break; |
|
323 | break; | |
324 | } |
|
324 | } | |
325 | if both_visit.contains(&curr) { |
|
325 | if both_visit.contains(&curr) { | |
326 | // curr's parents might have made it into revs_visit through |
|
326 | // curr's parents might have made it into revs_visit through | |
327 | // another path |
|
327 | // another path | |
328 | // TODO optim: Rust's HashSet.remove returns a boolean telling |
|
328 | // TODO optim: Rust's HashSet.remove returns a boolean telling | |
329 | // if it happened. This will spare us one set lookup |
|
329 | // if it happened. This will spare us one set lookup | |
330 | both_visit.remove(&curr); |
|
330 | both_visit.remove(&curr); | |
331 | for p in self.graph.parents(curr)?.iter().cloned() { |
|
331 | for p in self.graph.parents(curr)?.iter().cloned() { | |
332 | if p == NULL_REVISION { |
|
332 | if p == NULL_REVISION { | |
333 | continue; |
|
333 | continue; | |
334 | } |
|
334 | } | |
335 | revs_visit.remove(&p); |
|
335 | revs_visit.remove(&p); | |
336 | bases_visit.insert(p); |
|
336 | bases_visit.insert(p); | |
337 | both_visit.insert(p); |
|
337 | both_visit.insert(p); | |
338 | } |
|
338 | } | |
339 | continue; |
|
339 | continue; | |
340 | } |
|
340 | } | |
341 | // in Rust, one can't just use mutable variables assignation |
|
341 | // in Rust, one can't just use mutable variables assignation | |
342 | // to be more straightforward. Instead of Python's |
|
342 | // to be more straightforward. Instead of Python's | |
343 | // thisvisit and othervisit, we'll differentiate with a boolean |
|
343 | // thisvisit and othervisit, we'll differentiate with a boolean | |
344 | let this_visit_is_revs; |
|
344 | let this_visit_is_revs; | |
345 | if revs_visit.remove(&curr) { |
|
345 | if revs_visit.remove(&curr) { | |
346 | missing.push(curr); |
|
346 | missing.push(curr); | |
347 | this_visit_is_revs = true; |
|
347 | this_visit_is_revs = true; | |
348 | } else if bases_visit.contains(&curr) { |
|
|||
349 | this_visit_is_revs = false; |
|
|||
350 | } else { |
|
|||
351 | // not an ancestor of revs or bases: ignore |
|
|||
352 | continue; |
|
|||
353 | } |
|
|||
354 |
|
||||
355 | { |
|
|||
356 | for p in self.graph.parents(curr)?.iter().cloned() { |
|
348 | for p in self.graph.parents(curr)?.iter().cloned() { | |
357 | if p == NULL_REVISION { |
|
349 | if p == NULL_REVISION { | |
358 | continue; |
|
350 | continue; | |
359 | } |
|
351 | } | |
360 | let in_other_visit = if this_visit_is_revs { |
|
352 | let in_other_visit = if this_visit_is_revs { | |
361 | bases_visit.contains(&p) |
|
353 | bases_visit.contains(&p) | |
362 | } else { |
|
354 | } else { | |
363 | revs_visit.contains(&p) |
|
355 | revs_visit.contains(&p) | |
364 | }; |
|
356 | }; | |
365 | if in_other_visit || both_visit.contains(&p) { |
|
357 | if in_other_visit || both_visit.contains(&p) { | |
366 | // p is implicitely in this_visit. |
|
358 | // p is implicitely in this_visit. | |
367 | // This means p is or should be in bothvisit |
|
359 | // This means p is or should be in bothvisit | |
368 | // TODO optim: hence if bothvisit, we look up twice |
|
360 | // TODO optim: hence if bothvisit, we look up twice | |
369 | revs_visit.remove(&p); |
|
361 | revs_visit.remove(&p); | |
370 | bases_visit.insert(p); |
|
362 | bases_visit.insert(p); | |
371 | both_visit.insert(p); |
|
363 | both_visit.insert(p); | |
372 | } else { |
|
364 | } else { | |
373 | // visit later |
|
365 | // visit later | |
374 | if this_visit_is_revs { |
|
366 | if this_visit_is_revs { | |
375 | revs_visit.insert(p); |
|
367 | revs_visit.insert(p); | |
376 | } else { |
|
368 | } else { | |
377 | bases_visit.insert(p); |
|
369 | bases_visit.insert(p); | |
378 | } |
|
370 | } | |
379 | } |
|
371 | } | |
380 | } |
|
372 | } | |
|
373 | } else if bases_visit.contains(&curr) { | |||
|
374 | this_visit_is_revs = false; | |||
|
375 | for p in self.graph.parents(curr)?.iter().cloned() { | |||
|
376 | if p == NULL_REVISION { | |||
|
377 | continue; | |||
|
378 | } | |||
|
379 | let in_other_visit = if this_visit_is_revs { | |||
|
380 | bases_visit.contains(&p) | |||
|
381 | } else { | |||
|
382 | revs_visit.contains(&p) | |||
|
383 | }; | |||
|
384 | if in_other_visit || both_visit.contains(&p) { | |||
|
385 | // p is implicitely in this_visit. | |||
|
386 | // This means p is or should be in bothvisit | |||
|
387 | // TODO optim: hence if bothvisit, we look up twice | |||
|
388 | revs_visit.remove(&p); | |||
|
389 | bases_visit.insert(p); | |||
|
390 | both_visit.insert(p); | |||
|
391 | } else { | |||
|
392 | // visit later | |||
|
393 | if this_visit_is_revs { | |||
|
394 | revs_visit.insert(p); | |||
|
395 | } else { | |||
|
396 | bases_visit.insert(p); | |||
|
397 | } | |||
|
398 | } | |||
|
399 | } | |||
|
400 | } else { | |||
|
401 | // not an ancestor of revs or bases: ignore | |||
381 | } |
|
402 | } | |
382 | } |
|
403 | } | |
383 | missing.reverse(); |
|
404 | missing.reverse(); | |
384 | Ok(missing) |
|
405 | Ok(missing) | |
385 | } |
|
406 | } | |
386 | } |
|
407 | } | |
387 |
|
408 | |||
388 | #[cfg(test)] |
|
409 | #[cfg(test)] | |
389 | mod tests { |
|
410 | mod tests { | |
390 |
|
411 | |||
391 | use super::*; |
|
412 | use super::*; | |
392 | use std::iter::FromIterator; |
|
413 | use std::iter::FromIterator; | |
393 |
|
414 | |||
394 | #[derive(Clone, Debug)] |
|
415 | #[derive(Clone, Debug)] | |
395 | struct Stub; |
|
416 | struct Stub; | |
396 |
|
417 | |||
397 | /// This is the same as the dict from test-ancestors.py |
|
418 | /// This is the same as the dict from test-ancestors.py | |
398 | impl Graph for Stub { |
|
419 | impl Graph for Stub { | |
399 | fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> { |
|
420 | fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> { | |
400 | match rev { |
|
421 | match rev { | |
401 | 0 => Ok([-1, -1]), |
|
422 | 0 => Ok([-1, -1]), | |
402 | 1 => Ok([0, -1]), |
|
423 | 1 => Ok([0, -1]), | |
403 | 2 => Ok([1, -1]), |
|
424 | 2 => Ok([1, -1]), | |
404 | 3 => Ok([1, -1]), |
|
425 | 3 => Ok([1, -1]), | |
405 | 4 => Ok([2, -1]), |
|
426 | 4 => Ok([2, -1]), | |
406 | 5 => Ok([4, -1]), |
|
427 | 5 => Ok([4, -1]), | |
407 | 6 => Ok([4, -1]), |
|
428 | 6 => Ok([4, -1]), | |
408 | 7 => Ok([4, -1]), |
|
429 | 7 => Ok([4, -1]), | |
409 | 8 => Ok([-1, -1]), |
|
430 | 8 => Ok([-1, -1]), | |
410 | 9 => Ok([6, 7]), |
|
431 | 9 => Ok([6, 7]), | |
411 | 10 => Ok([5, -1]), |
|
432 | 10 => Ok([5, -1]), | |
412 | 11 => Ok([3, 7]), |
|
433 | 11 => Ok([3, 7]), | |
413 | 12 => Ok([9, -1]), |
|
434 | 12 => Ok([9, -1]), | |
414 | 13 => Ok([8, -1]), |
|
435 | 13 => Ok([8, -1]), | |
415 | r => Err(GraphError::ParentOutOfRange(r)), |
|
436 | r => Err(GraphError::ParentOutOfRange(r)), | |
416 | } |
|
437 | } | |
417 | } |
|
438 | } | |
418 | } |
|
439 | } | |
419 |
|
440 | |||
420 | fn list_ancestors<G: Graph>( |
|
441 | fn list_ancestors<G: Graph>( | |
421 | graph: G, |
|
442 | graph: G, | |
422 | initrevs: Vec<Revision>, |
|
443 | initrevs: Vec<Revision>, | |
423 | stoprev: Revision, |
|
444 | stoprev: Revision, | |
424 | inclusive: bool, |
|
445 | inclusive: bool, | |
425 | ) -> Vec<Revision> { |
|
446 | ) -> Vec<Revision> { | |
426 | AncestorsIterator::new(graph, initrevs, stoprev, inclusive) |
|
447 | AncestorsIterator::new(graph, initrevs, stoprev, inclusive) | |
427 | .unwrap() |
|
448 | .unwrap() | |
428 | .map(|res| res.unwrap()) |
|
449 | .map(|res| res.unwrap()) | |
429 | .collect() |
|
450 | .collect() | |
430 | } |
|
451 | } | |
431 |
|
452 | |||
432 | #[test] |
|
453 | #[test] | |
433 | /// Same tests as test-ancestor.py, without membership |
|
454 | /// Same tests as test-ancestor.py, without membership | |
434 | /// (see also test-ancestor.py.out) |
|
455 | /// (see also test-ancestor.py.out) | |
435 | fn test_list_ancestor() { |
|
456 | fn test_list_ancestor() { | |
436 | assert_eq!(list_ancestors(Stub, vec![], 0, false), vec![]); |
|
457 | assert_eq!(list_ancestors(Stub, vec![], 0, false), vec![]); | |
437 | assert_eq!( |
|
458 | assert_eq!( | |
438 | list_ancestors(Stub, vec![11, 13], 0, false), |
|
459 | list_ancestors(Stub, vec![11, 13], 0, false), | |
439 | vec![8, 7, 4, 3, 2, 1, 0] |
|
460 | vec![8, 7, 4, 3, 2, 1, 0] | |
440 | ); |
|
461 | ); | |
441 | assert_eq!(list_ancestors(Stub, vec![1, 3], 0, false), vec![1, 0]); |
|
462 | assert_eq!(list_ancestors(Stub, vec![1, 3], 0, false), vec![1, 0]); | |
442 | assert_eq!( |
|
463 | assert_eq!( | |
443 | list_ancestors(Stub, vec![11, 13], 0, true), |
|
464 | list_ancestors(Stub, vec![11, 13], 0, true), | |
444 | vec![13, 11, 8, 7, 4, 3, 2, 1, 0] |
|
465 | vec![13, 11, 8, 7, 4, 3, 2, 1, 0] | |
445 | ); |
|
466 | ); | |
446 | assert_eq!(list_ancestors(Stub, vec![11, 13], 6, false), vec![8, 7]); |
|
467 | assert_eq!(list_ancestors(Stub, vec![11, 13], 6, false), vec![8, 7]); | |
447 | assert_eq!( |
|
468 | assert_eq!( | |
448 | list_ancestors(Stub, vec![11, 13], 6, true), |
|
469 | list_ancestors(Stub, vec![11, 13], 6, true), | |
449 | vec![13, 11, 8, 7] |
|
470 | vec![13, 11, 8, 7] | |
450 | ); |
|
471 | ); | |
451 | assert_eq!(list_ancestors(Stub, vec![11, 13], 11, true), vec![13, 11]); |
|
472 | assert_eq!(list_ancestors(Stub, vec![11, 13], 11, true), vec![13, 11]); | |
452 | assert_eq!(list_ancestors(Stub, vec![11, 13], 12, true), vec![13]); |
|
473 | assert_eq!(list_ancestors(Stub, vec![11, 13], 12, true), vec![13]); | |
453 | assert_eq!( |
|
474 | assert_eq!( | |
454 | list_ancestors(Stub, vec![10, 1], 0, true), |
|
475 | list_ancestors(Stub, vec![10, 1], 0, true), | |
455 | vec![10, 5, 4, 2, 1, 0] |
|
476 | vec![10, 5, 4, 2, 1, 0] | |
456 | ); |
|
477 | ); | |
457 | } |
|
478 | } | |
458 |
|
479 | |||
459 | #[test] |
|
480 | #[test] | |
460 | /// Corner case that's not directly in test-ancestors.py, but |
|
481 | /// Corner case that's not directly in test-ancestors.py, but | |
461 | /// that happens quite often, as demonstrated by running the whole |
|
482 | /// that happens quite often, as demonstrated by running the whole | |
462 | /// suite. |
|
483 | /// suite. | |
463 | /// For instance, run tests/test-obsolete-checkheads.t |
|
484 | /// For instance, run tests/test-obsolete-checkheads.t | |
464 | fn test_nullrev_input() { |
|
485 | fn test_nullrev_input() { | |
465 | let mut iter = |
|
486 | let mut iter = | |
466 | AncestorsIterator::new(Stub, vec![-1], 0, false).unwrap(); |
|
487 | AncestorsIterator::new(Stub, vec![-1], 0, false).unwrap(); | |
467 | assert_eq!(iter.next(), None) |
|
488 | assert_eq!(iter.next(), None) | |
468 | } |
|
489 | } | |
469 |
|
490 | |||
470 | #[test] |
|
491 | #[test] | |
471 | fn test_contains() { |
|
492 | fn test_contains() { | |
472 | let mut lazy = |
|
493 | let mut lazy = | |
473 | AncestorsIterator::new(Stub, vec![10, 1], 0, true).unwrap(); |
|
494 | AncestorsIterator::new(Stub, vec![10, 1], 0, true).unwrap(); | |
474 | assert!(lazy.contains(1).unwrap()); |
|
495 | assert!(lazy.contains(1).unwrap()); | |
475 | assert!(!lazy.contains(3).unwrap()); |
|
496 | assert!(!lazy.contains(3).unwrap()); | |
476 |
|
497 | |||
477 | let mut lazy = |
|
498 | let mut lazy = | |
478 | AncestorsIterator::new(Stub, vec![0], 0, false).unwrap(); |
|
499 | AncestorsIterator::new(Stub, vec![0], 0, false).unwrap(); | |
479 | assert!(!lazy.contains(NULL_REVISION).unwrap()); |
|
500 | assert!(!lazy.contains(NULL_REVISION).unwrap()); | |
480 | } |
|
501 | } | |
481 |
|
502 | |||
482 | #[test] |
|
503 | #[test] | |
483 | fn test_peek() { |
|
504 | fn test_peek() { | |
484 | let mut iter = |
|
505 | let mut iter = | |
485 | AncestorsIterator::new(Stub, vec![10], 0, true).unwrap(); |
|
506 | AncestorsIterator::new(Stub, vec![10], 0, true).unwrap(); | |
486 | // peek() gives us the next value |
|
507 | // peek() gives us the next value | |
487 | assert_eq!(iter.peek(), Some(10)); |
|
508 | assert_eq!(iter.peek(), Some(10)); | |
488 | // but it's not been consumed |
|
509 | // but it's not been consumed | |
489 | assert_eq!(iter.next(), Some(Ok(10))); |
|
510 | assert_eq!(iter.next(), Some(Ok(10))); | |
490 | // and iteration resumes normally |
|
511 | // and iteration resumes normally | |
491 | assert_eq!(iter.next(), Some(Ok(5))); |
|
512 | assert_eq!(iter.next(), Some(Ok(5))); | |
492 |
|
513 | |||
493 | // let's drain the iterator to test peek() at the end |
|
514 | // let's drain the iterator to test peek() at the end | |
494 | while iter.next().is_some() {} |
|
515 | while iter.next().is_some() {} | |
495 | assert_eq!(iter.peek(), None); |
|
516 | assert_eq!(iter.peek(), None); | |
496 | } |
|
517 | } | |
497 |
|
518 | |||
498 | #[test] |
|
519 | #[test] | |
499 | fn test_empty() { |
|
520 | fn test_empty() { | |
500 | let mut iter = |
|
521 | let mut iter = | |
501 | AncestorsIterator::new(Stub, vec![10], 0, true).unwrap(); |
|
522 | AncestorsIterator::new(Stub, vec![10], 0, true).unwrap(); | |
502 | assert!(!iter.is_empty()); |
|
523 | assert!(!iter.is_empty()); | |
503 | while iter.next().is_some() {} |
|
524 | while iter.next().is_some() {} | |
504 | assert!(!iter.is_empty()); |
|
525 | assert!(!iter.is_empty()); | |
505 |
|
526 | |||
506 | let iter = AncestorsIterator::new(Stub, vec![], 0, true).unwrap(); |
|
527 | let iter = AncestorsIterator::new(Stub, vec![], 0, true).unwrap(); | |
507 | assert!(iter.is_empty()); |
|
528 | assert!(iter.is_empty()); | |
508 |
|
529 | |||
509 | // case where iter.seen == {NULL_REVISION} |
|
530 | // case where iter.seen == {NULL_REVISION} | |
510 | let iter = AncestorsIterator::new(Stub, vec![0], 0, false).unwrap(); |
|
531 | let iter = AncestorsIterator::new(Stub, vec![0], 0, false).unwrap(); | |
511 | assert!(iter.is_empty()); |
|
532 | assert!(iter.is_empty()); | |
512 | } |
|
533 | } | |
513 |
|
534 | |||
514 | /// A corrupted Graph, supporting error handling tests |
|
535 | /// A corrupted Graph, supporting error handling tests | |
515 | #[derive(Clone, Debug)] |
|
536 | #[derive(Clone, Debug)] | |
516 | struct Corrupted; |
|
537 | struct Corrupted; | |
517 |
|
538 | |||
518 | impl Graph for Corrupted { |
|
539 | impl Graph for Corrupted { | |
519 | fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> { |
|
540 | fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> { | |
520 | match rev { |
|
541 | match rev { | |
521 | 1 => Ok([0, -1]), |
|
542 | 1 => Ok([0, -1]), | |
522 | r => Err(GraphError::ParentOutOfRange(r)), |
|
543 | r => Err(GraphError::ParentOutOfRange(r)), | |
523 | } |
|
544 | } | |
524 | } |
|
545 | } | |
525 | } |
|
546 | } | |
526 |
|
547 | |||
527 | #[test] |
|
548 | #[test] | |
528 | fn test_initrev_out_of_range() { |
|
549 | fn test_initrev_out_of_range() { | |
529 | // inclusive=false looks up initrev's parents right away |
|
550 | // inclusive=false looks up initrev's parents right away | |
530 | match AncestorsIterator::new(Stub, vec![25], 0, false) { |
|
551 | match AncestorsIterator::new(Stub, vec![25], 0, false) { | |
531 | Ok(_) => panic!("Should have been ParentOutOfRange"), |
|
552 | Ok(_) => panic!("Should have been ParentOutOfRange"), | |
532 | Err(e) => assert_eq!(e, GraphError::ParentOutOfRange(25)), |
|
553 | Err(e) => assert_eq!(e, GraphError::ParentOutOfRange(25)), | |
533 | } |
|
554 | } | |
534 | } |
|
555 | } | |
535 |
|
556 | |||
536 | #[test] |
|
557 | #[test] | |
537 | fn test_next_out_of_range() { |
|
558 | fn test_next_out_of_range() { | |
538 | // inclusive=false looks up initrev's parents right away |
|
559 | // inclusive=false looks up initrev's parents right away | |
539 | let mut iter = |
|
560 | let mut iter = | |
540 | AncestorsIterator::new(Corrupted, vec![1], 0, false).unwrap(); |
|
561 | AncestorsIterator::new(Corrupted, vec![1], 0, false).unwrap(); | |
541 | assert_eq!(iter.next(), Some(Err(GraphError::ParentOutOfRange(0)))); |
|
562 | assert_eq!(iter.next(), Some(Err(GraphError::ParentOutOfRange(0)))); | |
542 | } |
|
563 | } | |
543 |
|
564 | |||
544 | #[test] |
|
565 | #[test] | |
545 | fn test_lazy_iter_contains() { |
|
566 | fn test_lazy_iter_contains() { | |
546 | let mut lazy = |
|
567 | let mut lazy = | |
547 | LazyAncestors::new(Stub, vec![11, 13], 0, false).unwrap(); |
|
568 | LazyAncestors::new(Stub, vec![11, 13], 0, false).unwrap(); | |
548 |
|
569 | |||
549 | let revs: Vec<Revision> = lazy.iter().map(|r| r.unwrap()).collect(); |
|
570 | let revs: Vec<Revision> = lazy.iter().map(|r| r.unwrap()).collect(); | |
550 | // compare with iterator tests on the same initial revisions |
|
571 | // compare with iterator tests on the same initial revisions | |
551 | assert_eq!(revs, vec![8, 7, 4, 3, 2, 1, 0]); |
|
572 | assert_eq!(revs, vec![8, 7, 4, 3, 2, 1, 0]); | |
552 |
|
573 | |||
553 | // contains() results are correct, unaffected by the fact that |
|
574 | // contains() results are correct, unaffected by the fact that | |
554 | // we consumed entirely an iterator out of lazy |
|
575 | // we consumed entirely an iterator out of lazy | |
555 | assert_eq!(lazy.contains(2), Ok(true)); |
|
576 | assert_eq!(lazy.contains(2), Ok(true)); | |
556 | assert_eq!(lazy.contains(9), Ok(false)); |
|
577 | assert_eq!(lazy.contains(9), Ok(false)); | |
557 | } |
|
578 | } | |
558 |
|
579 | |||
559 | #[test] |
|
580 | #[test] | |
560 | fn test_lazy_contains_iter() { |
|
581 | fn test_lazy_contains_iter() { | |
561 | let mut lazy = |
|
582 | let mut lazy = | |
562 | LazyAncestors::new(Stub, vec![11, 13], 0, false).unwrap(); // reminder: [8, 7, 4, 3, 2, 1, 0] |
|
583 | LazyAncestors::new(Stub, vec![11, 13], 0, false).unwrap(); // reminder: [8, 7, 4, 3, 2, 1, 0] | |
563 |
|
584 | |||
564 | assert_eq!(lazy.contains(2), Ok(true)); |
|
585 | assert_eq!(lazy.contains(2), Ok(true)); | |
565 | assert_eq!(lazy.contains(6), Ok(false)); |
|
586 | assert_eq!(lazy.contains(6), Ok(false)); | |
566 |
|
587 | |||
567 | // after consumption of 2 by the inner iterator, results stay |
|
588 | // after consumption of 2 by the inner iterator, results stay | |
568 | // consistent |
|
589 | // consistent | |
569 | assert_eq!(lazy.contains(2), Ok(true)); |
|
590 | assert_eq!(lazy.contains(2), Ok(true)); | |
570 | assert_eq!(lazy.contains(5), Ok(false)); |
|
591 | assert_eq!(lazy.contains(5), Ok(false)); | |
571 |
|
592 | |||
572 | // iter() still gives us a fresh iterator |
|
593 | // iter() still gives us a fresh iterator | |
573 | let revs: Vec<Revision> = lazy.iter().map(|r| r.unwrap()).collect(); |
|
594 | let revs: Vec<Revision> = lazy.iter().map(|r| r.unwrap()).collect(); | |
574 | assert_eq!(revs, vec![8, 7, 4, 3, 2, 1, 0]); |
|
595 | assert_eq!(revs, vec![8, 7, 4, 3, 2, 1, 0]); | |
575 | } |
|
596 | } | |
576 |
|
597 | |||
577 | #[test] |
|
598 | #[test] | |
578 | /// Test constructor, add/get bases |
|
599 | /// Test constructor, add/get bases | |
579 | fn test_missing_bases() { |
|
600 | fn test_missing_bases() { | |
580 | let mut missing_ancestors = |
|
601 | let mut missing_ancestors = | |
581 | MissingAncestors::new(Stub, [5, 3, 1, 3].iter().cloned()); |
|
602 | MissingAncestors::new(Stub, [5, 3, 1, 3].iter().cloned()); | |
582 | let mut as_vec: Vec<Revision> = |
|
603 | let mut as_vec: Vec<Revision> = | |
583 | missing_ancestors.get_bases().iter().cloned().collect(); |
|
604 | missing_ancestors.get_bases().iter().cloned().collect(); | |
584 | as_vec.sort(); |
|
605 | as_vec.sort(); | |
585 | assert_eq!(as_vec, [1, 3, 5]); |
|
606 | assert_eq!(as_vec, [1, 3, 5]); | |
586 |
|
607 | |||
587 | missing_ancestors.add_bases([3, 7, 8].iter().cloned()); |
|
608 | missing_ancestors.add_bases([3, 7, 8].iter().cloned()); | |
588 | as_vec = missing_ancestors.get_bases().iter().cloned().collect(); |
|
609 | as_vec = missing_ancestors.get_bases().iter().cloned().collect(); | |
589 | as_vec.sort(); |
|
610 | as_vec.sort(); | |
590 | assert_eq!(as_vec, [1, 3, 5, 7, 8]); |
|
611 | assert_eq!(as_vec, [1, 3, 5, 7, 8]); | |
591 | } |
|
612 | } | |
592 |
|
613 | |||
593 | fn assert_missing_remove( |
|
614 | fn assert_missing_remove( | |
594 | bases: &[Revision], |
|
615 | bases: &[Revision], | |
595 | revs: &[Revision], |
|
616 | revs: &[Revision], | |
596 | expected: &[Revision], |
|
617 | expected: &[Revision], | |
597 | ) { |
|
618 | ) { | |
598 | let mut missing_ancestors = |
|
619 | let mut missing_ancestors = | |
599 | MissingAncestors::new(Stub, bases.iter().cloned()); |
|
620 | MissingAncestors::new(Stub, bases.iter().cloned()); | |
600 | let mut revset: HashSet<Revision> = revs.iter().cloned().collect(); |
|
621 | let mut revset: HashSet<Revision> = revs.iter().cloned().collect(); | |
601 | missing_ancestors |
|
622 | missing_ancestors | |
602 | .remove_ancestors_from(&mut revset) |
|
623 | .remove_ancestors_from(&mut revset) | |
603 | .unwrap(); |
|
624 | .unwrap(); | |
604 | let mut as_vec: Vec<Revision> = revset.into_iter().collect(); |
|
625 | let mut as_vec: Vec<Revision> = revset.into_iter().collect(); | |
605 | as_vec.sort(); |
|
626 | as_vec.sort(); | |
606 | assert_eq!(as_vec.as_slice(), expected); |
|
627 | assert_eq!(as_vec.as_slice(), expected); | |
607 | } |
|
628 | } | |
608 |
|
629 | |||
609 | #[test] |
|
630 | #[test] | |
610 | fn test_missing_remove() { |
|
631 | fn test_missing_remove() { | |
611 | assert_missing_remove( |
|
632 | assert_missing_remove( | |
612 | &[1, 2, 3, 4, 7], |
|
633 | &[1, 2, 3, 4, 7], | |
613 | Vec::from_iter(1..10).as_slice(), |
|
634 | Vec::from_iter(1..10).as_slice(), | |
614 | &[5, 6, 8, 9], |
|
635 | &[5, 6, 8, 9], | |
615 | ); |
|
636 | ); | |
616 | assert_missing_remove(&[10], &[11, 12, 13, 14], &[11, 12, 13, 14]); |
|
637 | assert_missing_remove(&[10], &[11, 12, 13, 14], &[11, 12, 13, 14]); | |
617 | assert_missing_remove(&[7], &[1, 2, 3, 4, 5], &[3, 5]); |
|
638 | assert_missing_remove(&[7], &[1, 2, 3, 4, 5], &[3, 5]); | |
618 | } |
|
639 | } | |
619 |
|
640 | |||
620 | fn assert_missing_ancestors( |
|
641 | fn assert_missing_ancestors( | |
621 | bases: &[Revision], |
|
642 | bases: &[Revision], | |
622 | revs: &[Revision], |
|
643 | revs: &[Revision], | |
623 | expected: &[Revision], |
|
644 | expected: &[Revision], | |
624 | ) { |
|
645 | ) { | |
625 | let mut missing_ancestors = |
|
646 | let mut missing_ancestors = | |
626 | MissingAncestors::new(Stub, bases.iter().cloned()); |
|
647 | MissingAncestors::new(Stub, bases.iter().cloned()); | |
627 | let missing = missing_ancestors |
|
648 | let missing = missing_ancestors | |
628 | .missing_ancestors(revs.iter().cloned()) |
|
649 | .missing_ancestors(revs.iter().cloned()) | |
629 | .unwrap(); |
|
650 | .unwrap(); | |
630 | assert_eq!(missing.as_slice(), expected); |
|
651 | assert_eq!(missing.as_slice(), expected); | |
631 | } |
|
652 | } | |
632 |
|
653 | |||
633 | #[test] |
|
654 | #[test] | |
634 | fn test_missing_ancestors() { |
|
655 | fn test_missing_ancestors() { | |
635 | // examples taken from test-ancestors.py by having it run |
|
656 | // examples taken from test-ancestors.py by having it run | |
636 | // on the same graph (both naive and fast Python algs) |
|
657 | // on the same graph (both naive and fast Python algs) | |
637 | assert_missing_ancestors(&[10], &[11], &[3, 7, 11]); |
|
658 | assert_missing_ancestors(&[10], &[11], &[3, 7, 11]); | |
638 | assert_missing_ancestors(&[11], &[10], &[5, 10]); |
|
659 | assert_missing_ancestors(&[11], &[10], &[5, 10]); | |
639 | assert_missing_ancestors(&[7], &[9, 11], &[3, 6, 9, 11]); |
|
660 | assert_missing_ancestors(&[7], &[9, 11], &[3, 6, 9, 11]); | |
640 | } |
|
661 | } | |
641 |
|
662 | |||
642 | // A Graph represented by a vector whose indices are revisions |
|
663 | // A Graph represented by a vector whose indices are revisions | |
643 | // and values are parents of the revisions |
|
664 | // and values are parents of the revisions | |
644 | type VecGraph = Vec<[Revision; 2]>; |
|
665 | type VecGraph = Vec<[Revision; 2]>; | |
645 |
|
666 | |||
646 | impl Graph for VecGraph { |
|
667 | impl Graph for VecGraph { | |
647 | fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> { |
|
668 | fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> { | |
648 | Ok(self[rev as usize]) |
|
669 | Ok(self[rev as usize]) | |
649 | } |
|
670 | } | |
650 | } |
|
671 | } | |
651 |
|
672 | |||
652 | /// An interesting case found by a random generator similar to |
|
673 | /// An interesting case found by a random generator similar to | |
653 | /// the one in test-ancestor.py. An early version of Rust MissingAncestors |
|
674 | /// the one in test-ancestor.py. An early version of Rust MissingAncestors | |
654 | /// failed this, yet none of the integration tests of the whole suite |
|
675 | /// failed this, yet none of the integration tests of the whole suite | |
655 | /// catched it. |
|
676 | /// catched it. | |
656 | #[test] |
|
677 | #[test] | |
657 | fn test_remove_ancestors_from_case1() { |
|
678 | fn test_remove_ancestors_from_case1() { | |
658 | let graph: VecGraph = vec![ |
|
679 | let graph: VecGraph = vec![ | |
659 | [NULL_REVISION, NULL_REVISION], |
|
680 | [NULL_REVISION, NULL_REVISION], | |
660 | [0, NULL_REVISION], |
|
681 | [0, NULL_REVISION], | |
661 | [1, 0], |
|
682 | [1, 0], | |
662 | [2, 1], |
|
683 | [2, 1], | |
663 | [3, NULL_REVISION], |
|
684 | [3, NULL_REVISION], | |
664 | [4, NULL_REVISION], |
|
685 | [4, NULL_REVISION], | |
665 | [5, 1], |
|
686 | [5, 1], | |
666 | [2, NULL_REVISION], |
|
687 | [2, NULL_REVISION], | |
667 | [7, NULL_REVISION], |
|
688 | [7, NULL_REVISION], | |
668 | [8, NULL_REVISION], |
|
689 | [8, NULL_REVISION], | |
669 | [9, NULL_REVISION], |
|
690 | [9, NULL_REVISION], | |
670 | [10, 1], |
|
691 | [10, 1], | |
671 | [3, NULL_REVISION], |
|
692 | [3, NULL_REVISION], | |
672 | [12, NULL_REVISION], |
|
693 | [12, NULL_REVISION], | |
673 | [13, NULL_REVISION], |
|
694 | [13, NULL_REVISION], | |
674 | [14, NULL_REVISION], |
|
695 | [14, NULL_REVISION], | |
675 | [4, NULL_REVISION], |
|
696 | [4, NULL_REVISION], | |
676 | [16, NULL_REVISION], |
|
697 | [16, NULL_REVISION], | |
677 | [17, NULL_REVISION], |
|
698 | [17, NULL_REVISION], | |
678 | [18, NULL_REVISION], |
|
699 | [18, NULL_REVISION], | |
679 | [19, 11], |
|
700 | [19, 11], | |
680 | [20, NULL_REVISION], |
|
701 | [20, NULL_REVISION], | |
681 | [21, NULL_REVISION], |
|
702 | [21, NULL_REVISION], | |
682 | [22, NULL_REVISION], |
|
703 | [22, NULL_REVISION], | |
683 | [23, NULL_REVISION], |
|
704 | [23, NULL_REVISION], | |
684 | [2, NULL_REVISION], |
|
705 | [2, NULL_REVISION], | |
685 | [3, NULL_REVISION], |
|
706 | [3, NULL_REVISION], | |
686 | [26, 24], |
|
707 | [26, 24], | |
687 | [27, NULL_REVISION], |
|
708 | [27, NULL_REVISION], | |
688 | [28, NULL_REVISION], |
|
709 | [28, NULL_REVISION], | |
689 | [12, NULL_REVISION], |
|
710 | [12, NULL_REVISION], | |
690 | [1, NULL_REVISION], |
|
711 | [1, NULL_REVISION], | |
691 | [1, 9], |
|
712 | [1, 9], | |
692 | [32, NULL_REVISION], |
|
713 | [32, NULL_REVISION], | |
693 | [33, NULL_REVISION], |
|
714 | [33, NULL_REVISION], | |
694 | [34, 31], |
|
715 | [34, 31], | |
695 | [35, NULL_REVISION], |
|
716 | [35, NULL_REVISION], | |
696 | [36, 26], |
|
717 | [36, 26], | |
697 | [37, NULL_REVISION], |
|
718 | [37, NULL_REVISION], | |
698 | [38, NULL_REVISION], |
|
719 | [38, NULL_REVISION], | |
699 | [39, NULL_REVISION], |
|
720 | [39, NULL_REVISION], | |
700 | [40, NULL_REVISION], |
|
721 | [40, NULL_REVISION], | |
701 | [41, NULL_REVISION], |
|
722 | [41, NULL_REVISION], | |
702 | [42, 26], |
|
723 | [42, 26], | |
703 | [0, NULL_REVISION], |
|
724 | [0, NULL_REVISION], | |
704 | [44, NULL_REVISION], |
|
725 | [44, NULL_REVISION], | |
705 | [45, 4], |
|
726 | [45, 4], | |
706 | [40, NULL_REVISION], |
|
727 | [40, NULL_REVISION], | |
707 | [47, NULL_REVISION], |
|
728 | [47, NULL_REVISION], | |
708 | [36, 0], |
|
729 | [36, 0], | |
709 | [49, NULL_REVISION], |
|
730 | [49, NULL_REVISION], | |
710 | [NULL_REVISION, NULL_REVISION], |
|
731 | [NULL_REVISION, NULL_REVISION], | |
711 | [51, NULL_REVISION], |
|
732 | [51, NULL_REVISION], | |
712 | [52, NULL_REVISION], |
|
733 | [52, NULL_REVISION], | |
713 | [53, NULL_REVISION], |
|
734 | [53, NULL_REVISION], | |
714 | [14, NULL_REVISION], |
|
735 | [14, NULL_REVISION], | |
715 | [55, NULL_REVISION], |
|
736 | [55, NULL_REVISION], | |
716 | [15, NULL_REVISION], |
|
737 | [15, NULL_REVISION], | |
717 | [23, NULL_REVISION], |
|
738 | [23, NULL_REVISION], | |
718 | [58, NULL_REVISION], |
|
739 | [58, NULL_REVISION], | |
719 | [59, NULL_REVISION], |
|
740 | [59, NULL_REVISION], | |
720 | [2, NULL_REVISION], |
|
741 | [2, NULL_REVISION], | |
721 | [61, 59], |
|
742 | [61, 59], | |
722 | [62, NULL_REVISION], |
|
743 | [62, NULL_REVISION], | |
723 | [63, NULL_REVISION], |
|
744 | [63, NULL_REVISION], | |
724 | [NULL_REVISION, NULL_REVISION], |
|
745 | [NULL_REVISION, NULL_REVISION], | |
725 | [65, NULL_REVISION], |
|
746 | [65, NULL_REVISION], | |
726 | [66, NULL_REVISION], |
|
747 | [66, NULL_REVISION], | |
727 | [67, NULL_REVISION], |
|
748 | [67, NULL_REVISION], | |
728 | [68, NULL_REVISION], |
|
749 | [68, NULL_REVISION], | |
729 | [37, 28], |
|
750 | [37, 28], | |
730 | [69, 25], |
|
751 | [69, 25], | |
731 | [71, NULL_REVISION], |
|
752 | [71, NULL_REVISION], | |
732 | [72, NULL_REVISION], |
|
753 | [72, NULL_REVISION], | |
733 | [50, 2], |
|
754 | [50, 2], | |
734 | [74, NULL_REVISION], |
|
755 | [74, NULL_REVISION], | |
735 | [12, NULL_REVISION], |
|
756 | [12, NULL_REVISION], | |
736 | [18, NULL_REVISION], |
|
757 | [18, NULL_REVISION], | |
737 | [77, NULL_REVISION], |
|
758 | [77, NULL_REVISION], | |
738 | [78, NULL_REVISION], |
|
759 | [78, NULL_REVISION], | |
739 | [79, NULL_REVISION], |
|
760 | [79, NULL_REVISION], | |
740 | [43, 33], |
|
761 | [43, 33], | |
741 | [81, NULL_REVISION], |
|
762 | [81, NULL_REVISION], | |
742 | [82, NULL_REVISION], |
|
763 | [82, NULL_REVISION], | |
743 | [83, NULL_REVISION], |
|
764 | [83, NULL_REVISION], | |
744 | [84, 45], |
|
765 | [84, 45], | |
745 | [85, NULL_REVISION], |
|
766 | [85, NULL_REVISION], | |
746 | [86, NULL_REVISION], |
|
767 | [86, NULL_REVISION], | |
747 | [NULL_REVISION, NULL_REVISION], |
|
768 | [NULL_REVISION, NULL_REVISION], | |
748 | [88, NULL_REVISION], |
|
769 | [88, NULL_REVISION], | |
749 | [NULL_REVISION, NULL_REVISION], |
|
770 | [NULL_REVISION, NULL_REVISION], | |
750 | [76, 83], |
|
771 | [76, 83], | |
751 | [44, NULL_REVISION], |
|
772 | [44, NULL_REVISION], | |
752 | [92, NULL_REVISION], |
|
773 | [92, NULL_REVISION], | |
753 | [93, NULL_REVISION], |
|
774 | [93, NULL_REVISION], | |
754 | [9, NULL_REVISION], |
|
775 | [9, NULL_REVISION], | |
755 | [95, 67], |
|
776 | [95, 67], | |
756 | [96, NULL_REVISION], |
|
777 | [96, NULL_REVISION], | |
757 | [97, NULL_REVISION], |
|
778 | [97, NULL_REVISION], | |
758 | [NULL_REVISION, NULL_REVISION], |
|
779 | [NULL_REVISION, NULL_REVISION], | |
759 | ]; |
|
780 | ]; | |
760 | let problem_rev = 28 as Revision; |
|
781 | let problem_rev = 28 as Revision; | |
761 | let problem_base = 70 as Revision; |
|
782 | let problem_base = 70 as Revision; | |
762 | // making the problem obvious: problem_rev is a parent of problem_base |
|
783 | // making the problem obvious: problem_rev is a parent of problem_base | |
763 | assert_eq!(graph.parents(problem_base).unwrap()[1], problem_rev); |
|
784 | assert_eq!(graph.parents(problem_base).unwrap()[1], problem_rev); | |
764 |
|
785 | |||
765 | let mut missing_ancestors: MissingAncestors<VecGraph> = |
|
786 | let mut missing_ancestors: MissingAncestors<VecGraph> = | |
766 | MissingAncestors::new( |
|
787 | MissingAncestors::new( | |
767 | graph, |
|
788 | graph, | |
768 | [60, 26, 70, 3, 96, 19, 98, 49, 97, 47, 1, 6] |
|
789 | [60, 26, 70, 3, 96, 19, 98, 49, 97, 47, 1, 6] | |
769 | .iter() |
|
790 | .iter() | |
770 | .cloned(), |
|
791 | .cloned(), | |
771 | ); |
|
792 | ); | |
772 | assert!(missing_ancestors.bases.contains(&problem_base)); |
|
793 | assert!(missing_ancestors.bases.contains(&problem_base)); | |
773 |
|
794 | |||
774 | let mut revs: HashSet<Revision> = |
|
795 | let mut revs: HashSet<Revision> = | |
775 | [4, 12, 41, 28, 68, 38, 1, 30, 56, 44] |
|
796 | [4, 12, 41, 28, 68, 38, 1, 30, 56, 44] | |
776 | .iter() |
|
797 | .iter() | |
777 | .cloned() |
|
798 | .cloned() | |
778 | .collect(); |
|
799 | .collect(); | |
779 | missing_ancestors.remove_ancestors_from(&mut revs).unwrap(); |
|
800 | missing_ancestors.remove_ancestors_from(&mut revs).unwrap(); | |
780 | assert!(!revs.contains(&problem_rev)); |
|
801 | assert!(!revs.contains(&problem_rev)); | |
781 | } |
|
802 | } | |
782 |
|
803 | |||
783 | } |
|
804 | } |
General Comments 0
You need to be logged in to leave comments.
Login now