MCPcopy Create free account
hub / github.com/cogentcore/core / QuickRatio

Method QuickRatio

texteditor/difflib/difflib.go:486–512  ·  view source on GitHub ↗

QuickRatio returns an upper bound on ratio() relatively quickly. This isn't defined beyond that it is an upper bound on .Ratio(), and is faster to compute.

()

Source from the content-addressed store, hash-verified

484// This isn't defined beyond that it is an upper bound on .Ratio(), and
485// is faster to compute.
486func (m *SequenceMatcher) QuickRatio() float64 {
487 // viewing a and b as multisets, set matches to the cardinality
488 // of their intersection; this counts the number of matches
489 // without regard to order, so is clearly an upper bound
490 if m.fullBCount == nil {
491 m.fullBCount = map[string]int{}
492 for _, s := range m.b {
493 m.fullBCount[s] = m.fullBCount[s] + 1
494 }
495 }
496
497 // avail[x] is the number of times x appears in 'b' less the
498 // number of times we've seen it in 'a' so far ... kinda
499 avail := map[string]int{}
500 matches := 0
501 for _, s := range m.a {
502 n, ok := avail[s]
503 if !ok {
504 n = m.fullBCount[s]
505 }
506 avail[s] = n - 1
507 if n > 0 {
508 matches += 1
509 }
510 }
511 return calculateRatio(matches, len(m.a)+len(m.b))
512}
513
514// RealQuickRatio returns an upper bound on ratio() very quickly.
515//

Callers 3

TestSequenceMatcherRatioFunction · 0.45
FancyReplaceMethod · 0.45

Calls 1

calculateRatioFunction · 0.70

Tested by 2

TestSequenceMatcherRatioFunction · 0.36