• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2exports.__esModule = true;
3exports.distance = exports.closest = void 0;
4var peq = new Uint32Array(0x10000);
5var myers_32 = function (a, b) {
6    var n = a.length;
7    var m = b.length;
8    var lst = 1 << (n - 1);
9    var pv = -1;
10    var mv = 0;
11    var sc = n;
12    var i = n;
13    while (i--) {
14        peq[a.charCodeAt(i)] |= 1 << i;
15    }
16    for (i = 0; i < m; i++) {
17        var eq = peq[b.charCodeAt(i)];
18        var xv = eq | mv;
19        eq |= ((eq & pv) + pv) ^ pv;
20        mv |= ~(eq | pv);
21        pv &= eq;
22        if (mv & lst) {
23            sc++;
24        }
25        if (pv & lst) {
26            sc--;
27        }
28        mv = (mv << 1) | 1;
29        pv = (pv << 1) | ~(xv | mv);
30        mv &= xv;
31    }
32    i = n;
33    while (i--) {
34        peq[a.charCodeAt(i)] = 0;
35    }
36    return sc;
37};
38var myers_x = function (b, a) {
39    var n = a.length;
40    var m = b.length;
41    var mhc = [];
42    var phc = [];
43    var hsize = Math.ceil(n / 32);
44    var vsize = Math.ceil(m / 32);
45    for (var i = 0; i < hsize; i++) {
46        phc[i] = -1;
47        mhc[i] = 0;
48    }
49    var j = 0;
50    for (; j < vsize - 1; j++) {
51        var mv_1 = 0;
52        var pv_1 = -1;
53        var start_1 = j * 32;
54        var vlen_1 = Math.min(32, m) + start_1;
55        for (var k = start_1; k < vlen_1; k++) {
56            peq[b.charCodeAt(k)] |= 1 << k;
57        }
58        for (var i = 0; i < n; i++) {
59            var eq = peq[a.charCodeAt(i)];
60            var pb = (phc[(i / 32) | 0] >>> i) & 1;
61            var mb = (mhc[(i / 32) | 0] >>> i) & 1;
62            var xv = eq | mv_1;
63            var xh = ((((eq | mb) & pv_1) + pv_1) ^ pv_1) | eq | mb;
64            var ph = mv_1 | ~(xh | pv_1);
65            var mh = pv_1 & xh;
66            if ((ph >>> 31) ^ pb) {
67                phc[(i / 32) | 0] ^= 1 << i;
68            }
69            if ((mh >>> 31) ^ mb) {
70                mhc[(i / 32) | 0] ^= 1 << i;
71            }
72            ph = (ph << 1) | pb;
73            mh = (mh << 1) | mb;
74            pv_1 = mh | ~(xv | ph);
75            mv_1 = ph & xv;
76        }
77        for (var k = start_1; k < vlen_1; k++) {
78            peq[b.charCodeAt(k)] = 0;
79        }
80    }
81    var mv = 0;
82    var pv = -1;
83    var start = j * 32;
84    var vlen = Math.min(32, m - start) + start;
85    for (var k = start; k < vlen; k++) {
86        peq[b.charCodeAt(k)] |= 1 << k;
87    }
88    var score = m;
89    for (var i = 0; i < n; i++) {
90        var eq = peq[a.charCodeAt(i)];
91        var pb = (phc[(i / 32) | 0] >>> i) & 1;
92        var mb = (mhc[(i / 32) | 0] >>> i) & 1;
93        var xv = eq | mv;
94        var xh = ((((eq | mb) & pv) + pv) ^ pv) | eq | mb;
95        var ph = mv | ~(xh | pv);
96        var mh = pv & xh;
97        score += (ph >>> (m - 1)) & 1;
98        score -= (mh >>> (m - 1)) & 1;
99        if ((ph >>> 31) ^ pb) {
100            phc[(i / 32) | 0] ^= 1 << i;
101        }
102        if ((mh >>> 31) ^ mb) {
103            mhc[(i / 32) | 0] ^= 1 << i;
104        }
105        ph = (ph << 1) | pb;
106        mh = (mh << 1) | mb;
107        pv = mh | ~(xv | ph);
108        mv = ph & xv;
109    }
110    for (var k = start; k < vlen; k++) {
111        peq[b.charCodeAt(k)] = 0;
112    }
113    return score;
114};
115var distance = function (a, b) {
116    if (a.length < b.length) {
117        var tmp = b;
118        b = a;
119        a = tmp;
120    }
121    if (b.length === 0) {
122        return a.length;
123    }
124    if (a.length <= 32) {
125        return myers_32(a, b);
126    }
127    return myers_x(a, b);
128};
129exports.distance = distance;
130var closest = function (str, arr) {
131    var min_distance = Infinity;
132    var min_index = 0;
133    for (var i = 0; i < arr.length; i++) {
134        var dist = distance(str, arr[i]);
135        if (dist < min_distance) {
136            min_distance = dist;
137            min_index = i;
138        }
139    }
140    return arr[min_index];
141};
142exports.closest = closest;
143