@trie-data-structure/uncompressed-trie
v1.0.0
Published
Trie data structures for JavaScript
Downloads
14
Readme
:chains: @trie-data-structure/uncompressed-trie
Trie data structures for JavaScript. See docs.
:warning: Depending on your environment, the code may require
regeneratorRuntime
to be defined, for instance by importing regenerator-runtime/runtime.
import {Trie, ArrayNode} from '@trie-data-structure/uncompressed-trie';
import {range} from '@iterable-iterator/range';
import {map} from '@iterable-iterator/map';
const encode = (key) => map((i) => key.charCodeAt(i), range(key.length));
const degree = 256;
const trie = new Trie(new ArrayNode(degree));
const set = (key, value) => trie.set(encode(key), value);
const get = (key) => trie.get(encode(key));
set('abra', 1);
set('cadabra', 2);
set('abracadabra', 3);
get('abra'); // 1
get('cadabra'); // 2
get('abracadabra'); // 3