|
@@ -3,13 +3,11 @@
|
|
|
/// In the database I also use two Skiplist in memory to store datas temperary.</p>
|
|
|
///</p> We refer the source of leveldb,however we know that the first time, google use C++ writting leveldb and then rewrite it by go.</p>
|
|
|
|
|
|
-
|
|
|
#[allow(unused)]
|
|
|
pub mod skip_lists
|
|
|
{
|
|
|
static mut COUNTS:i32=0;
|
|
|
- use std::{borrow::{Borrow, BorrowMut}, cell::{Ref, RefCell}, collections::btree_map::Values, fmt::Display, rc::Rc};
|
|
|
- use std::cmp::PartialOrd;
|
|
|
+ use std::{borrow::{Borrow, BorrowMut}, cell::{Ref, RefCell}, collections::btree_map::Values, fmt::Display, rc::Rc,time::{Duration, Instant},cmp::PartialOrd};
|
|
|
use crate::algorithm::{baisc_data::basic_data, random};
|
|
|
use crate::algorithm::baisc_data;
|
|
|
use rand::Rng;
|
|
@@ -54,8 +52,7 @@ pub mod skip_lists
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
///## Skiplist
|
|
|
///
|
|
|
///
|
|
@@ -102,9 +99,7 @@ pub mod skip_lists
|
|
|
if self.current_size==0 //如果shiplist是全空的,直接将点插入
|
|
|
{
|
|
|
//let random_level: usize=random::random::lgc(self.max_level); 没空研究随机数算法,先用一下别人的
|
|
|
- let random_level: usize=rng.gen_range(1..(self.max_level));
|
|
|
-
|
|
|
- dbg!(random_level);
|
|
|
+ let random_level: usize=rng.gen_range(1..(self.max_level+1));
|
|
|
let new_node: Rc<RefCell<Node<K, V>>>=Self::creat_node(key,value,random_level); //创建新的节点
|
|
|
for i in 0..=(random_level-1)
|
|
|
{
|
|
@@ -112,34 +107,28 @@ pub mod skip_lists
|
|
|
}
|
|
|
self.current_size=self.current_size+1;
|
|
|
self.current_level=random_level;
|
|
|
- dbg!(self.current_size);
|
|
|
}
|
|
|
else //当list不为空的时候需要判定插入的位置
|
|
|
{
|
|
|
- let random_level:usize=rng.gen_range(1..(self.max_level));
|
|
|
+ let random_level:usize=rng.gen_range(1..(self.max_level+1));
|
|
|
let mut cur_point:Option<Rc<RefCell<Node<K,V>>>>=None;
|
|
|
let mut cur_node:Vec<Option<Rc<RefCell<Node<K,V>>>>>=vec![None;self.max_level];
|
|
|
let mut cur_level: i32=(self.current_level as i32)-1;
|
|
|
- dbg!(random_level);
|
|
|
- dbg!(self.current_level);
|
|
|
while(cur_level>=0)
|
|
|
{
|
|
|
- // dbg!(self.current_size);
|
|
|
if self.head[cur_level as usize].is_none() //如果起始点为空则直接跳过(在这一层不会有节点)
|
|
|
{
|
|
|
cur_level=cur_level-1;
|
|
|
- dbg!("cur_level={}",cur_level);
|
|
|
continue;
|
|
|
}
|
|
|
else
|
|
|
{ //判定每一层的起始位置
|
|
|
- if cur_level ==((self.max_level) as i32)-1 || cur_node[(cur_level as usize+1)].is_none()
|
|
|
+ if cur_level >=((self.max_level) as i32)-1 || cur_node[(cur_level as usize+1)].is_none()
|
|
|
{
|
|
|
|
|
|
cur_point=self.head[cur_level as usize].clone();
|
|
|
if (*(cur_point.clone().unwrap())).borrow().key>key //如果第一个节点key就大于当前数据(主要是没有一个空的头节点就比较复杂)
|
|
|
{
|
|
|
- dbg!("find");
|
|
|
cur_node[cur_level as usize]=None;
|
|
|
cur_level=cur_level-1;
|
|
|
continue;
|
|
@@ -155,26 +144,21 @@ pub mod skip_lists
|
|
|
{
|
|
|
if (*(*(cur_point.clone().unwrap())).borrow_mut()).forward[cur_level as usize].is_none()
|
|
|
{
|
|
|
- dbg!("None");
|
|
|
-
|
|
|
cur_node[cur_level as usize]=cur_point.clone();
|
|
|
break;
|
|
|
}
|
|
|
else if (*(((*(*(cur_point.clone().unwrap())).borrow_mut()).forward[cur_level as usize]).clone()).unwrap()).borrow().key==key
|
|
|
{
|
|
|
- dbg!("same");
|
|
|
(*(((*(*(cur_point.clone().unwrap())).borrow_mut()).forward[cur_level as usize]).clone()).unwrap()).borrow_mut().value=value; //将对应的节点数据修改
|
|
|
return InsertResult::Exist;
|
|
|
}
|
|
|
else if (*(((*(*(cur_point.clone().unwrap())).borrow_mut()).forward[cur_level as usize]).clone()).unwrap()).borrow().key>key
|
|
|
{
|
|
|
- dbg!("find");
|
|
|
cur_node[cur_level as usize]=cur_point.clone();
|
|
|
break;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- dbg!("next");
|
|
|
cur_point=(*((cur_point.clone()).unwrap())).borrow().forward[cur_level as usize].clone();
|
|
|
}
|
|
|
}
|
|
@@ -190,13 +174,16 @@ pub mod skip_lists
|
|
|
|
|
|
for i in 0..=(random_level-1)
|
|
|
{
|
|
|
- // if(self.current_size!=1)
|
|
|
- // {
|
|
|
- // assert!(!cur_node[i].is_none());
|
|
|
- // }
|
|
|
- if cur_node[i].is_none()
|
|
|
+ if cur_node[i].is_none() //前面给出了两种none的情况一种是这一层什么都没有,一种是这一层第一个节点就大于当前
|
|
|
{
|
|
|
- self.head[i]=Some(new_nodes.clone());
|
|
|
+ if self.head[i].is_none()
|
|
|
+ {
|
|
|
+ self.head[i]=Some(new_nodes.clone());
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ (*new_nodes).borrow_mut().forward[i]=self.head[i].clone();
|
|
|
+ self.head[i]=Some(new_nodes.clone());
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -205,12 +192,11 @@ pub mod skip_lists
|
|
|
}
|
|
|
}
|
|
|
self.current_size=self.current_size+1;
|
|
|
- dbg!(self.current_size);
|
|
|
}
|
|
|
InsertResult::Successful
|
|
|
}
|
|
|
|
|
|
- pub fn found(&self,key:K) ->Option<Rc<RefCell<Node<K,V>>>> //查找数据返回对应节点指针
|
|
|
+ pub fn search(&self,key:K) ->Option<Rc<RefCell<Node<K,V>>>> //查找数据返回对应节点指针
|
|
|
{
|
|
|
let mut middle:Option<Rc<RefCell<Node<K, V>>>>=self.head[(self.current_level-1)].clone();
|
|
|
let mut tmp_level: i32=(self.current_level-1) as i32;
|
|
@@ -259,6 +245,9 @@ pub mod skip_lists
|
|
|
return None //如果查找失败返回一个None
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
pub fn printskip(&self,level:usize)
|
|
|
{
|
|
|
|