Skip to content

Commit 93f0af8

Browse files
committed
Use unordered_map instead of vector
Signed-off-by: Jono Yang <[email protected]>
1 parent c733a28 commit 93f0af8

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

src/licensedcode/seq.pyx

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from collections import namedtuple as _namedtuple
44

55
cimport cython
6+
from libcpp.algorithm cimport sort as cpp_sort
7+
from libcpp.unordered_map cimport unordered_map
68
from libcpp.vector cimport vector
7-
from libcpp.algorithm cimport fill, sort as cpp_sort
89

910

1011
Match = _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

Comments
 (0)