fix bug in trie
This commit is contained in:
parent
075cc1244f
commit
fe6e86e1a4
2 changed files with 112 additions and 24 deletions
|
|
@ -1,7 +1,14 @@
|
|||
import { pretty } from "../lib/util/pretty.js";
|
||||
import { insert, emptyTrie, growPrefix, suggest } from "../lib/util/trie.js";
|
||||
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";
|
||||
|
||||
|
||||
///////////////////
|
||||
// Setup TRIE 1 ...
|
||||
|
||||
// insertion
|
||||
const with1Item = insert(emptyTrie)('abba')('dancing queen');
|
||||
console.log(pretty(with1Item));
|
||||
const with2Items = insert(with1Item)('aboriginal')('australia');
|
||||
|
|
@ -19,14 +26,67 @@ console.log(pretty(with7Items));
|
|||
const with8Items = insert(with7Items)('')('hi!');
|
||||
console.log(pretty(with8Items));
|
||||
|
||||
///////////////////
|
||||
// Setup TRIE 2 ...
|
||||
|
||||
const bigTrie = module2Env(ModuleStd).name2dyn;
|
||||
|
||||
///////////////////
|
||||
// Test...
|
||||
|
||||
// grow key (for auto-complete)
|
||||
console.log(growPrefix(with6Items)("a")); // b
|
||||
console.log(growPrefix(with6Items)("ab")); // (empty string)
|
||||
console.log(growPrefix(with6Items)("abb")); // a
|
||||
console.log(growPrefix(with6Items)("f")); // ood
|
||||
console.log(growPrefix(with6Items)("abo")); // riginal
|
||||
assert.equal(growPrefix(with6Items)("a" ), "b" );
|
||||
assert.equal(growPrefix(with6Items)("ab" ), "" );
|
||||
assert.equal(growPrefix(with6Items)("abb"), "a" );
|
||||
assert.equal(growPrefix(with6Items)("f" ), "ood" );
|
||||
assert.equal(growPrefix(with6Items)("abo"), "riginal");
|
||||
|
||||
// suggest (also for auto-complete)
|
||||
console.log(suggest(with8Items)("a")(3)); // 'ab', 'abba', 'aboriginal'
|
||||
console.log(suggest(with8Items)("a")(4)); // 'ab', 'abba', 'aboriginal', 'aboriginally'
|
||||
console.log(suggest(with8Items)("a")(5)); // 'ab', 'abba', 'aboriginal', 'aboriginally', 'absent'
|
||||
assert.deepEqual(
|
||||
suggest(with8Items)("a")(3),
|
||||
[
|
||||
[ 'ab', 'yup' ],
|
||||
[ 'abba', 'dancing queen' ],
|
||||
[ 'aboriginal', 'australia' ]
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
suggest(with8Items)("a")(4),
|
||||
[
|
||||
[ 'ab', 'yup' ],
|
||||
[ 'abba', 'dancing queen' ],
|
||||
[ 'aboriginal', 'australia' ],
|
||||
[ 'aboriginally', '??' ]
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
suggest(with8Items)("a")(5),
|
||||
[
|
||||
[ 'ab', 'yup' ],
|
||||
[ 'abba', 'dancing queen' ],
|
||||
[ 'aboriginal', 'australia' ],
|
||||
[ 'aboriginally', '??' ],
|
||||
[ 'absent', 'not here' ]
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
suggest(with8Items)("a")(15),
|
||||
[
|
||||
[ 'ab', 'yup' ],
|
||||
[ 'abba', 'dancing queen' ],
|
||||
[ 'aboriginal', 'australia' ],
|
||||
[ 'aboriginally', '??' ],
|
||||
[ 'absent', 'not here' ]
|
||||
]);
|
||||
|
||||
assert.equal(
|
||||
isProperlySorted(with8Items),
|
||||
true,
|
||||
"trie should always be properly sorted!"
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
isProperlySorted(bigTrie),
|
||||
true,
|
||||
"trie should always be properly sorted!"
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue