/pruning_radix_trie

Rust implementation of Pruning Radix Trie, originally by Wolf Garbe

Primary LanguageRustMIT LicenseMIT

Pruning Radix Trie

Rust implementation of Pruning Radix Trie, originally by Wolf Garbe (see credits/PruningRadixTrieLicense.txt).

Usage

Add terms with payloads to the trie with:

pub fn add(&mut self, term: &str, payload: T, weight: U);

After which you can prefix match with:

pub fn find(&self, prefix: &str, top_k: usize) 
    -> Vec<Result<T,U>>

Results are returned in descending order based on weight.

Example

use pruning_radix_trie::PruningRadixTrie;

fn main() {
    let mut trie = PruningRadixTrie::new();
    trie.add("heyo", vec![1, 2, 3], 5);
    trie.add("hello", vec![4, 5, 6], 10);
    trie.add("hej", vec![7, 8, 9], 20);

    let results = trie.find("he", 10);

    for Result { term, payload, weight } in results {
        println!("{:10}{:?}{:>4}", term, payload, weight);
    }
    //hej       [7, 8, 9]  20
    //hello     [4, 5, 6]  10
    //heyo      [1, 2, 3]   5
}

Testing

Measuring code coverage

N.B.: At the moment, the nightly channel of Rust is required.

First of all, install grcov

cargo install grcov

Second, install the llvm-tools Rust component (llvm-tools-preview for now, it might become llvm-tools soon):

rustup component add llvm-tools-preview

To run tests with code coverage run

bash run_source_cov.sh

A html coverage report will be generated in ./target/debug/coverage/

See rust-code-coverage-sample for details.