-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskip_node.go
57 lines (48 loc) · 1.22 KB
/
skip_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package skiptable
type SkipListLevel struct {
//指向下一个结点
forward *SkipListNode
/*
* 到下一个node的距离;
* 思考,为啥是记录到下一个node, 而不是记录上一个node到这的距离
*/
span int64
}
type SkipListNode struct {
//指向上一个结点
backward *SkipListNode
//索引用的层
level []SkipListLevel
//存储的值
value ISkipListNode
//排名用的分数
score float64
}
func NewSkipListNode(level int, score float64, value ISkipListNode) *SkipListNode {
return &SkipListNode{
backward: nil,
level: make([]SkipListLevel, level),
value: value,
score: score,
}
}
//第i层的下一个元素
func (node *SkipListNode) Next(i int) *SkipListNode {
return node.level[i].forward
}
//设置第i层的下一个元素
func (node *SkipListNode) SetNext(i int, next *SkipListNode) {
node.level[i].forward = next
}
//第i层的span值
func (node *SkipListNode) Span(i int) int64 {
return node.level[i].span
}
//设置第i层的span值
func (node *SkipListNode) SetSpan(i int, span int64) {
node.level[i].span = span
}
//上一个元素 想一下,为啥指向上一个的元素不需要i呢???
func (node *SkipListNode) Pre() *SkipListNode {
return node.backward
}