Struct TrieTree

Source
pub struct TrieTree<T> { /* private fields */ }
Expand description

§A TrieTree that supports the following functions:

  • Insert values
  • Remove values
  • Traversal values
  • Prefix lookup values
  • Debug Struct

Implementations§

Source§

impl<T> TrieTree<T>

Source

pub fn count(&self) -> usize

return values count

Source§

impl<T> TrieTree<T>
where T: Hash + Eq,

Source

pub fn new() -> Self

new Self

  • use Self::default()
Source

pub fn clear(&mut self)

clear all values

§Examples
let mut tree = TrieTree::new();
assert_eq!(tree.count(), 0);
assert!(tree.insert("abc".chars()));
assert_eq!(tree.count(), 1);
assert!(tree.query("abc".chars()));
tree.clear();
assert_eq!(tree.count(), 0);
assert!(! tree.query("abc".chars()));
assert!(tree.insert("abc".chars()));
Source

pub fn insert(&mut self, iter: impl IntoIterator<Item = T>) -> bool

insert values.

  • return whether the insertion values was successful
§Examples
let mut tree = TrieTree::new();
assert_eq!(tree.count(), 0);
assert!(tree.insert("abc".chars()));
assert_eq!(tree.count(), 1);
assert!(! tree.insert("abc".chars()));
assert_eq!(tree.count(), 1);
Source

pub fn query<Q>(&self, iter: impl IntoIterator<Item = Q>) -> bool
where Q: Borrow<T>,

§Whether the query values is in the tree Result list examples
querydataresult
abcdabcdtrue
abcabcdfalse
bcdabcdfalse
abcdeabcdfalse
§Examples
let mut tree = TrieTree::new();
assert!(tree.insert("abcd".chars()));
assert!(tree.query("abcd".chars()));
assert!(! tree.query("abc".chars()));
assert!(! tree.query("bcd".chars()));
assert!(! tree.query("abcde".chars()));
Source

pub fn query_nostop<Q>(&self, iter: impl IntoIterator<Item = Q>) -> Option<bool>
where Q: Borrow<T>,

§Whether the query values head is in the tree Result list examples
querydataresult
abcdSome(false)
abcabcdSome(false)
abcdabcdSome(true)
bcdabcdNone
abcdeabcdNone
§Examples
let mut tree = TrieTree::new();
assert_eq!(tree.query_nostop("".chars()),       Some(false));
assert!(tree.insert("abcd".chars()));
assert_eq!(tree.query_nostop("abc".chars()),    Some(false));
assert_eq!(tree.query_nostop("abcd".chars()),   Some(true));
assert_eq!(tree.query_nostop("bcd".chars()),    None);
assert_eq!(tree.query_nostop("abcde".chars()),  None);
Source

pub fn query_iter<Q>( &self, iter: impl IntoIterator<Item = Q>, ) -> Option<Iter<'_, T>>
where Q: Borrow<T>,

Get the Iterator matching the prefix
If there is no matching value, return None

§Examples
let mut tree = TrieTree::new();
assert!(tree.insert("abc".chars()));
assert!(tree.insert("ace".chars()));
assert!(tree.insert("bee".chars()));
let mut res: Vec<_> = tree.query_iter("a".chars()).unwrap().collect();
res.sort();
assert_eq!(res, vec![vec![&'b', &'c'], vec![&'c', &'e']]);
Source

pub fn remove<Q>(&mut self, iter: impl IntoIterator<Item = Q>) -> bool
where Q: Borrow<T>,

remove values and clean up overhanging branches

§Examples
let mut tree = TrieTree::new();
assert_eq!(tree.count(), 0);
assert!(tree.insert("abc".chars()));
assert_eq!(tree.count(), 1);
assert!(tree.query("abc".chars()));
assert!(tree.remove("abc".chars()));
assert!(! tree.remove("abc".chars()));
assert!(! tree.query("abc".chars()));
assert_eq!(tree.count(), 0);
Source

pub fn iter(&self) -> Iter<'_, T>

get iterator

§Examples
let tree = TrieTree::from_iter(["abc".chars(), "bcd".chars()]);
let mut items: Vec<Vec<&char>> = tree.iter().collect();
items.sort();
assert_eq!(items[0], [&'a', &'b', &'c']);
assert_eq!(items[1], [&'b', &'c', &'d']);
Source

pub fn shrink_to_fit(&mut self)

Shrink each HashMap in the tree

§Examples
let mut tree = TrieTree::from_iter(["abc".chars(), "bcd".chars()]);
tree.shrink_to_fit();

Trait Implementations§

Source§

impl<T> AsRef<TrieTree<T>> for TrieTree<T>

Source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Clone for TrieTree<T>
where T: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for TrieTree<T>
where T: Hash + Eq + Debug,

Source§

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

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

impl<T> Default for TrieTree<T>
where T: Hash + Eq,

Source§

fn default() -> Self

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

impl<T, I> Extend<I> for TrieTree<T>
where T: Hash + Eq, I: IntoIterator<Item = T>,

Source§

fn extend<IntoIter: IntoIterator<Item = I>>(&mut self, iter: IntoIter)

§Examples
let mut tree = TrieTree::new();
tree.extend([&[1, 2, 3][..], &[1, 2][..]]);
assert!(tree.query(&[1, 2, 3]));
assert!(tree.query(&[1, 2]));
assert!(! tree.query(&[1]));
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T, I> FromIterator<I> for TrieTree<T>
where T: Hash + Eq, I: IntoIterator<Item = T>,

Source§

fn from_iter<IntoIter: IntoIterator<Item = I>>(iter: IntoIter) -> Self

§Examples
let tree: TrieTree<_> = [[1, 2, 3], [1, 3, 5]].into_iter().collect();
assert!(tree.query([1, 2, 3]));
assert!(tree.query([1, 3, 5]));
Source§

impl<'a, T> IntoIterator for &'a TrieTree<T>
where T: Hash + Eq,

Source§

type Item = Vec<&'a T>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> PartialEq for TrieTree<T>
where T: Hash + Eq,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Hash + Eq> Eq for TrieTree<T>

Auto Trait Implementations§

§

impl<T> Freeze for TrieTree<T>

§

impl<T> RefUnwindSafe for TrieTree<T>
where T: RefUnwindSafe,

§

impl<T> Send for TrieTree<T>
where T: Send,

§

impl<T> Sync for TrieTree<T>
where T: Sync,

§

impl<T> Unpin for TrieTree<T>
where T: Unpin,

§

impl<T> UnwindSafe for TrieTree<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.