Skip to content

Commit

Permalink
graph: inline ReverseGraphIterator to callers
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Dec 23, 2024
1 parent ec85302 commit 6f00c56
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cli/src/commands/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use clap_complete::ArgValueCompleter;
use jj_lib::backend::CommitId;
use jj_lib::config::ConfigGetError;
use jj_lib::config::ConfigGetResultExt as _;
use jj_lib::graph::reverse_graph;
use jj_lib::graph::GraphEdgeType;
use jj_lib::graph::ReverseGraphIterator;
use jj_lib::graph::TopoGroupedGraphIterator;
use jj_lib::repo::Repo;
use jj_lib::revset::RevsetEvaluationError;
Expand Down Expand Up @@ -208,7 +208,7 @@ pub(crate) fn cmd_log(
}
}
if args.reversed {
Box::new(ReverseGraphIterator::new(forward_iter)?)
Box::new(reverse_graph(forward_iter)?.into_iter().map(Ok))
} else {
Box::new(forward_iter)
}
Expand Down
28 changes: 1 addition & 27 deletions lib/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ use std::collections::HashSet;
use std::collections::VecDeque;
use std::hash::Hash;

use crate::revset::RevsetEvaluationError;

/// Node and edges pair of type `N`.
pub type GraphNode<N> = (N, Vec<GraphEdge<N>>);

Expand Down Expand Up @@ -74,32 +72,8 @@ fn reachable_targets<N>(edges: &[GraphEdge<N>]) -> impl DoubleEndedIterator<Item
.map(|edge| &edge.target)
}

pub struct ReverseGraphIterator<N> {
items: std::vec::IntoIter<GraphNode<N>>,
}

impl<N> ReverseGraphIterator<N>
where
N: Hash + Eq + Clone,
{
pub fn new(
input: impl Iterator<Item = Result<GraphNode<N>, RevsetEvaluationError>>,
) -> Result<Self, RevsetEvaluationError> {
let items = reverse_graph(input)?.into_iter();
Ok(Self { items })
}
}

impl<N> Iterator for ReverseGraphIterator<N> {
type Item = Result<GraphNode<N>, RevsetEvaluationError>;

fn next(&mut self) -> Option<Self::Item> {
self.items.next().map(Ok)
}
}

/// Creates new graph in which nodes and edges are reversed.
fn reverse_graph<N: Clone + Eq + Hash, E>(
pub fn reverse_graph<N: Clone + Eq + Hash, E>(
input: impl Iterator<Item = Result<GraphNode<N>, E>>,
) -> Result<Vec<GraphNode<N>>, E> {
let mut entries = vec![];
Expand Down
9 changes: 3 additions & 6 deletions lib/tests/test_revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use jj_lib::commit::Commit;
use jj_lib::fileset::FilesetExpression;
use jj_lib::git;
use jj_lib::git_backend::GitBackend;
use jj_lib::graph::reverse_graph;
use jj_lib::graph::GraphEdge;
use jj_lib::graph::ReverseGraphIterator;
use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::RefTarget;
Expand Down Expand Up @@ -3942,7 +3942,7 @@ fn test_evaluate_expression_conflict() {
}

#[test]
fn test_reverse_graph_iterator() {
fn test_reverse_graph() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
Expand Down Expand Up @@ -3977,10 +3977,7 @@ fn test_reverse_graph_iterator() {
repo.as_ref(),
&[&commit_a, &commit_c, &commit_d, &commit_e, &commit_f],
);
let commits: Vec<_> = ReverseGraphIterator::new(revset.iter_graph())
.unwrap()
.try_collect()
.unwrap();
let commits = reverse_graph(revset.iter_graph()).unwrap();
assert_eq!(commits.len(), 5);
assert_eq!(commits[0].0, *commit_a.id());
assert_eq!(commits[1].0, *commit_c.id());
Expand Down

0 comments on commit 6f00c56

Please sign in to comment.