Struct zerotrie::ZeroTrieSimpleAscii

source ·
#[repr(transparent)]
pub struct ZeroTrieSimpleAscii<Store: ?Sized> { /* private fields */ }
Expand description

A data structure that compactly maps from ASCII strings to integers.

For more information, see ZeroTrie.

§Examples

use litemap::LiteMap;
use zerotrie::ZeroTrieSimpleAscii;

let mut map = LiteMap::new_vec();
map.insert(&b"foo"[..], 1);
map.insert(b"bar", 2);
map.insert(b"bazzoo", 3);

let trie = ZeroTrieSimpleAscii::try_from(&map)?;

assert_eq!(trie.get(b"foo"), Some(1));
assert_eq!(trie.get(b"bar"), Some(2));
assert_eq!(trie.get(b"bazzoo"), Some(3));
assert_eq!(trie.get(b"unknown"), None);

The trie can only store ASCII bytes; a string with non-ASCII always returns None:

use zerotrie::ZeroTrieSimpleAscii;

// A trie with two values: "abc" and "abcdef"
let trie = ZeroTrieSimpleAscii::from_bytes(b"abc\x80def\x81");

assert!(matches!(trie.get(b"ab\xFF"), None));

Implementations§

source§

impl<const N: usize> ZeroTrieSimpleAscii<[u8; N]>

source

pub const fn from_sorted_u8_tuples(tuples: &[(&[u8], usize)]) -> Self

Const Constructor: Creates an ZeroTrieSimpleAscii from a sorted slice of keys and values.

This function needs to know the exact length of the resulting trie at compile time. To figure out N, first set N to be too large (say 0xFFFF), then look at the resulting compile error which will tell you how to set N, like this:

the evaluated program panicked at ‘Buffer too large. Size needed: 17’

That error message says you need to set N to 17.

Also see Self::from_sorted_str_tuples.

§Panics

Panics if items is not sorted or if N is not correct.

§Examples

Create a const ZeroTrieSimpleAscii at compile time:

use zerotrie::ZeroTrieSimpleAscii;

// The required capacity for this trie happens to be 17 bytes
const TRIE: ZeroTrieSimpleAscii<[u8; 17]> =
    ZeroTrieSimpleAscii::from_sorted_u8_tuples(&[
        (b"bar", 2),
        (b"bazzoo", 3),
        (b"foo", 1),
    ]);

assert_eq!(TRIE.get(b"foo"), Some(1));
assert_eq!(TRIE.get(b"bar"), Some(2));
assert_eq!(TRIE.get(b"bazzoo"), Some(3));
assert_eq!(TRIE.get(b"unknown"), None);

Panics if strings are not sorted:

const TRIE: ZeroTrieSimpleAscii<[u8; 17]> = ZeroTrieSimpleAscii::from_sorted_u8_tuples(&[
    (b"foo", 1),
    (b"bar", 2),
    (b"bazzoo", 3),
]);

Panics if capacity is too small:

const TRIE: ZeroTrieSimpleAscii<[u8; 15]> = ZeroTrieSimpleAscii::from_sorted_u8_tuples(&[
    (b"bar", 2),
    (b"bazzoo", 3),
    (b"foo", 1),
]);

Panics if capacity is too large:

const TRIE: ZeroTrieSimpleAscii<[u8; 20]> = ZeroTrieSimpleAscii::from_sorted_u8_tuples(&[
    (b"bar", 2),
    (b"bazzoo", 3),
    (b"foo", 1),
]);
source

pub const fn from_sorted_str_tuples(tuples: &[(&str, usize)]) -> Self

Const Constructor: Creates an ZeroTrieSimpleAscii from a sorted slice of keys and values.

This function needs to know the exact length of the resulting trie at compile time. To figure out N, first set N to be too large (say 0xFFFF), then look at the resulting compile error which will tell you how to set N, like this:

the evaluated program panicked at ‘Buffer too large. Size needed: 17’

That error message says you need to set N to 17.

Also see Self::from_sorted_u8_tuples.

§Panics

Panics if items is not sorted, if N is not correct, or if any of the strings contain non-ASCII characters.

§Examples

Create a const ZeroTrieSimpleAscii at compile time:

use zerotrie::ZeroTrieSimpleAscii;

