Fix D&D extraction issues and retire the completed audit
This commit is contained in:
@@ -2,7 +2,6 @@ package shared
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
)
|
||||
@@ -10,8 +9,8 @@ import (
|
||||
// SourceRefOrder provides a stable snapshot of a source document's unit
|
||||
// ordering for source-reference comparison and canonicalization.
|
||||
type SourceRefOrder struct {
|
||||
sourceID string
|
||||
positions map[int]int
|
||||
sourceID string
|
||||
index source.DocumentIndex
|
||||
}
|
||||
|
||||
// NewSourceRefOrder captures the source identity and unit positions from doc.
|
||||
@@ -19,13 +18,7 @@ func NewSourceRefOrder(doc *source.SourceDocument) SourceRefOrder {
|
||||
if doc == nil {
|
||||
return SourceRefOrder{}
|
||||
}
|
||||
positions := make(map[int]int, len(doc.Units))
|
||||
for position, unit := range doc.Units {
|
||||
if _, exists := positions[unit.ID]; !exists {
|
||||
positions[unit.ID] = position
|
||||
}
|
||||
}
|
||||
return SourceRefOrder{sourceID: doc.ID, positions: positions}
|
||||
return SourceRefOrder{sourceID: doc.ID, index: source.NewDocumentIndex(doc)}
|
||||
}
|
||||
|
||||
// Less orders references by source identity, then document positions when
|
||||
@@ -34,14 +27,13 @@ func (o SourceRefOrder) Less(left, right source.SourceRef) bool {
|
||||
if left.SourceID != right.SourceID {
|
||||
return left.SourceID < right.SourceID
|
||||
}
|
||||
positions := o.positionsFor(left.SourceID)
|
||||
if lessEndpoint(positions, left.StartUnitID, right.StartUnitID) {
|
||||
if o.lessEndpoint(left.SourceID, left.StartUnitID, right.StartUnitID) {
|
||||
return true
|
||||
}
|
||||
if lessEndpoint(positions, right.StartUnitID, left.StartUnitID) {
|
||||
if o.lessEndpoint(left.SourceID, right.StartUnitID, left.StartUnitID) {
|
||||
return false
|
||||
}
|
||||
return lessEndpoint(positions, left.EndUnitID, right.EndUnitID)
|
||||
return o.lessEndpoint(left.SourceID, left.EndUnitID, right.EndUnitID)
|
||||
}
|
||||
|
||||
// EarliestValid returns the earliest document position among valid refs.
|
||||
@@ -52,14 +44,10 @@ func (o SourceRefOrder) EarliestValid(refs []source.SourceRef) (int, bool) {
|
||||
found := false
|
||||
earliest := 0
|
||||
for _, ref := range refs {
|
||||
if ref.SourceID != o.sourceID || strings.TrimSpace(ref.SourceID) != ref.SourceID || ref.StartUnitID <= 0 || ref.EndUnitID <= 0 {
|
||||
continue
|
||||
}
|
||||
start, startOK := o.positions[ref.StartUnitID]
|
||||
end, endOK := o.positions[ref.EndUnitID]
|
||||
if !startOK || !endOK || start > end {
|
||||
if o.index.ValidateRef(ref) != nil {
|
||||
continue
|
||||
}
|
||||
start, _ := o.index.Position(ref.StartUnitID)
|
||||
if !found || start < earliest {
|
||||
earliest = start
|
||||
found = true
|
||||
@@ -87,16 +75,9 @@ func (o SourceRefOrder) Canonicalize(refs []source.SourceRef) []source.SourceRef
|
||||
return unique
|
||||
}
|
||||
|
||||
func (o SourceRefOrder) positionsFor(sourceID string) map[int]int {
|
||||
if o.sourceID == "" || sourceID != o.sourceID {
|
||||
return nil
|
||||
}
|
||||
return o.positions
|
||||
}
|
||||
|
||||
func lessEndpoint(positions map[int]int, left, right int) bool {
|
||||
leftPosition, leftOK := positions[left]
|
||||
rightPosition, rightOK := positions[right]
|
||||
func (o SourceRefOrder) lessEndpoint(sourceID string, left, right int) bool {
|
||||
leftPosition, leftOK := o.position(sourceID, left)
|
||||
rightPosition, rightOK := o.position(sourceID, right)
|
||||
if leftOK != rightOK {
|
||||
return leftOK
|
||||
}
|
||||
@@ -105,3 +86,10 @@ func lessEndpoint(positions map[int]int, left, right int) bool {
|
||||
}
|
||||
return left < right
|
||||
}
|
||||
|
||||
func (o SourceRefOrder) position(sourceID string, unitID int) (int, bool) {
|
||||
if o.sourceID == "" || sourceID != o.sourceID {
|
||||
return 0, false
|
||||
}
|
||||
return o.index.Position(unitID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user