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