// The required capacity for this trie happens to be 17 bytes
const TRIE: ZeroTrieSimpleAscii<[u8; 17]> =
    ZeroTrieSimpleAscii::from_sorted_str_tuples(&[
        ("bar", 2),
        ("bazzoo", 3),
        ("foo", 1),
    ]);

assert_eq!(TRIE.get(b"foo"), Some(1));
assert_eq!(TRIE.get(b"bar"), Some(2));
assert_eq!(TRIE.get(b"bazzoo"), Some(3));
assert_eq!(TRIE.get(b"unknown"), None);

Panics if the strings are not ASCII:

const TRIE: ZeroTrieSimpleAscii<[u8; 100]> = ZeroTrieSimpleAscii::from_sorted_str_tuples(&[
    ("bár", 2),
    ("båzzöo", 3),
    ("foo", 1),
]);
source§

impl<Store> ZeroTrieSimpleAscii<Store>
where Store: AsRef<[u8]> + ?Sized,

source

pub fn cursor(&self) -> ZeroTrieSimpleAsciiCursor<'_>

Gets a cursor into the current trie.

Useful to query a trie with data that is not a slice.

This is currently supported only on ZeroTrieSimpleAscii and ZeroAsciiIgnoreCaseTrie.

§Examples

Get a value out of a trie by writing it to the cursor:

use core::fmt::Write;
use zerotrie::ZeroTrieSimpleAscii;

// A trie with two values: "abc" and "abcdef"
let trie = ZeroTrieSimpleAscii::from_bytes(b"abc\x80def\x81");

// Get out the value for "abc"
let mut cursor = trie.cursor();
write!(&mut cursor, "abc");
assert_eq!(cursor.take_value(), Some(0));

Find the longest prefix match:

use zerotrie::ZeroTrieSimpleAscii;

// A trie with two values: "abc" and "abcdef"
let trie = ZeroTrieSimpleAscii::from_bytes(b"abc\x80def\x81");

// Find the longest prefix of the string "abcdxy":
let query = b"abcdxy";
let mut longest_prefix = 0;
let mut cursor = trie.cursor();
for (i, b) in query.iter().enumerate() {
    // Checking is_empty() is not required, but it is
    // good for efficiency
    if cursor.is_empty() {
        break;
    }
    if cursor.take_value().is_some() {
        longest_prefix = i;
    }
    cursor.step(*b);
}

// The longest prefix is "abc" which is length 3:
assert_eq!(longest_prefix, 3);
source§

impl<'a> ZeroTrieSimpleAscii<&'a [u8]>

source

pub fn into_cursor(self) -> ZeroTrieSimpleAsciiCursor<'a>

Same as ZeroTrieSimpleAscii::cursor() but moves self to avoid having to doubly anchor the trie to the stack.

source§

impl<Store> ZeroTrieSimpleAscii<Store>

source

pub const fn into_zerotrie(self) -> ZeroTrie<Store>

Wrap this specific ZeroTrie variant into a ZeroTrie.

source§

impl<Store> ZeroTrieSimpleAscii<Store>

source

pub const fn from_store(store: Store) -> Self

Create a trie directly from a store.

If the store does not contain valid bytes, unexpected behavior may occur.

source

pub fn take_store(self) -> Store

Takes the byte store from this trie.

source

pub fn convert_store<X: From<Store>>(self) -> ZeroTrieSimpleAscii<X>

Converts this trie’s store to a different store implementing the From trait.

For example, use this to change ZeroTrieSimpleAscii<Vec<u8>> to ZeroTrieSimpleAscii<Cow<[u8]>>.

§Examples
use std::borrow::Cow;
use zerotrie::ZeroTrieSimpleAscii;

let trie: ZeroTrieSimpleAscii<Vec<u8>> = ZeroTrieSimpleAscii::from_bytes(b"abc\x85").to_owned();
let cow: ZeroTrieSimpleAscii<Cow<[u8]>> = trie.convert_store();

assert_eq!(cow.get(b"abc"), Some(5));
source§

impl<Store> ZeroTrieSimpleAscii<Store>
where Store: AsRef<[u8]> + ?Sized,

source

pub fn get<K>(&self, key: K) -> Option<usize>
where K: AsRef<[u8]>,

Queries the trie for a string.

source

pub fn is_empty(&self) -> bool

