Browse Source

完成insert函数的压测,同时处理完其中的所有bug

Gogs 4 tháng trước cách đây
mục cha
commit
82b12f948b

+ 2 - 1
src/algorithm/mod.rs

@@ -1,2 +1,3 @@
 pub mod random;
-pub mod baisc_data;
+pub mod baisc_data;
+pub mod sha256;

+ 4 - 0
src/algorithm/sha256.rs

@@ -0,0 +1,4 @@
+mod sha_256
+{
+    
+}

+ 2 - 1
src/basic_structure/mod.rs

@@ -1,2 +1,3 @@
 pub mod skiplist;
-pub mod bloomfilter;
+pub mod bloomfilter;
+pub mod test;

+ 18 - 29
src/basic_structure/skiplist.rs

@@ -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)
         {
 

+ 16 - 0
src/basic_structure/test.rs

@@ -0,0 +1,16 @@
+mod test{
+    use rand::Rng;
+    use std::time::{Duration, Instant};
+    #[test]
+    fn test_insert()
+    {
+       
+        let mut data: crate::basic_structure::skiplist::skip_lists::Skiplists<i32, i32>=crate::basic_structure::skiplist::skip_lists::Skiplists::<i32,i32>::new(5);
+        let mut rng: rand::prelude::ThreadRng = rand::thread_rng();
+        for i in 0..100
+        {
+            let keys=rng.gen_range(1..102);
+            data.insert(keys, i as i32);
+        }
+    }
+}

+ 30 - 6
src/main.rs

@@ -1,13 +1,37 @@
 
 use basic_structure::skiplist::skip_lists;
-
 mod basic_structure;
 mod algorithm;
+#[warn(unused_imports)]
+use std::time::{Duration, Instant};
+use rand::Rng;
+
 fn main() {
-    let mut data: skip_lists::Skiplists<i32, i32>=skip_lists::Skiplists::<i32,i32>::new(5);
+    let mut data:skip_lists::Skiplists<i32, i32>=skip_lists::Skiplists::<i32,i32>::new(15);
+    let mut rng: rand::prelude::ThreadRng = rand::thread_rng();
+    let start: Instant = Instant::now();
+    for i in 0..1000000
+    {
+        let keys=rng.gen_range(1..10002);
+        data.insert(keys, i as i32);
+    }
+    let duration: Duration = start.elapsed();
+    data.printskip(0);
+    println!("time={:?}",duration);
+
+
+
+    // let value=0;
 
-    println!();
-    // data.printskip(2);
-    // println!();
-    // data.printskip(3);
+    // data.insert(2, value);
+    // data.insert(7, value);
+    // data.insert(5, value);
+    // data.insert(6, value);
+    // data.insert(4, value);
+    // data.insert(1, value);
+    // data.insert(3, value);
+    // data.insert(9, value);
+    // data.insert(5, value);
+    // data.insert(3, value);
+    // data.printskip(0);
 }