| 1 |
package skip |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/rand" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
type ( |
| 10 |
node struct { |
| 11 |
next []*node |
| 12 |
key interface{} |
| 13 |
value interface{} |
| 14 |
} |
| 15 |
SkipList struct { |
| 16 |
root *node |
| 17 |
size int |
| 18 |
less func(interface{},interface{})bool |
| 19 |
gen *rand.Rand |
| 20 |
probability float64 |
| 21 |
} |
| 22 |
) |
| 23 |
|
| 24 |
func New(less func(interface{},interface{})bool) *SkipList { |
| 25 |
gen := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 26 |
n := &node{make([]*node, 0),nil,nil} |
| 27 |
return &SkipList{n, 0, less, gen, 0.75} |
| 28 |
} |
| 29 |
func (this *SkipList) Do(f func(interface{}, interface{})bool) { |
| 30 |
if this.size == 0 { |
| 31 |
return |
| 32 |
} |
| 33 |
cur := this.root.next[0] |
| 34 |
for cur != nil { |
| 35 |
if !f(cur.key, cur.value) { |
| 36 |
break |
| 37 |
} |
| 38 |
cur = cur.next[0] |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func (this *SkipList) Get(key interface{}) interface{} { |
| 43 |
if this.size == 0 { |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
cur := this.root |
| 48 |
|
| 49 |
for i := len(cur.next)-1; i >= 0; i-- { |
| 50 |
for this.less(cur.next[i].key, key) { |
| 51 |
cur = cur.next[i] |
| 52 |
} |
| 53 |
} |
| 54 |
cur = cur.next[0] |
| 55 |
|
| 56 |
if this.equals(cur.key, key) { |
| 57 |
return cur.value |
| 58 |
} |
| 59 |
|
| 60 |
return nil |
| 61 |
} |
| 62 |
|
| 63 |
func (this *SkipList) Insert(key interface{}, value interface{}) { |
| 64 |
prev := this.getPrevious(key) |
| 65 |
|
| 66 |
|
| 67 |
if len(prev) > 0 && prev[0].next[0] != nil && this.equals(prev[0].next[0].key, key) { |
| 68 |
prev[0].next[0].value = value |
| 69 |
return |
| 70 |
} |
| 71 |
|
| 72 |
h := len(this.root.next) |
| 73 |
nh := this.pickHeight() |
| 74 |
n := &node{make([]*node, nh),key,value} |
| 75 |
|
| 76 |
|
| 77 |
if nh > h { |
| 78 |
this.root.next = append(this.root.next, n) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
for i := 0; i < h && i < nh; i++ { |
| 83 |
n.next[i] = prev[i].next[i] |
| 84 |
prev[i].next[i] = n |
| 85 |
} |
| 86 |
|
| 87 |
this.size++ |
| 88 |
} |
| 89 |
|
| 90 |
func (this *SkipList) Len() int { |
| 91 |
return this.size |
| 92 |
} |
| 93 |
|
| 94 |
func (this *SkipList) Remove(key interface{}) interface{} { |
| 95 |
prev := this.getPrevious(key) |
| 96 |
if len(prev) == 0 { |
| 97 |
return nil |
| 98 |
} |
| 99 |
cur := prev[0].next[0] |
| 100 |
|
| 101 |
|
| 102 |
if cur != nil && this.equals(key, cur.key) { |
| 103 |
|
| 104 |
for i := 0; i < len(prev); i++ { |
| 105 |
if prev[i] != nil && prev[i].next[i] != nil { |
| 106 |
prev[i].next[i] = cur.next[i] |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
for i := len(this.root.next)-1; i>=0; i-- { |
| 112 |
if this.root.next[i] == nil { |
| 113 |
this.root.next = this.root.next[:i] |
| 114 |
} else { |
| 115 |
break |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
this.size-- |
| 120 |
|
| 121 |
return cur.value |
| 122 |
} |
| 123 |
|
| 124 |
return nil |
| 125 |
} |
| 126 |
|
| 127 |
func (this *SkipList) String() string { |
| 128 |
str := "{" |
| 129 |
if len(this.root.next) > 0 { |
| 130 |
cur := this.root.next[0] |
| 131 |
for cur != nil { |
| 132 |
str += fmt.Sprint(cur.key) |
| 133 |
str += ":" |
| 134 |
str += fmt.Sprint(cur.value) |
| 135 |
str += " " |
| 136 |
cur = cur.next[0] |
| 137 |
} |
| 138 |
} |
| 139 |
str += "}" |
| 140 |
|
| 141 |
return str |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
func (this *SkipList) getPrevious(key interface{}) []*node { |
| 146 |
cur := this.root |
| 147 |
h := len(cur.next) |
| 148 |
nodes := make([]*node, h) |
| 149 |
for i := h-1; i >= 0; i-- { |
| 150 |
for cur.next[i] != nil && this.less(cur.next[i].key, key) { |
| 151 |
cur = cur.next[i] |
| 152 |
} |
| 153 |
nodes[i] = cur |
| 154 |
} |
| 155 |
return nodes |
| 156 |
} |
| 157 |
|
| 158 |
func (this *SkipList) equals(a, b interface{}) bool { |
| 159 |
return !this.less(a,b) && !this.less(b,a) |
| 160 |
} |
| 161 |
|
| 162 |
func (this *SkipList) pickHeight() int { |
| 163 |
h := 1 |
| 164 |
for this.gen.Float64() > this.probability { |
| 165 |
h++ |
| 166 |
} |
| 167 |
if h > len(this.root.next) { |
| 168 |
return h + 1 |
| 169 |
} |
| 170 |
return h |
| 171 |
} |