-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic.js
72 lines (56 loc) · 1.95 KB
/
dynamic.js
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
(function () {
// a dynamic programming solution
// see http://www.algorithmist.com/index.php/Dynamic_Programming
var DynamicMatch = {
match : function (needle, haystack) {
var y = 0, n = 0, ret, wild, map;
map = DynamicMatch.map(needle, haystack);
do {
// success: we are at the end of both needle and haystack
if( y == haystack.length && n == needle.length ) {
ret = true;
}
// wildcards are zero-width so only move the needle; bookmark for backtracking
else if( needle[n] == "*" ){
wild = { y : y, n : n};
n++;
}
// fail: needle is longer than haystack
else if( y == haystack.length ) {
ret = false;
}
// match: advance both needle and haystack
else if( map[y] && map[y][n] ) {
y++;
n++;
}
// miss with backtrack: jump to last bookmark, but down the haystack one
else if( wild ) {
y = wild.y + 1;
n = wild.n;
}
// miss with no backtrack: hard fail
else {
ret = false;
}
}
while( ret == null );
return ret;
},
map : function (a, b) {
var ai,
bi = -1;
var map = new Array(b.length);
while(++bi < b.length ) {
map[bi] = new Array(a.length);
ai = -1;
while(++ai < a.length ) {
map[bi][ai] =
(a[ai] == b[bi] || a[ai] == "." || a[ai] == "*") ? 1 : 0
}
}
return map;
}
};
window.match = DynamicMatch.match
})();