Skip to content

Commit 18cc5d3

Browse files
committed
add ttl
1 parent 4cd9314 commit 18cc5d3

File tree

8 files changed

+987
-119
lines changed

8 files changed

+987
-119
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "algorithm"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
edition = "2021"
55
authors = ["tickbh <[email protected]>"]
6-
description = "about algorithm data structure, now has lru/lru-k/lfu/slab/rbtree/roaring_bitmap/timer_wheelss, 关于算法常用的数据结构"
6+
description = "about algorithm data structure, now has ttl with lru/lru-k/lfu/arc and slab/rbtree/roaring_bitmap/timer_wheelss, 关于算法常用的数据结构"
77
repository = "https://github.com/tickbh/algorithm-rs"
88
license = "Apache-2.0"
99
keywords = ["arc", "lru", "lfu", "timerwheel", "slab"]
@@ -24,5 +24,6 @@ opt-level = 3
2424
debug = true
2525

2626
[features]
27+
default = ["hashbrown", "ttl"]
2728
hashbrown=[]
2829
ttl=[]

src/arr/fix_vec.rs

+17-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::cmp::Ordering;
2-
use std::ptr::NonNull;
32

43
#[derive(Debug)]
54
struct FixedVecNode<T> {
@@ -400,10 +399,16 @@ impl<T> FixedVec<T> {
400399
/// ```
401400
#[inline]
402401
pub fn iter_mut(&mut self) -> FixedVecIterMut<'_, T> {
403-
let head = self.head;
404-
let tail = self.tail;
405-
let len = self.len();
406-
FixedVecIterMut::new(&mut self.nodes, head, tail, len)
402+
FixedVecIterMut {
403+
head: self.head,
404+
tail: self.tail,
405+
len: self.len(),
406+
list: self,
407+
}
408+
// let head = self.head;
409+
// let tail = self.tail;
410+
// let len = self.len();
411+
// FixedVecIterMut::new(self, head, tail, len)
407412
}
408413

409414
fn reorder(&mut self) {
@@ -670,46 +675,23 @@ impl<'a, T> DoubleEndedIterator for FixedVecIter<'a, T> {
670675
}
671676
}
672677

678+
#[derive(Debug)]
673679
pub struct FixedVecIterMut<'a, T> {
674-
ptr: NonNull<Option<FixedVecNode<T>>>,
680+
list: &'a mut FixedVec<T>,
675681
head: usize,
676682
tail: usize,
677683
len: usize,
678-
_marker: std::marker::PhantomData<&'a mut T>,
679-
}
680-
681-
impl<'a, T> FixedVecIterMut<'a, T> {
682-
#[allow(unsafe_code)]
683-
fn new(
684-
slice: &'a mut [Option<FixedVecNode<T>>],
685-
head: usize,
686-
tail: usize,
687-
len: usize,
688-
) -> Self {
689-
let ptr = slice.as_mut_ptr();
690-
Self {
691-
ptr: unsafe { NonNull::new_unchecked(ptr) },
692-
head,
693-
tail,
694-
len,
695-
_marker: std::marker::PhantomData,
696-
}
697-
}
698684
}
699685

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

703-
#[allow(unsafe_code)]
704689
fn next(&mut self) -> Option<Self::Item> {
705690
if self.len > 0 {
706691
let head = self.head;
707-
let node_ref = unsafe {
708-
let ptr = NonNull::new_unchecked(self.ptr.as_ptr().add(head)).as_ptr();
709-
&mut *ptr
692+
let node = unsafe {
693+
core::mem::transmute::<&'_ mut FixedVecNode<T>, &'a mut FixedVecNode<T>>(self.list.node_mut(head).unwrap())
710694
};
711-
712-
let node = node_ref.as_mut().unwrap();
713695
self.head = node.next;
714696
self.len -= 1;
715697
Some((head, &mut node.data))
@@ -724,21 +706,17 @@ impl<'a, T> Iterator for FixedVecIterMut<'a, T> {
724706
}
725707

726708
impl<'a, T> DoubleEndedIterator for FixedVecIterMut<'a, T> {
727-
#[allow(unsafe_code)]
728709
fn next_back(&mut self) -> Option<Self::Item> {
729710
if self.len > 0 {
730711
let tail = self.tail;
731-
let node_ref = unsafe {
732-
let ptr = NonNull::new_unchecked(self.ptr.as_ptr().add(tail)).as_ptr();
733-
&mut *ptr
712+
let node = unsafe {
713+
core::mem::transmute::<&'_ mut FixedVecNode<T>, &'a mut FixedVecNode<T>>(self.list.node_mut(tail).unwrap())
734714
};
735-
736-
let node = node_ref.as_mut().unwrap();
737715
self.tail = node.prev;
738716
self.len -= 1;
739717
Some((tail, &mut node.data))
740718
} else {
741719
None
742720
}
743721
}
744-
}
722+
}

0 commit comments

Comments
 (0)