Skip to content

Commit b2424c2

Browse files
committed
add skip list
1 parent c4b7482 commit b2424c2

File tree

5 files changed

+240
-29
lines changed

5 files changed

+240
-29
lines changed

examples/map_rbm.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use algorithm::RoaringBitMap;
1+
2+
use algorithm::SkipList;
23
fn main() {
3-
let mut map = RoaringBitMap::new();
4-
map.add_range(9, 16);
5-
let mut sub_map = RoaringBitMap::new();
6-
sub_map.add_range(7, 12);
7-
let map = map.union(&sub_map);
8-
assert!(map.iter().collect::<Vec<_>>() == vec![7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
4+
let mut val = SkipList::new();
5+
val.insert(1);
6+
assert_eq!(val.len(), 1);
97
}

examples/skip_list.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use algorithm::SkipList;
2+
fn main() {
3+
let mut val = SkipList::new();
4+
val.insert(4);
5+
val.insert(2);
6+
val.insert(1);
7+
let mut iter = val.iter();
8+
assert_eq!(iter.next(), Some(&1));
9+
assert_eq!(iter.next(), Some(&2));
10+
assert_eq!(iter.next(), Some(&4));
11+
assert_eq!(iter.next(), None);
12+
let mut iter = val.iter().rev();
13+
assert_eq!(iter.next(), Some(&4));
14+
assert_eq!(iter.next(), Some(&2));
15+
assert_eq!(iter.next(), Some(&1));
16+
assert_eq!(iter.next(), None);
17+
}

examples/zset.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use algorithm::ZSet;
2+
fn main() {
3+
let mut val = ZSet::new();
4+
val.add_or_update("key", 1);
5+
assert_eq!(val.len(), 1);
6+
println!("ok!!!!!!!!!!");
7+
}

src/arr/skip_list.rs

Lines changed: 192 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ pub struct SkipNode<T> {
2929
levels: Vec<LevelType<T>>,
3030
}
3131

32+
/// 跳表, 链表的一种扩展, 相对更复杂, 但是效率更高
33+
///
34+
/// # Examples
35+
///
36+
/// ```
37+
/// use algorithm::SkipList;
38+
/// fn main() {
39+
/// let mut val = SkipList::new();
40+
/// val.insert(1);
41+
/// val.insert(10);
42+
/// val.insert(100);
43+
/// assert_eq!(val.len(), 3);
44+
/// }
45+
/// ```
46+
#[derive(Debug)]
3247
pub struct SkipList<T: Default + PartialEq + PartialOrd> {
3348
length: usize,
3449
level: usize,
@@ -37,8 +52,8 @@ pub struct SkipList<T: Default + PartialEq + PartialOrd> {
3752
tail: *mut SkipNode<T>,
3853
}
3954

40-
const MAX_LEVEL: usize = 32;
41-
const PERCENT: u16 = 25;
55+
const MAX_LEVEL: usize = 16;
56+
const PERCENT: u16 = 50;
4257

4358
impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
4459
pub fn new() -> Self {
@@ -52,7 +67,7 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
5267
sl
5368
}
5469

55-
pub fn free_all(&mut self) {
70+
fn free_all(&mut self) {
5671
while !self.header.is_null() {
5772
unsafe {
5873
let next = (*self.header).levels[0].forward;
@@ -63,6 +78,20 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
6378
self.header = ptr::null_mut();
6479
}
6580

81+
/// 清除表内的所有内容
82+
///
83+
/// # Examples
84+
///
85+
/// ```
86+
/// use algorithm::SkipList;
87+
/// fn main() {
88+
/// let mut val = SkipList::new();
89+
/// val.insert(1);
90+
/// assert_eq!(val.len(), 1);
91+
/// val.clear();
92+
/// assert_eq!(val.len(), 0);
93+
/// }
94+
/// ```
6695
pub fn clear(&mut self) {
6796
self.free_all();
6897

@@ -105,16 +134,25 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
105134
level
106135
}
107136

137+
/// 插入内容, 将根据score放至合适的排序中
138+
///
139+
/// # Examples
140+
///
141+
/// ```
142+
/// use algorithm::SkipList;
143+
/// fn main() {
144+
/// let mut val = SkipList::new();
145+
/// val.insert(1);
146+
/// assert_eq!(val.len(), 1);
147+
/// }
148+
/// ```
108149
pub fn insert(&mut self, score: T) -> *mut SkipNode<T> {
109150
let mut update: [*mut SkipNode<T>; MAX_LEVEL] = [ptr::null_mut(); MAX_LEVEL];
110151
let mut rank = [0; MAX_LEVEL];
111152
let mut x = self.header;
112-
for i in (0..self.level as usize).rev() {
113-
rank[i] = if i == (self.level - 1) as usize {
114-
0
115-
} else {
116-
rank[i + 1]
117-
};
153+
for i in (0..self.level).rev() {
154+
println!("i = {}, level = {}", i, self.level);
155+
rank[i] = if i == self.level - 1 { 0 } else { rank[i + 1] };
118156

119157
unsafe {
120158
while (*x).levels[i].forward != ptr::null_mut()
@@ -128,8 +166,10 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
128166
}
129167

130168
let level = Self::rand_level();
169+
println!("level == {}", level);
131170
if level > self.level {
132-
for i in level as usize..self.level as usize {
171+
for i in self.level..level {
172+
println!("aaaa i = {}, level = {}", i, self.level);
133173
rank[i] = 0;
134174
update[i] = self.header;
135175
unsafe {
@@ -142,6 +182,7 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
142182
x = Self::make_node(level, score);
143183
unsafe {
144184
for i in 0..level {
185+
println!("i ==== {}", i);
145186
(*x).levels[i].forward = (*update[i]).levels[i].forward;
146187
(*update[i]).levels[i].forward = x;
147188

@@ -169,6 +210,19 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
169210
x
170211
}
171212

213+
/// 更新内容, 查找原值, 并更新新值
214+
///
215+
/// # Examples
216+
///
217+
/// ```
218+
/// use algorithm::SkipList;
219+
/// fn main() {
220+
/// let mut val = SkipList::new();
221+
/// val.insert(1);
222+
/// val.update(&1, 2);
223+
/// assert_eq!(val.len(), 1);
224+
/// }
225+
/// ```
172226
pub fn update(&mut self, cur_score: &T, new_score: T) -> *mut SkipNode<T> {
173227
let mut update: [*mut SkipNode<T>; MAX_LEVEL] = [ptr::null_mut(); MAX_LEVEL];
174228
let mut rank = [0; MAX_LEVEL];
@@ -186,11 +240,10 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
186240
}
187241
x = (*x).levels[0].forward;
188242

189-
assert!(x != ptr::null_mut() && cur_score == &(*x).score);
243+
assert!(!x.is_null() && cur_score == &(*x).score);
190244

191-
if ((*x).backward != ptr::null_mut() || (*(*x).backward).score < new_score)
192-
&& ((*x).levels[0].forward != ptr::null_mut()
193-
|| (*(*x).levels[0].forward).score < new_score)
245+
if ((*x).backward.is_null() || (*(*x).backward).score < new_score)
246+
&& ((*x).levels[0].forward.is_null() || (*(*x).levels[0].forward).score < new_score)
194247
{
195248
(*x).score = new_score;
196249
return x;
@@ -207,10 +260,10 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
207260
unsafe {
208261
for i in 0..self.level {
209262
if (*update[i]).levels[i].forward == x {
210-
(*update[i]).levels[i].span += (*x).levels[i].span - 1;
263+
(*update[i]).levels[i].span += (*x).levels[i].span.saturating_sub(1);
211264
(*update[i]).levels[i].forward = (*x).levels[i].forward;
212265
} else {
213-
(*update[i]).levels[i].span -= 1;
266+
(*update[i]).levels[i].span = (*update[i]).levels[i].span.saturating_sub(1);
214267
}
215268
}
216269

@@ -227,6 +280,23 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
227280
}
228281
}
229282

283+
/// 获取排序值, 得到该值在序列中的排序
284+
///
285+
/// # Examples
286+
///
287+
/// ```
288+
/// use algorithm::SkipList;
289+
/// fn main() {
290+
/// let mut val = SkipList::new();
291+
/// val.insert(4);
292+
/// val.insert(2);
293+
/// val.insert(1);
294+
/// assert_eq!(val.get_rank(&1), 1);
295+
/// assert_eq!(val.get_rank(&4), 3);
296+
/// val.insert(3);
297+
/// assert_eq!(val.get_rank(&4), 4);
298+
/// }
299+
/// ```
230300
pub fn get_rank(&mut self, score: &T) -> usize {
231301
let mut x = self.header;
232302
let mut rank = 0;
@@ -246,7 +316,7 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
246316
0
247317
}
248318

249-
fn find_by_rank(&mut self, rank: usize) -> *mut SkipNode<T> {
319+
pub fn find_by_rank(&mut self, rank: usize) -> *mut SkipNode<T> {
250320
let mut x = self.header;
251321
let mut traversed = 0;
252322
for i in (0..self.level).rev() {
@@ -264,7 +334,25 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
264334
ptr::null_mut()
265335
}
266336

267-
pub fn erase(&mut self, score: &T) -> bool {
337+
/// 移除指定值
338+
///
339+
/// # Examples
340+
///
341+
/// ```
342+
/// use algorithm::SkipList;
343+
/// fn main() {
344+
/// let mut val = SkipList::new();
345+
/// val.insert(4);
346+
/// val.insert(2);
347+
/// val.insert(1);
348+
/// assert_eq!(val.len(), 3);
349+
/// val.remove(&3);
350+
/// assert_eq!(val.len(), 3);
351+
/// val.remove(&4);
352+
/// assert_eq!(val.len(), 2);
353+
/// }
354+
/// ```
355+
pub fn remove(&mut self, score: &T) -> bool {
268356
let mut update: [*mut SkipNode<T>; MAX_LEVEL] = [ptr::null_mut(); MAX_LEVEL];
269357
let mut x = self.header;
270358
unsafe {
@@ -287,7 +375,93 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
287375
return false;
288376
}
289377

378+
/// 获取长度
290379
pub fn len(&self) -> usize {
291380
self.length
292381
}
382+
383+
/// 遍历值
384+
///
385+
/// # Examples
386+
///
387+
/// ```
388+
/// use algorithm::SkipList;
389+
/// fn main() {
390+
/// let mut val = SkipList::new();
391+
/// val.insert(4);
392+
/// val.insert(2);
393+
/// val.insert(1);
394+
/// let mut iter = val.iter();
395+
/// assert_eq!(iter.next(), Some(&1));
396+
/// assert_eq!(iter.next(), Some(&2));
397+
/// assert_eq!(iter.next(), Some(&4));
398+
/// assert_eq!(iter.next(), None);
399+
/// }
400+
/// ```
401+
pub fn iter(&self) -> Iter<'_, T> {
402+
let first = unsafe { (*self.header).levels[0].forward };
403+
Iter::new(self.length, first, self.tail)
404+
}
405+
}
406+
407+
pub struct Iter<'a, T: 'a + Default + PartialEq + PartialOrd> {
408+
len: usize,
409+
header: *mut SkipNode<T>,
410+
tail: *mut SkipNode<T>,
411+
data: PhantomData<&'a ()>,
412+
}
413+
414+
impl<'a, T: Default + PartialEq + PartialOrd> Iter<'a, T> {
415+
pub fn new(len: usize, header: *mut SkipNode<T>, tail: *mut SkipNode<T>) -> Self {
416+
Self {
417+
len,
418+
header,
419+
tail,
420+
data: PhantomData,
421+
}
422+
}
423+
}
424+
425+
impl<'a, T: Default + PartialEq + PartialOrd> Iterator for Iter<'a, T> {
426+
type Item = &'a T;
427+
428+
fn next(&mut self) -> Option<Self::Item> {
429+
if self.len == 0 {
430+
return None;
431+
}
432+
433+
self.len -= 1;
434+
435+
unsafe {
436+
let node = self.header;
437+
self.header = (*self.header).levels[0].forward;
438+
return Some(&(*node).score);
439+
}
440+
}
441+
442+
fn size_hint(&self) -> (usize, Option<usize>) {
443+
(self.len, Some(self.len))
444+
}
445+
}
446+
447+
impl<'a, T: Default + PartialEq + PartialOrd> DoubleEndedIterator for Iter<'a, T> {
448+
fn next_back(&mut self) -> Option<Self::Item> {
449+
if self.len == 0 {
450+
return None;
451+
}
452+
453+
self.len -= 1;
454+
455+
unsafe {
456+
let node = self.tail;
457+
self.tail = (*self.tail).backward;
458+
return Some(&(*node).score);
459+
}
460+
}
461+
}
462+
463+
impl<T: Default + PartialEq + PartialOrd> Drop for SkipList<T> {
464+
fn drop(&mut self) {
465+
self.clear();
466+
}
293467
}

0 commit comments

Comments
 (0)