File: mergelcs.py

package info (click to toggle)
codeville 0.8.0-2.1
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 1,140 kB
  • sloc: python: 10,335; ansic: 89; sh: 62; makefile: 25
file content (223 lines) | stat: -rwxr-xr-x 4,959 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Written by Uoti Urpala


from array import array


def unique_lcs(a, b, alo, ahi, blo, bhi):
    index = {}
    for i in xrange(blo, bhi):
        line = b[i]
        if line in index:
            index[line] = None
        else:
            index[line] = i
    s = {}
    get = index.get
    for i in xrange(alo, ahi):
        line = a[i]
        if get(line) is not None:
            if line in s:
                del s[line]
                del index[line]
            else:
                s[line] = index[line] + 1
    back = {}
    values = {}
    values[0] = 0
    matches = {}
    sz = bhi - blo + 1
    while True:
        sz2 = sz
        sz |= sz >> 1
        if sz == sz2:
            break
    sz += 1
    tree = array('b', [0] * (2 * sz))
    sz2 = sz
    while sz2 > 0:
        tree[sz2] = 1
        sz2 >>= 1
    get = s.get
    i = alo
    while i < ahi:
        line = a[i]
        i += 1
        j = get(line)
        if j is None:
            continue
        l = 1
        j2 = i
        while i < ahi and j < bhi and a[i] == b[j]:
            if a[i] in s:
                l += 1
            i += 1
            j += 1
        j -= blo
        matches[j] = j2 - 1
        left = j + sz
        while True:
            tree[left] = 1
            j2 = left >> 1
            if tree[j2]:
                break
            left = j2
        j2 = left
        while not left & 1 or not tree[left - 1]:
            left >>= 1
        left -= 1
        while not left & sz:
            left <<= 1
            if tree[left + 1]:
                left += 1
        left -= sz
        back[j] = left
        l += values[left]
        values[j] = l
        while True:
            while j2 & 1 or not tree[j2 + 1]:
                j2 >>= 1
            if j2 == 0:
                break
            j2 += 1
            while not j2 & sz:
                j2 <<= 1
                if not tree[j2]:
                    j2 += 1
            j = values[j2-sz]
            if j > l:
                break
            while True:
                tree[j2] = 0
                if tree[j2 ^ 1]:
                    break
                j2 >>= 1
            if j == l:
                break
    j = 1
    while not j & sz:
        j <<= 1
        if tree[j + 1]:
            j += 1
    r = []
    j -= sz
    while j:
        i = matches[j]
        j2 = s[a[i]] - 1
        r.append((i, j2, j + blo - j2))
        j = back[j]
    r.reverse()
    return r

def recurse(a, b, alo, ahi, blo, bhi, answer, maxrecursion=20):
    if maxrecursion < 0:
        return
    m = unique_lcs(a, b, alo, ahi, blo, bhi)
    i = alo
    j = blo
    for i2, j2, l in m:
        while i2 > i and j2 > j and a[i2-1] == b[j2-1]:
            i2 -= 1
            j2 -= 1
            l += 1
        if l < 2:
            continue
        if i2 > alo and j2 > blo:
            recurse(a, b, i, i2, j, j2, answer, maxrecursion - 1)
        answer.append((i2, j2, l))
        i = i2 + l
        j = j2 + l
    if i > alo and i < ahi and j < bhi:
        recurse(a, b, i, ahi, j, bhi, answer, maxrecursion - 1)









def x(a, b):
    import time
    t1 = time.time()
    r = unique_lcs(a, b, 0, len(a), 0, len(b))
    t2 = time.time()
    print r
    for i, j, l in r:
        assert a[i:i+l] == b[j:j+l]
        print ''.join(a[i:i+l]),

    print
    print 'time:', t2-t1

def x2(a, b):
    import time
    t1 = time.time()
    r = []
    recurse(a, b, 0, len(a), 0, len(b), r)
    t2 = time.time()
    print r
    for i, j, l in r:
        assert a[i:i+l] == b[j:j+l]
        print ''.join(a[i:i+l]),

    print
    print 'time:', t2-t1


def test(x):
    a = [str(i) for i in range(100000)]
    import random
    b = list(a)
    random.shuffle(b)
    print ' '.join(b)
    print
    x(a, b)

#test(x)
#x2('0123456789abc', '    01 89a 34 67')
#x2('AB 1 CD 1 AB P XY Q AB 1 CD 1 AB', 'AB 2 CD 2 AB Q XY P AB 2 CD 2 AB')
#x2('AjBjC', 'AjC')
#x2('01 2', '01')

try:
    import psyco
    psyco.bind(recurse)
except ImportError:
    pass

def find_matches(a, b):
    r = []
    recurse(a, b, 0, len(a), 0, len(b), r)
    return r

if __name__ == '__main__':
    import sys
    a = file(sys.argv[1]).readlines()
    b = file(sys.argv[2]).readlines()
    r = []
    recurse(a, b, 0, len(a), 0, len(b), r)
    i = 0
    j = 0
    for i2, j2, l in r:
        for i in range(i, i2):
            print '-' + a[i],
        for j in range(j, j2):
            print '+' + b[j],
        if l <= 6:
            for i in range(i2, i2 + l):
                print ' ' + a[i],
        else:
            if i2 > 0 or j2 > 0:
                for i in range(i2, i2 + 3):
                    print ' ' + a[i],
            print '@@ -'+str(i2+l-2)+' +'+str(j2+l-2)+' @@'
            for i in range(i2 + l - 3, i2 + l):
                print ' ' + a[i],
        i = i2 + l
        j = j2 + l
    for i in range(i, len(a)):
        print '-'  + a[i],
    for j in range(j, len(b)):
        print '+' + b[j],