Pair unique layer-only shape changes

This commit is contained in:
Jan Petykiewicz 2026-11-18 23:16:09 -08:00
commit ab308f7b7f
5 changed files with 253 additions and 38 deletions

View file

@ -68,6 +68,8 @@ displayed rows gets one legend entry. Object details use `-` for removals, `+` f
and `~` for likely replacements. Identical unmatched objects share a row and `~` for likely replacements. Identical unmatched objects share a row
with a count such as `+ 12× 1/0: path …`. Unmatched paths with five or more with a count such as `+ 12× 1/0: path …`. Unmatched paths with five or more
vertices show their width, vertex count, and vertex bounds in one line. vertices show their width, vertex count, and vertex bounds in one line.
A unique polygon, box, or path replacement that differs only in layer or
datatype appears on one row, for example `~ 1/0 (WG) → 2/0 (CORE) · rectangle`.
Use `--format json` for details of individual objects and `--hierarchy` to see Use `--format json` for details of individual objects and `--hierarchy` to see
which parent cells are affected. which parent cells are affected.
@ -128,8 +130,9 @@ comparison, totals, or exit status. A layer display filter hides references
without a numeric layer. `--max-changes N` limits displayed object occurrences without a numeric layer. `--max-changes N` limits displayed object occurrences
after filtering. Text defaults to 100 occurrences and shows a replacement only after filtering. Text defaults to 100 occurrences and shows a replacement only
when both objects fit within the limit. Grouped rows count each object toward when both objects fit within the limit. Grouped rows count each object toward
the limit. A single omission line lists changes hidden by filters or the the limit. When a layer filter selects one side of a layer-only replacement,
limit. JSON has no default object limit and reports that side appears as an addition or removal. A single omission line lists
changes hidden by filters or the limit. JSON has no default object limit and reports
omissions from both display filters and any explicit limit in `report_selection`. omissions from both display filters and any explicit limit in `report_selection`.
See the [JSON reference](diff-reports.md) for the selection counters. See the [JSON reference](diff-reports.md) for the selection counters.

View file

@ -90,9 +90,12 @@ explicit defaults remain significant. For gzip inputs, source offsets locate
records in the decompressed layout. records in the decompressed layout.
Replacements link report-local removed/added IDs with `match_rule`, Replacements link report-local removed/added IDs with `match_rule`,
`field_changes`, and `properties_changed`. Rules are `unique_kind_layer_bounds`, `field_changes`, and `properties_changed`. The `unique_layer_change` rule
`unique_kind_layer`, or `unique_kind_target`. IDs are not persistent identities; matches a unique polygon, box, or path on each side with identical retained
ambiguous candidates stay separate. Explanation text is for display only. fields and properties outside the layer and datatype fields. The other rules
are `unique_kind_layer_bounds`, `unique_kind_layer`, and `unique_kind_target`.
Ambiguous candidates stay separate. Replacement IDs refer only to objects in
the current report. Explanation text is for display only.
## Diagnostics and hierarchy ## Diagnostics and hierarchy

View file

