trie-rs
Memory efficient trie (prefix tree) and map library based on LOUDS.
Master API Docs | Released API Docs | Benchmark Results | Changelog
Quickstart
To use trie-rs, add the following to your Cargo.toml
file:
[]
= "0.4.2"
Usage Overview
use str;
use TrieBuilder;
let mut builder = new; // Inferred `TrieBuilder<u8>` automatically
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push; // Word `push`ed twice is just ignored.
builder.push;
let trie = builder.build;
// exact_match(): Find a word exactly match to query.
assert_eq!;
assert_eq!;
assert_eq!;
// predictive_search(): Find words which include `query` as their prefix.
let results_in_u8s: = trie.predictive_search.collect;
let results_in_str: = trie.predictive_search.collect;
assert_eq!;
// common_prefix_search(): Find words which is included in `query`'s prefix.
let results_in_u8s: = trie.common_prefix_search.collect;
let results_in_str: = trie.common_prefix_search.collect;
assert_eq!;
Using with Various Data Types
TrieBuilder
is implemented using generic type like following:
impl<Label: Ord> TrieBuilder<Label> {
...
pub fn push<Arr: AsRef<[Label]>>(&mut self, word: Arr) where Label: Clone { ... }
...
}
In the above Usage Overview
example, we used Label=u8, Arr=&str
. If
Label
does not implement Clone
, use
[insert()
][crate::trie::TrieBuilder::insert].
Here shows other Label
and Arr
type examples.
Label=&str, Arr=Vec<&str>
Say Label
is English words and Arr
is English phrases.
use TrieBuilder;
let mut builder = new;
builder.push;
builder.push;
builder.push;
let trie = builder.build;
assert_eq!;
let r: = trie.predictive_search.collect;
assert_eq!;
let s: = trie.common_prefix_search.collect;
assert_eq!;
Label=u8, Arr=[u8; n]
Say Label
is a digit in Pi (= 3.14...) and Arr is a window to separate pi's digit by 10.
use TrieBuilder;
let mut builder = new; // Pi = 3.14...
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
let trie = builder.build;
assert_eq!;
let t: = trie.predictive_search.collect;
assert_eq!;
let u: = trie.common_prefix_search.collect;
assert_eq!;
Trie Map Usage
To store a value with each word, use trie_rs::map::{Trie, TrieBuilder}
.
use str;
use TrieBuilder;
let mut builder = new; // Inferred `TrieBuilder<u8, u8>` automatically
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push; // Word `push`ed twice uses last value.
builder.push;
let mut trie = builder.build;
// exact_match(): Find a word exactly match to query.
assert_eq!;
assert_eq!;
assert_eq!;
// Values can be modified.
let v = trie.exact_match_mut.unwrap;
*v = 8;
assert_eq!;
Incremental Search
For interactive applications, one can use an incremental search to get the best performance. See [IncSearch][crate::inc_search::IncSearch].
use str;
use ;
let mut builder = new; // Inferred `TrieBuilder<u8, u8>` automatically
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
builder.push;
let trie = builder.build;
let mut search = trie.inc_search;
// Query by the byte.
assert_eq!;
assert_eq!;
assert_eq!;
// Reset the query to go again.
search.reset;
// For unicode its easier to use .query_until().
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
search.reset;
assert_eq!; // No match on byte at index 2.
Features
- Generic type support: As the above examples show, trie-rs can be used for searching not only UTF-8 string but also other data types.
- Based on louds-rs, which is fast, parallelized, and memory efficient.
- Latest benchmark results are always accessible: trie-rs is continuously benchmarked in Travis CI using Criterion.rs. Graphical benchmark results are published here.
map::Trie
associates aValue
with each entry.Value
does not require any traits.Label: Clone
not required to createTrie<Label>
but useful for many reifying search operations likepredictive_search()
.- Many search operations are implemented via iterators which are lazy, require less memory, and can be short circuited.
- Incremental search available for "online" applications, i.e., searching one
Label
at a time.
Cargo features
- "rayon"
Enables rayon a data parallelism library.
- "mem_dbg"
Can determine the size in bytes of nested data structures like the trie itself.
- "serde"
Can serialize and deserialize the trie.
Acknowledgments
edict.furigana
is used for benchmark.
This file is constructed in the following step:
- Download
edict.gz
from EDICT. - Convert it from original EUC into UTF-8.
- Translate it into CSV file with edict-to-csv.
- Extract field $1 for Hiragana/Katakana words, and field $3 for other (like Kanji) words.
- Translate Katakana into Hiragana with kana2hira.
Many thanks for these dictionaries and tools.
Versions
trie-rs uses semantic versioning.
Since current major version is 0, minor version update might involve breaking public API change (although it is carefully avoided).
Rust Version Supports
trie-rs is continuously tested with these Rust versions in with the github CI:
- 1.75.0 with all features
- 1.67.0 with no features
- Latest stable version
So it is expected to work with Rust 1.75.0 and any newer versions.
Older versions may also work but are not tested or guaranteed.
Earlier Rust Verion Supports
If support for Rust prior to 1.67.0 is required, trie-rs 0.2.0 supports Rust 1.33.0 and later.
Contributing
Any kind of pull requests are appreciated.
License
MIT OR Apache-2.0