#![deny(missing_docs)]
use std::borrow::Borrow;
use std::collections::hash_map::{
Drain, Entry, ExtractIf, HashMap, IntoIter, IntoKeys, IntoValues, Iter, IterMut, Keys,
RandomState, Values, ValuesMut,
};
use std::default::Default;
use std::hash::{BuildHasher, Hash};
use std::ops::Index;
#[derive(Clone, Debug)]
pub struct DefaultHashMap<K, V, S = RandomState>
where
K: Eq + Hash,
V: Default,
{
_inner: HashMap<K, V, S>,
_default: V,
}
impl<K, V> DefaultHashMap<K, V, RandomState>
where
K: Eq + Hash,
V: Default,
{
#[must_use]
pub fn new() -> Self {
Self {
_inner: HashMap::new(),
_default: V::default(),
}
}
}
impl<K, V, S> DefaultHashMap<K, V, S>
where
K: Eq + Hash,
V: Default,
S: BuildHasher,
{
#[inline]
pub fn capacity(&self) -> usize {
self._inner.capacity()
}
#[inline]
pub fn clear(&mut self) {
self._inner.clear()
}
#[inline]
pub fn contains_key(&self, key: &K) -> bool
where
K: Eq + Hash,
{
self._inner.contains_key(key)
}
#[inline]
pub fn drain(&mut self) -> Drain<'_, K, V> {
self._inner.drain()
}
#[inline]
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
self._inner.entry(key)
}
pub fn extract_if<F>(&mut self, predicate: F) -> ExtractIf<'_, K, V, F>
where
F: FnMut(&K, &mut V) -> bool,
{
self._inner.extract_if(predicate)
}
pub fn get_disjoint_mut<Q, const N: usize>(&mut self, keys: [&Q; N]) -> [Option<&mut V>; N]
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._inner.get_disjoint_mut(keys)
}
#[must_use]
pub fn get<Q>(&self, key: &Q) -> &V
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._inner.get(key).unwrap_or(&self._default)
}
#[must_use]
pub fn get_key_value<'a>(&'a self, key: &'a K) -> (&'a K, &'a V)
where
K: Eq + Hash,
{
self._inner
.get_key_value(key)
.unwrap_or((key, &self._default))
}
#[must_use]
pub fn get_mut(&mut self, key: &K) -> &mut V
where
K: Hash + Eq + Clone,
{
let exists = self._inner.keys().any(|k| key == k);
if !exists {
self.insert(key.clone(), V::default());
}
self._inner.get_mut(key).unwrap()
}
#[inline]
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
self._inner.insert(key, value)
}
#[inline]
pub fn into_keys(self) -> IntoKeys<K, V> {
self._inner.into_keys()
}
#[inline]
pub fn into_values(self) -> IntoValues<K, V> {
self._inner.into_values()
}
#[inline]
pub fn is_empty(&self) -> bool {
self._inner.is_empty()
}
#[inline]
pub fn keys(&self) -> Keys<'_, K, V> {
self._inner.keys()
}
#[inline]
pub fn len(&self) -> usize {
self._inner.len()
}
#[must_use]
pub fn remove<Q>(&mut self, key: &Q) -> V
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._inner.remove(key).unwrap_or_default()
}
#[must_use]
pub fn remove_entry(&mut self, key: &K) -> (K, V)
where
K: Clone,
V: Clone,
{
self._inner
.remove_entry(key)
.unwrap_or((key.clone(), self._default.to_owned()))
}
pub fn retain<F>(&mut self, func: F)
where
F: FnMut(&K, &mut V) -> bool,
{
self._inner.retain(func);
}
#[inline]
pub fn values(&self) -> Values<'_, K, V> {
self._inner.values()
}
#[inline]
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
self._inner.values_mut()
}
#[inline]
pub fn with_hasher(hash_builder: S) -> Self {
DefaultHashMap {
_inner: HashMap::with_hasher(hash_builder),
_default: V::default(),
}
}
}
impl<K, V> Default for DefaultHashMap<K, V>
where
K: Eq + Hash,
V: Default,
{
fn default() -> Self {
Self::new()
}
}
impl<K, V, S> PartialEq for DefaultHashMap<K, V, S>
where
K: Eq + Hash,
V: PartialEq + Default,
S: BuildHasher,
{
fn eq(&self, other: &DefaultHashMap<K, V, S>) -> bool {
self._inner == other._inner && self._default == other._default
}
}
impl<K, V, S> Eq for DefaultHashMap<K, V, S>
where
K: Eq + Hash,
V: Eq + Default,
S: BuildHasher,
{
}
impl<K, V, S> IntoIterator for DefaultHashMap<K, V, S>
where
K: Eq + Hash + Clone,
V: Default,
S: BuildHasher,
{
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
self._inner.into_iter()
}
}
impl<'a, K, V, S> IntoIterator for &'a DefaultHashMap<K, V, S>
where
K: Eq + Hash,
V: Default,
S: BuildHasher,
{
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self._inner.iter()
}
}
impl<K, V, S> Index<&K> for DefaultHashMap<K, V, S>
where
K: Eq + Hash,
V: Default,
S: BuildHasher,
{
type Output = V;
fn index(&self, key: &K) -> &V {
self._inner.get(key).unwrap_or(&self._default)
}
}
impl<'a, K, V, S> IntoIterator for &'a mut DefaultHashMap<K, V, S>
where
K: Eq + Hash,
V: Default,
S: BuildHasher,
{
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self._inner.iter_mut()
}
}
impl<K, V, S> From<HashMap<K, V, S>> for DefaultHashMap<K, V, S>
where
K: Eq + Hash,
V: Default,
S: BuildHasher,
{
fn from(hashmap: HashMap<K, V, S>) -> Self {
Self {
_inner: hashmap,
_default: V::default(),
}
}
}
impl<K, V, S> From<DefaultHashMap<K, V, S>> for HashMap<K, V, S>
where
K: Eq + Hash,
V: Default,
S: BuildHasher,
{
fn from(hashmap: DefaultHashMap<K, V, S>) -> Self {
hashmap._inner
}
}
impl<K, V, S> FromIterator<(K, V)> for DefaultHashMap<K, V, S>
where
K: Eq + Hash,
V: Default,
S: BuildHasher + Default,
{
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
let mut map = DefaultHashMap::with_hasher(Default::default());
for (k, v) in iter {
let _ = map.insert(k, v);
}
map
}
}
#[macro_export]
macro_rules! defaulthashmap {
( ) => {
{
DefaultHashMap::new()
}
};
( $( ($key:expr, $val:expr) ),* $(,)? ) => {
{
let mut map = DefaultHashMap::new();
$(
let _ = map.insert($key, $val);
)*
map
}
};
( $( $key:expr ),* $(,)? ) => {
{
let mut map = DefaultHashMap::new();
$(
let _ = map.get_mut(&$key);
)*
map
}
};
}