##// END OF EJS Templates
rust-dagops: roots...
Georges Racinet -
r42354:be073355 default
parent child Browse files
Show More
@@ -81,6 +81,27 b' pub fn retain_heads('
81 Ok(())
81 Ok(())
82 }
82 }
83
83
84 /// Roots of `revs`, passed as a `HashSet`
85 ///
86 /// They are returned in arbitrary order
87 pub fn roots<G: Graph>(
88 graph: &G,
89 revs: &HashSet<Revision>,
90 ) -> Result<Vec<Revision>, GraphError> {
91 let mut roots: Vec<Revision> = Vec::new();
92 for rev in revs {
93 if graph
94 .parents(*rev)?
95 .iter()
96 .filter(|p| **p != NULL_REVISION)
97 .all(|p| !revs.contains(p))
98 {
99 roots.push(*rev);
100 }
101 }
102 Ok(roots)
103 }
104
84 /// Compute the topological range between two collections of revisions
105 /// Compute the topological range between two collections of revisions
85 ///
106 ///
86 /// This is equivalent to the revset `<roots>::<heads>`.
107 /// This is equivalent to the revset `<roots>::<heads>`.
@@ -203,6 +224,30 b' mod tests {'
203 Ok(())
224 Ok(())
204 }
225 }
205
226
227 /// Apply `roots()` and sort the result for easier comparison
228 fn roots_sorted(
229 graph: &impl Graph,
230 revs: &[Revision],
231 ) -> Result<Vec<Revision>, GraphError> {
232 let mut as_vec = roots(graph, &revs.iter().cloned().collect())?;
233 as_vec.sort();
234 Ok(as_vec)
235 }
236
237 #[test]
238 fn test_roots() -> Result<(), GraphError> {
239 assert_eq!(roots_sorted(&SampleGraph, &[4, 5, 6])?, vec![4]);
240 assert_eq!(
241 roots_sorted(&SampleGraph, &[4, 1, 6, 12, 0])?,
242 vec![0, 4, 12]
243 );
244 assert_eq!(
245 roots_sorted(&SampleGraph, &[1, 2, 3, 4, 5, 6, 7, 8, 9])?,
246 vec![1, 8]
247 );
248 Ok(())
249 }
250
206 /// Apply `range()` and convert the result into a Vec for easier comparison
251 /// Apply `range()` and convert the result into a Vec for easier comparison
207 fn range_vec(
252 fn range_vec(
208 graph: impl Graph + Clone,
253 graph: impl Graph + Clone,
General Comments 0
You need to be logged in to leave comments. Login now