The simplest Skiplist
implement ever with 100% test coverage (based on Go 1.18+ Generics).
go get github.com/LaJunkai/skiplist
package main
import (
"fmt"
"github.com/LaJunkai/skiplist"
)
func main() {
list := skiplist.New[string, string]()
// set
if err := list.Set("user-0001.name", "John Wick"); err != nil {
panic(err)
}
// get
value, err := list.Get("user-0001.name")
if err != nil {
panic(err)
}
fmt.Printf("got value: %v\n", value)
// delete
deleted := list.Delete("user-0001.name")
fmt.Printf("successfully deleted: %v", deleted)
// range
list.Range(func(key, value string) bool {
fmt.Printf("key=%v; value=%v", key, value)
return true
})
}
list := skiplist.New[string, string](
skiplist.Concurrent(false),
skiplist.MaxLevels(32),
)
- skiplist use mutex to sync operations from different goroutines by default.
Use
skiplist.Concurrent(false)
to disable the concurrent control. - skiplist support levels in range of
1 - 64
(default max level is 48). Useskiplist.MaxLevels(n)
to custom the max level limit.
err := list.Set(
"key-1", "value-1",
skiplist.OnNotExist(),
)
- Set method support
OnNotExist()
option. With this option passed, an attempt to set an existed key may receive anErrDuplicatedKey
error.
list := New[string, string](Concurrent(true))
list.Range(
func(key, value string) bool {
fmt.Printf("key=%v, value=%v", key, value)
return true
},
From("key-001", true),
To("key-099", false),
)
- Range method support
From(startKey, includeBoundary)
andTo(stopKey, includeBoundary)
options which enable developer to iterate the list with a specified range.