Revise definitions for ST idx

next
Sayan Nandan 2 years ago
parent 7a2e8e4b58
commit a9900e2bd6
No known key found for this signature in database
GPG Key ID: 42EEDF4AE9D96B54

@ -26,8 +26,8 @@
use core::{borrow::Borrow, hash::Hash}; use core::{borrow::Borrow, hash::Hash};
mod stord;
mod stdhm; mod stdhm;
mod stord;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -81,11 +81,7 @@ pub struct DummyMetrics;
/// The base spec for any index. Iterators have meaningless order, and that is intentional and oftentimes /// The base spec for any index. Iterators have meaningless order, and that is intentional and oftentimes
/// consequential. For more specialized impls, use the [`STIndex`], [`MTIndex`] or [`STIndexSeq`] traits /// consequential. For more specialized impls, use the [`STIndex`], [`MTIndex`] or [`STIndexSeq`] traits
pub trait IndexBaseSpec<K, V> pub trait IndexBaseSpec<K, V> {
where
K: AsKey,
V: AsValue,
{
/// Index supports prealloc? /// Index supports prealloc?
const PREALLOC: bool; const PREALLOC: bool;
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@ -125,11 +121,7 @@ where
} }
/// An unordered MTIndex /// An unordered MTIndex
pub trait MTIndex<K, V>: IndexBaseSpec<K, V> pub trait MTIndex<K, V>: IndexBaseSpec<K, V> {
where
K: AsKey,
V: AsValue,
{
/// Attempts to compact the backing storage /// Attempts to compact the backing storage
fn mt_compact(&self) {} fn mt_compact(&self) {}
/// Clears all the entries in the MTIndex /// Clears all the entries in the MTIndex
@ -137,50 +129,58 @@ where
// write // write
/// Returns true if the entry was inserted successfully; returns false if the uniqueness constraint is /// Returns true if the entry was inserted successfully; returns false if the uniqueness constraint is
/// violated /// violated
fn mt_insert(&self, key: K, val: V) -> bool; fn mt_insert(&self, key: K, val: V) -> bool
where
K: AsKey,
V: AsValue;
/// Updates or inserts the given value /// Updates or inserts the given value
fn mt_upsert(&self, key: K, val: V); fn mt_upsert(&self, key: K, val: V)
where
K: AsKey,
V: AsValue;
// read // read
fn mt_contains<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q> + AsKey,
Q: ?Sized + AsKeyRef;
/// Returns a reference to the value corresponding to the key, if it exists /// Returns a reference to the value corresponding to the key, if it exists
fn mt_get<Q>(&self, key: &Q) -> Option<&V> fn mt_get<Q>(&self, key: &Q) -> Option<&V>
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
/// Returns a clone of the value corresponding to the key, if it exists /// Returns a clone of the value corresponding to the key, if it exists
fn mt_get_cloned<Q>(&self, key: &Q) -> Option<V> fn mt_get_cloned<Q>(&self, key: &Q) -> Option<V>
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
// update // update
/// Returns true if the entry is updated /// Returns true if the entry is updated
fn mt_update<Q>(&self, key: &Q, val: V) -> bool fn mt_update<Q>(&self, key: &Q, val: V) -> bool
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
V: AsValue,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
/// Updates the entry and returns the old value, if it exists /// Updates the entry and returns the old value, if it exists
fn mt_update_return<Q>(&self, key: &Q, val: V) -> Option<V> fn mt_update_return<Q>(&self, key: &Q, val: V) -> Option<V>
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
V: AsValue,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
// delete // delete
/// Returns true if the entry was deleted /// Returns true if the entry was deleted
fn mt_delete<Q>(&self, key: &Q) -> bool fn mt_delete<Q>(&self, key: &Q) -> bool
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
/// Removes the entry and returns it, if it exists /// Removes the entry and returns it, if it exists
fn mt_delete_return<Q>(&self, key: &Q) -> Option<V> fn mt_delete_return<Q>(&self, key: &Q) -> Option<V>
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
} }
/// An unordered STIndex /// An unordered STIndex
pub trait STIndex<K, V>: IndexBaseSpec<K, V> pub trait STIndex<K, V>: IndexBaseSpec<K, V> {
where
K: AsKey,
V: AsValue,
{
/// Attempts to compact the backing storage /// Attempts to compact the backing storage
fn st_compact(&mut self) {} fn st_compact(&mut self) {}
/// Clears all the entries in the STIndex /// Clears all the entries in the STIndex
@ -188,49 +188,57 @@ where
// write // write
/// Returns true if the entry was inserted successfully; returns false if the uniqueness constraint is /// Returns true if the entry was inserted successfully; returns false if the uniqueness constraint is
/// violated /// violated
fn st_insert(&mut self, key: K, val: V) -> bool; fn st_insert(&mut self, key: K, val: V) -> bool
where
K: AsKey,
V: AsValue;
/// Updates or inserts the given value /// Updates or inserts the given value
fn st_upsert(&mut self, key: K, val: V); fn st_upsert(&mut self, key: K, val: V)
where
K: AsKey,
V: AsValue;
// read // read
fn st_contains<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q> + AsKey,
Q: ?Sized + AsKeyRef;
/// Returns a reference to the value corresponding to the key, if it exists /// Returns a reference to the value corresponding to the key, if it exists
fn st_get<Q>(&self, key: &Q) -> Option<&V> fn st_get<Q>(&self, key: &Q) -> Option<&V>
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
/// Returns a clone of the value corresponding to the key, if it exists /// Returns a clone of the value corresponding to the key, if it exists
fn st_get_cloned<Q>(&self, key: &Q) -> Option<V> fn st_get_cloned<Q>(&self, key: &Q) -> Option<V>
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
// update // update
/// Returns true if the entry is updated /// Returns true if the entry is updated
fn st_update<Q>(&mut self, key: &Q, val: V) -> bool fn st_update<Q>(&mut self, key: &Q, val: V) -> bool
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
V: AsValue,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
/// Updates the entry and returns the old value, if it exists /// Updates the entry and returns the old value, if it exists
fn st_update_return<Q>(&mut self, key: &Q, val: V) -> Option<V> fn st_update_return<Q>(&mut self, key: &Q, val: V) -> Option<V>
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
V: AsValue,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
// delete // delete
/// Returns true if the entry was deleted /// Returns true if the entry was deleted
fn st_delete<Q>(&mut self, key: &Q) -> bool fn st_delete<Q>(&mut self, key: &Q) -> bool
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
/// Removes the entry and returns it, if it exists /// Removes the entry and returns it, if it exists
fn st_delete_return<Q>(&mut self, key: &Q) -> Option<V> fn st_delete_return<Q>(&mut self, key: &Q) -> Option<V>
where where
K: Borrow<Q>, K: AsKey + Borrow<Q>,
Q: ?Sized + AsKeyRef; Q: ?Sized + AsKeyRef;
} }
pub trait STIndexSeq<K, V>: STIndex<K, V> pub trait STIndexSeq<K, V>: STIndex<K, V> {
where
K: AsKey,
V: AsValue,
{
/// An ordered iterator over the keys and values /// An ordered iterator over the keys and values
type IterOrdKV<'a>: Iterator<Item = (&'a K, &'a V)> + DoubleEndedIterator<Item = (&'a K, &'a V)> type IterOrdKV<'a>: Iterator<Item = (&'a K, &'a V)> + DoubleEndedIterator<Item = (&'a K, &'a V)>
where where

@ -24,8 +24,9 @@
* *
*/ */
use super::{AsKey, AsValue, DummyMetrics, IndexBaseSpec, STIndex}; use super::{AsKey, AsKeyRef, AsValue, DummyMetrics, IndexBaseSpec, STIndex};
use std::{ use std::{
borrow::Borrow,
collections::{ collections::{
hash_map::{Entry, Iter as StdMapIterKV, Keys as StdMapIterKey, Values as StdMapIterVal}, hash_map::{Entry, Iter as StdMapIterKV, Keys as StdMapIterKey, Values as StdMapIterVal},
HashMap as StdMap, HashMap as StdMap,
@ -36,8 +37,6 @@ use std::{
impl<K, V, S> IndexBaseSpec<K, V> for StdMap<K, V, S> impl<K, V, S> IndexBaseSpec<K, V> for StdMap<K, V, S>
where where
K: AsKey,
V: AsValue,
S: BuildHasher + Default, S: BuildHasher + Default,
{ {
const PREALLOC: bool = true; const PREALLOC: bool = true;
@ -113,34 +112,42 @@ where
let _ = self.insert(key, val); let _ = self.insert(key, val);
} }
fn st_contains<Q>(&self, k: &Q) -> bool
where
K: Borrow<Q>,
Q: ?Sized + AsKeyRef,
{
self.contains_key(k)
}
fn st_get<Q>(&self, key: &Q) -> Option<&V> fn st_get<Q>(&self, key: &Q) -> Option<&V>
where where
K: std::borrow::Borrow<Q>, K: Borrow<Q>,
Q: ?Sized + super::AsKeyRef, Q: ?Sized + AsKeyRef,
{ {
self.get(key) self.get(key)
} }
fn st_get_cloned<Q>(&self, key: &Q) -> Option<V> fn st_get_cloned<Q>(&self, key: &Q) -> Option<V>
where where
K: std::borrow::Borrow<Q>, K: Borrow<Q>,
Q: ?Sized + super::AsKeyRef, Q: ?Sized + AsKeyRef,
{ {
self.get(key).cloned() self.get(key).cloned()
} }
fn st_update<Q>(&mut self, key: &Q, val: V) -> bool fn st_update<Q>(&mut self, key: &Q, val: V) -> bool
where where
K: std::borrow::Borrow<Q>, K: Borrow<Q>,
Q: ?Sized + super::AsKeyRef, Q: ?Sized + AsKeyRef,
{ {
self.get_mut(key).map(move |e| *e = val).is_some() self.get_mut(key).map(move |e| *e = val).is_some()
} }
fn st_update_return<Q>(&mut self, key: &Q, val: V) -> Option<V> fn st_update_return<Q>(&mut self, key: &Q, val: V) -> Option<V>
where where
K: std::borrow::Borrow<Q>, K: Borrow<Q>,
Q: ?Sized + super::AsKeyRef, Q: ?Sized + AsKeyRef,
{ {
self.get_mut(key).map(move |e| { self.get_mut(key).map(move |e| {
let mut new = val; let mut new = val;
@ -151,16 +158,16 @@ where
fn st_delete<Q>(&mut self, key: &Q) -> bool fn st_delete<Q>(&mut self, key: &Q) -> bool
where where
K: std::borrow::Borrow<Q>, K: Borrow<Q>,
Q: ?Sized + super::AsKeyRef, Q: ?Sized + AsKeyRef,
{ {
self.remove(key).is_some() self.remove(key).is_some()
} }
fn st_delete_return<Q>(&mut self, key: &Q) -> Option<V> fn st_delete_return<Q>(&mut self, key: &Q) -> Option<V>
where where
K: std::borrow::Borrow<Q>, K: Borrow<Q>,
Q: ?Sized + super::AsKeyRef, Q: ?Sized + AsKeyRef,
{ {
self.remove(key) self.remove(key)
} }

@ -358,6 +358,39 @@ impl<K, V, S> IndexSTSeqDll<K, V, S> {
} }
} }
impl<K, V, S> IndexSTSeqDll<K, V, S> {
#[inline(always)]
fn _iter_unord_kv<'a>(&'a self) -> IndexSTSeqDllIterUnordKV<'a, K, V> {
IndexSTSeqDllIterUnordKV::new(&self.m)
}
#[inline(always)]
fn _iter_unord_k<'a>(&'a self) -> IndexSTSeqDllIterUnordKey<'a, K, V> {
IndexSTSeqDllIterUnordKey::new(&self.m)
}
#[inline(always)]
fn _iter_unord_v<'a>(&'a self) -> IndexSTSeqDllIterUnordValue<'a, K, V> {
IndexSTSeqDllIterUnordValue::new(&self.m)
}
#[inline(always)]
fn _iter_ord_kv<'a>(&'a self) -> IndexSTSeqDllIterOrdKV<'a, K, V> {
IndexSTSeqDllIterOrdKV {
i: IndexSTSeqDllIterOrdBase::new(self),
}
}
#[inline(always)]
fn _iter_ord_k<'a>(&'a self) -> IndexSTSeqDllIterOrdKey<'a, K, V> {
IndexSTSeqDllIterOrdKey {
i: IndexSTSeqDllIterOrdBase::new(self),
}
}
#[inline(always)]
fn _iter_ord_v<'a>(&'a self) -> IndexSTSeqDllIterOrdValue<'a, K, V> {
IndexSTSeqDllIterOrdValue {
i: IndexSTSeqDllIterOrdBase::new(self),
}
}
}
impl<K: AsKey, V: AsValue, S: BuildHasher> IndexSTSeqDll<K, V, S> { impl<K: AsKey, V: AsValue, S: BuildHasher> IndexSTSeqDll<K, V, S> {
#[inline(always)] #[inline(always)]
/// Clean up unused and cached memory /// Clean up unused and cached memory
@ -497,36 +530,6 @@ impl<K: AsKey, V: AsValue, S: BuildHasher> IndexSTSeqDll<K, V, S> {
} }
} }
} }
#[inline(always)]
fn _iter_unord_kv<'a>(&'a self) -> IndexSTSeqDllIterUnordKV<'a, K, V> {
IndexSTSeqDllIterUnordKV::new(&self.m)
}
#[inline(always)]
fn _iter_unord_k<'a>(&'a self) -> IndexSTSeqDllIterUnordKey<'a, K, V> {
IndexSTSeqDllIterUnordKey::new(&self.m)
}
#[inline(always)]
fn _iter_unord_v<'a>(&'a self) -> IndexSTSeqDllIterUnordValue<'a, K, V> {
IndexSTSeqDllIterUnordValue::new(&self.m)
}
#[inline(always)]
fn _iter_ord_kv<'a>(&'a self) -> IndexSTSeqDllIterOrdKV<'a, K, V> {
IndexSTSeqDllIterOrdKV {
i: IndexSTSeqDllIterOrdBase::new(self),
}
}
#[inline(always)]
fn _iter_ord_k<'a>(&'a self) -> IndexSTSeqDllIterOrdKey<'a, K, V> {
IndexSTSeqDllIterOrdKey {
i: IndexSTSeqDllIterOrdBase::new(self),
}
}
#[inline(always)]
fn _iter_ord_v<'a>(&'a self) -> IndexSTSeqDllIterOrdValue<'a, K, V> {
IndexSTSeqDllIterOrdValue {
i: IndexSTSeqDllIterOrdBase::new(self),
}
}
} }
impl<K, V, S> Drop for IndexSTSeqDll<K, V, S> { impl<K, V, S> Drop for IndexSTSeqDll<K, V, S> {
@ -554,11 +557,7 @@ impl<K: AsKey, V: AsValue, S: BuildHasher + Default> FromIterator<(K, V)>
} }
} }
impl<K, V, S: BuildHasher + Default> IndexBaseSpec<K, V> for IndexSTSeqDll<K, V, S> impl<K, V, S: BuildHasher + Default> IndexBaseSpec<K, V> for IndexSTSeqDll<K, V, S> {
where
K: AsKey,
V: AsValue,
{
const PREALLOC: bool = true; const PREALLOC: bool = true;
type Metrics = IndexSTSeqDllMetrics; type Metrics = IndexSTSeqDllMetrics;
@ -626,6 +625,17 @@ where
let _ = self._upsert(key, val); let _ = self._upsert(key, val);
} }
fn st_contains<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q> + AsKey,
Q: ?Sized + AsKeyRef,
{
self.m.contains_key(unsafe {
// UNSAFE(@ohsayan): Valid ref with correct bounds
IndexSTSeqDllQref::from_ref(key)
})
}
fn st_get<Q>(&self, key: &Q) -> Option<&V> fn st_get<Q>(&self, key: &Q) -> Option<&V>
where where
K: Borrow<Q>, K: Borrow<Q>,

Loading…
Cancel
Save