mirror of
https://github.com/jlengrand/webcomponentsjs.git
synced 2026-03-10 08:51:22 +00:00
spacing.
This commit is contained in:
@@ -12,244 +12,244 @@
|
||||
};
|
||||
}
|
||||
|
||||
var EDIT_LEAVE = 0;
|
||||
var EDIT_UPDATE = 1;
|
||||
var EDIT_ADD = 2;
|
||||
var EDIT_DELETE = 3;
|
||||
var EDIT_LEAVE = 0;
|
||||
var EDIT_UPDATE = 1;
|
||||
var EDIT_ADD = 2;
|
||||
var EDIT_DELETE = 3;
|
||||
|
||||
function ArraySplice() {}
|
||||
function ArraySplice() {}
|
||||
|
||||
ArraySplice.prototype = {
|
||||
ArraySplice.prototype = {
|
||||
|
||||
// Note: This function is *based* on the computation of the Levenshtein
|
||||
// "edit" distance. The one change is that "updates" are treated as two
|
||||
// edits - not one. With Array splices, an update is really a delete
|
||||
// followed by an add. By retaining this, we optimize for "keeping" the
|
||||
// maximum array items in the original array. For example:
|
||||
//
|
||||
// 'xxxx123' -> '123yyyy'
|
||||
//
|
||||
// With 1-edit updates, the shortest path would be just to update all seven
|
||||
// characters. With 2-edit updates, we delete 4, leave 3, and add 4. This
|
||||
// leaves the substring '123' intact.
|
||||
calcEditDistances: function(current, currentStart, currentEnd,
|
||||
old, oldStart, oldEnd) {
|
||||
// "Deletion" columns
|
||||
var rowCount = oldEnd - oldStart + 1;
|
||||
var columnCount = currentEnd - currentStart + 1;
|
||||
var distances = new Array(rowCount);
|
||||
// Note: This function is *based* on the computation of the Levenshtein
|
||||
// "edit" distance. The one change is that "updates" are treated as two
|
||||
// edits - not one. With Array splices, an update is really a delete
|
||||
// followed by an add. By retaining this, we optimize for "keeping" the
|
||||
// maximum array items in the original array. For example:
|
||||
//
|
||||
// 'xxxx123' -> '123yyyy'
|
||||
//
|
||||
// With 1-edit updates, the shortest path would be just to update all seven
|
||||
// characters. With 2-edit updates, we delete 4, leave 3, and add 4. This
|
||||
// leaves the substring '123' intact.
|
||||
calcEditDistances: function(current, currentStart, currentEnd,
|
||||
old, oldStart, oldEnd) {
|
||||
// "Deletion" columns
|
||||
var rowCount = oldEnd - oldStart + 1;
|
||||
var columnCount = currentEnd - currentStart + 1;
|
||||
var distances = new Array(rowCount);
|
||||
|
||||
// "Addition" rows. Initialize null column.
|
||||
for (var i = 0; i < rowCount; i++) {
|
||||
distances[i] = new Array(columnCount);
|
||||
distances[i][0] = i;
|
||||
}
|
||||
// "Addition" rows. Initialize null column.
|
||||
for (var i = 0; i < rowCount; i++) {
|
||||
distances[i] = new Array(columnCount);
|
||||
distances[i][0] = i;
|
||||
}
|
||||
|
||||
// Initialize null row
|
||||
for (var j = 0; j < columnCount; j++)
|
||||
distances[0][j] = j;
|
||||
// Initialize null row
|
||||
for (var j = 0; j < columnCount; j++)
|
||||
distances[0][j] = j;
|
||||
|
||||
for (var i = 1; i < rowCount; i++) {
|
||||
for (var j = 1; j < columnCount; j++) {
|
||||
if (this.equals(current[currentStart + j - 1], old[oldStart + i - 1]))
|
||||
distances[i][j] = distances[i - 1][j - 1];
|
||||
else {
|
||||
var north = distances[i - 1][j] + 1;
|
||||
var west = distances[i][j - 1] + 1;
|
||||
distances[i][j] = north < west ? north : west;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (var i = 1; i < rowCount; i++) {
|
||||
for (var j = 1; j < columnCount; j++) {
|
||||
if (this.equals(current[currentStart + j - 1], old[oldStart + i - 1]))
|
||||
distances[i][j] = distances[i - 1][j - 1];
|
||||
else {
|
||||
var north = distances[i - 1][j] + 1;
|
||||
var west = distances[i][j - 1] + 1;
|
||||
distances[i][j] = north < west ? north : west;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return distances;
|
||||
},
|
||||
return distances;
|
||||
},
|
||||
|
||||
// This starts at the final weight, and walks "backward" by finding
|
||||
// the minimum previous weight recursively until the origin of the weight
|
||||
// matrix.
|
||||
spliceOperationsFromEditDistances: function(distances) {
|
||||
var i = distances.length - 1;
|
||||
var j = distances[0].length - 1;
|
||||
var current = distances[i][j];
|
||||
var edits = [];
|
||||
while (i > 0 || j > 0) {
|
||||
if (i == 0) {
|
||||
edits.push(EDIT_ADD);
|
||||
j--;
|
||||
continue;
|
||||
}
|
||||
if (j == 0) {
|
||||
edits.push(EDIT_DELETE);
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
var northWest = distances[i - 1][j - 1];
|
||||
var west = distances[i - 1][j];
|
||||
var north = distances[i][j - 1];
|
||||
// This starts at the final weight, and walks "backward" by finding
|
||||
// the minimum previous weight recursively until the origin of the weight
|
||||
// matrix.
|
||||
spliceOperationsFromEditDistances: function(distances) {
|
||||
var i = distances.length - 1;
|
||||
var j = distances[0].length - 1;
|
||||
var current = distances[i][j];
|
||||
var edits = [];
|
||||
while (i > 0 || j > 0) {
|
||||
if (i == 0) {
|
||||
edits.push(EDIT_ADD);
|
||||
j--;
|
||||
continue;
|
||||
}
|
||||
if (j == 0) {
|
||||
edits.push(EDIT_DELETE);
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
var northWest = distances[i - 1][j - 1];
|
||||
var west = distances[i - 1][j];
|
||||
var north = distances[i][j - 1];
|
||||
|
||||
var min;
|
||||
if (west < north)
|
||||
min = west < northWest ? west : northWest;
|
||||
else
|
||||
min = north < northWest ? north : northWest;
|
||||
var min;
|
||||
if (west < north)
|
||||
min = west < northWest ? west : northWest;
|
||||
else
|
||||
min = north < northWest ? north : northWest;
|
||||
|
||||
if (min == northWest) {
|
||||
if (northWest == current) {
|
||||
edits.push(EDIT_LEAVE);
|
||||
} else {
|
||||
edits.push(EDIT_UPDATE);
|
||||
current = northWest;
|
||||
}
|
||||
i--;
|
||||
j--;
|
||||
} else if (min == west) {
|
||||
edits.push(EDIT_DELETE);
|
||||
i--;
|
||||
current = west;
|
||||
} else {
|
||||
edits.push(EDIT_ADD);
|
||||
j--;
|
||||
current = north;
|
||||
}
|
||||
}
|
||||
if (min == northWest) {
|
||||
if (northWest == current) {
|
||||
edits.push(EDIT_LEAVE);
|
||||
} else {
|
||||
edits.push(EDIT_UPDATE);
|
||||
current = northWest;
|
||||
}
|
||||
i--;
|
||||
j--;
|
||||
} else if (min == west) {
|
||||
edits.push(EDIT_DELETE);
|
||||
i--;
|
||||
current = west;
|
||||
} else {
|
||||
edits.push(EDIT_ADD);
|
||||
j--;
|
||||
current = north;
|
||||
}
|
||||
}
|
||||
|
||||
edits.reverse();
|
||||
return edits;
|
||||
},
|
||||
edits.reverse();
|
||||
return edits;
|
||||
},
|
||||
|
||||
/**
|
||||
* Splice Projection functions:
|
||||
*
|
||||
* A splice map is a representation of how a previous array of items
|
||||
* was transformed into a new array of items. Conceptually it is a list of
|
||||
* tuples of
|
||||
*
|
||||
* <index, removed, addedCount>
|
||||
*
|
||||
* which are kept in ascending index order of. The tuple represents that at
|
||||
* the |index|, |removed| sequence of items were removed, and counting forward
|
||||
* from |index|, |addedCount| items were added.
|
||||
*/
|
||||
/**
|
||||
* Splice Projection functions:
|
||||
*
|
||||
* A splice map is a representation of how a previous array of items
|
||||
* was transformed into a new array of items. Conceptually it is a list of
|
||||
* tuples of
|
||||
*
|
||||
* <index, removed, addedCount>
|
||||
*
|
||||
* which are kept in ascending index order of. The tuple represents that at
|
||||
* the |index|, |removed| sequence of items were removed, and counting forward
|
||||
* from |index|, |addedCount| items were added.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Lacking individual splice mutation information, the minimal set of
|
||||
* splices can be synthesized given the previous state and final state of an
|
||||
* array. The basic approach is to calculate the edit distance matrix and
|
||||
* choose the shortest path through it.
|
||||
*
|
||||
* Complexity: O(l * p)
|
||||
* l: The length of the current array
|
||||
* p: The length of the old array
|
||||
*/
|
||||
calcSplices: function(current, currentStart, currentEnd,
|
||||
old, oldStart, oldEnd) {
|
||||
var prefixCount = 0;
|
||||
var suffixCount = 0;
|
||||
/**
|
||||
* Lacking individual splice mutation information, the minimal set of
|
||||
* splices can be synthesized given the previous state and final state of an
|
||||
* array. The basic approach is to calculate the edit distance matrix and
|
||||
* choose the shortest path through it.
|
||||
*
|
||||
* Complexity: O(l * p)
|
||||
* l: The length of the current array
|
||||
* p: The length of the old array
|
||||
*/
|
||||
calcSplices: function(current, currentStart, currentEnd,
|
||||
old, oldStart, oldEnd) {
|
||||
var prefixCount = 0;
|
||||
var suffixCount = 0;
|
||||
|
||||
var minLength = Math.min(currentEnd - currentStart, oldEnd - oldStart);
|
||||
if (currentStart == 0 && oldStart == 0)
|
||||
prefixCount = this.sharedPrefix(current, old, minLength);
|
||||
var minLength = Math.min(currentEnd - currentStart, oldEnd - oldStart);
|
||||
if (currentStart == 0 && oldStart == 0)
|
||||
prefixCount = this.sharedPrefix(current, old, minLength);
|
||||
|
||||
if (currentEnd == current.length && oldEnd == old.length)
|
||||
suffixCount = this.sharedSuffix(current, old, minLength - prefixCount);
|
||||
if (currentEnd == current.length && oldEnd == old.length)
|
||||
suffixCount = this.sharedSuffix(current, old, minLength - prefixCount);
|
||||
|
||||
currentStart += prefixCount;
|
||||
oldStart += prefixCount;
|
||||
currentEnd -= suffixCount;
|
||||
oldEnd -= suffixCount;
|
||||
currentStart += prefixCount;
|
||||
oldStart += prefixCount;
|
||||
currentEnd -= suffixCount;
|
||||
oldEnd -= suffixCount;
|
||||
|
||||
if (currentEnd - currentStart == 0 && oldEnd - oldStart == 0)
|
||||
return [];
|
||||
if (currentEnd - currentStart == 0 && oldEnd - oldStart == 0)
|
||||
return [];
|
||||
|
||||
if (currentStart == currentEnd) {
|
||||
var splice = newSplice(currentStart, [], 0);
|
||||
while (oldStart < oldEnd)
|
||||
splice.removed.push(old[oldStart++]);
|
||||
if (currentStart == currentEnd) {
|
||||
var splice = newSplice(currentStart, [], 0);
|
||||
while (oldStart < oldEnd)
|
||||
splice.removed.push(old[oldStart++]);
|
||||
|
||||
return [ splice ];
|
||||
} else if (oldStart == oldEnd)
|
||||
return [ newSplice(currentStart, [], currentEnd - currentStart) ];
|
||||
return [ splice ];
|
||||
} else if (oldStart == oldEnd)
|
||||
return [ newSplice(currentStart, [], currentEnd - currentStart) ];
|
||||
|
||||
var ops = this.spliceOperationsFromEditDistances(
|
||||
this.calcEditDistances(current, currentStart, currentEnd,
|
||||
old, oldStart, oldEnd));
|
||||
var ops = this.spliceOperationsFromEditDistances(
|
||||
this.calcEditDistances(current, currentStart, currentEnd,
|
||||
old, oldStart, oldEnd));
|
||||
|
||||
var splice = undefined;
|
||||
var splices = [];
|
||||
var index = currentStart;
|
||||
var oldIndex = oldStart;
|
||||
for (var i = 0; i < ops.length; i++) {
|
||||
switch(ops[i]) {
|
||||
case EDIT_LEAVE:
|
||||
if (splice) {
|
||||
splices.push(splice);
|
||||
splice = undefined;
|
||||
}
|
||||
var splice = undefined;
|
||||
var splices = [];
|
||||
var index = currentStart;
|
||||
var oldIndex = oldStart;
|
||||
for (var i = 0; i < ops.length; i++) {
|
||||
switch(ops[i]) {
|
||||
case EDIT_LEAVE:
|
||||
if (splice) {
|
||||
splices.push(splice);
|
||||
splice = undefined;
|
||||
}
|
||||
|
||||
index++;
|
||||
oldIndex++;
|
||||
break;
|
||||
case EDIT_UPDATE:
|
||||
if (!splice)
|
||||
splice = newSplice(index, [], 0);
|
||||
index++;
|
||||
oldIndex++;
|
||||
break;
|
||||
case EDIT_UPDATE:
|
||||
if (!splice)
|
||||
splice = newSplice(index, [], 0);
|
||||
|
||||
splice.addedCount++;
|
||||
index++;
|
||||
splice.addedCount++;
|
||||
index++;
|
||||
|
||||
splice.removed.push(old[oldIndex]);
|
||||
oldIndex++;
|
||||
break;
|
||||
case EDIT_ADD:
|
||||
if (!splice)
|
||||
splice = newSplice(index, [], 0);
|
||||
splice.removed.push(old[oldIndex]);
|
||||
oldIndex++;
|
||||
break;
|
||||
case EDIT_ADD:
|
||||
if (!splice)
|
||||
splice = newSplice(index, [], 0);
|
||||
|
||||
splice.addedCount++;
|
||||
index++;
|
||||
break;
|
||||
case EDIT_DELETE:
|
||||
if (!splice)
|
||||
splice = newSplice(index, [], 0);
|
||||
splice.addedCount++;
|
||||
index++;
|
||||
break;
|
||||
case EDIT_DELETE:
|
||||
if (!splice)
|
||||
splice = newSplice(index, [], 0);
|
||||
|
||||
splice.removed.push(old[oldIndex]);
|
||||
oldIndex++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
splice.removed.push(old[oldIndex]);
|
||||
oldIndex++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (splice) {
|
||||
splices.push(splice);
|
||||
}
|
||||
return splices;
|
||||
},
|
||||
if (splice) {
|
||||
splices.push(splice);
|
||||
}
|
||||
return splices;
|
||||
},
|
||||
|
||||
sharedPrefix: function(current, old, searchLength) {
|
||||
for (var i = 0; i < searchLength; i++)
|
||||
if (!this.equals(current[i], old[i]))
|
||||
return i;
|
||||
return searchLength;
|
||||
},
|
||||
sharedPrefix: function(current, old, searchLength) {
|
||||
for (var i = 0; i < searchLength; i++)
|
||||
if (!this.equals(current[i], old[i]))
|
||||
return i;
|
||||
return searchLength;
|
||||
},
|
||||
|
||||
sharedSuffix: function(current, old, searchLength) {
|
||||
var index1 = current.length;
|
||||
var index2 = old.length;
|
||||
var count = 0;
|
||||
while (count < searchLength && this.equals(current[--index1], old[--index2]))
|
||||
count++;
|
||||
sharedSuffix: function(current, old, searchLength) {
|
||||
var index1 = current.length;
|
||||
var index2 = old.length;
|
||||
var count = 0;
|
||||
while (count < searchLength && this.equals(current[--index1], old[--index2]))
|
||||
count++;
|
||||
|
||||
return count;
|
||||
},
|
||||
return count;
|
||||
},
|
||||
|
||||
calculateSplices: function(current, previous) {
|
||||
return this.calcSplices(current, 0, current.length, previous, 0,
|
||||
previous.length);
|
||||
},
|
||||
calculateSplices: function(current, previous) {
|
||||
return this.calcSplices(current, 0, current.length, previous, 0,
|
||||
previous.length);
|
||||
},
|
||||
|
||||
equals: function(currentValue, previousValue) {
|
||||
return currentValue === previousValue;
|
||||
}
|
||||
};
|
||||
equals: function(currentValue, previousValue) {
|
||||
return currentValue === previousValue;
|
||||
}
|
||||
};
|
||||
|
||||
// exports
|
||||
scope.ArraySplice = ArraySplice;
|
||||
// exports
|
||||
scope.ArraySplice = ArraySplice;
|
||||
|
||||
})(window.ShadowDOMPolyfill);
|
||||
Reference in New Issue
Block a user