changeset 2:0c8c5df504fd

Use geometric distribution for skips heights
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 09 Jun 2016 05:56:31 +0000
parents ca4d8694fb33
children cd0da27876f0
files src/skipmap.rs
diffstat 1 files changed, 8 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/skipmap.rs	Wed Jun 08 20:58:50 2016 +0200
+++ b/src/skipmap.rs	Thu Jun 09 05:56:31 2016 +0000
@@ -6,6 +6,7 @@
 use std::mem::{replace, transmute_copy};
 
 const MAX_HEIGHT: usize = 12;
+const BRANCHING_FACTOR: u32 = 4;
 
 pub trait Comparator {
     fn cmp(&Vec<u8>, &Vec<u8>) -> Ordering;
@@ -63,7 +64,13 @@
         self.len
     }
     fn random_height(&mut self) -> usize {
-        1 + (self.rand.next_u32() as usize % (MAX_HEIGHT - 1))
+        let mut height = 1;
+
+        while height < MAX_HEIGHT && self.rand.next_u32() % BRANCHING_FACTOR == 0 {
+            height += 1;
+        }
+
+        height
     }
 
     fn contains(&mut self, key: &Vec<u8>) -> bool {