Returns true if the trie is empty.

source

pub fn byte_len(&self) -> usize

Returns the size of the trie in number of bytes.

To get the number of keys in the trie, use .iter().count():

use zerotrie::ZeroTrieSimpleAscii;

// A trie with two values: "abc" and "abcdef"
let trie: &ZeroTrieSimpleAscii<[u8]> = ZeroTrieSimpleAscii::from_bytes(b"abc\x80def\x81");

assert_eq!(8, trie.byte_len());
assert_eq!(2, trie.iter().count());
source

pub fn as_bytes(&self) -> &[u8]

Returns the bytes contained in the underlying store.

source

pub fn as_borrowed(&self) -> &ZeroTrieSimpleAscii<[u8]>

Returns this trie as a reference transparent over a byte slice.

source

pub fn as_borrowed_slice(&self) -> ZeroTrieSimpleAscii<&[u8]>

Returns a trie with a store borrowing from this trie.

source§

impl<Store> ZeroTrieSimpleAscii<Store>
where Store: AsRef<[u8]> + ?Sized,

source

pub fn to_owned(&self) -> ZeroTrieSimpleAscii<Vec<u8>>

Converts a possibly-borrowed $name to an owned one.

Enabled with the alloc Cargo feature.

§Examples
use zerotrie::ZeroTrieSimpleAscii;

let trie: &ZeroTrieSimpleAscii<[u8]> = ZeroTrieSimpleAscii::from_bytes(b"abc\x85");
let owned: ZeroTrieSimpleAscii<Vec<u8>> = trie.to_owned();

assert_eq!(trie.get(b"abc"), Some(5));
assert_eq!(owned.get(b"abc"), Some(5));
source

pub fn iter(&self) -> impl Iterator<Item = (String, usize)> + '_

Returns an iterator over the key/value pairs in this trie.

Enabled with the alloc Cargo feature.

§Examples
use zerotrie::ZeroTrieSimpleAscii;

// A trie with two values: "abc" and "abcdef"
let trie: &ZeroTrieSimpleAscii<[u8]> = ZeroTrieSimpleAscii::from_bytes(b"abc\x80def\x81");

let mut it = trie.iter();
assert_eq!(it.next(), Some(("abc".into(), 0)));
assert_eq!(it.next(), Some(("abcdef".into(), 1)));
assert_eq!(it.next(), None);
source§

impl ZeroTrieSimpleAscii<[u8]>

source

pub fn from_bytes(trie: &[u8]) -> &Self

Casts from a byte slice to a reference to a trie with the same lifetime.

If the bytes are not a valid trie, unexpected behavior may occur.

source§

impl ZeroTrieSimpleAscii<Vec<u8>>

source

pub(crate) fn try_from_tuple_slice( items: &[(&ByteStr, usize)], ) -> Result<Self, Error>

source§

impl<Store> ZeroTrieSimpleAscii<Store>
where Store: AsRef<[u8]> + ?Sized,

source

pub fn to_btreemap(&self) -> BTreeMap<String, usize>

Exports the data from this ZeroTrie type into a BTreeMap.

Enabled with the alloc Cargo feature.

§Examples
use zerotrie::ZeroTrieSimpleAscii;
use std::collections::BTreeMap;

let trie = ZeroTrieSimpleAscii::from_bytes(b"abc\x81def\x82");
let items = trie.to_btreemap();

assert_eq!(items.len(), 2);

let recovered_trie: ZeroTrieSimpleAscii<Vec<u8>> = items
    .into_iter()
    .collect();
assert_eq!(trie.as_bytes(), recovered_trie.as_bytes());
source

pub(crate) fn to_btreemap_bytes(&self) -> BTreeMap<Box<[u8]>, usize>

Trait Implementations§

source§

impl<Store> AsRef<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<Store>
where Store: AsRef<[u8]> + ?Sized,

source§

fn as_ref(&self) -> &ZeroTrieSimpleAscii<[u8]>

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

impl Borrow<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<&[u8]>

source§

fn borrow(&self) -> &ZeroTrieSimpleAscii<[u8]>

Immutably borrows from an owned value. Read more
source§

impl Borrow<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<Box<[u8]>>

source§

fn borrow(&self) -> &ZeroTrieSimpleAscii<[u8]>

