use crate::iter::*;
use crate::TrieNode;
use crate::{SubTrie, SubTrieMut, Trie, TrieKey};
use nibble_vec::Nibblet;
pub trait TrieCommon<'a, K: 'a, V: 'a>: ContainsTrieNode<'a, K, V>
where
K: TrieKey,
Self: Sized,
{
#[inline]
fn key(self) -> Option<&'a K> {
self.trie_node().key()
}
#[inline]
fn value(self) -> Option<&'a V> {
self.trie_node().value()
}
fn len(self) -> usize;
#[inline]
fn is_empty(self) -> bool {
self.len() == 0
}
#[inline]
fn is_leaf(self) -> bool {
self.trie_node().child_count == 0
}
#[inline]
fn iter(self) -> Iter<'a, K, V> {
Iter::new(self.trie_node())
}
#[inline]
fn keys(self) -> Keys<'a, K, V> {
Keys::new(self.iter())
}
#[inline]
fn values(self) -> Values<'a, K, V> {
Values::new(self.iter())
}
fn children(self) -> Children<'a, K, V>;
#[inline]
fn prefix(self) -> &'a Nibblet {
&self.trie_node().key
}
}
pub trait ContainsTrieNode<'a, K: 'a, V: 'a>
where
K: TrieKey,
{
fn trie_node(self) -> &'a TrieNode<K, V>;
}
impl<'a, K: 'a, V: 'a> ContainsTrieNode<'a, K, V> for &'a Trie<K, V>
where
K: TrieKey,
{
#[inline]
fn trie_node(self) -> &'a TrieNode<K, V> {
&self.node
}
}
impl<'a, K: 'a, V: 'a> TrieCommon<'a, K, V> for &'a Trie<K, V>
where
K: TrieKey,
{
#[inline]
fn len(self) -> usize {
self.length
}
#[inline]
fn children(self) -> Children<'a, K, V> {
Children::new(self.node.key.clone(), &self.node)
}
}
impl<'a: 'b, 'b, K: 'a, V: 'a> ContainsTrieNode<'a, K, V> for &'b SubTrie<'a, K, V>
where
K: TrieKey,
{
#[inline]
fn trie_node(self) -> &'a TrieNode<K, V> {
self.node
}
}
impl<'a: 'b, 'b, K: 'a, V: 'a> TrieCommon<'a, K, V> for &'b SubTrie<'a, K, V>
where
K: TrieKey,
{
#[inline]
fn len(self) -> usize {
self.node.compute_size()
}
#[inline]
fn children(self) -> Children<'a, K, V> {
Children::new(self.prefix.clone(), self.node)
}
}
impl<'a, K: 'a, V: 'a> ContainsTrieNode<'a, K, V> for SubTrieMut<'a, K, V>
where
K: TrieKey,
{
#[inline]
fn trie_node(self) -> &'a TrieNode<K, V> {
self.node
}
}
impl<'a, K: 'a, V: 'a> TrieCommon<'a, K, V> for SubTrieMut<'a, K, V>
where
K: TrieKey,
{
#[inline]
fn len(self) -> usize {
self.node.compute_size()
}
#[inline]
fn children(self) -> Children<'a, K, V> {
Children::new(self.prefix.clone(), self.node)
}
}
impl<'a: 'b, 'b, K: 'a, V: 'a> ContainsTrieNode<'b, K, V> for &'b SubTrieMut<'a, K, V>
where
K: TrieKey,
{
#[inline]
fn trie_node(self) -> &'b TrieNode<K, V> {
self.node
}
}
impl<'a: 'b, 'b, K: 'a, V: 'a> TrieCommon<'b, K, V> for &'b SubTrieMut<'a, K, V>
where
K: TrieKey,
{
#[inline]
fn len(self) -> usize {
self.node.compute_size()
}
#[inline]
fn children(self) -> Children<'b, K, V> {
Children::new(self.prefix.clone(), self.node)
}
}