#![doc(html_root_url = "https://docs.rs/byte_trie/0.3.0")]
use crate::child::Child;
use crate::keys::KeyMatch;
use std::fmt::Display;
mod child;
pub mod keys;
pub mod nodes;
#[cfg(feature = "serde")]
mod serde;
pub mod tries;
pub trait BytesTrie<T> {
fn new() -> Self;
fn insert(&mut self, key: &[u8], value: T);
}
#[derive(Debug)]
pub struct AdaptiveNode<K: BytesKey, V> {
pub(crate) key: K,
pub(crate) value: Option<V>,
pub(crate) child: Option<Child<K, V>>,
}
pub trait BytesKey: Display {
fn new(vec: Vec<u8>) -> Self;
fn from_bytes(bytes: &[u8]) -> Self;
fn get(&self) -> &[u8];
fn get_mut(&mut self) -> &mut Vec<u8>;
fn compare(&self, other: &Self) -> KeyMatch {
let prefix = self
.get()
.iter()
.zip(other.get().iter())
.take_while(|(&lhs, &rhs)| lhs == rhs)
.count();
let self_len = self.get().len();
let other_len = other.get().len();
if prefix == self_len && prefix == other_len {
KeyMatch::Exact
} else if prefix == self_len && other_len > self_len {
KeyMatch::FullSelf(prefix)
} else if prefix == other_len && self_len > other_len {
KeyMatch::FullOther(prefix)
} else if prefix > 0 {
KeyMatch::Partial(prefix)
} else {
KeyMatch::None
}
}
}
pub mod prelude {
pub use crate::keys::{BitKey, ByteKey, NibbleKey};
pub use crate::nodes::AdaptiveNode;
pub use crate::tries::{BitTrie, ByteTrie, NibbleTrie};
pub use crate::{BytesKey, BytesTrie};
}