trie: add get function
This commit is contained in:
parent
dd35e2b577
commit
dadee965e5
2 changed files with 26 additions and 1 deletions
|
|
@ -160,6 +160,19 @@ export const suggest = trie => key => maxSuggestions => {
|
||||||
return __suggest(trie, "", key, maxSuggestions);
|
return __suggest(trie, "", key, maxSuggestions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const get = trie => key => {
|
||||||
|
if (key === '') {
|
||||||
|
return trie.value;
|
||||||
|
}
|
||||||
|
const [pos] = binarySearch(trie.children, key);
|
||||||
|
if (pos < trie.children.length) {
|
||||||
|
const [haveKey, haveChildNode] = trie.children[pos];
|
||||||
|
if (key.startsWith(haveKey)) {
|
||||||
|
return get(haveChildNode)(key.slice(haveKey.length));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const __suggest = (trie, path, remaining, maxSuggestions) => {
|
const __suggest = (trie, path, remaining, maxSuggestions) => {
|
||||||
if (maxSuggestions === 0) {
|
if (maxSuggestions === 0) {
|
||||||
return [];
|
return [];
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,9 @@ import * as assert from "node:assert";
|
||||||
import { module2Env } from "../lib/environment/env.js";
|
import { module2Env } from "../lib/environment/env.js";
|
||||||
import { ModuleStd } from "../lib/stdlib.js";
|
import { ModuleStd } from "../lib/stdlib.js";
|
||||||
import { pretty } from "../lib/util/pretty.js";
|
import { pretty } from "../lib/util/pretty.js";
|
||||||
import { emptyTrie, growPrefix, insert, isProperlySorted, suggest } from "../lib/util/trie.js";
|
import { emptyTrie, get, growPrefix, insert, isProperlySorted, suggest } from "../lib/util/trie.js";
|
||||||
|
import { push } from "../lib/structures/list.js";
|
||||||
|
import { getInst } from "../lib/primitives/dynamic.js";
|
||||||
|
|
||||||
|
|
||||||
///////////////////
|
///////////////////
|
||||||
|
|
@ -104,4 +106,14 @@ assert.deepEqual(
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
suggest(bigTrie)("df")(2),
|
suggest(bigTrie)("df")(2),
|
||||||
[]
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
getInst(get(bigTrie)("list.push")),
|
||||||
|
push
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
get(bigTrie)("ooo"),
|
||||||
|
undefined
|
||||||
);
|
);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue