Share occurrence duplicate grouping bookkeeping

This commit is contained in:
2026-11-26 02:53:03 +00:00
parent 6275c62d25
commit f9dd53f4c2
9 changed files with 180 additions and 66 deletions

View File

@@ -160,44 +160,29 @@ func cloneOccurrence(input dnd.NPCOccurrence) dnd.NPCOccurrence {
return output
}
type duplicateGroup struct {
retainedIndex int
removed []int
}
func collapseDuplicates(records []normalizedRecord, documentIndex source.DocumentIndex) ([]dnd.NPCOccurrence, []diagnostics.Finding) {
if len(records) == 0 {
return make([]dnd.NPCOccurrence, 0), nil
}
keep := make([]bool, len(records))
groups := make([]duplicateGroup, 0)
groupByKey := make(map[string]int)
for index, record := range records {
if !occurrencemodel.ValidSourceRefs(documentIndex, record.occurrence.SourceRefs) {
keep[index] = true
continue
// This family owns eligibility and maps shared positions to original indices.
retained, groups := shared.GroupDuplicates(len(records), func(index int) (string, bool) {
occurrence := records[index].occurrence
if !occurrencemodel.ValidSourceRefs(documentIndex, occurrence.SourceRefs) {
return "", false
}
key := occurrencemodel.ExactIdentity(record.occurrence)
groupIndex, exists := groupByKey[key]
if !exists {
groupByKey[key] = len(groups)
groups = append(groups, duplicateGroup{retainedIndex: record.inputIndex})
keep[index] = true
continue
}
groups[groupIndex].removed = append(groups[groupIndex].removed, record.inputIndex)
return occurrencemodel.ExactIdentity(occurrence), true
})
output := make([]dnd.NPCOccurrence, 0, len(retained))
for _, index := range retained {
output = append(output, cloneOccurrence(records[index].occurrence))
}
output := make([]dnd.NPCOccurrence, 0, len(records))
for index, record := range records {
if keep[index] {
output = append(output, cloneOccurrence(record.occurrence))
}
}
findings := make([]diagnostics.Finding, 0)
findings := make([]diagnostics.Finding, 0, len(groups))
for _, group := range groups {
if len(group.removed) != 0 {
findings = append(findings, duplicateFinding(group.retainedIndex, group.removed))
removed := make([]int, len(group.Removed))
for index, position := range group.Removed {
removed[index] = records[position].inputIndex
}
findings = append(findings, duplicateFinding(records[group.Representative].inputIndex, removed))
}
if len(records) == 0 {
return output, nil
}
return output, findings
}