use dashmap::DashMap;
use std::hash::Hash;
use crate::{PositionalHash, PositionalSequenceHash};
#[derive(Clone)]
pub struct PositionalRadixTree<V, K = PositionalSequenceHash>
where
K: PositionalHash + Hash + Eq + Clone,
{
map: DashMap<u64, DashMap<K, V>>,
}
impl<V, K> PositionalRadixTree<V, K>
where
K: PositionalHash + Hash + Eq + Clone,
{
pub fn new() -> Self {
Self {
map: DashMap::new(),
}
}
pub fn prefix(&self, key: &K) -> dashmap::mapref::one::RefMut<'_, u64, DashMap<K, V>> {
let position = key.position();
self.map.entry(position).or_default()
}
pub fn position(
&self,
position: u64,
) -> Option<dashmap::mapref::one::RefMut<'_, u64, DashMap<K, V>>> {
self.map.get_mut(&position)
}
pub fn len(&self) -> usize {
if self.map.is_empty() {
return 0;
}
self.map.iter().map(|level| level.len()).sum()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<V, K> Default for PositionalRadixTree<V, K>
where
K: PositionalHash + Hash + Eq + Clone,
{
fn default() -> Self {
Self {
map: DashMap::new(),
}
}
}