Catrope has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/203473

Change subject: [BREAKING CHANGE] Generalize ve.compareOffsetPaths() to 
ve.compareTuples()
......................................................................

[BREAKING CHANGE] Generalize ve.compareOffsetPaths() to ve.compareTuples()

ve.compareOffsetPaths() was essentially a generic tuple comparator
already. This commit makes its behavior for tuples of different
lengths more useful and renames it to ve.compareTuples().

Change-Id: I693129cfbdb061099a9fa14779c07b8b9d2d7363
---
M src/ve.utils.js
1 file changed, 23 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/73/203473/1

diff --git a/src/ve.utils.js b/src/ve.utils.js
index cf63d0e..3831087 100644
--- a/src/ve.utils.js
+++ b/src/ve.utils.js
@@ -1234,20 +1234,33 @@
 };
 
 /**
- * Compare two offset paths for position in document
+ * Compare two tuples in lexicographical order.
  *
- * @param {number[]} path1 First offset path
- * @param {number[]} path2 Second offset path
- * @return {number} negative, zero or positive number
+ * This function first compares a[0] with b[0], then a[1] with b[1], etc.
+ * until it encounters a pair where a[k] != b[k]; then returns a[k]-b[k].
+ * If a[k] == b[k] for every k, this function returns 0.
+ *
+ * If a and b are of unequal length, but a[k] == b[k] for all k that exist in 
both a and b, then
+ * this function returns Infinity (if a is longer) or -Infinity (if b is 
longer).
+ *
+ * @param {number[]} a First tuple
+ * @param {number[]} b Second tuple
+ * @return {number} a[k]-b[k] where k is the lowest k such that a[k] != b[k]
  */
-ve.compareOffsetPaths = function ( path1, path2 ) {
+ve.compareTuples = function ( a, b ) {
        var i, len;
-       for ( i = 0, len = Math.min( path1.length, path2.length ); i < len; i++ 
) {
-               if ( path1[ i ] !== path2[ i ] ) {
-                       return path1[ i ] - path2[ i ];
+       for ( i = 0, len = Math.min( a.length, b.length ); i < len; i++ ) {
+               if ( a[i] !== b[i] ) {
+                       return a[i] - b[i];
                }
        }
-       return path1.length - path2.length;
+       if ( a.length > b.length ) {
+               return Infinity;
+       }
+       if ( a.length < b.length ) {
+               return -Infinity;
+       }
+       return 0;
 };
 
 /**
@@ -1260,12 +1273,11 @@
  * @return {number} negative, zero or positive number
  */
 ve.compareDocumentOrder = function ( node1, offset1, node2, offset2 ) {
-
        var commonAncestor = ve.getCommonAncestor( node1, node2 );
        if ( commonAncestor === null ) {
                throw new Error( 'No common ancestor' );
        }
-       return ve.compareOffsetPaths(
+       return ve.compareTuples(
                ve.getOffsetPath( commonAncestor, node1, offset1 ),
                ve.getOffsetPath( commonAncestor, node2, offset2 )
        );

-- 
To view, visit https://gerrit.wikimedia.org/r/203473
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I693129cfbdb061099a9fa14779c07b8b9d2d7363
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to