use super::defs::*;
use super::map::{self, RadixMap};
pub struct RadixSet {
base: RadixMap<()>,
}
impl RadixSet {
#[inline]
pub fn new() -> Self {
Default::default()
}
#[inline]
pub fn len(&self) -> usize {
self.base.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.base.is_empty()
}
#[inline]
pub fn capture<'u>(&self, path: &'u [u8]) -> (bool, Vec<(Bytes, &'u [u8])>) {
let (data, capt) = self.base.capture(path);
(data.is_some(), capt)
}
#[inline]
pub fn contains(&self, path: &[u8]) -> bool {
self.base.contains_key(path)
}
#[inline]
pub fn iter(&self) -> Iter<'_> {
Iter::from(&self.base)
}
#[inline]
pub fn insert(&mut self, path: impl Into<Bytes>) -> RadixResult<bool> {
self.base.insert(path, ()).map(|data| data.is_some())
}
#[inline]
pub fn remove(&mut self, path: &[u8]) -> bool {
self.base.remove(path).is_some()
}
#[inline]
pub fn clear(&mut self) {
self.base.clear();
}
}
impl<const N: usize> TryFrom<[Bytes; N]> for RadixSet {
type Error = RadixError;
#[inline]
fn try_from(value: [Bytes; N]) -> Result<Self, Self::Error> {
let mut set = RadixSet::default();
for path in value {
set.insert(path)?;
}
Ok(set)
}
}
impl<const N: usize> TryFrom<[&'static [u8]; N]> for RadixSet {
type Error = RadixError;
#[inline]
fn try_from(value: [&'static [u8]; N]) -> Result<Self, Self::Error> {
value.map(Bytes::from).try_into()
}
}
impl<const N: usize> TryFrom<[&'static str; N]> for RadixSet {
type Error = RadixError;
#[inline]
fn try_from(value: [&'static str; N]) -> Result<Self, Self::Error> {
value.map(Bytes::from).try_into()
}
}
impl Default for RadixSet {
#[inline]
fn default() -> Self {
Self { base: Default::default() }
}
}
impl Clone for RadixSet {
#[inline]
fn clone(&self) -> Self {
Self { base: self.base.clone() }
}
}
impl Debug for RadixSet {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}
impl Eq for RadixSet {}
impl PartialEq for RadixSet {
fn eq(&self, other: &Self) -> bool {
self.base == other.base
}
}
pub type Order = map::Order;
pub type Iter<'n> = map::Keys<'n, ()>;