bugfix...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-07-17 03:08:22 +03:00
parent 9686f3f827
commit 222ee9c7ea

70
diff.js
View File

@ -85,9 +85,19 @@ var zip = function(func, ...arrays){
// get common chunks (LCS)... // get common chunks (LCS)...
// XXX handle sparse arrays correctly... //
// ...now empty slots get filled with undefined... // Format:
// XXX this does not distinguish empty and null/undefined... // [
// <total-intersection-length>,
//
// {
// A: <offset-A>,
// B: <offset-B>,
// length: <section-length>,
// },
// ...
// ]
//
var getCommonSections = function(A, B, cmp, min_chunk){ var getCommonSections = function(A, B, cmp, min_chunk){
cmp = cmp || function(a, b){ cmp = cmp || function(a, b){
return a === b || a == b } return a === b || a == b }
@ -109,11 +119,11 @@ var getCommonSections = function(A, B, cmp, min_chunk){
length: 0, length: 0,
} }
var l = chunk.length var l = chunk.length
while(a+l < A.length while((a+l < A.length && b+l < B.length)
&& b+l < B.length // cmp non-empty slots only...
&& a+l in A && ((a+l in A && b+l in B) ?
&& b+l in B cmp(A[a+l], B[b+l])
&& cmp(A[a+l], B[b+l])){ : (!(a+l in A) && !(b+l in B)))){
l = chunk.length += 1 l = chunk.length += 1
} }
// ignore small chunks... // ignore small chunks...
@ -161,16 +171,14 @@ var getCommonSections = function(A, B, cmp, min_chunk){
// Format: // Format:
// [ // [
// [ // [
// [<gap-offset-A>, // [<offset-A>,
// [ item, ... ]], // [ <item>, ... ]],
// [<gap-offset-B>, // [<offset-B>,
// [ item, ... ]], // [ <item>, ... ]],
// ], // ],
// ... // ...
// ] // ]
// //
// XXX handle sparse arrays correctly...
// ...now empty slots get filled with undefined...
var getDiffSections = function(A, B, cmp, min_chunk){ var getDiffSections = function(A, B, cmp, min_chunk){
// find the common sections... // find the common sections...
var common_sections = getCommonSections(A, B, cmp, min_chunk) var common_sections = getCommonSections(A, B, cmp, min_chunk)
@ -209,26 +217,45 @@ var getDiffSections = function(A, B, cmp, min_chunk){
var partHandlers = { var partHandlers = {
// XXX might be good to consider item ordering // XXX might be good to consider item ordering
// ...i.e. how an item's indes changed // ...i.e. how an item's indes changed
// XXX handle sparse arrays correctly...
// ...now empty slots get filled with undefined...
items: function(diff, A, B, options){ items: function(diff, A, B, options){
return getDiffSections(A, B, options.cmp) return getDiffSections(A, B, options.cmp)
.map(function(gap){ .map(function(gap){
var i = gap[0][0] var i = gap[0][0]
var j = gap[1][0] var j = gap[1][0]
var a = gap[0][1]
var b = gap[1][1]
console.log('!!!!', a, b)
return zip( return zip(
function(n, elems){ function(n, elems){
return [ return [
0 in elems ? i+n : null, (0 in elems || n < a.length) ?
1 in elems ? j+n : null, i+n
: null,
(1 in elems || n < b.length) ?
j+n
: null,
diff( diff(
0 in elems ? elems[0] : NONE, 0 in elems ?
1 in elems ? elems[1] : NONE, elems[0]
: n < a.length ?
EMPTY
: NONE,
1 in elems ?
elems[1]
: n < b.length ?
EMPTY
: NONE,
options), options),
] ]
}, },
gap[0][1], a,
gap[1][1]) b)
.filter(function(e){ return e[2] != null}) .filter(function(e){
return e[2] != null})
}) })
.reduce(function(res, e){ .reduce(function(res, e){
return res.concat(e) }, []) return res.concat(e) }, [])
@ -388,6 +415,7 @@ Types.handle = function(type, obj, ...args){
return obj return obj
} }
//
// NOTE: this will include direct links to items. // NOTE: this will include direct links to items.
// XXX do we need to differentiate things like: new Number(123) vs. 123??? // XXX do we need to differentiate things like: new Number(123) vs. 123???
// XXX check seen -- avoid recursion... // XXX check seen -- avoid recursion...