fix bug in radix trie
This commit is contained in:
parent
70fb80a9fc
commit
4fcfea409a
2 changed files with 32 additions and 14 deletions
|
|
@ -19,7 +19,7 @@ export const isProperlySorted = trie => {
|
|||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// find maximal common prefix, and whether string A is smaller than B
|
||||
const commonPrefix = (strA, strB) => {
|
||||
|
|
@ -34,12 +34,12 @@ const commonPrefix = (strA, strB) => {
|
|||
}
|
||||
}
|
||||
return [strA.slice(0, i), false];
|
||||
}
|
||||
};
|
||||
|
||||
// a funny kind of binary search, that assumes 'ls' is a sorted list of strings, and none of the strings have a common prefix
|
||||
const binarySearch = (ls, key) => {
|
||||
return __binarySearch(ls, key, 0, ls.length);
|
||||
}
|
||||
};
|
||||
const __binarySearch = (ls, key, min, max) => {
|
||||
if (min === max) {
|
||||
return [max, ""]; // otherwise we go out of bounds
|
||||
|
|
@ -54,7 +54,7 @@ const __binarySearch = (ls, key, min, max) => {
|
|||
return __binarySearch(ls, key, min, middle);
|
||||
}
|
||||
return __binarySearch(ls, key, middle+1, max);
|
||||
}
|
||||
};
|
||||
|
||||
const check = trie => {
|
||||
// uncomment if you think shit is broken:
|
||||
|
|
@ -62,7 +62,7 @@ const check = trie => {
|
|||
// throw new Error('not properly sorted!')
|
||||
// }
|
||||
return trie;
|
||||
}
|
||||
};
|
||||
|
||||
// insert (key,value) into trie.
|
||||
export const insert = trie => key => value => {
|
||||
|
|
@ -118,15 +118,20 @@ export const insert = trie => key => value => {
|
|||
children: trie.children.with(
|
||||
insertPos, // position to update
|
||||
[prefix, {
|
||||
value: (postFix.length === 0) ? value : undefined,
|
||||
children: (havePostFix < postFix)
|
||||
? [
|
||||
[havePostFix, haveChildNode],
|
||||
[postFix, {value, children: []}],
|
||||
]
|
||||
: [
|
||||
[postFix, {value, children: []}],
|
||||
[havePostFix, haveChildNode],
|
||||
],
|
||||
: ((postFix.length > 0)
|
||||
? [
|
||||
[postFix, {value, children: []}],
|
||||
[havePostFix, haveChildNode],
|
||||
]
|
||||
: [
|
||||
[havePostFix, haveChildNode],
|
||||
]),
|
||||
}],
|
||||
),
|
||||
});
|
||||
|
|
@ -153,12 +158,12 @@ export const growPrefix = trie => key => {
|
|||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
};
|
||||
|
||||
// get array of (key, value) entries whose keys are prefixed by 'key'.
|
||||
export const suggest = trie => key => maxSuggestions => {
|
||||
return __suggest(trie, "", key, maxSuggestions);
|
||||
}
|
||||
};
|
||||
|
||||
export const get = trie => key => {
|
||||
if (key === '') {
|
||||
|
|
@ -171,7 +176,7 @@ export const get = trie => key => {
|
|||
return get(haveChildNode)(key.slice(haveKey.length));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const __suggest = (trie, path, remaining, maxSuggestions) => {
|
||||
if (maxSuggestions === 0) {
|
||||
|
|
@ -205,4 +210,4 @@ const __suggest = (trie, path, remaining, maxSuggestions) => {
|
|||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue