Mnemonist

TrieMapMap


The TrieMap is basically a Trie in which you can associate an arbitraty value to the prefixes you insert.

var TrieMap = require('mnemonist/trie-map');

Constructor

The TrieMap optionally takes as single argument the type of sequence you are going to feed it.

// For a trie containing string prefixes
var trie = new TrieMap();

// For a trie containing arbitrary sequences fed as arrays
var trie = new TrieMap(Array);

Static #.from

Alternatively, one can build a TrieMap from an arbitrary JavaScript iterable likewise:

var list = TrieMap.from({
  roman: 1,
  romanesque: 2
});

Members

Methods

Mutation

Read

Iteration

#.size

Number of prefixes in the trie.

var trie = new TrieMap();
trie.size
>>> 0

#.set

Inserts a prefix into the Trie and associates it to the given value.

O(m), m being the size of the inserted string.

var trie = new TrieMap();
trie.set('hello', 'world');

trie.get('hello');
>>> 'world'

// Using custom tokens
trie.set(['I', 'am', 'very', 'happy'], 'world');

#.delete

Deletes a prefix from the TrieMap. Returns true if the prefix was deleted & false if the prefix was not in the TrieMap.

O(m), m being the size of the deleted string.

var trie = new TrieMap();
trie.add('hello');

trie.delete('hello');
>>> true

trie.delete('world');
>>> false

#.clear

Completely clears the trie.

var trie = new TrieMap();
trie.set('hello', 1);
trie.set('roman', 2);

trie.clear();

trie.size
>>> 0

#.get

Returns the value associated to the given prefix or undefined if the prefix does not exist in the trie.

O(m), m being the size of the searched string.

var trie = new TrieMap();
trie.set('hello', 'world');

trie.get('hello');
>>> 'world'

#.has

Returns whether the given prefix exists in the trie.

O(m), m being the size of the searched string.

var trie = new TrieMap();
trie.set('hello', 'world');

trie.has('hello');
>>> true

trie.has('world');
>>> false

#.find

Returns an array of couples of every prefixes and their associated value found in the trie with the given prefix.

O(m + n), m being the size of the query, n being the cumulated size of the matched prefixes.

var trie = new TrieMap();
trie.set('roman', 1);
trie.set('romanesque', 2);
trie.set('greek', 3);

trie.find('rom');
>>> [['roman', 1], ['romanesque', 2]]

trie.find('gr');
>>> [['greek', 3]]

trie.find('hel');
>>> []

#.entries

Returns an iterator over the trie’s entries (note that the order on which the trie will iterate over its entries is arbitrary).

var trie = TrieMap.from({roman: 1, romanesque: 2});

var iterator = trie.entries();

iterator.next().value
>>> ['roman', 1]

#.keys

Alias of #.prefixes.

#.prefixes

Returns an iterator over the trie’s prefixes (note that the order on which the trie will iterate over its prefixes is arbitrary).

var trie = TrieMap.from({roman: 1, romanesque: 2});

var iterator = trie.prefixes();

iterator.next().value
>>> 'roman'

#.values

Returns an iterator over the trie’s values (note that the order on which the trie will iterate over its values is arbitrary).

var trie = TrieMap.from({roman: 1, romanesque: 2});

var iterator = trie.values();

iterator.next().value
>>> 1

Iterable

Alternatively, you can iterate over a trie’s entries using ES2015 for...of protocol:

var trie = TrieMap.from({roman: 1, romanesque: 2});

for (var entry of trie) {
  console.log(entry);
}