trie: add get function

This commit is contained in:
Joeri Exelmans 2025-05-11 09:30:48 +02:00
parent dd35e2b577
commit dadee965e5
2 changed files with 26 additions and 1 deletions

View file

@ -160,6 +160,19 @@ export const 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) => {
if (maxSuggestions === 0) {
return [];

View file

@ -3,7 +3,9 @@ import * as assert from "node:assert";
import { module2Env } from "../lib/environment/env.js";
import { ModuleStd } from "../lib/stdlib.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";
///////////////////
@ -105,3 +107,13 @@ assert.deepEqual(
suggest(bigTrie)("df")(2),
[]
);
assert.equal(
getInst(get(bigTrie)("list.push")),
push
);
assert.equal(
get(bigTrie)("ooo"),
undefined
);