Immutably borrows from an owned value. Read more
source§

impl Borrow<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<Vec<u8>>

source§

fn borrow(&self) -> &ZeroTrieSimpleAscii<[u8]>

Immutably borrows from an owned value. Read more
source§

impl<Store: Clone + ?Sized> Clone for ZeroTrieSimpleAscii<Store>

source§

fn clone(&self) -> ZeroTrieSimpleAscii<Store>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<Store: Debug + ?Sized> Debug for ZeroTrieSimpleAscii<Store>

source§

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

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

impl<Store: Default + ?Sized> Default for ZeroTrieSimpleAscii<Store>

source§

fn default() -> ZeroTrieSimpleAscii<Store>

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

impl<Store> From<&ZeroTrieSimpleAscii<Store>> for BTreeMap<String, usize>
where Store: AsRef<[u8]> + ?Sized,

source§

fn from(other: &ZeroTrieSimpleAscii<Store>) -> Self

Converts to this type from the input type.
source§

impl<'a, K> FromIterator<(K, usize)> for ZeroTrieSimpleAscii<Vec<u8>>
where K: AsRef<[u8]>,

source§

fn from_iter<T: IntoIterator<Item = (K, usize)>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<Store: PartialEq + ?Sized> PartialEq for ZeroTrieSimpleAscii<Store>

source§

fn eq(&self, other: &ZeroTrieSimpleAscii<Store>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl ToOwned for ZeroTrieSimpleAscii<[u8]>

source§

fn to_owned(&self) -> Self::Owned

This impl allows ZeroTrieSimpleAscii to be used inside of a Cow.

Note that it is also possible to use ZeroTrieSimpleAscii<ZeroVec<u8>> for a similar result.

Enabled with the alloc Cargo feature.

§Examples
use std::borrow::Cow;
use zerotrie::ZeroTrieSimpleAscii;

let trie: Cow<ZeroTrieSimpleAscii<[u8]>> = Cow::Borrowed(ZeroTrieSimpleAscii::from_bytes(b"abc\x85"));
assert_eq!(trie.get(b"abc"), Some(5));
§

type Owned = ZeroTrieSimpleAscii<Box<[u8]>>

The resulting type after obtaining ownership.
1.63.0 · source§

fn clone_into(&self, target: &mut Self::Owned)

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

impl<'a, K> TryFrom<&'a BTreeMap<K, usize>> for ZeroTrieSimpleAscii<Vec<u8>>
where K: Borrow<[u8]>,

§

type Error = Error

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

fn try_from(map: &'a BTreeMap<K, usize>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'zf, Store1, Store2> ZeroFrom<'zf, ZeroTrieSimpleAscii<Store1>> for ZeroTrieSimpleAscii<Store2>
where Store2: ZeroFrom<'zf, Store1>,

source§

fn zero_from(other: &'zf ZeroTrieSimpleAscii<Store1>) -> Self

Clone the other C into a struct that may retain references into C.
source§

impl<S: ?Sized> ZeroTrieWithOptions for ZeroTrieSimpleAscii<S>

All branch nodes are binary search and there are no span nodes.

source§

impl<Store: Copy + ?Sized> Copy for ZeroTrieSimpleAscii<Store>

source§

impl<Store: Eq + ?Sized> Eq for ZeroTrieSimpleAscii<Store>

source§

impl<Store: ?Sized> StructuralPartialEq for ZeroTrieSimpleAscii<Store>

Auto Trait Implementations§

§

impl<Store> Freeze for ZeroTrieSimpleAscii<Store>
where Store: Freeze + ?Sized,

§

impl<Store> RefUnwindSafe for ZeroTrieSimpleAscii<Store>
where Store: RefUnwindSafe + ?Sized,

§

impl<Store> Send for ZeroTrieSimpleAscii<Store>
where Store: Send + ?Sized,

§

impl<Store> Sync for ZeroTrieSimpleAscii<Store>
where Store: Sync + ?Sized,

§

impl<Store> Unpin for ZeroTrieSimpleAscii<Store>
where Store: Unpin + ?Sized,

§

impl<Store> UnwindSafe for ZeroTrieSimpleAscii<Store>
where Store: UnwindSafe + ?Sized,

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

§

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

§

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> ErasedDestructor for T
where T: 'static,