Skip to content

Commit

Permalink
optimize skiplist (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng authored Aug 24, 2022
1 parent f225bbc commit 0c7f953
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions skiplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ func (sl *SkipList[K, V]) Insert(key K, value V) {
level := sl.randomLevel()
node = newSkipListNode(level, key, value)

// Insert node to each level
for i := 0; i < Min(level, sl.level); i++ {
node.next[i] = prevs[i].next[i]
prevs[i].next[i] = node
}

if level > sl.level {
// Increase the level
for i := sl.level; i < level; i++ {
sl.head.next[i] = node
}
Expand Down Expand Up @@ -156,9 +158,10 @@ func (sl *SkipList[K, V]) Remove(key K) bool {
if node == nil {
return false
}
for i, v := range node.next {
for i, v := range node.next { // Nomove the node from each level's links.
prevs[i].next[i] = v
}
// Decrease the level if the top level become empty
for sl.level > 1 && sl.head.next[sl.level-1] == nil {
sl.level--
}
Expand Down Expand Up @@ -253,7 +256,7 @@ func (sl *SkipList[K, V]) init() {

func (sl *SkipList[K, V]) randomLevel() int {
total := uint64(1)<<uint64(skipListMaxLevel) - 1 // 2^n-1
k := sl.rander.Uint64() % total
k := sl.rander.Uint64() & total
level := skipListMaxLevel - bits.Len64(k) + 1
// Since levels are randomly generated, most should be less than log2(s.len).
// Then make a limit according to sl.len to avoid unexpectedly large value.
Expand Down

0 comments on commit 0c7f953

Please sign in to comment.