Struct mytrie::Trie

source ·
pub struct Trie(_);
Expand description

A prefix tree

A trie is able to store char sequences and iterate over those with a common prefix.

It saves common prefixes only once, enabling fast iteration over all suffixes belonging to a prefix.

Create an empty trie and insert something:

use mytrie::Trie;

let mut trie = Trie::default();
trie.insert("Hello World!");
trie.remove("Hello World!").unwrap();

Implementations§

source§

impl Trie

source

pub fn from(content: impl IntoIterator<Item = impl AsRef<str>>) -> Self

Initialize a trie from a set of strings.

Example:

use mytrie::Trie;

let trie = Trie::from(["Hello", "world"]);
source

pub fn insert(&mut self, content: &str)

Adds a string to the trie

Example:

use mytrie::Trie;

let mut trie = Trie::default();
trie.insert("…");
source

pub fn iter_suffixes<'a>(
    &'a self,
    prefix: &str
) -> impl Iterator<Item = String> + 'a

Iterate all suffixes that follow this prefix

The order of iteration is arbitrary.

Example:

use mytrie::Trie;

let trie = Trie::from(["Hallo", "Hallöchen"]);
let mut suffixes: Vec<String> = trie.iter_suffixes("Hall").collect();

suffixes.sort();
assert_eq!(suffixes, ["o", "öchen"]);
source

pub fn iter_content<'a>(
    &'a self,
    prefix: &'a str
) -> impl Iterator<Item = String> + 'a

Iterate all strings in the trie with this prefix

The order of iteration is arbitrary.

Example:

use mytrie::Trie;

let trie = Trie::from(["Hallo", "Hallöchen", "Tschüs"]);
let mut content: Vec<String> = trie.iter_content("Hall").collect();

content.sort();
assert_eq!(content, ["Hallo", "Hallöchen"]);
source

pub fn remove(&mut self, content: &str) -> Option<()>

Remove a string from the trie

On successful removal, Some(()) is returned. If content was not present, None is returned.

Example:

use mytrie::Trie;

let mut trie = Trie::from(["Hallo", "Hallöchen"]);
trie.remove("Hallo").unwrap();
source

pub fn contains_prefix(&self, prefix: &str) -> bool

Checks if something with this prefix is in the trie

Example:

use mytrie::Trie;

let trie = Trie::from(["Hallo", "Hallöchen", "Tschüs", "Hallo Welt"]);
assert!(trie.contains_prefix("Hall"));
assert!(trie.contains_prefix("Hallo"));
assert!(!trie.contains_prefix("ABC"));
assert!(trie.contains_prefix(""));
source

pub fn contains(&self, content: &str) -> bool

Checks if the specified string was inserted into the trie Example:

use mytrie::Trie;

let trie = Trie::from(["Hallo"]);
assert!(!trie.contains("Hall"));
assert!(trie.contains("Hallo"));
source

pub fn is_empty(&self) -> bool

Returns true if the trie contains no content

Example:

use mytrie::Trie;

let mut trie = Trie::default();
assert!(trie.is_empty());
trie.insert("");
assert!(!trie.is_empty());
source

pub fn remove_suffixes(&mut self, prefix: &str) -> Option<Self>

Remove everything that follows this suffix

If a subtree was deleted, returns it. This will be another trie, containing all those removed strings minus the prefix.

An example for clarification:

use mytrie::Trie;

let mut trie = Trie::from(["Hallo", "Hallöchen", "Tschüs"]);
let removed: Trie = trie.remove_suffixes("Hal").unwrap();
let mut removed: Vec<String> = removed.iter_content("").collect();
removed.sort();

assert_eq!(removed, vec!["lo", "löchen"]);

Trait Implementations§

source§

impl Debug for Trie

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Trie

source§

fn default() -> Trie

Returns the “default value” for a type. Read more
source§

impl<S> FromIterator<S> for Triewhere
    S: AsRef<str>,

source§

fn from_iter<T>(string_iter: T) -> Triewhere
    T: IntoIterator<Item = S>,

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Trie

§

impl Send for Trie

§

impl Sync for Trie

§

impl Unpin for Trie

§

impl UnwindSafe for Trie

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.