use alloc::borrow::ToOwned;
use core::borrow::Borrow;
use core::fmt;
use core::iter::FromIterator;
use core::ops::{Index, IndexMut};
use entry::{make_entry, Entry};
use iter::{IntoIter, Iter, IterMut, Keys, Values, ValuesMut};
use node::{Leaf, Node};
use subtrie::SubTrie;
use util::nybble_mismatch;
use wrapper::{BStr, BString};
#[derive(Clone, PartialEq, Eq)]
pub struct Trie<K, V> {
root: Option<Node<K, V>>,
count: usize,
}
impl<K, V> Default for Trie<K, V> {
fn default() -> Self {
Trie::new()
}
}
impl<K: fmt::Debug + ToOwned, V: fmt::Debug> fmt::Debug for Trie<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.root {
Some(ref node) => f.debug_map().entries(node.iter()).finish(),
None => f.debug_map().finish(),
}
}
}
impl<K, V> IntoIterator for Trie<K, V> {
type IntoIter = IntoIter<K, V>;
type Item = (K, V);
fn into_iter(self) -> Self::IntoIter {
self.root
.map(Node::into_iter)
.unwrap_or_else(IntoIter::default)
}
}
impl<K: Borrow<[u8]>, V> FromIterator<(K, V)> for Trie<K, V> {
fn from_iter<I>(iterable: I) -> Trie<K, V>
where
I: IntoIterator<Item = (K, V)>,
{
let mut trie = Trie::new();
for (key, val) in iterable {
trie.insert(key, val);
}
trie
}
}
impl<K: Borrow<[u8]>, V> Extend<(K, V)> for Trie<K, V> {
fn extend<I>(&mut self, iterable: I)
where
I: IntoIterator<Item = (K, V)>,
{
for (key, val) in iterable {
self.insert(key, val);
}
}
}
impl<K, V> Trie<K, V> {
pub fn new() -> Trie<K, V> {
Trie {
root: None,
count: 0,
}
}
pub fn iter(&self) -> Iter<K, V> {
match self.root {
Some(ref node) => Iter::new(node),
None => Iter::default(),
}
}
pub fn iter_mut(&mut self) -> IterMut<K, V> {
match self.root {
Some(ref mut node) => IterMut::new(node),
None => IterMut::default(),
}
}
pub fn keys(&self) -> Keys<K, V> {
match self.root {
Some(ref node) => Keys::new(node),
None => Keys::default(),
}
}
pub fn values(&self) -> Values<K, V> {
match self.root {
Some(ref node) => Values::new(node),
None => Values::default(),
}
}
pub fn values_mut(&mut self) -> ValuesMut<K, V> {
match self.root {
Some(ref mut node) => ValuesMut::new(node),
None => ValuesMut::default(),
}
}
pub fn clear(&mut self) {
self.root = None;
self.count = 0;
}
pub fn is_empty(&self) -> bool {
self.root.is_none()
}
}
impl<K: Borrow<[u8]>, V> Trie<K, V> {
pub fn iter_prefix<'a, Q: ?Sized>(&'a self, prefix: &Q) -> Iter<'a, K, V>
where
K: Borrow<Q>,
Q: Borrow<[u8]>,
{
match self
.root
.as_ref()
.and_then(|node| node.get_prefix(prefix.borrow()))
{
Some(node) => Iter::new(node),
None => Iter::default(),
}
}
pub fn iter_prefix_mut<'a, Q: ?Sized>(&'a mut self, prefix: &Q) -> IterMut<'a, K, V>
where
K: Borrow<Q>,
Q: Borrow<[u8]>,
{
match self
.root
.as_mut()
.and_then(|node| node.get_prefix_mut(prefix.borrow()))
{
Some(node) => IterMut::new(node),
None => IterMut::default(),
}
}
pub fn subtrie<'a, Q: ?Sized>(&'a self, prefix: &Q) -> SubTrie<'a, K, V>
where
K: Borrow<Q>,
Q: Borrow<[u8]>,
{
SubTrie {
root: self
.root
.as_ref()
.and_then(|node| node.get_prefix(prefix.borrow())),
}
}
pub fn longest_common_prefix<'a, Q: ?Sized>(&'a self, key: &Q) -> &'a K::Split
where
K: Borrow<Q> + Break,
Q: Borrow<[u8]>,
{
match self.root.as_ref() {
Some(root) => {
let exemplar = root.get_exemplar(key.borrow());
match nybble_mismatch(exemplar.key_slice(), key.borrow()) {
Some(i) => exemplar.key.find_break(i / 2),
None => exemplar.key.borrow(),
}
}
None => K::empty(),
}
}
pub fn count(&self) -> usize {
self.count
}
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Borrow<[u8]>,
{
self.root
.as_ref()
.and_then(|node| node.get(key.borrow()))
.is_some()
}
pub fn get<'a, Q: ?Sized>(&'a self, key: &Q) -> Option<&'a V>
where
K: Borrow<Q>,
Q: Borrow<[u8]>,
{
self.root
.as_ref()
.and_then(|node| node.get(key.borrow()))
.map(|leaf| &leaf.val)
}
pub fn get_mut<'a, Q: ?Sized>(&'a mut self, key: &Q) -> Option<&'a mut V>
where
K: Borrow<Q>,
Q: Borrow<[u8]>,
{
self.root
.as_mut()
.and_then(|node| node.get_mut(key.borrow()))
.map(|leaf| &mut leaf.val)
}
pub fn insert(&mut self, key: K, val: V) -> Option<V> {
match self.root {
Some(ref mut root) => {
let old = root.insert(key, val);
if old.is_none() {
self.count += 1;
}
old
}
None => {
self.root = Some(Node::Leaf(Leaf::new(key, val)));
self.count += 1;
None
}
}
}
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Borrow<[u8]>,
{
let node = Node::remove(&mut self.root, key.borrow()).map(|leaf| leaf.val);
if node.is_some() {
self.count -= 1;
}
node
}
pub fn remove_prefix<Q: ?Sized>(&mut self, prefix: &Q) -> Trie<K, V>
where
K: Borrow<Q>,
Q: Borrow<[u8]>,
{
let root = Node::remove_prefix(&mut self.root, prefix.borrow());
let count = root.as_ref().map(Node::count).unwrap_or(0);
self.count -= count;
Trie { root, count }
}
pub fn entry(&mut self, key: K) -> Entry<K, V> {
make_entry(key, &mut self.root, &mut self.count)
}
}
impl<'a, K: Borrow<[u8]>, V, Q: ?Sized> Index<&'a Q> for Trie<K, V>
where
K: Borrow<Q>,
Q: Borrow<[u8]>,
{
type Output = V;
fn index(&self, key: &Q) -> &V {
self.get(key).unwrap()
}
}
impl<'a, K: Borrow<[u8]>, V, Q: ?Sized> IndexMut<&'a Q> for Trie<K, V>
where
K: Borrow<Q>,
Q: Borrow<[u8]>,
{
fn index_mut(&mut self, key: &Q) -> &mut V {
self.get_mut(key).unwrap()
}
}
pub trait Break: Borrow<<Self as Break>::Split> {
type Split: ?Sized;
fn empty<'a>() -> &'a Self::Split;
fn find_break(&self, loc: usize) -> &Self::Split;
}
impl Break for [u8] {
type Split = [u8];
#[inline]
fn empty<'a>() -> &'a [u8] {
<&'a [u8]>::default()
}
#[inline]
fn find_break(&self, loc: usize) -> &[u8] {
&self[..loc]
}
}
impl<'b> Break for &'b [u8] {
type Split = [u8];
#[inline]
fn empty<'a>() -> &'a [u8] {
<&'a [u8]>::default()
}
#[inline]
fn find_break(&self, loc: usize) -> &[u8] {
&self[..loc]
}
}
impl<V> Trie<BString, V> {
pub fn iter_prefix_str<'a, Q: ?Sized>(&'a self, key: &Q) -> Iter<'a, BString, V>
where
Q: Borrow<str>,
{
self.iter_prefix(AsRef::<BStr>::as_ref(key.borrow()))
}
pub fn iter_prefix_mut_str<'a, Q: ?Sized>(&'a mut self, key: &Q) -> IterMut<'a, BString, V>
where
Q: Borrow<str>,
{
self.iter_prefix_mut(AsRef::<BStr>::as_ref(key.borrow()))
}
pub fn subtrie_str<'a, Q: ?Sized>(&'a self, prefix: &Q) -> SubTrie<'a, BString, V>
where
Q: Borrow<str>,
{
self.subtrie(AsRef::<BStr>::as_ref(prefix.borrow()))
}
pub fn contains_key_str<Q: ?Sized>(&self, key: &Q) -> bool
where
Q: Borrow<str>,
{
self.contains_key(AsRef::<BStr>::as_ref(key.borrow()))
}
pub fn get_str<'a, Q: ?Sized>(&'a self, key: &Q) -> Option<&'a V>
where
Q: Borrow<str>,
{
self.get(AsRef::<BStr>::as_ref(key.borrow()))
}
pub fn get_mut_str<'a, Q: ?Sized>(&'a mut self, key: &Q) -> Option<&'a mut V>
where
Q: Borrow<str>,
{
self.get_mut(AsRef::<BStr>::as_ref(key.borrow()))
}
pub fn insert_str<Q: ?Sized>(&mut self, key: &Q, val: V) -> Option<V>
where
Q: Borrow<str>,
{
self.insert(key.borrow().into(), val)
}
pub fn remove_str<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where
Q: Borrow<str>,
{
self.remove(AsRef::<BStr>::as_ref(key.borrow()))
}
pub fn remove_prefix_str<Q: ?Sized>(&mut self, prefix: &Q) -> Trie<BString, V>
where
Q: Borrow<str>,
{
self.remove_prefix(AsRef::<BStr>::as_ref(prefix.borrow()))
}
}