|
@@ -7,13 +7,14 @@
|
|
|
#[allow(unused)]
|
|
|
pub mod skip_lists
|
|
|
{
|
|
|
-
|
|
|
- use std::{borrow::{Borrow, BorrowMut}, cell::{Ref, RefCell}, collections::btree_map::Values, rc::Rc};
|
|
|
+ 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 crate::algorithm::{baisc_data::basic_data, random};
|
|
|
use crate::algorithm::baisc_data;
|
|
|
-
|
|
|
-
|
|
|
+ use rand::Rng;
|
|
|
+
|
|
|
+ #[derive(PartialEq)]
|
|
|
pub enum InsertResult
|
|
|
{
|
|
|
Successful,
|
|
@@ -26,9 +27,9 @@ pub mod skip_lists
|
|
|
/// <p>We will build a kv database,so the node has two members key and value.</p>
|
|
|
///
|
|
|
/// <p>The node would never be used out of the mod.</p>
|
|
|
- struct Node<K:PartialOrd,V>
|
|
|
- where K:Copy ,
|
|
|
- V:Clone
|
|
|
+ pub struct Node<K:PartialOrd,V>
|
|
|
+ where K:Copy+Display,
|
|
|
+ V:Clone+Display
|
|
|
{
|
|
|
key:K,
|
|
|
value:V,
|
|
@@ -36,8 +37,8 @@ pub mod skip_lists
|
|
|
forward:Vec<Option<Rc<RefCell<Node<K,V>>>>>
|
|
|
}
|
|
|
impl<K:PartialOrd,V> Node<K,V>
|
|
|
- where K:Copy,
|
|
|
- V:Clone{
|
|
|
+ where K:Copy+Display,
|
|
|
+ V:Clone+Display{
|
|
|
fn new(_key:K,_value:V,_level:usize)->Rc<RefCell<Self>>
|
|
|
{
|
|
|
Rc::new(
|
|
@@ -60,9 +61,9 @@ pub mod skip_lists
|
|
|
///
|
|
|
#[derive(Debug)]
|
|
|
pub struct Skiplists<K,V>
|
|
|
- where K:Copy,
|
|
|
+ where K:Copy+Display,
|
|
|
K:PartialOrd,
|
|
|
- V:Clone
|
|
|
+ V:Clone+Display
|
|
|
{
|
|
|
max_level:usize,
|
|
|
current_level:usize,
|
|
@@ -72,8 +73,8 @@ pub mod skip_lists
|
|
|
}
|
|
|
|
|
|
impl<K:PartialOrd,V> Skiplists<K,V>
|
|
|
- where K:Copy,
|
|
|
- V:Clone
|
|
|
+ where K:Copy+Display,
|
|
|
+ V:Clone+Display
|
|
|
{
|
|
|
pub fn new(_max_size:usize)->Self
|
|
|
{
|
|
@@ -94,84 +95,105 @@ pub mod skip_lists
|
|
|
Node::new(_key, _value, _level)
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
pub fn insert(&mut self,key:K,value:V)->InsertResult
|
|
|
{
|
|
|
+ let mut rng: rand::prelude::ThreadRng = rand::thread_rng();
|
|
|
if self.current_size==0 //如果shiplist是全空的,直接将点插入
|
|
|
{
|
|
|
- let random_level: usize=random::random::lgc(self.max_level);
|
|
|
+ //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 new_node: Rc<RefCell<Node<K, V>>>=Self::creat_node(key,value,random_level); //创建新的节点
|
|
|
- for i in 0..=random_level
|
|
|
+ for i in 0..=(random_level-1)
|
|
|
{
|
|
|
self.head[i]=Some(new_node.clone());
|
|
|
}
|
|
|
+ self.current_size=self.current_size+1;
|
|
|
+ self.current_level=random_level;
|
|
|
+ dbg!(self.current_size);
|
|
|
}
|
|
|
else //当list不为空的时候需要判定插入的位置
|
|
|
{
|
|
|
- let random_level:usize=random::random::lgc(self.max_level);
|
|
|
+ let random_level:usize=rng.gen_range(1..(self.max_level));
|
|
|
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-1) as i32;
|
|
|
-
|
|
|
+ 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)-2 //如果self.max_level小于2那么这个显然不成立
|
|
|
+ else
|
|
|
+ { //判定每一层的起始位置
|
|
|
+ if cur_level ==((self.max_level) as i32)-1 || cur_node[(cur_level as usize+1)].is_none()
|
|
|
{
|
|
|
- if 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就大于当前数据(主要是没有一个空的头节点就比较复杂)
|
|
|
{
|
|
|
- cur_point=self.head[cur_level as usize].clone();
|
|
|
- if (*(cur_point.clone().unwrap())).borrow().key<key //如果第一个节点key就大于当前数据(主要是没有一个空的头节点就比较复杂)
|
|
|
- {
|
|
|
- cur_level=cur_level-1;
|
|
|
- continue;
|
|
|
- }
|
|
|
- else if (*(cur_point.clone().unwrap())).borrow().key==key
|
|
|
- {
|
|
|
- (*(*(cur_point.clone().unwrap())).borrow_mut()).value=value.clone();
|
|
|
- return InsertResult::Exist;
|
|
|
- }
|
|
|
+ dbg!("find");
|
|
|
+ cur_node[cur_level as usize]=None;
|
|
|
+ cur_level=cur_level-1;
|
|
|
+ continue;
|
|
|
}
|
|
|
- }
|
|
|
- else {
|
|
|
- cur_point=cur_node[cur_level as usize+1].clone();
|
|
|
+ else if (*(cur_point.clone().unwrap())).borrow().key==key
|
|
|
+ {
|
|
|
+ (*(*(cur_point.clone().unwrap())).borrow_mut()).value=value.clone();
|
|
|
+ return InsertResult::Exist;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
loop
|
|
|
{
|
|
|
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
|
|
|
{
|
|
|
- (*(((*(*(cur_point.clone().unwrap())).borrow_mut()).forward[cur_level as usize]).clone()).unwrap()).borrow_mut().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
|
|
|
+ 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();
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
cur_level=cur_level-1;
|
|
|
}
|
|
|
}
|
|
|
let mut middle_ptr:Option<Rc<RefCell<Node<K,V>>>>=None;
|
|
|
let mut new_nodes:Rc<RefCell<Node<K, V>>> =Self::creat_node(key, value, random_level);
|
|
|
- for i in 0..=random_level-1
|
|
|
+ if random_level>self.current_level
|
|
|
{
|
|
|
+ self.current_level=random_level;
|
|
|
+ }
|
|
|
+
|
|
|
+ for i in 0..=(random_level-1)
|
|
|
+ {
|
|
|
+ // if(self.current_size!=1)
|
|
|
+ // {
|
|
|
+ // assert!(!cur_node[i].is_none());
|
|
|
+ // }
|
|
|
if cur_node[i].is_none()
|
|
|
{
|
|
|
self.head[i]=Some(new_nodes.clone());
|
|
@@ -182,9 +204,79 @@ pub mod skip_lists
|
|
|
(*(cur_node[i].clone().unwrap())).borrow_mut().forward[i]=Some(new_nodes.clone());
|
|
|
}
|
|
|
}
|
|
|
+ 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>>>> //查找数据返回对应节点指针
|
|
|
+ {
|
|
|
+ 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;
|
|
|
+ loop
|
|
|
+ {
|
|
|
+ if (*((*((middle.clone()).unwrap())).borrow())).key==key //头节点等于所需查找的节点
|
|
|
+ {
|
|
|
+ return middle.clone();
|
|
|
+ }
|
|
|
+ else if (*((*((middle.clone()).unwrap())).borrow())).key<key
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ tmp_level=tmp_level-1;
|
|
|
+ }
|
|
|
+ //当查找到最下面的时候所有的key都大于需要的说明不存在
|
|
|
+ if(tmp_level>=0)
|
|
|
+ {
|
|
|
+ middle=self.head[tmp_level as usize].clone();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return None;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ while(tmp_level>=0)
|
|
|
+ {
|
|
|
+ while(!((*((middle.clone()).unwrap())).borrow().forward[tmp_level as usize]).is_none()) //如果当这个下一个节点非空的时候非空的时候
|
|
|
+ {
|
|
|
+ if ((*((*((middle.clone()).unwrap())).borrow().forward[tmp_level as usize]).clone().unwrap()).borrow().key)>=key
|
|
|
+ {
|
|
|
+ if ((*((*((middle.clone()).unwrap())).borrow().forward[tmp_level as usize]).clone().unwrap()).borrow().key)==key
|
|
|
+ {
|
|
|
+ return ((*((middle.clone()).unwrap())).borrow().forward[tmp_level as usize]).clone();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ middle=(*((middle.clone()).unwrap())).borrow().forward[tmp_level as usize].clone(); //向后推进到下一个节点
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tmp_level=tmp_level-1;
|
|
|
+ }
|
|
|
+ return None //如果查找失败返回一个None
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn printskip(&self,level:usize)
|
|
|
+ {
|
|
|
+
|
|
|
+ let mut middle:Option<Rc<RefCell<Node<K, V>>>>=self.head[level].clone(); //直接遍历第0行即可
|
|
|
+ while !((middle.clone()).is_none())
|
|
|
+ {
|
|
|
+ print!("(");
|
|
|
+ print!("key={}",((*(middle.clone().unwrap())).borrow()).key);
|
|
|
+ print!("value={}",((*(middle.clone().unwrap())).borrow()).value);
|
|
|
+ print!(")=>");
|
|
|
+ middle=(*((middle.clone()).unwrap())).borrow().forward[level].clone();
|
|
|
+
|
|
|
+ unsafe
|
|
|
+ {
|
|
|
+ COUNTS=COUNTS+1;
|
|
|
+ println!("COUNT={}",COUNTS);
|
|
|
+ }
|
|
|
}
|
|
|
- InsertResult::Successful
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
}
|