-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdelta.js
More file actions
3898 lines (3274 loc) · 121 KB
/
delta.js
File metadata and controls
3898 lines (3274 loc) · 121 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var require = function (file, cwd) {
var resolved = require.resolve(file, cwd || '/');
var mod = require.modules[resolved];
if (!mod) throw new Error(
'Failed to resolve module ' + file + ', tried ' + resolved
);
var res = mod._cached ? mod._cached : mod();
return res;
}
require.paths = [];
require.modules = {};
require.extensions = [".js",".coffee"];
require._core = {
'assert': true,
'events': true,
'fs': true,
'path': true,
'vm': true
};
require.resolve = (function () {
return function (x, cwd) {
if (!cwd) cwd = '/';
if (require._core[x]) return x;
var path = require.modules.path();
var y = cwd || '.';
if (x.match(/^(?:\.\.?\/|\/)/)) {
var m = loadAsFileSync(path.resolve(y, x))
|| loadAsDirectorySync(path.resolve(y, x));
if (m) return m;
}
var n = loadNodeModulesSync(x, y);
if (n) return n;
throw new Error("Cannot find module '" + x + "'");
function loadAsFileSync (x) {
if (require.modules[x]) {
return x;
}
for (var i = 0; i < require.extensions.length; i++) {
var ext = require.extensions[i];
if (require.modules[x + ext]) return x + ext;
}
}
function loadAsDirectorySync (x) {
x = x.replace(/\/+$/, '');
var pkgfile = x + '/package.json';
if (require.modules[pkgfile]) {
var pkg = require.modules[pkgfile]();
var b = pkg.browserify;
if (typeof b === 'object' && b.main) {
var m = loadAsFileSync(path.resolve(x, b.main));
if (m) return m;
}
else if (typeof b === 'string') {
var m = loadAsFileSync(path.resolve(x, b));
if (m) return m;
}
else if (pkg.main) {
var m = loadAsFileSync(path.resolve(x, pkg.main));
if (m) return m;
}
}
return loadAsFileSync(x + '/index');
}
function loadNodeModulesSync (x, start) {
var dirs = nodeModulesPathsSync(start);
for (var i = 0; i < dirs.length; i++) {
var dir = dirs[i];
var m = loadAsFileSync(dir + '/' + x);
if (m) return m;
var n = loadAsDirectorySync(dir + '/' + x);
if (n) return n;
}
var m = loadAsFileSync(x);
if (m) return m;
}
function nodeModulesPathsSync (start) {
var parts;
if (start === '/') parts = [ '' ];
else parts = path.normalize(start).split('/');
var dirs = [];
for (var i = parts.length - 1; i >= 0; i--) {
if (parts[i] === 'node_modules') continue;
var dir = parts.slice(0, i + 1).join('/') + '/node_modules';
dirs.push(dir);
}
return dirs;
}
};
})();
require.alias = function (from, to) {
var path = require.modules.path();
var res = null;
try {
res = require.resolve(from + '/package.json', '/');
}
catch (err) {
res = require.resolve(from, '/');
}
var basedir = path.dirname(res);
var keys = (Object.keys || function (obj) {
var res = [];
for (var key in obj) res.push(key)
return res;
})(require.modules);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (key.slice(0, basedir.length + 1) === basedir + '/') {
var f = key.slice(basedir.length);
require.modules[to + f] = require.modules[basedir + f];
}
else if (key === basedir) {
require.modules[to] = require.modules[basedir];
}
}
};
require.define = function (filename, fn) {
var dirname = require._core[filename]
? ''
: require.modules.path().dirname(filename)
;
var require_ = function (file) {
return require(file, dirname)
};
require_.resolve = function (name) {
return require.resolve(name, dirname);
};
require_.modules = require.modules;
require_.define = require.define;
var module_ = { exports : {} };
require.modules[filename] = function () {
require.modules[filename]._cached = module_.exports;
fn.call(
module_.exports,
require_,
module_,
module_.exports,
dirname,
filename
);
require.modules[filename]._cached = module_.exports;
return module_.exports;
};
};
if (typeof process === 'undefined') process = {};
if (!process.nextTick) process.nextTick = (function () {
var queue = [];
var canPost = typeof window !== 'undefined'
&& window.postMessage && window.addEventListener
;
if (canPost) {
window.addEventListener('message', function (ev) {
if (ev.source === window && ev.data === 'browserify-tick') {
ev.stopPropagation();
if (queue.length > 0) {
var fn = queue.shift();
fn();
}
}
}, true);
}
return function (fn) {
if (canPost) {
queue.push(fn);
window.postMessage('browserify-tick', '*');
}
else setTimeout(fn, 0);
};
})();
if (!process.title) process.title = 'browser';
if (!process.binding) process.binding = function (name) {
if (name === 'evals') return require('vm')
else throw new Error('No such module')
};
if (!process.cwd) process.cwd = function () { return '.' };
require.define("path", function (require, module, exports, __dirname, __filename) {
function filter (xs, fn) {
var res = [];
for (var i = 0; i < xs.length; i++) {
if (fn(xs[i], i, xs)) res.push(xs[i]);
}
return res;
}
// resolves . and .. elements in a path array with directory names there
// must be no slashes, empty elements, or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function normalizeArray(parts, allowAboveRoot) {
// if the path tries to go above the root, `up` ends up > 0
var up = 0;
for (var i = parts.length; i >= 0; i--) {
var last = parts[i];
if (last == '.') {
parts.splice(i, 1);
} else if (last === '..') {
parts.splice(i, 1);
up++;
} else if (up) {
parts.splice(i, 1);
up--;
}
}
// if the path is allowed to go above the root, restore leading ..s
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
}
// Regex to split a filename into [*, dir, basename, ext]
// posix version
var splitPathRe = /^(.+\/(?!$)|\/)?((?:.+?)?(\.[^.]*)?)$/;
// path.resolve([from ...], to)
// posix version
exports.resolve = function() {
var resolvedPath = '',
resolvedAbsolute = false;
for (var i = arguments.length; i >= -1 && !resolvedAbsolute; i--) {
var path = (i >= 0)
? arguments[i]
: process.cwd();
// Skip empty and invalid entries
if (typeof path !== 'string' || !path) {
continue;
}
resolvedPath = path + '/' + resolvedPath;
resolvedAbsolute = path.charAt(0) === '/';
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path
resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) {
return !!p;
}), !resolvedAbsolute).join('/');
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
};
// path.normalize(path)
// posix version
exports.normalize = function(path) {
var isAbsolute = path.charAt(0) === '/',
trailingSlash = path.slice(-1) === '/';
// Normalize the path
path = normalizeArray(filter(path.split('/'), function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';
}
if (path && trailingSlash) {
path += '/';
}
return (isAbsolute ? '/' : '') + path;
};
// posix version
exports.join = function() {
var paths = Array.prototype.slice.call(arguments, 0);
return exports.normalize(filter(paths, function(p, index) {
return p && typeof p === 'string';
}).join('/'));
};
exports.dirname = function(path) {
var dir = splitPathRe.exec(path)[1] || '';
var isWindows = false;
if (!dir) {
// No dirname
return '.';
} else if (dir.length === 1 ||
(isWindows && dir.length <= 3 && dir.charAt(1) === ':')) {
// It is just a slash or a drive letter with a slash
return dir;
} else {
// It is a full dirname, strip trailing slash
return dir.substring(0, dir.length - 1);
}
};
exports.basename = function(path, ext) {
var f = splitPathRe.exec(path)[2] || '';
// TODO: make this comparison case-insensitive on windows?
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
return f;
};
exports.extname = function(path) {
return splitPathRe.exec(path)[3] || '';
};
});
require.define("/lib/main.js", function (require, module, exports, __dirname, __filename) {
module.exports.fnv132 = require('./delta/fnv132');
module.exports.lcs = require('./delta/lcs');
module.exports.tree = require('./delta/tree');
module.exports.xcc = require('./delta/xcc');
module.exports.skelmatch = require('./delta/skelmatch');
module.exports.contextmatcher= require('./delta/contextmatcher');
module.exports.resolver = require('./delta/resolver');
module.exports.domtree = require('./delta/domtree');
module.exports.jsobjecttree = require('./delta/jsobjecttree');
module.exports.domdelta = require('./delta/domdelta');
module.exports.jsondelta= require('./delta/jsondelta');
module.exports.xmlpayload = require('./delta/xmlpayload');
module.exports.jsonpayload = require('./delta/jsonpayload');
module.exports.delta = require('./delta/delta');
});
require.define("/lib/delta/fnv132.js", function (require, module, exports, __dirname, __filename) {
/**
* @file: Implementation of FNV-1 32bit hash algorithm
* @see: http://isthe.com/chongo/tech/comp/fnv/
*
* @module fnv132
*/
/**
* Constant FNV-1 32bit prime number
*
* @constant
*/
var FNV132_PRIME = 16777619;
/**
* High 16 bits of FNV-1 32bit prime number
*
* @constant
*/
var FNV132_PRIME_H = (FNV132_PRIME >>> 16) & 0xFFFF;
/**
* Low 16 bits of FNV-1 32bit prime number
*
* @constant
*/
var FNV132_PRIME_L = FNV132_PRIME & 0xFFFF;
/**
* Constant FNV-1 32bit offset basis
*
* @constant
*/
var FNV132_INIT = 2166136261;
/**
* Create and initialize a new 32bit FNV-1 hash object.
*
* @constructor
*/
function FNV132Hash() {
this.hash = FNV132_INIT;
}
/**
* Update the hash with the given string and return the new hash value. No
* calculation is performed when the bytes-parameter is left out.
*/
FNV132Hash.prototype.update = function (bytes) {
var i, ah, al;
if (typeof bytes === 'undefined' || bytes === null) {
return this.get();
}
if (typeof bytes === 'number') {
// FXME: Actually we should test for non-integer numbers here.
bytes = String.fromCharCode(
(bytes & 0xFF000000) >>> 24,
(bytes & 0x00FF0000) >>> 16,
(bytes & 0x0000FF00) >>> 8,
(bytes & 0x000000FF)
);
}
if (typeof bytes !== 'string') {
throw new Error(typeof bytes + ' not supported by FNV-1 Hash algorithm');
}
for (i=0; i<(bytes && bytes.length); i++) {
// A rather complicated way to multiply this.hash times
// FNV132_PRIME. Regrettably a workaround is necessary because the
// value of a Number class is represented as a 64bit floating point
// internally. This can lead to precision issues if the factors are
// big enough.
//
// Each factor is separated into two 16bit numbers by shifting left
// the high part and masking the low one.
ah = (this.hash >>> 16) & 0xFFFF;
al = this.hash & 0xFFFF;
// Now the both low parts are multiplied. Also each low-high pair
// gets multiplied. There is no reason to multiply the high-high
// pair because overflow is guaranteed here. The result is the sum
// of the three multiplications. Because of the floating point
// nature of JavaScript numbers, bitwise operations are *not*
// faster than multiplications. Therefore we do not use "<< 16"
// here but instead "* 0x100000".
this.hash = (al * FNV132_PRIME_L) +
((ah * FNV132_PRIME_L) * 0x10000) +
((al * FNV132_PRIME_H) * 0x10000);
this.hash ^= bytes.charCodeAt(i);
}
// Get rid of signum
return this.hash >>> 0;
};
/**
* Return current hash value;
*/
FNV132Hash.prototype.get = function () {
return this.hash >>> 0;
};
// CommonJS exports
exports.Hash = FNV132Hash;
});
require.define("/lib/delta/lcs.js", function (require, module, exports, __dirname, __filename) {
/**
* @file: Implementation of Myers linear space longest common subsequence
* algorithm.
* @see:
* * http://dx.doi.org/10.1007/BF01840446
* * http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927
*
* @module lcs
*/
/**
* Create a new instance of the LCS implementation.
*
* @param a The first sequence
* @param b The second sequence
*
* @constructor
*/
function LCS(a, b) {
this.a = a;
this.b = b;
}
/**
* Returns true if the sequence members a and b are equal. Override this
* method if your sequences contain special things.
*/
LCS.prototype.equals = function(a, b) {
return (a === b);
};
/**
* Compute longest common subsequence using myers divide & conquer linear
* space algorithm.
*
* Call a callback for each snake which is part of the longest common
* subsequence.
*
* This algorithm works with strings and arrays. In order to modify the
* equality-test, just override the equals(a, b) method on the LCS
* object.
*
* @param callback A function(x, y) called for A[x] and B[y] for symbols
* taking part in the LCS.
* @param T Context object bound to "this" when the callback is
* invoked.
* @param limit A Limit instance constraining the window of operation to
* the given limit. If undefined the algorithm will iterate
* over the whole sequences a and b.
*/
LCS.prototype.compute = function(callback, T, limit) {
var midleft = new exports.KPoint(),
midright = new exports.KPoint(),
d;
if (typeof limit === 'undefined') {
limit = this.defaultLimit();
}
// Return if there is nothing left
if (limit.N <= 0 && limit.M <= 0) {
return 0;
}
// Callback for each right-edge when M is zero and return number of
// edit script operations.
if (limit.N > 0 && limit.M === 0) {
midleft.set(0, 0).translate(limit.left);
midright.set(1, 1).translate(limit.left);
for (d = 0; d < limit.N; d++) {
callback.call(T, midleft, midright);
midleft.moveright();
midright.moveright();
}
return d;
}
// Callback for each down-edge when N is zero and return number of edit
// script operations.
if (limit.N === 0 && limit.M > 0) {
midleft.set(0, 0).translate(limit.left);
midright.set(0, -1).translate(limit.left);
for (d = 0; d < limit.M; d++) {
callback.call(T, midleft, midright);
midleft.movedown();
midright.movedown();
}
return d;
}
// Find the middle snake and store the result in midleft and midright
d = this.middleSnake(midleft, midright, limit);
if (d === 0) {
// No single insert / delete operation was identified by the middle
// snake algorithm, this means that all the symbols between left and
// right are equal -> one straight diagonal on k=0
if (!limit.left.equal(limit.right)) {
callback.call(T, limit.left, limit.right);
}
}
else if (d === 1) {
// Middle-snake algorithm identified exactly one operation. Report
// the involved snake(s) to the caller.
if (!limit.left.equal(midleft)) {
callback.call(T, limit.left, midleft);
}
if (!midleft.equal(midright)) {
callback.call(T, midleft, midright);
}
if (!midright.equal(limit.right)) {
callback.call(T, midright, limit.right);
}
}
else {
// Recurse if the middle-snake algorithm encountered more than one
// operation.
if (!limit.left.equal(midleft)) {
this.compute(callback, T, new exports.Limit(limit.left, midleft));
}
if (!midleft.equal(midright)) {
callback.call(T, midleft, midright);
}
if (!midright.equal(limit.right)) {
this.compute(callback, T, new exports.Limit(midright, limit.right));
}
}
return d;
};
/**
* Call a callback for each symbol which is part of the longest common
* subsequence between A and B.
*
* Given that the two sequences A and B were supplied to the LCS
* constructor, invoke the callback for each pair A[x], B[y] which is part
* of the longest common subsequence of A and B.
*
* This algorithm works with strings and arrays. In order to modify the
* equality-test, just override the equals(a, b) method on the LCS
* object.
*
* Usage:
* <code>
* var lcs = [];
* var A = 'abcabba';
* var B = 'cbabac';
* var l = new LCS(A, B);
* l.forEachCommonSymbol(function(x, y) {
* lcs.push(A[x]);
* });
* console.log(lcs);
* // -> [ 'c', 'a', 'b', 'a' ]
* </code>
*
* @param callback A function(x, y) called for A[x] and B[y] for symbols
* taking part in the LCS.
* @param T Context object bound to "this" when the callback is
* invoked.
*/
LCS.prototype.forEachCommonSymbol = function(callback, T) {
return this.compute(function(left, right) {
this.forEachPositionInSnake(left, right, callback, T);
}, this);
};
/**
* Internal use. Compute new values for the next head on the given k-line
* in forward direction by examining the results of previous calculations
* in V in the neighborhood of the k-line k.
*
* @param head (Output) Reference to a KPoint which will be populated
* with the new values
* @param k (In) Current k-line
* @param kmin (In) Lowest k-line in current d-round
* @param kmax (In) Highest k-line in current d-round
* @param limit (In) Current lcs search limits (left, right, N, M, delta, dmax)
* @param V (In-/Out) Vector containing the results of previous
* calculations. This vector gets updated automatically by
* nextSnakeHeadForward method.
*/
LCS.prototype.nextSnakeHeadForward = function(head, k, kmin, kmax, limit, V) {
var k0, x, bx, by, n;
// Determine the preceeding snake head. Pick the one whose furthest
// reaching x value is greatest.
if (k === kmin || (k !== kmax && V[k-1] < V[k+1])) {
// Furthest reaching snake is above (k+1), move down.
k0 = k+1;
x = V[k0];
}
else {
// Furthest reaching snake is left (k-1), move right.
k0 = k-1;
x = V[k0] + 1;
}
// Follow the diagonal as long as there are common values in a and b.
bx = limit.left.x;
by = bx - (limit.left.k + k);
n = Math.min(limit.N, limit.M + k);
while (x < n && this.equals(this.a[bx + x], this.b[by + x])) {
x++;
}
// Store x value of snake head after traversing the diagonal in forward
// direction.
head.set(x, k).translate(limit.left);
// Memozie furthest reaching x for k
V[k] = x;
// Return k-value of preceeding snake head
return k0;
};
/**
* Internal use. Compute new values for the next head on the given k-line
* in reverse direction by examining the results of previous calculations
* in V in the neighborhood of the k-line k.
*
* @param head (Output) Reference to a KPoint which will be populated
* with the new values
* @param k (In) Current k-line
* @param kmin (In) Lowest k-line in current d-round
* @param kmax (In) Highest k-line in current d-round
* @param limit (In) Current lcs search limits (left, right, N, M, delta, dmax)
* @param V (In-/Out) Vector containing the results of previous
* calculations. This vector gets updated automatically by
* nextSnakeHeadForward method.
*/
LCS.prototype.nextSnakeHeadBackward = function(head, k, kmin, kmax, limit, V) {
var k0, x, bx, by, n;
// Determine the preceeding snake head. Pick the one whose furthest
// reaching x value is greatest.
if (k === kmax || (k !== kmin && V[k-1] < V[k+1])) {
// Furthest reaching snake is underneath (k-1), move up.
k0 = k-1;
x = V[k0];
}
else {
// Furthest reaching snake is left (k-1), move right.
k0 = k+1;
x = V[k0]-1;
}
// Store x value of snake head before traversing the diagonal in
// reverse direction.
head.set(x, k).translate(limit.left);
// Follow the diagonal as long as there are common values in a and b.
bx = limit.left.x - 1;
by = bx - (limit.left.k + k);
n = Math.max(k, 0);
while (x > n && this.equals(this.a[bx + x], this.b[by + x])) {
x--;
}
// Memozie furthest reaching x for k
V[k] = x;
// Return k-value of preceeding snake head
return k0;
};
/**
* Internal use. Find the middle snake and set lefthead to the left end and
* righthead to the right end.
*
* @param lefthead (Output) A reference to a KPoint which will be
* populated with the values corresponding to the left end
* of the middle snake.
* @param righthead (Output) A reference to a KPoint which will be
* populated with the values corresponding to the right
* end of the middle snake.
* @param limit (In) Current lcs search limits (left, right, N, M, delta, dmax)
*
* @returns d, number of edit script operations encountered within
* the given limit
*/
LCS.prototype.middleSnake = function (lefthead, righthead, limit) {
var d, k, head, k0;
var delta = limit.delta;
var dmax = Math.ceil(limit.dmax / 2);
var checkBwSnake = (delta % 2 === 0);
var Vf = {};
var Vb = {};
Vf[1] = 0;
Vb[delta-1] = limit.N;
for (d = 0; d <= dmax; d++) {
for (k = -d; k <= d; k+=2) {
k0 = this.nextSnakeHeadForward(righthead, k, -d, d, limit, Vf);
// check for overlap
if (!checkBwSnake && k >= -d-1+delta && k <= d-1+delta) {
if (Vf[k] >= Vb[k]) {
// righthead already contains the right stuff, now set
// the lefthead to the values of the last k-line.
lefthead.set(Vf[k0], k0).translate(limit.left);
// return the number of edit script operations
return 2 * d - 1;
}
}
}
for (k = -d+delta; k <= d+delta; k+=2) {
k0 = this.nextSnakeHeadBackward(lefthead, k, -d+delta, d+delta, limit, Vb);
// check for overlap
if (checkBwSnake && k >= -d && k <= d) {
if (Vf[k] >= Vb[k]) {
// lefthead already contains the right stuff, now set
// the righthead to the values of the last k-line.
righthead.set(Vb[k0], k0).translate(limit.left);
// return the number of edit script operations
return 2 * d;
}
}
}
}
};
/**
* Return the default limit spanning the whole input
*/
LCS.prototype.defaultLimit = function() {
return new exports.Limit(
new exports.KPoint(0,0),
new exports.KPoint(this.a.length, this.a.length - this.b.length));
};
/**
* Invokes a function for each position in the snake between the left and
* the right snake head.
*
* @param left Left KPoint
* @param right Right KPoint
* @param callback Callback of the form function(x, y)
* @param T Context object bound to "this" when the callback is
* invoked.
*/
LCS.prototype.forEachPositionInSnake = function(left, right, callback, T) {
var k = right.k;
var x = (k > left.k) ? left.x + 1 : left.x;
var n = right.x;
while (x < n) {
callback.call(T, x, x-k);
x++;
}
};
/**
* Create a new KPoint instance.
*
* A KPoint represents a point identified by an x-coordinate and the
* number of the k-line it is located at.
*
* @constructor
*/
var KPoint = function(x, k) {
/**
* The x-coordinate of the k-point.
*/
this.x = x;
/**
* The k-line on which the k-point is located at.
*/
this.k = k;
};
/**
* Return a new copy of this k-point.
*/
KPoint.prototype.copy = function() {
return new KPoint(this.x, this.k);
};
/**
* Set the values of a k-point.
*/
KPoint.prototype.set = function(x, k) {
this.x = x;
this.k = k;
return this;
};
/**
* Translate this k-point by adding the values of the given k-point.
*/
KPoint.prototype.translate = function(other) {
this.x += other.x;
this.k += other.k;
return this;
};
/**
* Move the point left by d units
*/
KPoint.prototype.moveleft = function(d) {
this.x -= d || 1;
this.k -= d || 1;
return this;
};
/**
* Move the point right by d units
*/
KPoint.prototype.moveright = function(d) {
this.x += d || 1;
this.k += d || 1;
return this;
};
/**
* Move the point up by d units
*/
KPoint.prototype.moveup = function(d) {
this.k -= d || 1;
return this;
};
/**
* Move the point down by d units
*/
KPoint.prototype.movedown = function(d) {
this.k += d || 1;
return this;
};
/**
* Returns true if the given k-point has equal values
*/
KPoint.prototype.equal = function(other) {
return (this.x === other.x && this.k === other.k);
};
/**
* Create a new LCS Limit instance. This is a pure data object which holds
* precalculated parameters for the lcs algorithm.
*
* @constructor
*/
var Limit = function(left, right) {
this.left = left;
this.right = right;
this.delta = right.k - left.k;
this.N = right.x - left.x;
this.M = this.N - this.delta;
this.dmax = this.N + this.M;
};
// CommonJS exports
exports.LCS = LCS;
exports.KPoint = KPoint;
exports.Limit = Limit;
});
require.define("/lib/delta/tree.js", function (require, module, exports, __dirname, __filename) {
/**
* @file: A collection of classes supporting tree structures and operations
* @module tree
*/
/**
* Create a new tree node and set its value and optionally user data.
*
* @param {String} [value] The node value.
* @param {object} [data] User data for this tree node. You may store a
* reference to the corresponding object in the underlying document
* structure. E.g. a reference to a DOM element.
*
* @constructor
*/
function Node(value, data) {
this.value = value;
this.data = data;
this.depth = 0;
// this.par = undefined;
// this.childidx = undefined;
this.children = [];
}
/**
* Append the given node as a child node.
*
* @param {object} child The new child node.
*/
// FIXME: par en parent (fork D3Node)
Node.prototype.append = function(child) {
if (child.par) {
throw new Error('Cannot append a child which already has a parent');
}