forked from sean-public/fast-skiplist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype.go
54 lines (45 loc) · 1.03 KB
/
type.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
package skiplist
import (
"math/rand"
"sync"
"sync/atomic"
"unsafe"
)
type elementNode struct {
list *SkipList
next []unsafe.Pointer
}
func (n *elementNode) Next() *Element {
return n.NextAt(0)
}
func (n *elementNode) NextAt(i int) *Element {
return (*Element)(atomic.LoadPointer(&n.next[i]))
}
type Element struct {
elementNode
key []byte
value interface{}
}
// Key allows retrieval of the key for a given Element
func (e *Element) Key() []byte {
return e.key
}
// Value allows retrieval of the value for a given Element
func (e *Element) Value() interface{} {
return e.value
}
// Next returns the following Element or nil if we're at the end of the list.
// Only operates on the bottom level of the skip list (a fully linked list).
func (element *Element) Next() *Element {
return element.elementNode.Next()
}
type SkipList struct {
elementNode
maxLevel int
Length int
randSource rand.Source
probability float64
probTable []float64
mutex sync.RWMutex
prevNodesCache []*elementNode
}