Struct jupiter::idb::trie::Trie

source ·
pub struct Trie<T> { /* private fields */ }
Expand description

Represents a trie to map strings to values of type T.

Implementations§

source§

impl<T> Trie<T>

source

pub fn new() -> Self

Creates a new and empty trie.

§Example
let trie : Trie<i32> = Trie::new();

assert_eq!(trie.num_nodes(), 0);
assert_eq!(trie.num_entries(), 0);
source

pub fn insert(&mut self, key: &str, value: T)

Inserts the given value for the given key.

Note that this is optimized for performance. Therefore this doesn’t check if the value is already present for the key. If the exactly same was already present it will still be added again. See Trie::insert_unique for a way of only inserting entries once.

§Example
let mut trie = Trie::new();

trie.insert("a", 1);
assert_eq!(trie.num_nodes(), 1);
assert_eq!(trie.num_entries(), 1);

// Duplicate values can be inserted...
trie.insert("a", 1);
assert_eq!(trie.num_nodes(), 1);
assert_eq!(trie.num_entries(), 2);

// The tree grows as required...
trie.insert("ab", 1);
assert_eq!(trie.num_nodes(), 2);
assert_eq!(trie.num_entries(), 3);
source

pub fn query_values(&self, query: impl AsRef<str>) -> Option<&Vec<T>>

Retrieves the values stored for a given string.

Note that Trie::query provides a boilerplate API which directly returns an iterator and therefore doesn’t require unwrapping the Option.

§Example
let mut trie = Trie::new();

// A simple value can be retrieved...
trie.insert("a", 1);
assert_eq!(trie.query_values("a").unwrap()[0], 1);

// A duplicate value can be retrieved...
trie.insert("a", 2);
assert_eq!(trie.query_values("a").unwrap()[0], 1);
assert_eq!(trie.query_values("a").unwrap()[1], 2);

// A "child-entry" can be retrieved and leaves the actual entries intact...
trie.insert("ab", 3);
assert_eq!(trie.query_values("a").unwrap()[0], 1);
assert_eq!(trie.query_values("a").unwrap()[1], 2);
assert_eq!(trie.query_values("ab").unwrap()[0], 3);
source

pub fn query(&self, query: impl AsRef<str>) -> OptionalIter<'_, T>

Provides a boilerplate way of querying values.

§Example
let mut trie = Trie::new();

trie.insert("a", 1);
assert_eq!(trie.query("a").next().unwrap(), &1);
assert_eq!(trie.query("unknown").next().is_none(), true);
source

pub fn prefix_query(&self, prefix: &str) -> PrefixIter<'_, T>

Returns an iterator which performs a depth first search yielding the complete sub tree below the given prefix.

§Example
let mut trie = Trie::new();
trie.insert("abc", 1);
trie.insert("abc", 2);
trie.insert("abcd", 3);

let results: Vec<&i32> = trie.prefix_query("abc").collect();
assert_eq!(results, vec![&1, &2, &3]);
source

pub fn allocated_size(&self) -> usize

Returns the amount of memory (in bytes) used by this trie.

source

pub fn num_entries(&self) -> usize

Returns the number of entries stored in this trie.

§Example
let mut trie = Trie::new();

trie.insert("a", 1);
assert_eq!(trie.num_entries(), 1);

// Duplicate values can be inserted and are counted as extra entry.
trie.insert("a", 1);
assert_eq!(trie.num_entries(), 2);
source

pub fn num_nodes(&self) -> usize

Returns the number of nodes in this trie.

§Example
let mut trie = Trie::new();

// A node per character is created...
trie.insert("abc", 1);
assert_eq!(trie.num_nodes(), 3);

// Re-using an existing path won't add new nodes.
trie.insert("abc", 1);
assert_eq!(trie.num_nodes(), 3);
source

pub fn scan<C>(&self, callback: &mut C)
where C: FnMut(&str, &T),

Performs a depth first walk and invokes the callback for each entry in the Trie.

Note that this might be quite a long running task and there is no way of interrupting it (as no iterator is used). Therefore this is should only be used for data management tasks and not for lookups of any kind.

Also note that this uses a stack based recursion. Therefore this must not be used if keys in this Trie are unhealthy long.

§Example
let mut trie = Trie::new();

// A node per character is created...
trie.insert("abc", 1);
trie.insert("abcd", 2);

let mut counter = 0;
trie.scan(&mut |_,_| counter += 1);
assert_eq!(counter, 2);
source§

impl<T: PartialEq> Trie<T>

source

pub fn insert_unique(&mut self, key: &str, value: T)

Inserts the given value for the given key.

In contrast to Trie::insert this checks if the value is already present for the key and won’t insert a duplicate. again.

§Example
let mut trie = Trie::new();

trie.insert_unique("a", 1);
assert_eq!(trie.num_nodes(), 1);
assert_eq!(trie.num_entries(), 1);

// Duplicate values will not be inserted...
trie.insert_unique("a", 1);
assert_eq!(trie.num_nodes(), 1);
assert_eq!(trie.num_entries(), 1);

Trait Implementations§

source§

impl<T> Default for Trie<T>

source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for Trie<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Trie<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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more