Show More
@@ -0,0 +1,136 b'' | |||||
|
1 | // dagops.rs | |||
|
2 | // | |||
|
3 | // Copyright 2019 Georges Racinet <georges.racinet@octobus.net> | |||
|
4 | // | |||
|
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. | |||
|
7 | ||||
|
8 | //! Miscellaneous DAG operations | |||
|
9 | //! | |||
|
10 | //! # Terminology | |||
|
11 | //! - By *relative heads* of a collection of revision numbers (`Revision`), | |||
|
12 | //! we mean those revisions that have no children among the collection. | |||
|
13 | //! - Similarly *relative roots* of a collection of `Revision`, we mean | |||
|
14 | //! those whose parents, if any, don't belong to the collection. | |||
|
15 | use super::{Graph, GraphError, Revision, NULL_REVISION}; | |||
|
16 | use std::collections::HashSet; | |||
|
17 | ||||
|
18 | fn remove_parents( | |||
|
19 | graph: &impl Graph, | |||
|
20 | rev: Revision, | |||
|
21 | set: &mut HashSet<Revision>, | |||
|
22 | ) -> Result<(), GraphError> { | |||
|
23 | for parent in graph.parents(rev)?.iter() { | |||
|
24 | if *parent != NULL_REVISION { | |||
|
25 | set.remove(parent); | |||
|
26 | } | |||
|
27 | } | |||
|
28 | Ok(()) | |||
|
29 | } | |||
|
30 | ||||
|
31 | /// Relative heads out of some revisions, passed as an iterator. | |||
|
32 | /// | |||
|
33 | /// These heads are defined as those revisions that have no children | |||
|
34 | /// among those emitted by the iterator. | |||
|
35 | /// | |||
|
36 | /// # Performance notes | |||
|
37 | /// Internally, this clones the iterator, and builds a `HashSet` out of it. | |||
|
38 | /// | |||
|
39 | /// This function takes an `Iterator` instead of `impl IntoIterator` to | |||
|
40 | /// guarantee that cloning the iterator doesn't result in cloning the full | |||
|
41 | /// construct it comes from. | |||
|
42 | pub fn heads<'a>( | |||
|
43 | graph: &impl Graph, | |||
|
44 | iter_revs: impl Clone + Iterator<Item = &'a Revision>, | |||
|
45 | ) -> Result<HashSet<Revision>, GraphError> { | |||
|
46 | let mut heads: HashSet<Revision> = iter_revs.clone().cloned().collect(); | |||
|
47 | heads.remove(&NULL_REVISION); | |||
|
48 | for rev in iter_revs { | |||
|
49 | remove_parents(graph, *rev, &mut heads)?; | |||
|
50 | } | |||
|
51 | Ok(heads) | |||
|
52 | } | |||
|
53 | ||||
|
54 | /// Retain in `revs` only its relative heads. | |||
|
55 | /// | |||
|
56 | /// This is an in-place operation, so that control of the incoming | |||
|
57 | /// set is left to the caller. | |||
|
58 | /// - a direct Python binding would probably need to build its own `HashSet` | |||
|
59 | /// from an incoming iterable, even if its sole purpose is to extract the | |||
|
60 | /// heads. | |||
|
61 | /// - a Rust caller can decide whether cloning beforehand is appropriate | |||
|
62 | /// | |||
|
63 | /// # Performance notes | |||
|
64 | /// Internally, this function will store a full copy of `revs` in a `Vec`. | |||
|
65 | pub fn retain_heads( | |||
|
66 | graph: &impl Graph, | |||
|
67 | revs: &mut HashSet<Revision>, | |||
|
68 | ) -> Result<(), GraphError> { | |||
|
69 | revs.remove(&NULL_REVISION); | |||
|
70 | // we need to construct an iterable copy of revs to avoid itering while | |||
|
71 | // mutating | |||
|
72 | let as_vec: Vec<Revision> = revs.iter().cloned().collect(); | |||
|
73 | for rev in as_vec { | |||
|
74 | remove_parents(graph, rev, revs)?; | |||
|
75 | } | |||
|
76 | Ok(()) | |||
|
77 | } | |||
|
78 | ||||
|
79 | #[cfg(test)] | |||
|
80 | mod tests { | |||
|
81 | ||||
|
82 | use super::*; | |||
|
83 | use crate::testing::SampleGraph; | |||
|
84 | ||||
|
85 | /// Apply `retain_heads()` to the given slice and return as a sorted `Vec` | |||
|
86 | fn retain_heads_sorted( | |||
|
87 | graph: &impl Graph, | |||
|
88 | revs: &[Revision], | |||
|
89 | ) -> Result<Vec<Revision>, GraphError> { | |||
|
90 | let mut revs: HashSet<Revision> = revs.iter().cloned().collect(); | |||
|
91 | retain_heads(graph, &mut revs)?; | |||
|
92 | let mut as_vec: Vec<Revision> = revs.iter().cloned().collect(); | |||
|
93 | as_vec.sort(); | |||
|
94 | Ok(as_vec) | |||
|
95 | } | |||
|
96 | ||||
|
97 | #[test] | |||
|
98 | fn test_retain_heads() -> Result<(), GraphError> { | |||
|
99 | assert_eq!(retain_heads_sorted(&SampleGraph, &[4, 5, 6])?, vec![5, 6]); | |||
|
100 | assert_eq!( | |||
|
101 | retain_heads_sorted(&SampleGraph, &[4, 1, 6, 12, 0])?, | |||
|
102 | vec![1, 6, 12] | |||
|
103 | ); | |||
|
104 | assert_eq!( | |||
|
105 | retain_heads_sorted(&SampleGraph, &[1, 2, 3, 4, 5, 6, 7, 8, 9])?, | |||
|
106 | vec![3, 5, 8, 9] | |||
|
107 | ); | |||
|
108 | Ok(()) | |||
|
109 | } | |||
|
110 | ||||
|
111 | /// Apply `heads()` to the given slice and return as a sorted `Vec` | |||
|
112 | fn heads_sorted( | |||
|
113 | graph: &impl Graph, | |||
|
114 | revs: &[Revision], | |||
|
115 | ) -> Result<Vec<Revision>, GraphError> { | |||
|
116 | let heads = heads(graph, revs.iter())?; | |||
|
117 | let mut as_vec: Vec<Revision> = heads.iter().cloned().collect(); | |||
|
118 | as_vec.sort(); | |||
|
119 | Ok(as_vec) | |||
|
120 | } | |||
|
121 | ||||
|
122 | #[test] | |||
|
123 | fn test_heads() -> Result<(), GraphError> { | |||
|
124 | assert_eq!(heads_sorted(&SampleGraph, &[4, 5, 6])?, vec![5, 6]); | |||
|
125 | assert_eq!( | |||
|
126 | heads_sorted(&SampleGraph, &[4, 1, 6, 12, 0])?, | |||
|
127 | vec![1, 6, 12] | |||
|
128 | ); | |||
|
129 | assert_eq!( | |||
|
130 | heads_sorted(&SampleGraph, &[1, 2, 3, 4, 5, 6, 7, 8, 9])?, | |||
|
131 | vec![3, 5, 8, 9] | |||
|
132 | ); | |||
|
133 | Ok(()) | |||
|
134 | } | |||
|
135 | ||||
|
136 | } |
@@ -3,6 +3,7 b'' | |||||
3 | // This software may be used and distributed according to the terms of the |
|
3 | // This software may be used and distributed according to the terms of the | |
4 | // GNU General Public License version 2 or any later version. |
|
4 | // GNU General Public License version 2 or any later version. | |
5 | mod ancestors; |
|
5 | mod ancestors; | |
|
6 | pub mod dagops; | |||
6 | pub use ancestors::{AncestorsIterator, LazyAncestors, MissingAncestors}; |
|
7 | pub use ancestors::{AncestorsIterator, LazyAncestors, MissingAncestors}; | |
7 | #[cfg(test)] |
|
8 | #[cfg(test)] | |
8 | pub mod testing; |
|
9 | pub mod testing; |
General Comments 0
You need to be logged in to leave comments.
Login now