33from collections import namedtuple as _namedtuple
44
55cimport cython
6+ from libcpp.algorithm cimport sort as cpp_sort
7+ from libcpp.unordered_map cimport unordered_map
68from libcpp.vector cimport vector
7- from libcpp.algorithm cimport fill, sort as cpp_sort
89
910
1011Match = _namedtuple(' Match' , ' a b size' )
@@ -67,13 +68,10 @@ cdef CMatch find_longest_match(a, b, Py_ssize_t alo, Py_ssize_t ahi, Py_ssize_t
6768 """
6869 cdef Py_ssize_t besti, bestj, bestsize
6970 cdef Py_ssize_t i, j, k
70- cdef vector[ Py_ssize_t] j2len
71- cdef vector[ Py_ssize_t] newj2len
71+ cdef unordered_map[Py_ssize_t, Py_ssize_t] j2len
72+ cdef unordered_map[Py_ssize_t, Py_ssize_t] newj2len
7273
7374 besti, bestj, bestsize = alo, blo, 0
74- bufsize = max (ahi, bhi) + 1
75- j2len.resize(bufsize)
76- newj2len.resize(bufsize)
7775 # find longest junk-free match
7876 # during an iteration of the loop, j2len[j] = length of longest
7977 # junk-free match ending with a[i-1] and b[j]
@@ -90,20 +88,14 @@ cdef CMatch find_longest_match(a, b, Py_ssize_t alo, Py_ssize_t ahi, Py_ssize_t
9088 continue
9189 if j >= bhi:
9290 break
93- if < Py_ssize_t> j2len.size() <= (j - 1 ):
94- k = j2len[j - 1 ] + 1
95- else :
96- k = 1
97- newj2len[j] = k
91+ k = newj2len[j] = j2len[j - 1 ] + 1
9892 if k > bestsize:
9993 besti = i - k + 1
10094 bestj = j - k + 1
10195 bestsize = k
96+ j2len.swap(newj2len)
97+ newj2len.clear()
10298
103- j2len.swap(newj2len)
104- fill(newj2len.begin() + blo, newj2len.begin() + bhi + 1 , 0 )
105-
106- fill(j2len.begin() + blo, j2len.begin() + bhi + 1 , 0 )
10799 return extend_match(besti, bestj, bestsize, a, b, alo, ahi, blo, bhi, matchables)
108100
109101
0 commit comments