@ -1543,7 +1543,7 @@ impl Comparison {
let body_start = out.len(); let body_start = out.len();
let mut removed_cells = String::new(); let mut removed_cells = String::new();
let mut added_cells = String::new(); let mut added_cells = String::new();
let mut layers = changes::LayerRows::new(&self.layer_legend); let mut layers = changes::LayerRows::new(&self.layer_legend, &self.layer_names);
let mut annotations = Vec::new(); let mut annotations = Vec::new();
let mut covered_edges = BTreeSet::new(); let mut covered_edges = BTreeSet::new();
let mut detail_total = 0; let mut detail_total = 0;
@ -1587,12 +1587,19 @@ impl Comparison {
let mut paired_old = BTreeSet::new(); let mut paired_old = BTreeSet::new();
let mut paired_new = BTreeSet::new(); let mut paired_new = BTreeSet::new();
for replacement in &cell.replacements { for replacement in &cell.replacements {
paired_old.insert(replacement.removed);
paired_new.insert(replacement.added);
let a = &cell.removed[replacement.removed]; let a = &cell.removed[replacement.removed];
let b = &cell.added[replacement.added]; let b = &cell.added[replacement.added];
if max_objects - shown >= 2 && visible(a) && visible(b) { if !visible(a) || !visible(b) {
if is_polygon(a.kind) { continue;
}
paired_old.insert(replacement.removed);
paired_new.insert(replacement.added);
if max_objects - shown >= 2 {
if a.layer != b.layer {
out.push_str(" ~ ");
changes::write_layer_transition(&mut out, a, b, Some(&mut layers));
out.push('\n');
} else if is_polygon(a.kind) {
polygons.replacement(a, b); polygons.replacement(a, b);
} else { } else {
out.push_str(" ~ "); out.push_str(" ~ ");

View file

@ -25,38 +25,82 @@ fn group(object: &ObjectChange, bounds: bool) -> Group {
) )
} }
fn unique_groups<'a, K: Ord>(
objects: &'a [ObjectChange],
key: impl Fn(usize, &'a ObjectChange) -> Option<K>,
) -> BTreeMap<K, Option<usize>> {
let mut groups = BTreeMap::new();
for (index, object) in objects.iter().enumerate() {
if let Some(key) = key(index, object) {
groups
.entry(key)
.and_modify(|value| *value = None)
.or_insert(Some(index));
}
}
groups
}
type ShapeKey<'a> = (u16, Vec<(u16, &'a [u8])>, &'a [(u16, Vec<u8>)]);
fn shape_without_layer(object: &ObjectChange) -> Option<ShapeKey<'_>> {
if object.layer.is_none() || !(is_polygon(object.kind) || object.kind == RTAG_PATH) {
return None;
}
let fields = object
.fields
.iter()
.filter(|field| !matches_layer_field(object.kind, field.tag))
.map(|field| (field.tag, field.data.as_slice()))
.collect();
Some((object.kind, fields, &object.properties))
}
fn layer_replacements(old: &[ObjectChange], new: &[ObjectChange]) -> Vec<Replacement> {
// Most small edits touch a single numeric pair.
let Some(layer) = old.iter().chain(new).find_map(|object| object.layer) else {
return Vec::new();
};
if !old
.iter()
.chain(new)
.any(|object| object.layer.is_some_and(|pair| pair != layer))
{
return Vec::new();
}
let a = unique_groups(old, |_, object| shape_without_layer(object));
let b = unique_groups(new, |_, object| shape_without_layer(object));
a.into_iter()
.filter_map(|(key, removed)| {
let removed = removed?;
let added = b.get(&key).copied().flatten()?;
(old[removed].layer != new[added].layer).then_some(Replacement {
removed,
added,
match_rule: "unique_layer_change",
})
})
.collect()
}
/// Pair only unique candidates in the remaining multisets. Ambiguous groups /// Pair only unique candidates in the remaining multisets. Ambiguous groups
/// remain additions/removals; no identity or geometric equivalence is inferred. /// remain additions/removals; no identity or geometric equivalence is inferred.
pub(super) fn replacements(old: &[ObjectChange], new: &[ObjectChange]) -> Vec<Replacement> { pub(super) fn replacements(old: &[ObjectChange], new: &[ObjectChange]) -> Vec<Replacement> {
if old.is_empty() || new.is_empty() { if old.is_empty() || new.is_empty() {
return Vec::new(); return Vec::new();
} }
let (mut used_old, mut used_new) = (BTreeSet::new(), BTreeSet::new()); let mut result = layer_replacements(old, new);
let mut result = Vec::new(); let (mut used_old, mut used_new): (BTreeSet<_>, BTreeSet<_>) =
result.iter().map(|pair| (pair.removed, pair.added)).unzip();
for bounds in [true, false] { for bounds in [true, false] {
let (mut a, mut b) = ( let a = unique_groups(old, |i, object| {
BTreeMap::<Group, Vec<usize>>::new(), (!used_old.contains(&i)).then(|| group(object, bounds))
BTreeMap::<Group, Vec<usize>>::new(), });
); let b = unique_groups(new, |i, object| {
for (i, object) in old (!used_new.contains(&i)).then(|| group(object, bounds))
.iter() });
.enumerate() for (key, index) in a {
.filter(|(i, _)| !used_old.contains(i)) if let (Some(i), Some(&Some(j))) = (index, b.get(&key)) {
{
a.entry(group(object, bounds)).or_default().push(i);
}
for (i, object) in new
.iter()
.enumerate()
.filter(|(i, _)| !used_new.contains(i))
{
b.entry(group(object, bounds)).or_default().push(i);
}
for (key, indices) in a {
if let (1, Some(others)) = (indices.len(), b.get(&key))
&& others.len() == 1
{
let (i, j) = (indices[0], others[0]);
result.push(Replacement { result.push(Replacement {
removed: i, removed: i,
added: j, added: j,
@ -407,17 +451,37 @@ impl ShownReferences {
pub(super) struct LayerRows<'a> { pub(super) struct LayerRows<'a> {
names: &'a BTreeMap<Layer, String>, names: &'a BTreeMap<Layer, String>,
name_changes: &'a [LayerNameChange],
rows: BTreeMap<Layer, Option<usize>>, rows: BTreeMap<Layer, Option<usize>>,
} }
impl<'a> LayerRows<'a> { impl<'a> LayerRows<'a> {
pub fn new(names: &'a BTreeMap<Layer, String>) -> Self { pub fn new(names: &'a BTreeMap<Layer, String>, name_changes: &'a [LayerNameChange]) -> Self {
Self { Self {
names, names,
name_changes,
rows: BTreeMap::new(), rows: BTreeMap::new(),
} }
} }
fn write_transition_layer(&mut self, out: &mut String, layer: Layer, side: Side) {
self.record(Some(layer), out.len());
write!(out, "{}/{}", layer.0, layer.1).unwrap();
if let Ok(index) = self
.name_changes
.binary_search_by_key(&layer, |change| change.layer)
{
let change = &self.name_changes[index];
let name = match side {
Side::Old => &change.old,
Side::New => &change.new,
};
if let Some(name) = name {
write!(out, " ({})", cell_label(name)).unwrap();
}
}
}
pub fn record(&mut self, layer: Option<Layer>, row_start: usize) { pub fn record(&mut self, layer: Option<Layer>, row_start: usize) {
if let Some(layer) = layer if let Some(layer) = layer
&& self.names.contains_key(&layer) && self.names.contains_key(&layer)
@ -433,8 +497,11 @@ impl<'a> LayerRows<'a> {
for (layer, first) in self.rows { for (layer, first) in self.rows {
let name = &self.names[&layer]; let name = &self.names[&layer];
if let Some(at) = first { if let Some(at) = first {
// Each recorded heading starts with the numeric layer/type and a colon. let at = at
let at = at + text[at..].find(':').unwrap(); + text[at..]
.bytes()
.take_while(|b| b.is_ascii_digit() || *b == b'/')
.count();
annotations.push((at, format!(" ({})", cell_label(name)))); annotations.push((at, format!(" ({})", cell_label(name))));
} else { } else {
writeln!(legend, "{}/{}: name {name:?}", layer.0, layer.1).unwrap(); writeln!(legend, "{}/{}: name {name:?}", layer.0, layer.1).unwrap();
@ -443,6 +510,24 @@ impl<'a> LayerRows<'a> {
} }
} }
pub(super) fn write_layer_transition(
out: &mut String,
old: &ObjectChange,
new: &ObjectChange,
layers: Option<&mut LayerRows<'_>>,
) {
let (from, to) = (old.layer.unwrap(), new.layer.unwrap());
if let Some(layers) = layers {
layers.write_transition_layer(out, from, Side::Old);
out.push_str(" → ");
layers.write_transition_layer(out, to, Side::New);
} else {
write!(out, "{}/{} → {}/{}", from.0, from.1, to.0, to.1).unwrap();
}
let rectangular = is_polygon(old.kind) && rectangle_size(object_xy(old)).is_some();
write!(out, " · {}", object_name(old.kind, rectangular)).unwrap();
}
fn polygon_replacement(old: &ObjectChange, new: &ObjectChange) -> String { fn polygon_replacement(old: &ObjectChange, new: &ObjectChange) -> String {
let before = object_xy(old); let before = object_xy(old);
let after = object_xy(new); let after = object_xy(new);
@ -471,6 +556,11 @@ fn polygon_replacement(old: &ObjectChange, new: &ObjectChange) -> String {
} }
pub(super) fn replacement_summary(old: &ObjectChange, new: &ObjectChange) -> String { pub(super) fn replacement_summary(old: &ObjectChange, new: &ObjectChange) -> String {
if old.layer != new.layer && old.layer.is_some() && new.layer.is_some() {
let mut out = String::new();
write_layer_transition(&mut out, old, new, None);
return out;
}
let polygon = is_polygon(old.kind); let polygon = is_polygon(old.kind);
let mut out = if polygon { let mut out = if polygon {
polygon_replacement(old, new) polygon_replacement(old, new)

View file

@ -1813,6 +1813,118 @@ fn replacement_explanations_are_unique_and_keep_full_evidence() {
assert!(!text.contains("xy=")); assert!(!text.contains("xy="));
} }
#[test]
fn layer_only_shape_replacements_keep_evidence_and_respect_selection() {
let rect = [[10, 0], [0, 0], [0, 10], [10, 10], [10, 0]];
let on_core = ring(RTAG_BOUNDARY, RTAG_DATATYPE, 2, &rect);
let named = |mut objects: Vec<Vec<u8>>, names: &[&str]| {
objects.push(label((900, 7), LAYER_SENTINEL));
objects.extend(names.iter().map(|name| label((900, 7), name)));
layout(&[("CHIP", objects)])
};
let old = named(vec![square()], &["1:0:WG", "2:0:CORE"]);
let new = named(vec![on_core.clone()], &["1:0:WG", "2:0:CORE"]);
let old = Document::parse(&old).unwrap();
let diff = Comparison::between(&old, &Document::parse(&new).unwrap());
assert_eq!(
diff.text(2),
"Different\nCells:\n* CHIP\n ~ 1/0 (WG) → 2/0 (CORE) · rectangle\n"
);
let limited = diff.text(1);
assert!(limited.contains("2 object changes over the limit"));
assert!(!limited.contains("Cells:"));
for (pair, suffix) in [
((1, 0), " 1/0 (WG): 1 rectangle removed\n"),
((2, 0), " 2/0 (CORE): 1 rectangle added\n"),
] {
let filtered = diff.text_filtered(
1,
&DisplayFilter {
cell: None,
layer: Some(pair),
},
);
assert!(filtered.contains("1 object change filtered out"));
assert!(filtered.ends_with(suffix), "{filtered}");
}
let json = diff.json_report(&Default::default(), &Default::default());
let replacement = &json["cell_changes"][0]["replacements"][0];
assert_eq!(replacement["match_rule"], "unique_layer_change");
assert_eq!(replacement["field_changes"].as_array().unwrap().len(), 1);
assert_eq!(replacement["field_changes"][0]["tag"], RTAG_LAYER);
assert_eq!(json["summary"]["removed_objects"], "1");
assert_eq!(json["summary"]["added_objects"], "1");
let old = named(vec![square()], &["1:0:WG"]);
let new = named(vec![on_core], &["2:0:CORE"]);
let diff = Comparison::between(
&Document::parse(&old).unwrap(),
&Document::parse(&new).unwrap(),
);
assert!(
diff.text(2)
.ends_with(" ~ 1/0 (WG) → 2/0 (CORE) · rectangle\n")
);
}
#[test]
fn layer_only_matching_requires_unique_shapes_and_identical_payloads() {
let core_square = ring(
RTAG_BOUNDARY,
RTAG_DATATYPE,
2,
&[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]],
);
for (old, new) in [
(vec![square(), square()], vec![core_square.clone()]),
(
vec![square()],
vec![core_square.clone(), core_square.clone()],
),
(vec![square()], vec![with_property(core_square, "new")]),
(
vec![square()],
vec![ring(
RTAG_BOUNDARY,
RTAG_DATATYPE,
2,
&[[0, 0], [10, 0], [0, 10], [0, 0]],
)],
),
] {
let old = layout(&[("CHIP", old)]);
let new = layout(&[("CHIP", new)]);
let diff = Comparison::between(
&Document::parse(&old).unwrap(),
&Document::parse(&new).unwrap(),
);
assert!(diff.cells[0].replacements.is_empty());
assert!(diff.text(100).contains(" removed\n"));
assert!(diff.text(100).contains(" added\n"));
}
}
#[test]
fn datatype_only_path_changes_pair_on_the_same_layer() {
let old = layout(&[("CHIP", vec![path(Some(8), &[[0, 0], [10, 10]])])]);
let old_doc = Document::parse(&old).unwrap();
let at = old_doc
.records
.iter()
.find(|record| record.tag == RTAG_DATATYPE)
.unwrap()
.offset
+ 4;
let mut new = old.clone();
new[at..at + 2].copy_from_slice(&3_u16.to_be_bytes());
let diff = Comparison::between(&old_doc, &Document::parse(&new).unwrap());
assert!(diff.text(2).ends_with(" ~ 1/0 → 1/3 · path\n"));
assert_eq!(
diff.cells[0].replacements[0].match_rule,
"unique_layer_change"
);
}
#[test] #[test]
fn polygon_diff_reports_single_displacements_and_groups_bulk_moves() { fn polygon_diff_reports_single_displacements_and_groups_bulk_moves() {
let before = layout(&[("CHIP", vec![with_property(square(), "old")])]); let before = layout(&[("CHIP", vec![with_property(square(), "old")])]);