- commit
- cc9dd5fe9c74801e7ce141b971fbe382afde1b5d
- parent
- 6f9ae00036ea13601a66aadc067a932bc9f28387
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-05-03 14:47
further simplify fuzzy
Diffstat
M | static/context.js | 45 | +++++---------------------------------------- |
1 files changed, 5 insertions, 40 deletions
diff --git a/static/context.js b/static/context.js
@@ -15,43 +15,16 @@ var applyExact = function(text, [pos, before, after], selection) { 15 15 }; 16 16 17 17 var fuzzyMerge = function(s1, s2) {18 -1 // https://en.wikipedia.org/wiki/Longest_common_subsequence_problem19 -120 18 var ret = '';21 -1 var m = [];22 -1 var i;23 -1 var j;24 -125 -1 for (i = 0; i < s1.length; i++) {26 -1 m.push([]);27 -1 for (j = 0; j < s2.length; j++) {28 -1 m[i].push(-1);29 -1 }30 -1 }-1 19 var i = s1.length - 1; -1 20 var j = s2.length - 1; 31 2132 -1 var getM = (ii, jj) => {33 -1 if (ii < 0 || jj < 0) {34 -1 return 0;35 -1 }36 -1 if (m[ii][jj] === -1) {37 -1 if (s1[ii] === s2[jj]) {38 -1 m[ii][jj] = getM(ii - 1, jj - 1) + 1;39 -1 } else {40 -1 m[ii][jj] = Math.max(getM(ii, jj - 1), getM(ii - 1, jj));41 -1 }42 -1 }43 -1 return m[ii][jj];44 -1 };45 -146 -1 i = s1.length - 1;47 -1 j = s2.length - 1;48 -149 -1 while (i >= 0 && j >= 0) {50 -1 if (s1[i] === s2[j]) {-1 22 while (i >= 0 || j >= 0) { -1 23 if (i >= 0 && j >= 0 && s1[i] === s2[j]) { 51 24 ret = s1[i] + ret; 52 25 i -= 1; 53 26 j -= 1;54 -1 } else if (getM(i, j - 1) > getM(i - 1, j)) {-1 27 } else if (i < 0) { 55 28 ret = s2[j] + ret; 56 29 j -= 1; 57 30 } else { @@ -59,14 +32,6 @@ var fuzzyMerge = function(s1, s2) { 59 32 i -= 1; 60 33 } 61 34 }62 -1 while (j >= 0) {63 -1 ret = s2[j] + ret;64 -1 j -= 1;65 -1 }66 -1 while (i >= 0) {67 -1 ret = s1[i] + ret;68 -1 i -= 1;69 -1 }70 35 71 36 return ret; 72 37 };