MCPcopy Create free account
hub / github.com/pmezard/go-difflib / QuickRatio

Method QuickRatio

difflib/difflib.go:477–503  ·  view source on GitHub ↗

Return 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

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

Callers 2

TestSequenceMatcherRatioFunction · 0.80

Calls 1

calculateRatioFunction · 0.85

Tested by 2

TestSequenceMatcherRatioFunction · 0.64