rust/hg-core/src/dagops.rs
changeset 44973 26114bd6ec60
parent 42841 ce6797ef6eab
child 49930 e98fd81bb151
equal deleted inserted replaced
44962:ef8dcee272ac 44973:26114bd6ec60
    14 //!   whose parents, if any, don't belong to the collection.
    14 //!   whose parents, if any, don't belong to the collection.
    15 use super::{Graph, GraphError, Revision, NULL_REVISION};
    15 use super::{Graph, GraphError, Revision, NULL_REVISION};
    16 use crate::ancestors::AncestorsIterator;
    16 use crate::ancestors::AncestorsIterator;
    17 use std::collections::{BTreeSet, HashSet};
    17 use std::collections::{BTreeSet, HashSet};
    18 
    18 
    19 fn remove_parents(
    19 fn remove_parents<S: std::hash::BuildHasher>(
    20     graph: &impl Graph,
    20     graph: &impl Graph,
    21     rev: Revision,
    21     rev: Revision,
    22     set: &mut HashSet<Revision>,
    22     set: &mut HashSet<Revision, S>,
    23 ) -> Result<(), GraphError> {
    23 ) -> Result<(), GraphError> {
    24     for parent in graph.parents(rev)?.iter() {
    24     for parent in graph.parents(rev)?.iter() {
    25         if *parent != NULL_REVISION {
    25         if *parent != NULL_REVISION {
    26             set.remove(parent);
    26             set.remove(parent);
    27         }
    27         }
    63 ///   heads.
    63 ///   heads.
    64 /// - a Rust caller can decide whether cloning beforehand is appropriate
    64 /// - a Rust caller can decide whether cloning beforehand is appropriate
    65 ///
    65 ///
    66 /// # Performance notes
    66 /// # Performance notes
    67 /// Internally, this function will store a full copy of `revs` in a `Vec`.
    67 /// Internally, this function will store a full copy of `revs` in a `Vec`.
    68 pub fn retain_heads(
    68 pub fn retain_heads<S: std::hash::BuildHasher>(
    69     graph: &impl Graph,
    69     graph: &impl Graph,
    70     revs: &mut HashSet<Revision>,
    70     revs: &mut HashSet<Revision, S>,
    71 ) -> Result<(), GraphError> {
    71 ) -> Result<(), GraphError> {
    72     revs.remove(&NULL_REVISION);
    72     revs.remove(&NULL_REVISION);
    73     // we need to construct an iterable copy of revs to avoid itering while
    73     // we need to construct an iterable copy of revs to avoid itering while
    74     // mutating
    74     // mutating
    75     let as_vec: Vec<Revision> = revs.iter().cloned().collect();
    75     let as_vec: Vec<Revision> = revs.iter().cloned().collect();
    82 }
    82 }
    83 
    83 
    84 /// Roots of `revs`, passed as a `HashSet`
    84 /// Roots of `revs`, passed as a `HashSet`
    85 ///
    85 ///
    86 /// They are returned in arbitrary order
    86 /// They are returned in arbitrary order
    87 pub fn roots<G: Graph>(
    87 pub fn roots<G: Graph, S: std::hash::BuildHasher>(
    88     graph: &G,
    88     graph: &G,
    89     revs: &HashSet<Revision>,
    89     revs: &HashSet<Revision, S>,
    90 ) -> Result<Vec<Revision>, GraphError> {
    90 ) -> Result<Vec<Revision>, GraphError> {
    91     let mut roots: Vec<Revision> = Vec::new();
    91     let mut roots: Vec<Revision> = Vec::new();
    92     for rev in revs {
    92     for rev in revs {
    93         if graph
    93         if graph
    94             .parents(*rev)?
    94             .parents(*rev)?
   227     /// Apply `roots()` and sort the result for easier comparison
   227     /// Apply `roots()` and sort the result for easier comparison
   228     fn roots_sorted(
   228     fn roots_sorted(
   229         graph: &impl Graph,
   229         graph: &impl Graph,
   230         revs: &[Revision],
   230         revs: &[Revision],
   231     ) -> Result<Vec<Revision>, GraphError> {
   231     ) -> Result<Vec<Revision>, GraphError> {
   232         let mut as_vec = roots(graph, &revs.iter().cloned().collect())?;
   232         let set: HashSet<_> = revs.iter().cloned().collect();
       
   233         let mut as_vec = roots(graph, &set)?;
   233         as_vec.sort();
   234         as_vec.sort();
   234         Ok(as_vec)
   235         Ok(as_vec)
   235     }
   236     }
   236 
   237 
   237     #[test]
   238     #[test]