use super::defs::*;
use super::node::{self, RadixNode};
pub struct RadixMap<V> {
root: RadixNode<V>,
size: usize,
}
impl<V> RadixMap<V> {
#[inline]
pub fn new() -> Self {
Default::default()
}
#[inline]
pub fn len(&self) -> usize {
self.size
}
#[inline]
pub fn is_empty(&self) -> bool {
self.size == 0
}
#[inline]
pub fn get(&self, path: &[u8]) -> Option<&V> {
self.root.lookup(path, true, false, &mut vec![], false).and_then(|node| node.data.as_ref())
}
#[inline]
pub fn get_mut(&mut self, path: &[u8]) -> Option<&mut V> {
self.root.lookup_mut(path, true, false, &mut vec![], false).and_then(|node| node.data.as_mut())
}
#[inline]
pub fn raw(&self, path: &[u8]) -> Option<&V> {
self.root.lookup(path, true, true, &mut vec![], false).and_then(|node| node.data.as_ref())
}
#[inline]
pub fn raw_mut(&mut self, path: &[u8]) -> Option<&mut V> {
self.root.lookup_mut(path, true, true, &mut vec![], false).and_then(|node| node.data.as_mut())
}
#[inline]
pub fn capture<'u>(&self, path: &'u [u8]) -> (Option<&V>, Vec<(Bytes, &'u [u8])>) {
let mut capt = vec![];
let node = self.root.lookup(path, true, false, &mut capt, true);
if node.is_none() {
capt.clear();
}
(node.and_then(|n| n.data.as_ref()), capt)
}
#[inline]
pub fn capture_mut<'u>(&mut self, path: &'u [u8]) -> (Option<&mut V>, Vec<(Bytes, &'u [u8])>) {
let mut capt = vec![];
let node = self.root.lookup_mut(path, true, false, &mut capt, true);
if node.is_none() {
capt.clear();
}
(node.and_then(|n| n.data.as_mut()), capt)
}
#[inline]
pub fn contains_key(&self, path: &[u8]) -> bool {
self.root.lookup(path, true, false, &mut vec![], false).map_or(false, |node| !node.is_empty())
}
#[inline]
pub fn contains_value(&self, data: &V) -> bool where V: PartialEq {
self.values().any(|value| value == data)
}
#[inline]
pub fn iter(&self) -> Iter<V> {
Iter::from(self)
}
#[inline]
pub fn iter_mut(&mut self) -> IterMut<V> {
IterMut::from(self)
}
#[inline]
pub fn keys(&self) -> Keys<V> {
Keys::from(self)
}
#[inline]
pub fn values(&self) -> Values<V> {
Values::from(self)
}
#[inline]
pub fn values_mut(&mut self) -> ValuesMut<V> {
ValuesMut::from(self)
}
#[inline]
pub fn insert(&mut self, path: impl Into<Bytes>, data: V) -> RadixResult<Option<V>> {
let ret = self.root.insert(path, data);
if let Ok(None) = &ret {
self.size += 1;
}
ret
}
#[inline]
pub fn remove(&mut self, path: &[u8]) -> Option<(Bytes, V)> {
let node = self.root.lookup_mut(path, true, false, &mut vec![], false)?;
let path = std::mem::take(&mut node.path);
let data = std::mem::take(&mut node.data);
self.size -= 1;
Some((path, data?))
}
#[inline]
pub fn clear(&mut self) {
self.root.clear();
self.size = 0;
}
}
impl<V, const N: usize> TryFrom<[(Bytes, V); N]> for RadixMap<V> {
type Error = RadixError;
#[inline]
fn try_from(value: [(Bytes, V); N]) -> Result<Self, Self::Error> {
let mut map = RadixMap::default();
for (path, data) in value {
map.insert(path, data)?;
}
Ok(map)
}
}
impl<V, const N: usize> TryFrom<[(&'static [u8], V); N]> for RadixMap<V> {
type Error = RadixError;
#[inline]
fn try_from(value: [(&'static [u8], V); N]) -> Result<Self, Self::Error> {
value.map(|(k, v)| (Bytes::from(k), v)).try_into()
}
}
impl<V, const N: usize> TryFrom<[(&'static str, V); N]> for RadixMap<V> {
type Error = RadixError;
#[inline]
fn try_from(value: [(&'static str, V); N]) -> Result<Self, Self::Error> {
value.map(|(k, v)| (Bytes::from(k), v)).try_into()
}
}
impl<V> Default for RadixMap<V> {
#[inline]
fn default() -> Self {
Self { root: RadixNode::default(), size: 0 }
}
}
impl<V: Clone> Clone for RadixMap<V> {
#[inline]
fn clone(&self) -> Self {
Self { root: self.root.clone(), size: self.size }
}
}
impl<V: Debug> Debug for RadixMap<V> {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
}
impl<V: Eq> Eq for RadixMap<V> {}
impl<V: PartialEq> PartialEq for RadixMap<V> {
fn eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}
let mut iter_a = self.iter();
let mut iter_b = other.iter();
for _ in 0..self.len() {
let item_a = iter_a.next();
let item_b = iter_b.next();
if item_a != item_b {
return false;
}
}
true
}
}
impl<V> Index<&[u8]> for RadixMap<V> {
type Output = V;
fn index(&self, path: &[u8]) -> &Self::Output {
self.get(path).unwrap_or_else(|| panic!("path not found"))
}
}
impl<V> IndexMut<&[u8]> for RadixMap<V> {
fn index_mut(&mut self, path: &[u8]) -> &mut Self::Output {
self.get_mut(path).unwrap_or_else(|| panic!("path not found"))
}
}
pub type Order = node::Order;
#[derive(Default, Clone)]
pub struct Iter<'n, V> {
iter: node::Iter<'n, V>
}
impl<'n, V> Iter<'n, V> {
#[inline]
pub fn with_prefix(mut self, path: &[u8], data: bool) -> Self {
self.iter = self.iter.with_prefix(path, data);
self
}
#[inline]
pub fn with_order(mut self, order: Order) -> Self {
self.iter = self.iter.with_order(order);
self
}
}
impl<'n, V> From<&'n RadixMap<V>> for Iter<'n, V> {
#[inline]
fn from(value: &'n RadixMap<V>) -> Self {
Self { iter: node::Iter::from(&value.root) }
}
}
impl<'n, V> Iterator for Iter<'n, V> {
type Item = (&'n Bytes, &'n V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().and_then(|node| node.item_ref())
}
}
#[derive(Default)]
pub struct IterMut<'n, V> {
iter: node::IterMut<'n, V>
}
impl<'n, V> IterMut<'n, V> {
#[inline]
pub fn with_prefix(mut self, path: &[u8], data: bool) -> Self {
self.iter = self.iter.with_prefix(path, data);
self
}
#[inline]
pub fn with_order(mut self, order: Order) -> Self {
self.iter = self.iter.with_order(order);
self
}
}
impl<'n, V> From<&'n mut RadixMap<V>> for IterMut<'n, V> {
#[inline]
fn from(value: &'n mut RadixMap<V>) -> Self {
Self { iter: node::IterMut::from(&mut value.root) }
}
}
impl<'n, V> Iterator for IterMut<'n, V> {
type Item = (&'n Bytes, &'n mut V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().and_then(|node| node.item_mut())
}
}
#[derive(Default, Clone)]
pub struct Keys<'n, V> {
iter: Iter<'n, V>
}
impl<'n, V> Keys<'n, V> {
#[inline]
pub fn with_prefix(mut self, path: &[u8], data: bool) -> Self {
self.iter = self.iter.with_prefix(path, data);
self
}
#[inline]
pub fn with_order(mut self, order: Order) -> Self {
self.iter = self.iter.with_order(order);
self
}
}
impl<'n, V> From<&'n RadixMap<V>> for Keys<'n, V> {
#[inline]
fn from(value: &'n RadixMap<V>) -> Self {
Self { iter: Iter::from(value) }
}
}
impl<'n, V> Iterator for Keys<'n, V> {
type Item = &'n Bytes;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|item| item.0)
}
}
#[derive(Default, Clone)]
pub struct Values<'n, V> {
iter: Iter<'n, V>
}
impl<'n, V> Values<'n, V> {
#[inline]
pub fn with_prefix(mut self, path: &[u8], data: bool) -> Self {
self.iter = self.iter.with_prefix(path, data);
self
}
#[inline]
pub fn with_order(mut self, order: Order) -> Self {
self.iter = self.iter.with_order(order);
self
}
}
impl<'n, V> From<&'n RadixMap<V>> for Values<'n, V> {
#[inline]
fn from(value: &'n RadixMap<V>) -> Self {
Self { iter: Iter::from(value) }
}
}
impl<'n, V> Iterator for Values<'n, V> {
type Item = &'n V;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|item| item.1)
}
}
#[derive(Default)]
pub struct ValuesMut<'n, V> {
iter: IterMut<'n, V>
}
impl<'n, V> ValuesMut<'n, V> {
#[inline]
pub fn with_prefix(mut self, path: &[u8], data: bool) -> Self {
self.iter = self.iter.with_prefix(path, data);
self
}
#[inline]
pub fn with_order(mut self, order: Order) -> Self {
self.iter = self.iter.with_order(order);
self
}
}
impl<'n, V> From<&'n mut RadixMap<V>> for ValuesMut<'n, V> {
#[inline]
fn from(value: &'n mut RadixMap<V>) -> Self {
Self { iter: IterMut::from(value) }
}
}
impl<'n, V> Iterator for ValuesMut<'n, V> {
type Item = &'n mut V;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|item| item.1)
}
}