Skip to content

Commit

Permalink
Using id_map everywhere except tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed May 23, 2024
1 parent 4973fd3 commit 7291351
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 256 deletions.
51 changes: 30 additions & 21 deletions src/diff/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@

use crate::{
hash::DftHashMap,
parse::syntax::{Syntax, SyntaxId},
parse::syntax::{Syntax, SyntaxId, SyntaxIdMap},
};

#[derive(PartialEq, Eq, Clone, Copy)]
pub(crate) enum ChangeKind<'a> {
Unchanged(&'a Syntax<'a>),
ReplacedComment(&'a Syntax<'a>, &'a Syntax<'a>),
ReplacedString(&'a Syntax<'a>, &'a Syntax<'a>),
pub(crate) enum ChangeKind {
Unchanged(SyntaxId),
ReplacedComment(SyntaxId, SyntaxId),
ReplacedString(SyntaxId, SyntaxId),
Novel,
}

#[derive(Debug, Default)]
pub(crate) struct ChangeMap<'a> {
changes: DftHashMap<SyntaxId, ChangeKind<'a>>,
pub(crate) struct ChangeMap {
changes: DftHashMap<SyntaxId, ChangeKind>,
}

impl<'a> ChangeMap<'a> {
pub(crate) fn insert(&mut self, node: &'a Syntax<'a>, ck: ChangeKind<'a>) {
self.changes.insert(node.id(), ck);
impl ChangeMap {
pub(crate) fn insert(&mut self, node: SyntaxId, ck: ChangeKind) {
self.changes.insert(node, ck);
}

pub(crate) fn get(&self, node: &Syntax<'a>) -> Option<ChangeKind<'a>> {
self.changes.get(&node.id()).copied()
pub(crate) fn get(&self, node: SyntaxId) -> Option<ChangeKind> {
self.changes.get(&node).copied()
}
}

pub(crate) fn insert_deep_unchanged<'a>(
node: &'a Syntax<'a>,
opposite_node: &'a Syntax<'a>,
change_map: &mut ChangeMap<'a>,
pub(crate) fn insert_deep_unchanged<'s>(
node_id: SyntaxId,
opposite_node_id: SyntaxId,
id_map: &SyntaxIdMap<'s>,
change_map: &mut ChangeMap,
) {
change_map.insert(node, ChangeKind::Unchanged(opposite_node));
let node = id_map[&node_id];
let opposite_node = id_map[&opposite_node_id];

change_map.insert(node_id, ChangeKind::Unchanged(opposite_node_id));

match (node, opposite_node) {
(
Expand All @@ -47,20 +51,25 @@ pub(crate) fn insert_deep_unchanged<'a>(
},
) => {
for (child, opposite_child) in node_children.iter().zip(opposite_children) {
insert_deep_unchanged(child, opposite_child, change_map);
insert_deep_unchanged(child.id(), opposite_child.id(), id_map, change_map);
}
}
(Syntax::Atom { .. }, Syntax::Atom { .. }) => {}
_ => unreachable!("Unchanged nodes should be both lists, or both atoms"),
}
}

pub(crate) fn insert_deep_novel<'a>(node: &'a Syntax<'a>, change_map: &mut ChangeMap<'a>) {
change_map.insert(node, ChangeKind::Novel);
pub(crate) fn insert_deep_novel<'s>(
node_id: SyntaxId,
id_map: &SyntaxIdMap<'s>,
change_map: &mut ChangeMap,
) {
change_map.insert(node_id, ChangeKind::Novel);

let node = id_map[&node_id];
if let Syntax::List { children, .. } = node {
for child in children.iter() {
insert_deep_novel(child, change_map);
insert_deep_novel(child.id(), id_map, change_map);
}
}
}
54 changes: 39 additions & 15 deletions src/diff/dijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn tree_count(root: Option<&Syntax>) -> u32 {
pub(crate) fn mark_syntax<'a>(
lhs_syntax_id: Option<SyntaxId>,
rhs_syntax_id: Option<SyntaxId>,
change_map: &mut ChangeMap<'a>,
change_map: &mut ChangeMap,
graph_limit: usize,
id_map: &DftHashMap<NonZeroU32, &'a Syntax<'a>>,
) -> Result<(), ExceededGraphLimit> {
Expand Down Expand Up @@ -226,7 +226,7 @@ pub(crate) fn mark_syntax<'a>(
// than graph_limit nodes.
let size_hint = std::cmp::min(lhs_node_count * rhs_node_count, graph_limit);

let start = Vertex::new(lhs_syntax, rhs_syntax);
let start = Vertex::new(lhs_syntax.map(|n| n.id()), rhs_syntax.map(|n| n.id()));
let vertex_arena = Bump::new();

let route = shortest_path(start, &vertex_arena, size_hint, graph_limit, id_map)?;
Expand All @@ -245,9 +245,9 @@ pub(crate) fn mark_syntax<'a>(
format!(
"{:20} {:20} --- {:3} {:?}",
v.lhs_syntax
.map_or_else(|| "None".into(), Syntax::dbg_content),
.map_or_else(|| "None".into(), |id| Syntax::dbg_content(id_map[&id])),
v.rhs_syntax
.map_or_else(|| "None".into(), Syntax::dbg_content),
.map_or_else(|| "None".into(), |id| Syntax::dbg_content(id_map[&id])),
edge.cost(),
edge,
)
Expand Down Expand Up @@ -293,7 +293,7 @@ mod tests {

let id_map = build_id_map(&[lhs], &[rhs]);

let start = Vertex::new(Some(lhs), Some(rhs));
let start = Vertex::new(Some(lhs.id()), Some(rhs.id()));
let vertex_arena = Bump::new();
let route = shortest_path(start, &vertex_arena, 0, DEFAULT_GRAPH_LIMIT, &id_map).unwrap();

Expand Down Expand Up @@ -337,7 +337,10 @@ mod tests {

let id_map = build_id_map(&lhs, &rhs);

let start = Vertex::new(lhs.get(0).copied(), rhs.get(0).copied());
let start = Vertex::new(
lhs.get(0).copied().map(|n| n.id()),
rhs.get(0).copied().map(|n| n.id()),
);
let vertex_arena = Bump::new();
let route = shortest_path(start, &vertex_arena, 0, DEFAULT_GRAPH_LIMIT, &id_map).unwrap();

Expand Down Expand Up @@ -381,7 +384,10 @@ mod tests {

let id_map = build_id_map(&lhs, &rhs);

let start = Vertex::new(lhs.get(0).copied(), rhs.get(0).copied());
let start = Vertex::new(
lhs.get(0).copied().map(|n| n.id()),
rhs.get(0).copied().map(|n| n.id()),
);
let vertex_arena = Bump::new();
let route = shortest_path(start, &vertex_arena, 0, DEFAULT_GRAPH_LIMIT, &id_map).unwrap();

Expand Down Expand Up @@ -429,7 +435,10 @@ mod tests {

let id_map = build_id_map(&lhs, &rhs);

let start = Vertex::new(lhs.get(0).copied(), rhs.get(0).copied());
let start = Vertex::new(
lhs.get(0).copied().map(|n| n.id()),
rhs.get(0).copied().map(|n| n.id()),
);
let vertex_arena = Bump::new();
let route = shortest_path(start, &vertex_arena, 0, DEFAULT_GRAPH_LIMIT, &id_map).unwrap();

Expand Down Expand Up @@ -472,7 +481,10 @@ mod tests {

let id_map = build_id_map(&lhs, &rhs);

let start = Vertex::new(lhs.get(0).copied(), rhs.get(0).copied());
let start = Vertex::new(
lhs.get(0).copied().map(|n| n.id()),
rhs.get(0).copied().map(|n| n.id()),
);
let vertex_arena = Bump::new();
let route = shortest_path(start, &vertex_arena, 0, DEFAULT_GRAPH_LIMIT, &id_map).unwrap();

Expand Down Expand Up @@ -506,7 +518,10 @@ mod tests {

let id_map = build_id_map(&lhs, &rhs);

let start = Vertex::new(lhs.get(0).copied(), rhs.get(0).copied());
let start = Vertex::new(
lhs.get(0).copied().map(|n| n.id()),
rhs.get(0).copied().map(|n| n.id()),
);
let vertex_arena = Bump::new();
let route = shortest_path(start, &vertex_arena, 0, DEFAULT_GRAPH_LIMIT, &id_map).unwrap();

Expand Down Expand Up @@ -548,7 +563,10 @@ mod tests {

let id_map = build_id_map(&lhs, &rhs);

let start = Vertex::new(lhs.get(0).copied(), rhs.get(0).copied());
let start = Vertex::new(
lhs.get(0).copied().map(|n| n.id()),
rhs.get(0).copied().map(|n| n.id()),
);
let vertex_arena = Bump::new();
let route = shortest_path(start, &vertex_arena, 0, DEFAULT_GRAPH_LIMIT, &id_map).unwrap();

Expand Down Expand Up @@ -583,8 +601,14 @@ mod tests {
)
.unwrap();

assert_eq!(change_map.get(lhs), Some(ChangeKind::Unchanged(rhs)));
assert_eq!(change_map.get(rhs), Some(ChangeKind::Unchanged(lhs)));
assert_eq!(
change_map.get(lhs.id()),
Some(ChangeKind::Unchanged(rhs.id()))
);
assert_eq!(
change_map.get(rhs.id()),
Some(ChangeKind::Unchanged(lhs.id()))
);
}

#[test]
Expand All @@ -605,7 +629,7 @@ mod tests {
&id_map,
)
.unwrap();
assert_eq!(change_map.get(lhs), Some(ChangeKind::Novel));
assert_eq!(change_map.get(rhs), Some(ChangeKind::Novel));
assert_eq!(change_map.get(lhs.id()), Some(ChangeKind::Novel));
assert_eq!(change_map.get(rhs.id()), Some(ChangeKind::Novel));
}
}
Loading

0 comments on commit 7291351

Please sign in to comment.