github.com/turingchain2020/turingchain@v1.1.21/common/skiplist/skiplist.go (about)

     1  package skiplist
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  )
     7  
     8  const maxLevel = 32
     9  const prob = 0.35
    10  
    11  // SkipValue 跳跃表节点的Value值
    12  type SkipValue struct {
    13  	Score int64
    14  	Value interface{}
    15  }
    16  
    17  //Compare Const
    18  const (
    19  	Big   = -1
    20  	Small = 1
    21  	Equal = 0
    22  )
    23  
    24  // Compare 比较函数,这样的比较排序是从大到小
    25  func (v *SkipValue) Compare(value *SkipValue) int {
    26  	if v.Score > value.Score {
    27  		return Big
    28  	} else if v.Score == value.Score {
    29  		return Equal
    30  	}
    31  	return Small
    32  }
    33  
    34  // skipListNode 跳跃表节点
    35  type skipListNode struct {
    36  	next  []*skipListNode
    37  	prev  *skipListNode
    38  	Value *SkipValue
    39  }
    40  
    41  // SkipList 跳跃表
    42  type SkipList struct {
    43  	header, tail *skipListNode
    44  	findcount    int
    45  	count        int
    46  	level        int
    47  }
    48  
    49  // Iterator 跳跃表迭代器
    50  type Iterator struct {
    51  	list *SkipList
    52  	node *skipListNode
    53  }
    54  
    55  // First 获取第一个节点Value值
    56  func (sli *Iterator) First() *SkipValue {
    57  	if sli.list.header.next[0] == nil {
    58  		return nil
    59  	}
    60  	sli.node = sli.list.header.next[0]
    61  	return sli.node.Value
    62  }
    63  
    64  // Last 获取最后一个节点Value值
    65  func (sli *Iterator) Last() *SkipValue {
    66  	if sli.list.tail == nil {
    67  		return nil
    68  	}
    69  	sli.node = sli.list.tail
    70  	return sli.node.Value
    71  }
    72  
    73  // Prev 获取迭代器的上一个节点
    74  func (sli *Iterator) Prev() *Iterator {
    75  	sli.node = sli.node.Prev()
    76  	return sli
    77  }
    78  
    79  // Next 获取迭代器的下一个节点
    80  func (sli *Iterator) Next() *Iterator {
    81  	sli.node = sli.node.Next()
    82  	return sli
    83  }
    84  
    85  // Value 获取迭代器当前的Value
    86  func (sli *Iterator) Value() *SkipValue {
    87  	return sli.node.Value
    88  }
    89  
    90  // Prev 获取上一个节点
    91  func (node *skipListNode) Prev() *skipListNode {
    92  	if node == nil || node.prev == nil {
    93  		return nil
    94  	}
    95  	return node.prev
    96  }
    97  
    98  // Next 获取下一个节点
    99  func (node *skipListNode) Next() *skipListNode {
   100  	if node == nil || node.next[0] == nil {
   101  		return nil
   102  	}
   103  	return node.next[0]
   104  }
   105  
   106  // Seek 迭代器在跳跃表中查找某个位置在传参后面或者与传参相等的SkipValue
   107  func (sli *Iterator) Seek(value *SkipValue) *SkipValue {
   108  	x := sli.list.find(value)
   109  	if x.next[0] == nil {
   110  		return nil
   111  	}
   112  	sli.node = x.next[0]
   113  	return sli.node.Value
   114  }
   115  
   116  func newskipListNode(level int, value *SkipValue) *skipListNode {
   117  	node := &skipListNode{}
   118  	node.next = make([]*skipListNode, level)
   119  	node.Value = value
   120  	return node
   121  }
   122  
   123  //NewSkipList 构建一个跳跃表
   124  func NewSkipList(min *SkipValue) *SkipList {
   125  	sl := &SkipList{}
   126  	sl.level = 1
   127  	sl.header = newskipListNode(maxLevel, min)
   128  	return sl
   129  }
   130  
   131  func randomLevel() int {
   132  	level := 1
   133  	t := prob * 0xFFFF
   134  	// #nosec
   135  	for rand.Int()&0xFFFF < int(t) {
   136  		level++
   137  		if level == maxLevel {
   138  			break
   139  		}
   140  	}
   141  	return level
   142  }
   143  
   144  // GetIterator 获取迭代器
   145  func (sl *SkipList) GetIterator() *Iterator {
   146  	it := &Iterator{}
   147  	it.list = sl
   148  	it.First()
   149  	return it
   150  }
   151  
   152  // Len 返回节点数
   153  func (sl *SkipList) Len() int {
   154  	return sl.count
   155  }
   156  
   157  // Level 返回跳跃表的层级
   158  func (sl *SkipList) Level() int {
   159  	return sl.level
   160  }
   161  
   162  func (sl *SkipList) find(value *SkipValue) *skipListNode {
   163  	x := sl.header
   164  	for i := sl.level - 1; i >= 0; i-- {
   165  		for x.next[i] != nil && x.next[i].Value.Compare(value) < 0 {
   166  			sl.findcount++
   167  			x = x.next[i]
   168  		}
   169  	}
   170  	return x
   171  }
   172  
   173  // FindCount 返回查询次数
   174  func (sl *SkipList) FindCount() int {
   175  	return sl.findcount
   176  }
   177  
   178  // Find 查找某个跳跃表中的SkipValue
   179  func (sl *SkipList) Find(value *SkipValue) *SkipValue {
   180  	x := sl.find(value)
   181  	if x.next[0] != nil && x.next[0].Value.Compare(value) == 0 {
   182  		return x.next[0].Value
   183  	}
   184  	return nil
   185  }
   186  
   187  // FindGreaterOrEqual 在跳跃表中查找某个位置在传参后面或者与传参相等的SkipValue
   188  func (sl *SkipList) FindGreaterOrEqual(value *SkipValue) *SkipValue {
   189  	x := sl.find(value)
   190  	if x.next[0] != nil {
   191  		return x.next[0].Value
   192  	}
   193  	return nil
   194  }
   195  
   196  // Insert 插入节点
   197  func (sl *SkipList) Insert(value *SkipValue) int {
   198  	var update [maxLevel]*skipListNode
   199  	x := sl.header
   200  	for i := sl.level - 1; i >= 0; i-- {
   201  		for x.next[i] != nil && x.next[i].Value.Compare(value) <= 0 {
   202  			x = x.next[i]
   203  		}
   204  		update[i] = x
   205  	}
   206  	//if x.next[0] != nil && x.next[0].Value.Compare(value) == 0 { //update
   207  	//	x.next[0].Value = value
   208  	//	return 0
   209  	//}
   210  	level := randomLevel()
   211  	if level > sl.level {
   212  		for i := sl.level; i < level; i++ {
   213  			update[i] = sl.header
   214  		}
   215  		sl.level = level
   216  	}
   217  	x = newskipListNode(level, value)
   218  	for i := 0; i < level; i++ {
   219  		x.next[i] = update[i].next[i]
   220  		update[i].next[i] = x
   221  	}
   222  	//形成一个双向链表
   223  	if update[0] != sl.header {
   224  		x.prev = update[0]
   225  	}
   226  	if x.next[0] != nil {
   227  		x.next[0].prev = x
   228  	} else {
   229  		sl.tail = x
   230  	}
   231  	sl.count++
   232  	return 1
   233  }
   234  
   235  // Delete 删除节点
   236  func (sl *SkipList) Delete(value *SkipValue) int {
   237  	if value == nil {
   238  		return 0
   239  	}
   240  	var update [maxLevel]*skipListNode
   241  	x := sl.header
   242  	for i := sl.level - 1; i >= 0; i-- {
   243  		for x.next[i] != nil && x.next[i].Value.Compare(value) < 0 {
   244  			x = x.next[i]
   245  		}
   246  		update[i] = x
   247  	}
   248  	if x.next[0] == nil || x.next[0].Value.Compare(value) != 0 { //not find
   249  		return 0
   250  	}
   251  	x = x.next[0]
   252  	for i := 0; i < sl.level; i++ {
   253  		if update[i].next[i] == x {
   254  			update[i].next[i] = x.next[i]
   255  		}
   256  	}
   257  	if x.next[0] != nil {
   258  		x.next[0].prev = x.prev
   259  	} else {
   260  		sl.tail = x.prev
   261  	}
   262  	for sl.level > 1 && sl.header.next[sl.level-1] == nil {
   263  		sl.level--
   264  	}
   265  	sl.count--
   266  	return 1
   267  }
   268  
   269  // Print 测试用的输出函数
   270  func (sl *SkipList) Print() {
   271  	if sl.count > 0 {
   272  		r := sl.header
   273  		for i := sl.level - 1; i >= 0; i-- {
   274  			e := r.next[i]
   275  			//fmt.Print(i)
   276  			for e != nil {
   277  				fmt.Print(e.Value.Score)
   278  				fmt.Print("    ")
   279  				fmt.Print(e.Value)
   280  				fmt.Println("")
   281  				e = e.next[i]
   282  			}
   283  			fmt.Println()
   284  		}
   285  	} else {
   286  		fmt.Println("空")
   287  	}
   288  }
   289  
   290  //Walk 遍历整个结构中SkipValue的Value,如果cb 返回false 那么停止遍历
   291  func (sl *SkipList) Walk(cb func(value interface{}) bool) {
   292  	for e := sl.header.Next(); e != nil; e = e.Next() {
   293  		if cb == nil {
   294  			return
   295  		}
   296  		if !cb(e.Value.Value) {
   297  			return
   298  		}
   299  	}
   300  }
   301  
   302  //WalkS 遍历整个结构中的SkipValue,如果cb 返回false 那么停止遍历
   303  func (sl *SkipList) WalkS(cb func(value interface{}) bool) {
   304  	for e := sl.header.Next(); e != nil; e = e.Next() {
   305  		if cb == nil {
   306  			return
   307  		}
   308  		if !cb(e.Value) {
   309  			return
   310  		}
   311  	}
   312  }