Browse Source

重写跳表逻辑,目前版本已经没有逻辑错误

Gogs 4 months ago
parent
commit
33ae6a352e
5 changed files with 125 additions and 24 deletions
  1. 4 0
      src/algorithm/baisc_data.rs
  2. 2 1
      src/algorithm/mod.rs
  3. 1 0
      src/algorithm/random.rs
  4. 113 21
      src/basic_structure/skiplist.rs
  5. 5 2
      src/main.rs

+ 4 - 0
src/algorithm/baisc_data.rs

@@ -0,0 +1,4 @@
+pub mod basic_data
+{
+    pub static MAX_SIZE_OF_SKIPLIST:usize=10240;
+}

+ 2 - 1
src/algorithm/mod.rs

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

+ 1 - 0
src/algorithm/random.rs

@@ -7,6 +7,7 @@
 pub mod random{
     ///
     /// ## LGC 
+    /// 线性混合同余法
     ///$$
     /// x_0=SEED
     /// $$

+ 113 - 21
src/basic_structure/skiplist.rs

@@ -1,28 +1,43 @@
 ///# Skiplist
 ///<p>skiplist is an important data structure of database and many database use this as the basic structure in memory.
 /// 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> 
 
-use crate::algorithm;
+
+#[allow(unused)]
 pub mod skip_lists
 {
-    use std::{cell::RefCell, rc::Rc};
+    
+    use std::{borrow::{Borrow, BorrowMut}, cell::{Ref, RefCell}, collections::btree_map::Values, rc::Rc};
+    use std::cmp::PartialOrd;
+    use crate::algorithm::{baisc_data::basic_data, random};
+    use crate::algorithm::baisc_data;
 
-    use crate::algorithm::random;
 
+    pub enum InsertResult
+    {
+        Successful,
+        Fault,
+        Exist
+    }
     #[derive(Debug,Clone)]
     ///## Node of skiplist
     ///
     /// <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,V>                              
+    struct Node<K:PartialOrd,V>
+    where  K:Copy ,
+           V:Clone                             
     {
         key:K,
         value:V,
         level:usize,
         forward:Vec<Option<Rc<RefCell<Node<K,V>>>>>
     }
-    impl<K,V> Node<K,V>{
+    impl<K:PartialOrd,V> Node<K,V>
+    where  K:Copy, 
+           V:Clone{
         fn new(_key:K,_value:V,_level:usize)->Rc<RefCell<Self>>
         {
             Rc::new(
@@ -45,14 +60,20 @@ pub mod skip_lists
     /// 
     #[derive(Debug)]
     pub struct Skiplists<K,V>
+    where  K:Copy,
+           K:PartialOrd,
+           V:Clone 
     {
         max_level:usize,
         current_level:usize,
         current_size:usize,
+        max_size:usize,
         head:Vec<Option<Rc<RefCell<Node<K,V>>>>>
     }
 
-    impl<K,V> Skiplists<K,V>
+    impl<K:PartialOrd,V> Skiplists<K,V>
+    where  K:Copy,
+           V:Clone 
     {
         pub fn new(_max_size:usize)->Self
         {
@@ -62,6 +83,7 @@ pub mod skip_lists
                 max_level:_max_size,
                 current_level:0,
                 current_size:0,
+                max_size:basic_data::MAX_SIZE_OF_SKIPLIST,
                 head:vec![None;_max_size+1]
 
             }
@@ -72,27 +94,97 @@ pub mod skip_lists
             Node::new(_key, _value, _level)
         }
 
-        fn insert(&mut self,_key:K,_value:V)->bool
+
+        pub fn insert(&mut self,key:K,value:V)->InsertResult
         {
-            let mut update:Vec<Option<Rc<RefCell<Node<K,V>>>>>=vec![None;self.current_level];
-            let mut _node_level=random::random::lgc(self.max_level);
-            let new_node: Rc<RefCell<Node<K, V>>>=Node::<K,V>::new(_key, _value, _node_level);
-            if self.current_size==0
+            if self.current_size==0                //如果shiplist是全空的,直接将点插入
             {
-                let mut begin:usize=0;
-                
-                while(begin<=_node_level)
+                let random_level: usize=random::random::lgc(self.max_level);
+                let new_node: Rc<RefCell<Node<K, V>>>=Self::creat_node(key,value,random_level);      //创建新的节点
+                for i in 0..=random_level
                 {
-                    self.head[begin]=Some(new_node.clone());             //if the skiplist is empty,insert directly.
-                    begin=begin+1;
+                    self.head[i]=Some(new_node.clone());
                 }
-                
             }
-            else {
-                let mut tmp:usize=_node_level;                     //search the correct place up to down
+            else                    //当list不为空的时候需要判定插入的位置
+            {
+                let random_level:usize=random::random::lgc(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;
                 
+                while(cur_level>=0)
+                {
+                    
+                    if self.head[cur_level as usize].is_none()                //如果起始点为空则直接跳过(在这一层不会有节点)
+                    {
+                        cur_level=cur_level-1;
+                        continue;
+                    }
+                    else {                                        //判定每一层的起始位置
+                        if cur_level <=((self.max_level) as i32)-2               //如果self.max_level小于2那么这个显然不成立
+                        {
+                            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_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;
+                                } 
+                            }
+                        }
+                        else {
+                            cur_point=cur_node[cur_level as usize+1].clone();
+                        }
+                        loop 
+                        {
+                            if (*(*(cur_point.clone().unwrap())).borrow_mut()).forward[cur_level as usize].is_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;      //将对应的节点数据修改
+                                return InsertResult::Exist;
+                            }
+                            else if (*(((*(*(cur_point.clone().unwrap())).borrow_mut()).forward[cur_level as usize]).clone()).unwrap()).borrow().key<key
+                            {
+                                cur_node[cur_level as usize]=cur_point.clone();
+                                break;
+                            }
+                            else
+                            {
+                                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 cur_node[i].is_none()
+                    {
+                        self.head[i]=Some(new_nodes.clone());
+                    }
+                    else
+                    {
+                        (*new_nodes).borrow_mut().forward[i]=((*(*((cur_node[i].clone()).unwrap())).borrow_mut()).forward[i]).clone();
+                        (*(cur_node[i].clone().unwrap())).borrow_mut().forward[i]=Some(new_nodes.clone());
+                    }
+                }
             }
-            true
+            InsertResult::Successful
+            
         }
     }
-}
+}

+ 5 - 2
src/main.rs

@@ -1,6 +1,9 @@
+use basic_structure::skiplist::skip_lists;
+
 mod basic_structure;
 mod algorithm;
 fn main() {
-    let  data: basic_structure::skiplist::skip_lists::Skiplists<i32, i32>=basic_structure::skiplist::skip_lists::Skiplists::<i32,i32>::new(1);
-    println!("Hello, world!");
+    let mut data=skip_lists::Skiplists::<i32,i32>::new(15);
+    data.insert(1, 7);
+    
 }