#data-structures

tst

Ternary search trie collection in rust with similar API to std::collections as it possible

22 releases

✓ Uses Rust 2018 edition

0.10.1 Feb 7, 2019
0.9.1 Jan 5, 2018
0.9.0 Dec 29, 2017
0.8.4 Aug 18, 2016
0.3.0 Jun 19, 2015

#85 in Data structures

Download history 74/week @ 2019-07-21 9/week @ 2019-07-28 16/week @ 2019-08-04 22/week @ 2019-08-11 67/week @ 2019-08-25 81/week @ 2019-09-01 68/week @ 2019-09-08 119/week @ 2019-09-15 240/week @ 2019-09-22 24/week @ 2019-09-29 1/week @ 2019-10-06 23/week @ 2019-10-13 24/week @ 2019-10-20 45/week @ 2019-10-27

160 downloads per month

MIT license

56KB
1K SLoC

tst

Build Status Coverage Status crates.io API

Ternary search tree collection in rust with similar API to std::collections as it possible.

Ternary search tree is a type of trie (sometimes called a prefix tree) where nodes are arranged in a manner similar to a binary search tree, but with up to three children rather than the binary tree's limit of two. Like other prefix trees, a ternary search tree can be used as an associative map structure with the ability for incremental string search. However, ternary search trees are more space efficient compared to standard prefix trees, at the cost of speed. Common applications for ternary search trees include spell-checking and auto-completion. TSTMap and TSTSet structures for map and set like usage.

Documentation is available at http://billyevans.github.io/tst/tst

It has special methods:

  • wildcard_iter/wildcard_iter_mut - get iterator by wildcard
  • prefix_iter/prefix_iter_mut - get iterator by prefix
  • longest_prefix - get longest prefix

Usage

Add this to your Cargo.toml:

[dependencies]
tst = "0.10.*"

Quick Start

#[macro_use]
extern crate tst;
use tst::TSTMap;

let m = tstmap! {
    "first" =>  1,
    "second" => 2,
    "firstthird" => 3,
    "firstsecond" => 12,
    "xirst" => -13,
};

// iterate
for (key, value) in m.iter() {
    println!("{}: {}", key, value);
}
assert_eq!(Some(&1), m.get("first"));
assert_eq!(5, m.len());

// calculating longest prefix
assert_eq!("firstsecond", m.longest_prefix("firstsecondthird"));

// get values with common prefix
for (key, value) in m.prefix_iter("first") {
    println!("{}: {}", key, value);
}

// get sum by wildcard iterator
assert_eq!(-12, m.wildcard_iter(".irst").fold(0, |sum, (_, val)| sum + val));

Iterating over keys with wildcard

#[macro_use]
extern crate tst;
use tst::TSTMap;

let m = tstmap! {
    "ac" => 1,
    "bd" => 2,
    "cc" => 3,
};

for (k, v) in m.wildcard_iter(".c") {
    println!("{} -> {}", k, v);
}

Itereting over keys with common prefix

#[macro_use]
extern crate tst;
use tst::TSTMap;

let m = tstmap! {
    "abc" => 1,
    "abcd" => 1,
    "abce" => 1,
    "abca" => 1,
    "zxd" => 1,
    "add" => 1,
    "abcdef" => 1,
};

for (key, value) in m.prefix_iter("abc") {
    println!("{}: {}", key, value);
}

Search for longest prefix in the tree

#[macro_use]
extern crate tst;
use tst::TSTMap;

let m = tstmap! {
    "abc" => 1,
    "abcd" => 1,
    "abce" => 1,
    "abca" => 1,
    "zxd" => 1,
    "add" => 1,
    "abcdef" => 1,
};

assert_eq!("abcd", m.longest_prefix("abcde"));

Implementation details

https://en.wikipedia.org/wiki/Ternary_search_tree

License

TST is distributed under the terms of the MIT license.

See LICENSE-MIT, and COPYRIGHT for details.

No runtime deps