use std::{borrow::Borrow, ops::RangeBounds};
use indexmap::IndexSet;
use smallvec_wrapper::TinyVec;
use super::{types::*, *};
mod hash_cm;
pub use hash_cm::*;
mod btree_cm;
pub use btree_cm::*;
pub type DefaultHasher = std::hash::DefaultHasher;
pub struct Marker<'a, C> {
marker: &'a mut C,
}
impl<'a, C> Marker<'a, C> {
#[inline]
pub fn new(marker: &'a mut C) -> Self {
Self { marker }
}
}
impl<'a, C: Cm> Marker<'a, C> {
pub fn mark(&mut self, k: &C::Key) {
self.marker.mark_read(k);
}
}
pub trait Cm: Sized {
type Error: std::error::Error;
type Key;
type Options;
fn new(options: Self::Options) -> Result<Self, Self::Error>;
fn mark_read(&mut self, key: &Self::Key);
fn mark_conflict(&mut self, key: &Self::Key);
fn has_conflict(&self, other: &Self) -> bool;
fn rollback(&mut self) -> Result<(), Self::Error>;
}
pub trait CmEquivalent: Cm {
fn mark_read_equivalent<Q>(&mut self, key: &Q)
where
Self::Key: core::borrow::Borrow<Q>,
Q: core::hash::Hash + Eq + ?Sized;
fn mark_conflict_equivalent<Q>(&mut self, key: &Q)
where
Self::Key: core::borrow::Borrow<Q>,
Q: core::hash::Hash + Eq + ?Sized;
}
pub trait CmComparable: Cm {
fn mark_read_comparable<Q>(&mut self, key: &Q)
where
Self::Key: core::borrow::Borrow<Q>,
Q: Ord + ?Sized;
fn mark_conflict_comparable<Q>(&mut self, key: &Q)
where
Self::Key: core::borrow::Borrow<Q>,
Q: Ord + ?Sized;
}
pub trait Pwm: Sized {
type Error: std::error::Error;
type Key;
type Value;
type Iter<'a>: Iterator<Item = (&'a Self::Key, &'a EntryValue<Self::Value>)>
where
Self: 'a;
type IntoIter: Iterator<Item = (Self::Key, EntryValue<Self::Value>)>;
type Options;
fn new(options: Self::Options) -> Result<Self, Self::Error>;
fn is_empty(&self) -> bool;
fn len(&self) -> usize;
fn validate_entry(&self, entry: &Entry<Self::Key, Self::Value>) -> Result<(), Self::Error>;
fn max_batch_size(&self) -> u64;
fn max_batch_entries(&self) -> u64;
fn estimate_size(&self, entry: &Entry<Self::Key, Self::Value>) -> u64;
fn get(&self, key: &Self::Key) -> Result<Option<&EntryValue<Self::Value>>, Self::Error>;
fn contains_key(&self, key: &Self::Key) -> Result<bool, Self::Error>;
fn insert(&mut self, key: Self::Key, value: EntryValue<Self::Value>) -> Result<(), Self::Error>;
fn remove_entry(
&mut self,
key: &Self::Key,
) -> Result<Option<(Self::Key, EntryValue<Self::Value>)>, Self::Error>;
fn iter(&self) -> Self::Iter<'_>;
fn into_iter(self) -> Self::IntoIter;
fn rollback(&mut self) -> Result<(), Self::Error>;
}
pub trait PwmRange: Pwm {
type Range<'a>: IntoIterator<Item = (&'a Self::Key, &'a EntryValue<Self::Value>)>
where
Self: 'a;
fn range<R: RangeBounds<Self::Key>>(&self, range: R) -> Self::Range<'_>;
}
pub trait PwmComparableRange: PwmRange + PwmComparable {
fn range_comparable<T, R>(&self, range: R) -> Self::Range<'_>
where
T: ?Sized + Ord,
Self::Key: Borrow<T> + Ord,
R: RangeBounds<T>;
}
pub trait PwmEquivalentRange: PwmRange + PwmEquivalent {
fn range_equivalent<T, R>(&self, range: R) -> Self::Range<'_>
where
T: ?Sized + Eq + core::hash::Hash,
Self::Key: Borrow<T> + Eq + core::hash::Hash,
R: RangeBounds<T>;
}
pub trait PwmEquivalent: Pwm {
fn get_equivalent<Q>(&self, key: &Q) -> Result<Option<&EntryValue<Self::Value>>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: core::hash::Hash + Eq + ?Sized;
fn get_entry_equivalent<Q>(
&self,
key: &Q,
) -> Result<Option<(&Self::Key, &EntryValue<Self::Value>)>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: core::hash::Hash + Eq + ?Sized;
fn contains_key_equivalent<Q>(&self, key: &Q) -> Result<bool, Self::Error>
where
Self::Key: Borrow<Q>,
Q: core::hash::Hash + Eq + ?Sized;
fn remove_entry_equivalent<Q>(
&mut self,
key: &Q,
) -> Result<Option<(Self::Key, EntryValue<Self::Value>)>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: core::hash::Hash + Eq + ?Sized;
}
pub trait PwmComparable: Pwm {
fn get_comparable<Q>(&self, key: &Q) -> Result<Option<&EntryValue<Self::Value>>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
fn get_entry_comparable<Q>(
&self,
key: &Q,
) -> Result<Option<(&Self::Key, &EntryValue<Self::Value>)>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
fn contains_key_comparable<Q>(&self, key: &Q) -> Result<bool, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
fn remove_entry_comparable<Q>(
&mut self,
key: &Q,
) -> Result<Option<(Self::Key, EntryValue<Self::Value>)>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
}
pub type IndexMapManager<K, V, S = std::hash::RandomState> = IndexMap<K, EntryValue<V>, S>;
pub type BTreeMapManager<K, V> = BTreeMap<K, EntryValue<V>>;
impl<K, V, S> Pwm for IndexMap<K, EntryValue<V>, S>
where
K: Eq + core::hash::Hash,
S: BuildHasher + Default,
{
type Error = std::convert::Infallible;
type Key = K;
type Value = V;
type Iter<'a> = indexmap::map::Iter<'a, K, EntryValue<V>> where Self: 'a;
type IntoIter = indexmap::map::IntoIter<K, EntryValue<V>>;
type Options = Option<S>;
fn new(options: Self::Options) -> Result<Self, Self::Error> {
Ok(match options {
Some(hasher) => Self::with_hasher(hasher),
None => Self::default(),
})
}
fn is_empty(&self) -> bool {
self.is_empty()
}
fn len(&self) -> usize {
self.len()
}
fn validate_entry(&self, _entry: &Entry<Self::Key, Self::Value>) -> Result<(), Self::Error> {
Ok(())
}
fn max_batch_size(&self) -> u64 {
u64::MAX
}
fn max_batch_entries(&self) -> u64 {
u64::MAX
}
fn estimate_size(&self, _entry: &Entry<Self::Key, Self::Value>) -> u64 {
core::mem::size_of::<Self::Key>() as u64 + core::mem::size_of::<Self::Value>() as u64
}
fn get(&self, key: &K) -> Result<Option<&EntryValue<V>>, Self::Error> {
Ok(self.get(key))
}
fn contains_key(&self, key: &K) -> Result<bool, Self::Error> {
Ok(self.contains_key(key))
}
fn insert(&mut self, key: K, value: EntryValue<V>) -> Result<(), Self::Error> {
self.insert(key, value);
Ok(())
}
fn remove_entry(&mut self, key: &K) -> Result<Option<(K, EntryValue<V>)>, Self::Error> {
Ok(self.shift_remove_entry(key))
}
fn iter(&self) -> Self::Iter<'_> {
IndexMap::iter(self)
}
fn into_iter(self) -> Self::IntoIter {
core::iter::IntoIterator::into_iter(self)
}
fn rollback(&mut self) -> Result<(), Self::Error> {
self.clear();
Ok(())
}
}
impl<K, V, S> PwmEquivalent for IndexMap<K, EntryValue<V>, S>
where
K: Eq + core::hash::Hash,
S: BuildHasher + Default,
{
fn get_equivalent<Q>(&self, key: &Q) -> Result<Option<&EntryValue<V>>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: core::hash::Hash + Eq + ?Sized,
{
Ok(self.get(key))
}
fn get_entry_equivalent<Q>(
&self,
key: &Q,
) -> Result<Option<(&Self::Key, &EntryValue<Self::Value>)>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: core::hash::Hash + Eq + ?Sized,
{
Ok(self.get_full(key).map(|(_, k, v)| (k, v)))
}
fn contains_key_equivalent<Q>(&self, key: &Q) -> Result<bool, Self::Error>
where
Self::Key: Borrow<Q>,
Q: core::hash::Hash + Eq + ?Sized,
{
Ok(self.contains_key(key))
}
fn remove_entry_equivalent<Q>(
&mut self,
key: &Q,
) -> Result<Option<(K, EntryValue<V>)>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: core::hash::Hash + Eq + ?Sized,
{
Ok(self.shift_remove_entry(key))
}
}
impl<K, V> Pwm for BTreeMap<K, EntryValue<V>>
where
K: Ord,
{
type Error = std::convert::Infallible;
type Key = K;
type Value = V;
type Iter<'a> = std::collections::btree_map::Iter<'a, K, EntryValue<V>> where Self: 'a;
type IntoIter = std::collections::btree_map::IntoIter<K, EntryValue<V>>;
type Options = ();
fn new(_: Self::Options) -> Result<Self, Self::Error> {
Ok(Self::default())
}
fn is_empty(&self) -> bool {
self.is_empty()
}
fn len(&self) -> usize {
self.len()
}
fn validate_entry(&self, _entry: &Entry<Self::Key, Self::Value>) -> Result<(), Self::Error> {
Ok(())
}
fn max_batch_size(&self) -> u64 {
u64::MAX
}
fn max_batch_entries(&self) -> u64 {
u64::MAX
}
fn estimate_size(&self, _entry: &Entry<Self::Key, Self::Value>) -> u64 {
core::mem::size_of::<Self::Key>() as u64 + core::mem::size_of::<Self::Value>() as u64
}
fn get(&self, key: &K) -> Result<Option<&EntryValue<Self::Value>>, Self::Error> {
Ok(self.get(key))
}
fn contains_key(&self, key: &K) -> Result<bool, Self::Error> {
Ok(self.contains_key(key))
}
fn insert(&mut self, key: K, value: EntryValue<Self::Value>) -> Result<(), Self::Error> {
self.insert(key, value);
Ok(())
}
fn remove_entry(&mut self, key: &K) -> Result<Option<(K, EntryValue<Self::Value>)>, Self::Error> {
Ok(self.remove_entry(key))
}
fn iter(&self) -> Self::Iter<'_> {
BTreeMap::iter(self)
}
fn into_iter(self) -> Self::IntoIter {
core::iter::IntoIterator::into_iter(self)
}
fn rollback(&mut self) -> Result<(), Self::Error> {
self.clear();
Ok(())
}
}
impl<K, V> PwmRange for BTreeMap<K, EntryValue<V>>
where
K: Ord,
{
type Range<'a> = std::collections::btree_map::Range<'a, K, EntryValue<V>> where Self: 'a;
fn range<R: RangeBounds<Self::Key>>(&self, range: R) -> Self::Range<'_> {
BTreeMap::range(self, range)
}
}
impl<K, V> PwmComparableRange for BTreeMap<K, EntryValue<V>>
where
K: Ord,
{
fn range_comparable<T, R>(&self, range: R) -> Self::Range<'_>
where
T: ?Sized + Ord,
Self::Key: Borrow<T> + Ord,
R: RangeBounds<T>,
{
BTreeMap::range(self, range)
}
}
impl<K, V> PwmComparable for BTreeMap<K, EntryValue<V>>
where
K: Ord,
{
fn get_comparable<Q>(&self, key: &Q) -> Result<Option<&EntryValue<Self::Value>>, Self::Error>
where
K: Borrow<Q>,
Q: Ord + ?Sized,
{
Ok(BTreeMap::get(self, key))
}
fn get_entry_comparable<Q>(
&self,
key: &Q,
) -> Result<Option<(&Self::Key, &EntryValue<Self::Value>)>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
Ok(BTreeMap::get_key_value(self, key))
}
fn contains_key_comparable<Q>(&self, key: &Q) -> Result<bool, Self::Error>
where
K: Borrow<Q>,
Q: Ord + ?Sized,
{
Ok(BTreeMap::contains_key(self, key))
}
fn remove_entry_comparable<Q>(
&mut self,
key: &Q,
) -> Result<Option<(K, EntryValue<V>)>, Self::Error>
where
K: Borrow<Q>,
Q: Ord + ?Sized,
{
Ok(BTreeMap::remove_entry(self, key))
}
}