TmoHash

Struct TmoHash 

Source
pub struct TmoHash<K, V>
where K: Eq + Hash,
{ /* private fields */ }

Implementations§

Source§

impl<K, V> TmoHash<K, V>
where K: Eq + Hash,

Source

pub fn new(capacity: usize) -> TmoHash<K, V>

Source

pub fn pool_len(&self) -> usize

返回内存池中当前剩余node的数量

Source

pub fn insert(&mut self, key: K, val: V) -> Option<&V>

插入一个k v对儿,如果已经存在,会替代。 返回插入value的引用 因为限于在流表场景下使用,所以在insert之前,用户需要确保节点不存在,也就是不要产生替代的情况

§Example
use libnsave::tmohash::TmoHash;

let mut tmo = TmoHash::new(10);
assert_eq!(Some(&"a"), tmo.insert(1, "a"));
assert!(tmo.contains_key(&1));
assert!(!tmo.contains_key(&2));
Source

pub fn insert_mut(&mut self, key: K, val: V) -> Option<&mut V>

插入一个k v对儿,如果已经存在,会替代。 返回插入value的可变引用 因为限于在流表场景下使用,所以在insert之前,用户需要确保节点不存在,也就是不要产生替代的情况

§Example
use libnsave::tmohash::TmoHash;

let mut tmo = TmoHash::new(10);
assert_eq!(Some(&mut "a"), tmo.insert_mut(1, "a"));
assert!(tmo.contains_key(&1));
assert!(!tmo.contains_key(&2));
Source

pub fn remove(&mut self, k: &K)

删除一个key

#Example

use libnsave::tmohash::TmoHash;

let mut tmo = TmoHash::new(10);
tmo.insert(1, "a");
tmo.insert(2, "b");
tmo.insert(3, "c");
tmo.remove(&2);
assert!(tmo.contains_key(&1));
assert!(tmo.contains_key(&3));
assert!(!tmo.contains_key(&2));
Source

pub fn contains_key(&self, key: &K) -> bool

根据key,判断是否存在节点

#Example

use libnsave::tmohash::TmoHash;

let mut tmo = TmoHash::new(10);
tmo.insert(1, "a");
tmo.insert(2, "b");
assert!(tmo.contains_key(&1));
assert!(tmo.contains_key(&2));
assert!(!tmo.contains_key(&3));
Source

pub fn capacity(&self) -> usize

返回最大容量

#Example

use libnsave::tmohash::TmoHash;

let mut tmo: TmoHash<usize, String> = TmoHash::new(10);
assert_eq!(tmo.capacity(), 10);
Source

pub fn len(&self) -> usize

返回已有数据的个数

#Example

use libnsave::tmohash::TmoHash;

let mut tmo = TmoHash::new(10);
assert_eq!(tmo.len(), 0);
tmo.insert(1, "a");
tmo.insert(2, "b");
assert_eq!(tmo.len(), 2);
Source

pub fn is_empty(&self) -> bool

返回是否为空

#Example

use libnsave::tmohash::TmoHash;

let mut tmo = TmoHash::new(10);
assert!(tmo.is_empty());
tmo.insert(1, "a");
assert!(!tmo.is_empty());
Source

pub fn is_full(&self) -> bool

返回是否已满

#Example

use libnsave::tmohash::TmoHash;

let mut tmo: TmoHash<usize, usize> = TmoHash::new(10);
assert!(!tmo.is_full());
for i in 0..10 {
    tmo.insert(i, i);
}
assert!(tmo.is_full());
Source

pub fn get(&self, k: &K) -> Option<&V>

根据key,查询返回value的引用。因为是不变引用,说明没有更新,所以不需要重新移动到链表尾部

#Example

use libnsave::tmohash::TmoHash;

let mut tmo = TmoHash::new(10);
tmo.insert(1, "a");
tmo.insert(2, "b");
tmo.insert(3, "c");
assert_eq!(Some((&"a")), tmo.get(&1));
assert_eq!(Some((&"b")), tmo.get(&2));
assert_eq!(Some((&"c")), tmo.get(&3));
assert_ne!(Some((&"d")), tmo.get(&4));
Source

pub fn get_mut(&mut self, k: &K) -> Option<&mut V>

根据key,查询返回value的可变引用

#Example

use libnsave::tmohash::TmoHash;

let mut tmo = TmoHash::new(10);
tmo.insert(1, "a");
tmo.insert(2, "b");
tmo.insert(3, "c");
let node = tmo.get_mut(&1).unwrap();
*node = &"d";
assert_eq!(Some((&"d")), tmo.get(&1));
Source

pub fn iter(&self) -> Iter<'_, K, V>

从最老的node开始迭代

#Example

use libnsave::tmohash::TmoHash;

let mut tmo = TmoHash::new(10);
tmo.insert("a", 1);
tmo.insert("b", 2);
tmo.insert("c", 3);
let sum = tmo.iter().map(|x| x.1).sum();
assert_eq!(6, sum);
Source

pub fn timeout<F>(&mut self, fun: F)
where F: Fn(&K, &V) -> bool,

从最老的一端开始遍历,根据闭包返回确定是否保留此节点 闭包为true 删除,为fals 不删除,保留

遇到第一个不满足条件的就返回。用于老化tmo场景。 从最老开始,一直删除到不满足闭包条件的第一个node为止。并不遍历所有节点。只从最老开始遍历到第一个不需要老化为止。 这样,既能尽快删除了所有需要老化的节点,也不会遍历过多

#Example

use libnsave::tmohash::TmoHash;

let mut tmo = TmoHash::new(10);
tmo.insert("a", 1);
tmo.insert("b", 2);
tmo.insert("c", 3);
tmo.insert("d", 10);
tmo.insert("e", 11);
tmo.insert("f", 12);
tmo.insert("g", 4);
tmo.timeout(|&_k, &v| v < 10);
assert_eq!(4, tmo.len());
assert!(!tmo.contains_key(&"a"));
assert!(!tmo.contains_key(&"b"));
assert!(!tmo.contains_key(&"c"));
assert!(tmo.contains_key(&"g"));

Trait Implementations§

Source§

impl<K, V> Debug for TmoHash<K, V>
where K: Debug + Hash + Eq, V: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V> Display for TmoHash<K, V>
where K: Display + Hash + Eq + Clone, V: Display,

display

§Examples

use libnsave::tmohash::TmoHash;

let mut tmo = TmoHash::new(10);
tmo.insert(1, "a");
tmo.insert(2, "b");
tmo.insert(3, "c");
assert_eq!("[1: a, 2: b, 3: c]", format!("{}", tmo));
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V> Drop for TmoHash<K, V>
where K: Hash + Eq,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<K, V: Send> Send for TmoHash<K, V>
where K: Eq + Hash + Send,

Source§

impl<K, V: Sync> Sync for TmoHash<K, V>
where K: Eq + Hash + Sync,

Auto Trait Implementations§

§

impl<K, V> Freeze for TmoHash<K, V>

§

impl<K, V> RefUnwindSafe for TmoHash<K, V>

§

impl<K, V> Unpin for TmoHash<K, V>

§

impl<K, V> UnwindSafe for TmoHash<K, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.