Prepare item and enemy event sort keys once
This commit is contained in:
@@ -101,30 +101,57 @@ func Less(order shared.SourceRefOrder, left, right dnd.ItemOccurrence) bool {
|
||||
// CanonicalLess defines the canonical occurrence order without modifying or
|
||||
// canonicalizing source references. Callers must provide canonical evidence.
|
||||
func CanonicalLess(order shared.SourceRefOrder, left, right dnd.ItemOccurrence) bool {
|
||||
leftPosition, leftHasEvidence := order.EarliestValid(left.SourceRefs)
|
||||
rightPosition, rightHasEvidence := order.EarliestValid(right.SourceRefs)
|
||||
if leftHasEvidence != rightHasEvidence {
|
||||
return leftHasEvidence
|
||||
return CanonicalLessPrepared(order, left, right, PrepareCanonicalOrder(order, left), PrepareCanonicalOrder(order, right))
|
||||
}
|
||||
|
||||
type holderOrderKey struct {
|
||||
present bool
|
||||
comparison, display string
|
||||
}
|
||||
|
||||
// CanonicalOrderKey holds invariant comparisons for an owned occurrence.
|
||||
type CanonicalOrderKey struct {
|
||||
earliest int
|
||||
hasEvidence bool
|
||||
nameComparison, nameDisplay string
|
||||
from, to holderOrderKey
|
||||
}
|
||||
|
||||
// PrepareCanonicalOrder requires source references already in canonical order.
|
||||
func PrepareCanonicalOrder(order shared.SourceRefOrder, value dnd.ItemOccurrence) CanonicalOrderKey {
|
||||
earliest, hasEvidence := order.EarliestValid(value.SourceRefs)
|
||||
holder := func(value string) holderOrderKey {
|
||||
display := DisplayValue(value)
|
||||
return holderOrderKey{present: display != "", comparison: ComparisonKey(display), display: display}
|
||||
}
|
||||
if leftHasEvidence && leftPosition != rightPosition {
|
||||
return leftPosition < rightPosition
|
||||
return CanonicalOrderKey{earliest: earliest, hasEvidence: hasEvidence, nameComparison: ComparisonKey(value.Name), nameDisplay: DisplayValue(value.Name), from: holder(value.From), to: holder(value.To)}
|
||||
}
|
||||
|
||||
// CanonicalLessPrepared compares values with keys prepared from those same
|
||||
// canonical, unchanged records; arbitrary input should use Less instead.
|
||||
func CanonicalLessPrepared(order shared.SourceRefOrder, left, right dnd.ItemOccurrence, leftKey, rightKey CanonicalOrderKey) bool {
|
||||
if leftKey.hasEvidence != rightKey.hasEvidence {
|
||||
return leftKey.hasEvidence
|
||||
}
|
||||
if leftKey.hasEvidence && leftKey.earliest != rightKey.earliest {
|
||||
return leftKey.earliest < rightKey.earliest
|
||||
}
|
||||
if left.ItemID != right.ItemID {
|
||||
return left.ItemID < right.ItemID
|
||||
}
|
||||
if leftKey, rightKey := ComparisonKey(left.Name), ComparisonKey(right.Name); leftKey != rightKey {
|
||||
return leftKey < rightKey
|
||||
if leftKey.nameComparison != rightKey.nameComparison {
|
||||
return leftKey.nameComparison < rightKey.nameComparison
|
||||
}
|
||||
if leftName, rightName := DisplayValue(left.Name), DisplayValue(right.Name); leftName != rightName {
|
||||
return leftName < rightName
|
||||
if leftKey.nameDisplay != rightKey.nameDisplay {
|
||||
return leftKey.nameDisplay < rightKey.nameDisplay
|
||||
}
|
||||
if left.Kind != right.Kind {
|
||||
return left.Kind < right.Kind
|
||||
}
|
||||
if less, decided := optionalStringLess(left.From, right.From); decided {
|
||||
if less, decided := holderKeyLess(leftKey.from, rightKey.from); decided {
|
||||
return less
|
||||
}
|
||||
if less, decided := optionalStringLess(left.To, right.To); decided {
|
||||
if less, decided := holderKeyLess(leftKey.to, rightKey.to); decided {
|
||||
return less
|
||||
}
|
||||
if less, decided := optionalQuantityLess(left.Quantity, right.Quantity); decided {
|
||||
@@ -133,6 +160,22 @@ func CanonicalLess(order shared.SourceRefOrder, left, right dnd.ItemOccurrence)
|
||||
return sourceRefsLess(order, left.SourceRefs, right.SourceRefs)
|
||||
}
|
||||
|
||||
func holderKeyLess(left, right holderOrderKey) (bool, bool) {
|
||||
if left.present != right.present {
|
||||
return !left.present, true
|
||||
}
|
||||
if !left.present {
|
||||
return false, false
|
||||
}
|
||||
if left.comparison != right.comparison {
|
||||
return left.comparison < right.comparison, true
|
||||
}
|
||||
if left.display != right.display {
|
||||
return left.display < right.display, true
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
|
||||
// ExactEqual reports whether occurrences are exact duplicates after their display
|
||||
// fields and evidence have been canonicalized for the supplied source order.
|
||||
func ExactEqual(order shared.SourceRefOrder, left, right dnd.ItemOccurrence) bool {
|
||||
@@ -197,23 +240,6 @@ func writeKeySourceRefs(builder *strings.Builder, refs []source.SourceRef) {
|
||||
}
|
||||
}
|
||||
|
||||
func optionalStringLess(left, right string) (bool, bool) {
|
||||
leftPresent, rightPresent := HolderPresent(left), HolderPresent(right)
|
||||
if leftPresent != rightPresent {
|
||||
return !leftPresent, true
|
||||
}
|
||||
if !leftPresent {
|
||||
return false, false
|
||||
}
|
||||
if leftKey, rightKey := ComparisonKey(left), ComparisonKey(right); leftKey != rightKey {
|
||||
return leftKey < rightKey, true
|
||||
}
|
||||
if leftValue, rightValue := DisplayValue(left), DisplayValue(right); leftValue != rightValue {
|
||||
return leftValue < rightValue, true
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
|
||||
func optionalQuantityLess(left, right *int) (bool, bool) {
|
||||
if (left == nil) != (right == nil) {
|
||||
return left == nil, true
|
||||
|
||||
Reference in New Issue
Block a user