use std::{
collections::BTreeMap,
fmt::Display,
ops::{Index, IndexMut},
};
use crate::detail;
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash)]
pub struct Dict<K, V> {
data: BTreeMap<K, V>,
}
impl<K: Ord, V> Dict<K, V> {
pub fn new() -> Self {
Self { data: BTreeMap::new() }
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn iter(&self) -> std::collections::btree_map::Iter<'_, K, V> {
self.data.iter()
}
pub fn find(&self, key: &K) -> Option<(&K, &V)> {
self.data.iter().find(|p| p.0 == key)
}
pub fn contains(&self, key: &K) -> bool {
self.data.contains_key(key)
}
pub fn get<'a>(&'a self, key: &K, defaults: &'a V) -> &'a V {
self.data.get(key).unwrap_or(defaults)
}
pub fn keys(&self) -> std::collections::btree_map::Keys<'_, K, V> {
self.data.keys()
}
pub fn values(&self) -> std::collections::btree_map::Values<'_, K, V> {
self.data.values()
}
pub fn add(&mut self, key: K, value: V) -> bool {
self.data.insert(key, value).is_none()
}
pub fn remove(&mut self, key: &K) -> bool {
self.data.remove(key).is_some()
}
pub fn pop(&mut self) -> Option<(K, V)> {
self.data.pop_first()
}
pub fn clear(&mut self) {
self.data.clear()
}
}
impl<K: Ord, V, const N: usize> From<[(K, V); N]> for Dict<K, V> {
fn from(value: [(K, V); N]) -> Self {
Self { data: BTreeMap::from(value) }
}
}
impl<K: Ord, V> Index<&K> for Dict<K, V> {
type Output = V;
fn index(&self, key: &K) -> &Self::Output {
if !self.contains(key) {
panic!("Error: Key is not found in the dict.");
}
&self.data[key]
}
}
impl<K: Ord, V> IndexMut<&K> for Dict<K, V> {
fn index_mut(&mut self, key: &K) -> &mut Self::Output {
if !self.contains(key) {
panic!("Error: Key is not found in the dict.");
}
&mut *self.data.get_mut(key).unwrap()
}
}
impl<K: Ord, V> Extend<(K, V)> for Dict<K, V> {
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
self.data.extend(iter)
}
}
struct Pair<K, V>(K, V);
impl<K: Display, V: Display> Display for Pair<K, V> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}: {}", self.0, self.1)
}
}
impl<K: Display, V: Display> Display for Dict<K, V> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
detail::print(f, self.data.iter().map(|p| Pair(p.0, p.1)), '{', '}')
}
}
impl<K: Ord, V> FromIterator<(K, V)> for Dict<K, V> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let data = iter.into_iter().collect();
Self { data }
}
}
impl<K, V> IntoIterator for Dict<K, V> {
type Item = (K, V);
type IntoIter = std::collections::btree_map::IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}