|
|
@ -106,21 +106,6 @@ function groupOps(ops) { |
|
|
|
return groupBy(eq, ops.sort()); |
|
|
|
} |
|
|
|
|
|
|
|
// See: https://en.wikipedia.org/wiki/Trie
|
|
|
|
/*function buildTrie(prefix, operators) { |
|
|
|
var grps = groupOps(operators); |
|
|
|
return [prefix, grps.map( |
|
|
|
function(ops) { |
|
|
|
if (ops.length === 1) |
|
|
|
return ops; |
|
|
|
return buildTrie(ops[0][0], |
|
|
|
ops.map( |
|
|
|
function(op) { |
|
|
|
return op.slice(1); |
|
|
|
}).filter(function (x){ return x;})); |
|
|
|
})]; |
|
|
|
}*/ |
|
|
|
|
|
|
|
function find(f, haystack) { |
|
|
|
for(var i = 0; i < haystack.length; i++) { |
|
|
|
if (f(haystack[i])) |
|
|
@ -129,65 +114,6 @@ function find(f, haystack) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
/*function matchNext(c, trie) { |
|
|
|
var next = find(function(path) { |
|
|
|
if (path.length === 1) { |
|
|
|
return path[0][0] === c; |
|
|
|
} |
|
|
|
return path[0] === c; |
|
|
|
}, trie); |
|
|
|
if (next !== false) |
|
|
|
return trie[next]; |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
function trieMatch(n, str, trie) { |
|
|
|
if (trie.length === 1 && ((typeof trie[0]) === "string")) { |
|
|
|
if (trie[0].slice(1) === str.slice(0, trie[0].length-1)) |
|
|
|
return trie[0].length + n; |
|
|
|
return false; |
|
|
|
} |
|
|
|
var matched = matchNext(str[0], trie); |
|
|
|
if (matched && matched.length > 1) |
|
|
|
return trieMatch(n+1, str.slice(1), matched[1]); |
|
|
|
else if (matched) |
|
|
|
return trieMatch(n, str.slice(1), matched) |
|
|
|
else |
|
|
|
return n; |
|
|
|
} |
|
|
|
|
|
|
|
function trieMatch(matches, iterated, n, str, trie) { |
|
|
|
console.log(str, trie); |
|
|
|
iterated = iterated + str[0]; |
|
|
|
if (matchable(iterated)) { |
|
|
|
matches.push(n); |
|
|
|
} |
|
|
|
|
|
|
|
var matched = matchNext(str[0], trie); |
|
|
|
if (matched) { |
|
|
|
return trieMatch(matches, iterated, n+1, str.slice(1), matched); |
|
|
|
} |
|
|
|
else |
|
|
|
return matches; |
|
|
|
} |
|
|
|
|
|
|
|
var ops = ["**","**%a&&","**%*^", "$$", "&s"]; |
|
|
|
function matchable(x) { |
|
|
|
return find(equal(x), ops) !== false; |
|
|
|
} |
|
|
|
|
|
|
|
var trie = buildTrie("", ["**","**%a&&","**%*^", "$$", "&s"])[1]; |
|
|
|
//console.log(find(function(x) { return x === "a";}, "tybabb"));
|
|
|
|
//console.log(matchNext('*', matchNext('*',trie)[1]));
|
|
|
|
//function matchStr(n, str, trie) {
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
var str = "**%a&&345454"; |
|
|
|
var matched = trieMatch([], "", 0, str, trie); |
|
|
|
console.log(matched); |
|
|
|
//console.log(matchStr(0, "**^*$4545", buildTrie("", ["**", "**^"])[1]));
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
* Problem: |
|
|
|