#[repr(transparent)]pub struct ZeroTriePerfectHash<Store: ?Sized> { /* private fields */ }
Expand description
A data structure that compactly maps from byte strings to integers.
For more information, see ZeroTrie
.
§Examples
use litemap::LiteMap;
use zerotrie::ZeroTriePerfectHash;
let mut map = LiteMap::<&[u8], usize>::new_vec();
map.insert("foo".as_bytes(), 1);
map.insert("bår".as_bytes(), 2);
map.insert("båzzøø".as_bytes(), 3);
let trie = ZeroTriePerfectHash::try_from(&map)?;
assert_eq!(trie.get("foo".as_bytes()), Some(1));
assert_eq!(trie.get("bår".as_bytes()), Some(2));
assert_eq!(trie.get("båzzøø".as_bytes()), Some(3));
assert_eq!(trie.get("bazzoo".as_bytes()), None);
Implementations§
Source§impl<Store> ZeroTriePerfectHash<Store>
impl<Store> ZeroTriePerfectHash<Store>
Sourcepub const fn into_zerotrie(self) -> ZeroTrie<Store>
pub const fn into_zerotrie(self) -> ZeroTrie<Store>
Wrap this specific ZeroTrie variant into a ZeroTrie.
Source§impl<Store> ZeroTriePerfectHash<Store>
impl<Store> ZeroTriePerfectHash<Store>
Sourcepub const fn from_store(store: Store) -> Self
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.
Sourcepub fn take_store(self) -> Store
pub fn take_store(self) -> Store
Takes the byte store from this trie.
Sourcepub fn convert_store<X: From<Store>>(self) -> ZeroTriePerfectHash<X>
pub fn convert_store<X: From<Store>>(self) -> ZeroTriePerfectHash<X>
Converts this trie’s store to a different store implementing the From
trait.
For example, use this to change ZeroTriePerfectHash<Vec<u8>>
to ZeroTriePerfectHash<Cow<[u8]>>
.
§Examples
use std::borrow::Cow;
use zerotrie::ZeroTriePerfectHash;
let trie: ZeroTriePerfectHash<Vec<u8>> = ZeroTriePerfectHash::from_bytes(b"abc\x85").to_owned();
let cow: ZeroTriePerfectHash<Cow<[u8]>> = trie.convert_store();
assert_eq!(cow.get(b"abc"), Some(5));
Source§impl<Store> ZeroTriePerfectHash<Store>
impl<Store> ZeroTriePerfectHash<Store>
Sourcepub fn byte_len(&self) -> usize
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::ZeroTriePerfectHash;
// A trie with two values: "abc" and "abcdef"
let trie: &ZeroTriePerfectHash<[u8]> = ZeroTriePerfectHash::from_bytes(b"abc\x80def\x81");
assert_eq!(8, trie.byte_len());
assert_eq!(2, trie.iter().count());
Sourcepub fn as_borrowed(&self) -> &ZeroTriePerfectHash<[u8]>
pub fn as_borrowed(&self) -> &ZeroTriePerfectHash<[u8]>
Returns this trie as a reference transparent over a byte slice.
Sourcepub fn as_borrowed_slice(&self) -> ZeroTriePerfectHash<&[u8]>
pub fn as_borrowed_slice(&self) -> ZeroTriePerfectHash<&[u8]>
Returns a trie with a store borrowing from this trie.
Source§impl<Store> ZeroTriePerfectHash<Store>
impl<Store> ZeroTriePerfectHash<Store>
Sourcepub fn to_owned(&self) -> ZeroTriePerfectHash<Vec<u8>>
pub fn to_owned(&self) -> ZeroTriePerfectHash<Vec<u8>>
Converts a possibly-borrowed $name to an owned one.
✨ Enabled with the alloc
Cargo feature.
§Examples
use zerotrie::ZeroTriePerfectHash;
let trie: &ZeroTriePerfectHash<[u8]> = ZeroTriePerfectHash::from_bytes(b"abc\x85");
let owned: ZeroTriePerfectHash<Vec<u8>> = trie.to_owned();
assert_eq!(trie.get(b"abc"), Some(5));
assert_eq!(owned.get(b"abc"), Some(5));
Sourcepub fn iter(&self) -> impl Iterator<Item = (Vec<u8>, usize)> + '_
pub fn iter(&self) -> impl Iterator<Item = (Vec<u8>, usize)> + '_
Returns an iterator over the key/value pairs in this trie.
✨ Enabled with the alloc
Cargo feature.
§Examples
use zerotrie::ZeroTriePerfectHash;
// A trie with two values: "abc" and "abcdef"
let trie: &ZeroTriePerfectHash<[u8]> = ZeroTriePerfectHash::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 ZeroTriePerfectHash<[u8]>
impl ZeroTriePerfectHash<[u8]>
Sourcepub fn from_bytes(trie: &[u8]) -> &Self
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 ZeroTriePerfectHash<Vec<u8>>
impl ZeroTriePerfectHash<Vec<u8>>
Source§impl<Store> ZeroTriePerfectHash<Store>
impl<Store> ZeroTriePerfectHash<Store>
Sourcepub fn to_btreemap(&self) -> BTreeMap<Vec<u8>, usize>
pub fn to_btreemap(&self) -> BTreeMap<Vec<u8>, usize>
Exports the data from this ZeroTrie type into a BTreeMap.
✨ Enabled with the alloc
Cargo feature.
§Examples
use zerotrie::ZeroTriePerfectHash;
use std::collections::BTreeMap;
let trie = ZeroTriePerfectHash::from_bytes(b"abc\x81def\x82");
let items = trie.to_btreemap();
assert_eq!(items.len(), 2);
let recovered_trie: ZeroTriePerfectHash<Vec<u8>> = items
.into_iter()
.collect();
assert_eq!(trie.as_bytes(), recovered_trie.as_bytes());
pub(crate) fn to_btreemap_bytes(&self) -> BTreeMap<Box<[u8]>, usize>
Trait Implementations§
Source§impl<Store> AsRef<ZeroTriePerfectHash<[u8]>> for ZeroTriePerfectHash<Store>
impl<Store> AsRef<ZeroTriePerfectHash<[u8]>> for ZeroTriePerfectHash<Store>
Source§fn as_ref(&self) -> &ZeroTriePerfectHash<[u8]>
fn as_ref(&self) -> &ZeroTriePerfectHash<[u8]>
Source§impl Borrow<ZeroTriePerfectHash<[u8]>> for ZeroTriePerfectHash<&[u8]>
impl Borrow<ZeroTriePerfectHash<[u8]>> for ZeroTriePerfectHash<&[u8]>
Source§impl Borrow<ZeroTriePerfectHash<[u8]>> for ZeroTriePerfectHash<Box<[u8]>>
impl Borrow<ZeroTriePerfectHash<[u8]>> for ZeroTriePerfectHash<Box<[u8]>>
Source§impl Borrow<ZeroTriePerfectHash<[u8]>> for ZeroTriePerfectHash<Vec<u8>>
impl Borrow<ZeroTriePerfectHash<[u8]>> for ZeroTriePerfectHash<Vec<u8>>
Source§impl<Store: Clone + ?Sized> Clone for ZeroTriePerfectHash<Store>
impl<Store: Clone + ?Sized> Clone for ZeroTriePerfectHash<Store>
Source§fn clone(&self) -> ZeroTriePerfectHash<Store>
fn clone(&self) -> ZeroTriePerfectHash<Store>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<Store: Default + ?Sized> Default for ZeroTriePerfectHash<Store>
impl<Store: Default + ?Sized> Default for ZeroTriePerfectHash<Store>
Source§fn default() -> ZeroTriePerfectHash<Store>
fn default() -> ZeroTriePerfectHash<Store>
Source§impl<Store> From<&ZeroTriePerfectHash<Store>> for BTreeMap<Vec<u8>, usize>
impl<Store> From<&ZeroTriePerfectHash<Store>> for BTreeMap<Vec<u8>, usize>
Source§fn from(other: &ZeroTriePerfectHash<Store>) -> Self
fn from(other: &ZeroTriePerfectHash<Store>) -> Self
Source§impl<'a, K> FromIterator<(K, usize)> for ZeroTriePerfectHash<Vec<u8>>
impl<'a, K> FromIterator<(K, usize)> for ZeroTriePerfectHash<Vec<u8>>
Source§impl ToOwned for ZeroTriePerfectHash<[u8]>
impl ToOwned for ZeroTriePerfectHash<[u8]>
Source§fn to_owned(&self) -> Self::Owned
fn to_owned(&self) -> Self::Owned
This impl allows ZeroTriePerfectHash
to be used inside of a Cow
.
Note that it is also possible to use ZeroTriePerfectHash<ZeroVec<u8>>
for a similar result.
✨ Enabled with the alloc
Cargo feature.
§Examples
use std::borrow::Cow;
use zerotrie::ZeroTriePerfectHash;
let trie: Cow<ZeroTriePerfectHash<[u8]>> = Cow::Borrowed(ZeroTriePerfectHash::from_bytes(b"abc\x85"));
assert_eq!(trie.get(b"abc"), Some(5));
1.63.0 · Source§fn clone_into(&self, target: &mut Self::Owned)
fn clone_into(&self, target: &mut Self::Owned)
Source§impl<'zf, Store1, Store2> ZeroFrom<'zf, ZeroTriePerfectHash<Store1>> for ZeroTriePerfectHash<Store2>where
Store2: ZeroFrom<'zf, Store1>,
impl<'zf, Store1, Store2> ZeroFrom<'zf, ZeroTriePerfectHash<Store1>> for ZeroTriePerfectHash<Store2>where
Store2: ZeroFrom<'zf, Store1>,
Source§fn zero_from(other: &'zf ZeroTriePerfectHash<Store1>) -> Self
fn zero_from(other: &'zf ZeroTriePerfectHash<Store1>) -> Self
C
into a struct that may retain references into C
.Source§impl<S: ?Sized> ZeroTrieWithOptions for ZeroTriePerfectHash<S>
Branch nodes could be either binary search or PHF.
impl<S: ?Sized> ZeroTrieWithOptions for ZeroTriePerfectHash<S>
Branch nodes could be either binary search or PHF.