mod raw;
pub use raw::RawEntry;
use crate::OrdBy;
use std::hash::Hash;
pub enum Entry<'v, T, K, V>
where
T: Ord + Clone,
K: Hash + Eq,
V: OrdBy<Target = T>,
{
Occupied(RawEntry<'v, T, K, V>),
Vacant(RawEntry<'v, T, K, V>),
}
impl<'v, T, K, V> Entry<'v, T, K, V>
where
T: Ord + Clone,
K: Hash + Eq,
V: OrdBy<Target = T>,
{
pub fn or_insert(&mut self, default: V) -> &mut V {
match self {
Entry::Occupied(entry) => entry.get_mut_with_key().1,
Entry::Vacant(entry) => {
entry.valord.free_indexs.pop_front();
entry.insert(default)
}
}
}
pub fn or_insert_with<F: FnOnce() -> V>(&mut self, default: F) -> &mut V {
match self {
Entry::Occupied(entry) => entry.get_mut_with_key().1,
Entry::Vacant(entry) => {
entry.valord.free_indexs.pop_front();
entry.insert(default())
}
}
}
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(&mut self, default: F) -> &mut V {
match self {
Entry::Occupied(entry) => entry.get_mut_with_key().1,
Entry::Vacant(entry) => {
entry.valord.free_indexs.pop_front();
entry.insert_with_key(default)
}
}
}
pub fn and_modify<F>(mut self, f: F) -> Self
where
F: FnOnce(&mut V),
{
if let Entry::Occupied(entry) = &mut self {
f(entry.get_mut_with_key().1);
}
self
}
}
impl<'v, T, K, V> Entry<'v, T, K, V>
where
T: Ord + Clone,
K: Hash + Eq,
V: OrdBy<Target = T> + Default,
{
pub fn or_default(&mut self) -> &mut V {
match self {
Entry::Occupied(entry) => entry.get_mut_with_key().1,
Entry::Vacant(entry) => {
entry.valord.free_indexs.pop_front();
entry.insert(V::default())
}
}
}
}