Skip to content

Commit

Permalink
add ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
tickbh committed Jun 24, 2024
1 parent 4cd9314 commit 18cc5d3
Show file tree
Hide file tree
Showing 8 changed files with 987 additions and 119 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "algorithm"
version = "0.1.6"
version = "0.1.7"
edition = "2021"
authors = ["tickbh <[email protected]>"]
description = "about algorithm data structure, now has lru/lru-k/lfu/slab/rbtree/roaring_bitmap/timer_wheelss, 关于算法常用的数据结构"
description = "about algorithm data structure, now has ttl with lru/lru-k/lfu/arc and slab/rbtree/roaring_bitmap/timer_wheelss, 关于算法常用的数据结构"
repository = "https://github.com/tickbh/algorithm-rs"
license = "Apache-2.0"
keywords = ["arc", "lru", "lfu", "timerwheel", "slab"]
Expand All @@ -24,5 +24,6 @@ opt-level = 3
debug = true

[features]
default = ["hashbrown", "ttl"]
hashbrown=[]
ttl=[]
56 changes: 17 additions & 39 deletions src/arr/fix_vec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::cmp::Ordering;
use std::ptr::NonNull;

#[derive(Debug)]
struct FixedVecNode<T> {
Expand Down Expand Up @@ -400,10 +399,16 @@ impl<T> FixedVec<T> {
/// ```
#[inline]
pub fn iter_mut(&mut self) -> FixedVecIterMut<'_, T> {
let head = self.head;
let tail = self.tail;
let len = self.len();
FixedVecIterMut::new(&mut self.nodes, head, tail, len)
FixedVecIterMut {
head: self.head,
tail: self.tail,
len: self.len(),
list: self,
}
// let head = self.head;
// let tail = self.tail;
// let len = self.len();
// FixedVecIterMut::new(self, head, tail, len)
}

fn reorder(&mut self) {
Expand Down Expand Up @@ -670,46 +675,23 @@ impl<'a, T> DoubleEndedIterator for FixedVecIter<'a, T> {
}
}

#[derive(Debug)]
pub struct FixedVecIterMut<'a, T> {
ptr: NonNull<Option<FixedVecNode<T>>>,
list: &'a mut FixedVec<T>,
head: usize,
tail: usize,
len: usize,
_marker: std::marker::PhantomData<&'a mut T>,
}

impl<'a, T> FixedVecIterMut<'a, T> {
#[allow(unsafe_code)]
fn new(
slice: &'a mut [Option<FixedVecNode<T>>],
head: usize,
tail: usize,
len: usize,
) -> Self {
let ptr = slice.as_mut_ptr();
Self {
ptr: unsafe { NonNull::new_unchecked(ptr) },
head,
tail,
len,
_marker: std::marker::PhantomData,
}
}
}

impl<'a, T> Iterator for FixedVecIterMut<'a, T> {
type Item = (usize, &'a mut T);

#[allow(unsafe_code)]
fn next(&mut self) -> Option<Self::Item> {
if self.len > 0 {
let head = self.head;
let node_ref = unsafe {
let ptr = NonNull::new_unchecked(self.ptr.as_ptr().add(head)).as_ptr();
&mut *ptr
let node = unsafe {
core::mem::transmute::<&'_ mut FixedVecNode<T>, &'a mut FixedVecNode<T>>(self.list.node_mut(head).unwrap())
};

let node = node_ref.as_mut().unwrap();
self.head = node.next;
self.len -= 1;
Some((head, &mut node.data))
Expand All @@ -724,21 +706,17 @@ impl<'a, T> Iterator for FixedVecIterMut<'a, T> {
}

impl<'a, T> DoubleEndedIterator for FixedVecIterMut<'a, T> {
#[allow(unsafe_code)]
fn next_back(&mut self) -> Option<Self::Item> {
if self.len > 0 {
let tail = self.tail;
let node_ref = unsafe {
let ptr = NonNull::new_unchecked(self.ptr.as_ptr().add(tail)).as_ptr();
&mut *ptr
let node = unsafe {
core::mem::transmute::<&'_ mut FixedVecNode<T>, &'a mut FixedVecNode<T>>(self.list.node_mut(tail).unwrap())
};

let node = node_ref.as_mut().unwrap();
self.tail = node.prev;
self.len -= 1;
Some((tail, &mut node.data))
} else {
None
}
}
}
}
Loading

0 comments on commit 18cc5d3

Please sign in to comment.