Reuse duplicate grouping for combat turns and spells
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package combatturns
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
)
|
||||
|
||||
func TestCollapseDuplicatesKeepsEvidenceAndOriginalIndices(t *testing.T) {
|
||||
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 10}, {ID: 20}}}
|
||||
ref := func(id int) source.SourceRef {
|
||||
return source.SourceRef{SourceID: doc.ID, StartUnitID: id, EndUnitID: id}
|
||||
}
|
||||
a, b, invalid := validTurn("Aria", ref(10)), validTurn("Aria", ref(20)), validTurn("Aria", ref(999))
|
||||
records := []normalizedRecord{{turn: a, inputIndex: 10}, {turn: b, inputIndex: 20}, {turn: invalid, inputIndex: 30}, {turn: b, inputIndex: 40}, {turn: invalid, inputIndex: 50}, {turn: a, inputIndex: 60}}
|
||||
output, findings := collapseDuplicates(records, source.NewDocumentIndex(doc))
|
||||
if want := []dnd.CombatTurn{a, b, invalid, invalid}; !reflect.DeepEqual(output, want) {
|
||||
t.Fatalf("output = %#v, want %#v", output, want)
|
||||
}
|
||||
if len(findings) != 2 || !strings.Contains(findings[0].Message, "retained input index 10") || !strings.Contains(findings[0].Message, "removed input index 60") || !strings.Contains(findings[1].Message, "retained input index 20") || !strings.Contains(findings[1].Message, "removed input index 40") {
|
||||
t.Fatalf("findings = %#v", findings)
|
||||
}
|
||||
output[0].SourceRefs[0].StartUnitID = 20
|
||||
if records[0].turn.SourceRefs[0].StartUnitID != 10 {
|
||||
t.Fatal("output aliases input evidence")
|
||||
}
|
||||
}
|
||||
@@ -200,48 +200,25 @@ func sourceRefsEqual(left, right []source.SourceRef) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type duplicateGroup struct {
|
||||
retainedIndex int
|
||||
removed []int
|
||||
}
|
||||
|
||||
func collapseDuplicates(records []normalizedRecord, documentIndex source.DocumentIndex) ([]dnd.CombatTurn, []diagnostics.Finding) {
|
||||
// Combat owns its identity and evidence eligibility; positions map back to inputs.
|
||||
retained, groups := shared.GroupDuplicates(len(records), func(index int) (string, bool) {
|
||||
return duplicateKey(records[index].turn, documentIndex)
|
||||
})
|
||||
output := make([]dnd.CombatTurn, 0, len(retained))
|
||||
for _, index := range retained {
|
||||
output = append(output, cloneCombatTurn(records[index].turn))
|
||||
}
|
||||
if len(records) == 0 {
|
||||
return make([]dnd.CombatTurn, 0), nil
|
||||
return output, nil
|
||||
}
|
||||
|
||||
keep := make([]bool, len(records))
|
||||
groups := make([]duplicateGroup, 0)
|
||||
groupByKey := make(map[string]int)
|
||||
for index, record := range records {
|
||||
key, eligible := duplicateKey(record.turn, documentIndex)
|
||||
if !eligible {
|
||||
keep[index] = true
|
||||
continue
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
output := make([]dnd.CombatTurn, 0, len(records))
|
||||
for index, record := range records {
|
||||
if keep[index] {
|
||||
output = append(output, cloneCombatTurn(record.turn))
|
||||
}
|
||||
}
|
||||
|
||||
findings := make([]diagnostics.Finding, 0)
|
||||
findings := make([]diagnostics.Finding, 0, len(groups))
|
||||
for _, group := range groups {
|
||||
if len(group.removed) == 0 {
|
||||
continue
|
||||
removed := make([]int, len(group.Removed))
|
||||
for index, position := range group.Removed {
|
||||
removed[index] = records[position].inputIndex
|
||||
}
|
||||
findings = append(findings, duplicateFinding(group.retainedIndex, group.removed))
|
||||
findings = append(findings, duplicateFinding(records[group.Representative].inputIndex, removed))
|
||||
}
|
||||
return output, findings
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user