GoPLS Viewer

Home|gopls/internal/diff/lcs/sequence.go
1// Copyright 2022 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package lcs
6
7// This file defines the abstract sequence over which the LCS algorithm operates.
8
9// sequences abstracts a pair of sequences, A and B.
10type sequences interface {
11    lengths() (intint)                    // len(A), len(B)
12    commonPrefixLen(aiajbibj intint // len(commonPrefix(A[ai:aj], B[bi:bj]))
13    commonSuffixLen(aiajbibj intint // len(commonSuffix(A[ai:aj], B[bi:bj]))
14}
15
16type stringSeqs struct{ ab string }
17
18func (s stringSeqslengths() (intint) { return len(s.a), len(s.b) }
19func (s stringSeqscommonPrefixLen(aiajbibj intint {
20    return commonPrefixLenString(s.a[ai:aj], s.b[bi:bj])
21}
22func (s stringSeqscommonSuffixLen(aiajbibj intint {
23    return commonSuffixLenString(s.a[ai:aj], s.b[bi:bj])
24}
25
26// The explicit capacity in s[i:j:j] leads to more efficient code.
27
28type bytesSeqs struct{ ab []byte }
29
30func (s bytesSeqslengths() (intint) { return len(s.a), len(s.b) }
31func (s bytesSeqscommonPrefixLen(aiajbibj intint {
32    return commonPrefixLenBytes(s.a[ai:aj:aj], s.b[bi:bj:bj])
33}
34func (s bytesSeqscommonSuffixLen(aiajbibj intint {
35    return commonSuffixLenBytes(s.a[ai:aj:aj], s.b[bi:bj:bj])
36}
37
38type runesSeqs struct{ ab []rune }
39
40func (s runesSeqslengths() (intint) { return len(s.a), len(s.b) }
41func (s runesSeqscommonPrefixLen(aiajbibj intint {
42    return commonPrefixLenRunes(s.a[ai:aj:aj], s.b[bi:bj:bj])
43}
44func (s runesSeqscommonSuffixLen(aiajbibj intint {
45    return commonSuffixLenRunes(s.a[ai:aj:aj], s.b[bi:bj:bj])
46}
47
48// TODO(adonovan): optimize these functions using ideas from:
49// - https://go.dev/cl/408116 common.go
50// - https://go.dev/cl/421435 xor_generic.go
51
52// TODO(adonovan): factor using generics when available,
53// but measure performance impact.
54
55// commonPrefixLen* returns the length of the common prefix of a[ai:aj] and b[bi:bj].
56func commonPrefixLenBytes(ab []byteint {
57    n := min(len(a), len(b))
58    i := 0
59    for i < n && a[i] == b[i] {
60        i++
61    }
62    return i
63}
64func commonPrefixLenRunes(ab []runeint {
65    n := min(len(a), len(b))
66    i := 0
67    for i < n && a[i] == b[i] {
68        i++
69    }
70    return i
71}
72func commonPrefixLenString(ab stringint {
73    n := min(len(a), len(b))
74    i := 0
75    for i < n && a[i] == b[i] {
76        i++
77    }
78    return i
79}
80
81// commonSuffixLen* returns the length of the common suffix of a[ai:aj] and b[bi:bj].
82func commonSuffixLenBytes(ab []byteint {
83    n := min(len(a), len(b))
84    i := 0
85    for i < n && a[len(a)-1-i] == b[len(b)-1-i] {
86        i++
87    }
88    return i
89}
90func commonSuffixLenRunes(ab []runeint {
91    n := min(len(a), len(b))
92    i := 0
93    for i < n && a[len(a)-1-i] == b[len(b)-1-i] {
94        i++
95    }
96    return i
97}
98func commonSuffixLenString(ab stringint {
99    n := min(len(a), len(b))
100    i := 0
101    for i < n && a[len(a)-1-i] == b[len(b)-1-i] {
102        i++
103    }
104    return i
105}
106
107func min(xy intint {
108    if x < y {
109        return x
110    } else {
111        return y
112    }
113}
114
MembersX
stringSeqs.a
runesSeqs.commonSuffixLen.ai
commonPrefixLenRunes
bytesSeqs.commonPrefixLen.aj
commonSuffixLenBytes
stringSeqs.lengths
stringSeqs.commonPrefixLen.ai
bytesSeqs.a
bytesSeqs.commonSuffixLen
runesSeqs.lengths
stringSeqs.commonSuffixLen.ai
bytesSeqs.commonPrefixLen.bj
runesSeqs.commonSuffixLen.aj
commonSuffixLenString.a
runesSeqs
runesSeqs.commonPrefixLen.ai
commonPrefixLenBytes.i
commonSuffixLenRunes.a
commonPrefixLenString.a
commonPrefixLenString.n
stringSeqs.lengths.s
stringSeqs.commonPrefixLen.bj
bytesSeqs.lengths
runesSeqs.commonSuffixLen.bj
commonPrefixLenRunes.n
commonPrefixLenRunes.i
commonSuffixLenBytes.b
commonSuffixLenRunes.b
commonSuffixLenString.n
stringSeqs.commonSuffixLen.s
bytesSeqs.commonPrefixLen.bi
commonPrefixLenString.i
commonSuffixLenBytes.n
commonSuffixLenRunes.i
commonSuffixLenString.i
stringSeqs.b
bytesSeqs.commonSuffixLen.bi
runesSeqs.commonPrefixLen
commonPrefixLenRunes.b
commonSuffixLenRunes.n
commonSuffixLenString
commonPrefixLenRunes.a
stringSeqs
bytesSeqs
bytesSeqs.lengths.s
runesSeqs.commonPrefixLen.aj
commonPrefixLenBytes
commonPrefixLenBytes.n
stringSeqs.commonPrefixLen.s
bytesSeqs.commonSuffixLen.ai
runesSeqs.a
runesSeqs.commonSuffixLen.s
commonSuffixLenRunes
min.x
commonPrefixLenString.b
min
stringSeqs.commonSuffixLen.aj
stringSeqs.commonSuffixLen.bj
bytesSeqs.commonSuffixLen.aj
runesSeqs.b
runesSeqs.commonPrefixLen.s
runesSeqs.commonPrefixLen.bj
bytesSeqs.commonPrefixLen
commonPrefixLenBytes.a
commonSuffixLenBytes.a
commonSuffixLenBytes.i
stringSeqs.commonPrefixLen.bi
stringSeqs.commonSuffixLen.bi
bytesSeqs.b
bytesSeqs.commonPrefixLen.s
bytesSeqs.commonSuffixLen.bj
runesSeqs.lengths.s
bytesSeqs.commonPrefixLen.ai
commonPrefixLenBytes.b
commonPrefixLenString
sequences
stringSeqs.commonPrefixLen
runesSeqs.commonSuffixLen
runesSeqs.commonSuffixLen.bi
stringSeqs.commonPrefixLen.aj
stringSeqs.commonSuffixLen
bytesSeqs.commonSuffixLen.s
runesSeqs.commonPrefixLen.bi
commonSuffixLenString.b
min.y
Members
X