Skip List in Golang

Skip list is an ordered map. See wikipedia page skip list to learn algorithm details about this data structure.
Highlights in this implementation:
- Built-in types can be used as key with predefined key types. See Int and related constants as a sample.
- Support custom comparable function so that any type can be used as key.
- Key sort order can be changed quite easily. See Reverse and LessThanFunc.
- Rand source and max level can be changed per list. It can be useful in performance critical scenarios.
Install
Install this package through go get
.
go get github.com/huandu/skiplist
Basic Usage
Here is a quick sample.
package main
import (
"fmt"
"github.com/huandu/skiplist"
)
func main() {
list := skiplist.New(skiplist.Int)
list.Set(12, "hello world")
list.Set(34, 56)
list.Set(78, 90.12)
elem := list.Get(34)
fmt.Println(elem.Value)
next := elem.Next()
prev := next.Prev()
fmt.Println(next.Value, prev.Value)
val, ok := list.GetValue(34)
fmt.Println(val, ok)
foundElem := list.Find(30)
fmt.Println(foundElem.Key(), foundElem.Value)
list.Remove(34)
}
Using GreaterThanFunc
and LessThanFunc
Define your own GreaterThanFunc
or LessThanFunc
to use any custom type as the key in a skip list.
The signature of GreaterThanFunc
are LessThanFunc
are the same.
The only difference between them is that LessThanFunc
reverses result returned by custom func
to make the list ordered by key in a reversed order.
type T struct {
Rad float64
}
list := New(GreaterThanFunc(func(k1, k2 interface{}) int {
s1 := math.Sin(k1.(T).Rad)
s2 := math.Sin(k2.(T).Rad)
if s1 > s2 {
return 1
} else if s1 < s2 {
return -1
}
return 0
}))
list.Set(T{math.Pi / 8}, "sin(π/8)")
list.Set(T{math.Pi / 2}, "sin(π/2)")
list.Set(T{math.Pi}, "sin(π)")
fmt.Println(list.Front().Value)
fmt.Println(list.Back().Value)
License
This library is licensed under MIT license. See LICENSE for details.