Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make list diff reuse _obj_diff results instead of making duplicate calls #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions jsondiff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,12 @@ def __init__(self, syntax='compact', load=False, dump=False, marshal=False,
for symbol in _all_symbols_
}

def _list_diff_0(self, C, X, Y):
def _list_diff_0(self, C, X, Y, similarity):
i, j = len(X), len(Y)
r = []
while True:
if i > 0 and j > 0:
d, s = self._obj_diff(X[i-1], Y[j-1])
d, s = similarity[i-1][j-1]
if s > 0 and C[i][j] == C[i-1][j-1] + s:
r.append((0, d, j-1, s))
i, j = i - 1, j - 1
Expand All @@ -494,9 +494,12 @@ def _list_diff(self, X, Y):
n = len(Y)
# An (m+1) times (n+1) matrix
C = [[0 for j in range(n+1)] for i in range(m+1)]
similarity = [[0 for j in range(n)] for i in range(m)]
for i in range(1, m+1):
for j in range(1, n+1):
_, s = self._obj_diff(X[i-1], Y[j-1])
child_diff = self._obj_diff(X[i-1], Y[j-1])
similarity[i-1][j-1] = child_diff
s = child_diff[1]
# Following lines are part of the original LCS algorithm
# left in the code in case modification turns out to be problematic
#if X[i-1] == Y[j-1]:
Expand All @@ -508,7 +511,7 @@ def _list_diff(self, X, Y):
changed = {}
tot_s = 0.0

for sign, value, pos, s in self._list_diff_0(C, X, Y):
for sign, value, pos, s in self._list_diff_0(C, X, Y, similarity):
if sign == 1:
inserted.append((pos, value))
elif sign == -1:
Expand Down