dag_walk: extract topo-order helper that doesn't reverse the result

I'll add an iterator wrapper that pop()s the sorted result.
This commit is contained in:
Yuya Nishihara 2023-06-09 19:32:34 +09:00
parent 4987c74d4b
commit fb7fff4409
2 changed files with 19 additions and 6 deletions

View file

@ -44,8 +44,7 @@ where
}) })
} }
/// Returns neighbors before the node itself. pub fn topo_order_forward<T, ID, II, NI>(
pub fn topo_order_reverse<T, ID, II, NI>(
start: II, start: II,
id_fn: impl Fn(&T) -> ID, id_fn: impl Fn(&T) -> ID,
mut neighbors_fn: impl FnMut(&T) -> NI, mut neighbors_fn: impl FnMut(&T) -> NI,
@ -75,6 +74,21 @@ where
result.push(node); result.push(node);
} }
} }
result
}
/// Returns neighbors before the node itself.
pub fn topo_order_reverse<T, ID, II, NI>(
start: II,
id_fn: impl Fn(&T) -> ID,
neighbors_fn: impl FnMut(&T) -> NI,
) -> Vec<T>
where
ID: Hash + Eq + Clone,
II: IntoIterator<Item = T>,
NI: IntoIterator<Item = T>,
{
let mut result = topo_order_forward(start, id_fn, neighbors_fn);
result.reverse(); result.reverse();
result result
} }

View file

@ -29,7 +29,6 @@ use self::dirty_cell::DirtyCell;
use crate::backend::{Backend, BackendError, BackendResult, ChangeId, CommitId, ObjectId, TreeId}; use crate::backend::{Backend, BackendError, BackendResult, ChangeId, CommitId, ObjectId, TreeId};
use crate::commit::Commit; use crate::commit::Commit;
use crate::commit_builder::CommitBuilder; use crate::commit_builder::CommitBuilder;
use crate::dag_walk::topo_order_reverse;
use crate::default_index_store::DefaultIndexStore; use crate::default_index_store::DefaultIndexStore;
use crate::git_backend::GitBackend; use crate::git_backend::GitBackend;
use crate::index::{HexPrefix, Index, IndexStore, MutableIndex, PrefixResolution, ReadonlyIndex}; use crate::index::{HexPrefix, Index, IndexStore, MutableIndex, PrefixResolution, ReadonlyIndex};
@ -46,7 +45,7 @@ use crate::simple_op_store::SimpleOpStore;
use crate::store::Store; use crate::store::Store;
use crate::transaction::Transaction; use crate::transaction::Transaction;
use crate::view::{RefName, View}; use crate::view::{RefName, View};
use crate::{backend, op_store}; use crate::{backend, dag_walk, op_store};
pub trait Repo { pub trait Repo {
fn store(&self) -> &Arc<Store>; fn store(&self) -> &Arc<Store>;
@ -849,7 +848,7 @@ impl MutableRepo {
self.view.get_mut().remove_head(parent_id); self.view.get_mut().remove_head(parent_id);
} }
} else { } else {
let missing_commits = topo_order_reverse( let missing_commits = dag_walk::topo_order_forward(
vec![head.clone()], vec![head.clone()],
|commit: &Commit| commit.id().clone(), |commit: &Commit| commit.id().clone(),
|commit: &Commit| -> Vec<Commit> { |commit: &Commit| -> Vec<Commit> {
@ -860,7 +859,7 @@ impl MutableRepo {
.collect() .collect()
}, },
); );
for missing_commit in missing_commits.iter().rev() { for missing_commit in &missing_commits {
self.index.add_commit(missing_commit); self.index.add_commit(missing_commit);
} }
self.view.get_mut().add_head(head.id()); self.view.get_mut().add_head(head.id());