Browse Source

完成了插入的逻辑,并且成功通过测试,目前还没有进行压测

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

+ 10 - 0
.vscode/launch.json

@@ -0,0 +1,10 @@
+{
+    // 使用 IntelliSense 了解相关属性。 
+    // 悬停以查看现有属性的描述。
+    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
+    
+        "version": "0.2.0",
+        "configurations": [
+            
+        ]
+    }

+ 133 - 0
Cargo.lock

@@ -2,6 +2,139 @@
 # It is not intended for manual editing.
 version = 3
 
+[[package]]
+name = "byteorder"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "getrandom"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.164"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f"
+
+[[package]]
+name = "ppv-lite86"
+version = "0.2.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
+dependencies = [
+ "zerocopy",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.89"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.37"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "rand"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+dependencies = [
+ "getrandom",
+]
+
 [[package]]
 name = "rustdb"
 version = "0.1.0"
+dependencies = [
+ "rand",
+]
+
+[[package]]
+name = "syn"
+version = "2.0.87"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
+
+[[package]]
+name = "wasi"
+version = "0.11.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
+
+[[package]]
+name = "zerocopy"
+version = "0.7.35"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
+dependencies = [
+ "byteorder",
+ "zerocopy-derive",
+]
+
+[[package]]
+name = "zerocopy-derive"
+version = "0.7.35"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]

+ 1 - 0
Cargo.toml

@@ -4,3 +4,4 @@ version = "0.1.0"
 edition = "2021"
 
 [dependencies]
+rand = "0.8.5"

+ 8 - 3
src/algorithm/random.rs

@@ -15,14 +15,19 @@ pub mod random{
     /// X_n=(A \times X_(n-1) + C)%M
     /// $$
     /// 
-    static mut SEED:usize=1;                    //全局静态变量用于保存数据
+    static mut SEED:usize=13;                    //全局静态变量用于保存数据
     static A:usize=1103515245;
     static C:usize=12345;
     
     pub fn lgc(_level:usize)->usize{                        //线性混合同余法求解随机数
     unsafe {
-        SEED=(SEED*A+C)%_level;
-        SEED
+            SEED=(SEED*A+C)%_level;
+            dbg!(SEED);
+            while SEED==0 
+            {
+                SEED=(SEED*A+C)%_level;
+            }
+            SEED
         }
     }
 

+ 5 - 0
src/basic_structure/bloomfilter.rs

@@ -0,0 +1,5 @@
+
+mod bloomfilter
+{
+    
+}

+ 2 - 1
src/basic_structure/mod.rs

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

+ 135 - 43
src/basic_structure/skiplist.rs

@@ -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
-            
         }
     }
 }

+ 7 - 3
src/main.rs

@@ -1,9 +1,13 @@
+
 use basic_structure::skiplist::skip_lists;
 
 mod basic_structure;
 mod algorithm;
 fn main() {
-    let mut data=skip_lists::Skiplists::<i32,i32>::new(15);
-    data.insert(1, 7);
-    
+    let mut data: skip_lists::Skiplists<i32, i32>=skip_lists::Skiplists::<i32,i32>::new(5);
+
+    println!();
+    // data.printskip(2);
+    // println!();
+    // data.printskip(3);
 }