Skip to content

Commit

Permalink
match passing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Koss committed Mar 25, 2011
1 parent 2513949 commit 3058c04
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
42 changes: 23 additions & 19 deletions scripts/ptrie.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace.lookup('org.startpad.trie.packed').define(function (ns) {
if (next == undefined) {
return false;
}
if (next.terminal) {
if (next.terminal && next.prefix == word) {
return true;
}
if (next.inode == undefined) {
Expand Down Expand Up @@ -103,40 +103,38 @@ namespace.lookup('org.startpad.trie.packed').define(function (ns) {
var node = this.nodes[inode], match, isTerminal;

if (node[0] == TERMINAL_PREFIX) {
if (word.length == 0) {
return {
terminal: true,
prefix: '',
inode: inode
};
}
isTerminal = true;
node = node.slice(1);
}

// Iterate through each pattern (prefix) string in the node
node.replace(reNodePart, function (w, prefix, ref) {
var common;
var common, isTerminal, fullMatch;

// Quick exit - already matched, or first chars don't match
// Note: Because of symbol hoisting, we can have two patterns
// with the same initial letter (unlike a strict Trie)

if (match || prefix[0] != word[0]) {
return;
}
if (prefix > word) {
console.log(node, word, prefix);
}

isTerminal = ref == STRING_SEP || ref == '';
fullMatch = prefix == word.slice(0, prefix.length);
common = commonPrefix(prefix, word);

match = {
terminal: isTerminal && prefix == word,
prefix: prefix,
inode: !isTerminal && prefix == word.slice(0, prefix.length) ?
fromAlphaCode(ref) : undefined
terminal: isTerminal && fullMatch,
prefix: common,
inode: !isTerminal && fullMatch ? fromAlphaCode(ref) : undefined
};
});

// No better match - then return the empty string match at this node
if (!match && isTerminal) {
return {
terminal: true,
prefix: '',
inode: undefined
};
}
// Found a symbol
if (match && match.inode != undefined) {
if (match.inode < this.symCount) {
Expand Down Expand Up @@ -191,4 +189,10 @@ namespace.lookup('org.startpad.trie.packed').define(function (ns) {
return s.slice(0, -1) + String.fromCharCode(asc + 1);
}

function commonPrefix(w1, w2) {
var maxlen = Math.min(w1.length, w2.length);
for (var i = 0; i < maxlen && w1[i] == w2[i]; i++) {}
return w1.slice(0, i);
}

});
6 changes: 3 additions & 3 deletions test/test-trie.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ namespace.lookup('org.startpad.trie.test').defineOnce(function (ns) {
var trie = new trieLib.Trie("cat cats dog dogs rat rats hi hit hither");
var ptrie = new ptrieLib.PackedTrie(trie.pack());

ut.assertEq(ptrie.match("catjzkd"), ['cat']);
ut.assertEq(ptrie.match("catjzkd"), 'cat');
ut.assertEq(ptrie.match("jzkdy"), undefined);
ut.assertEq(ptrie.match("jcatzkd"), undefined);
ut.assertEq(ptrie.match("hitherandyon"), ['hi', 'hit', 'hither']);
ut.assertEq(ptrie.match("hitherandyon"), 'hither');
});

ts.addTest("PackedTrie enumerate", function (ut) {
Expand Down Expand Up @@ -288,7 +288,7 @@ namespace.lookup('org.startpad.trie.test').defineOnce(function (ns) {
}
});

}).async().enable(false);
}).async().enable(true);

};
});

0 comments on commit 3058c04

Please sign in to comment.