https://github.com/liwnn/skiplist
A fast skip list implementation for Go
https://github.com/liwnn/skiplist
algorithm data-structures go golang skiplist
Last synced: 9 months ago
JSON representation
A fast skip list implementation for Go
- Host: GitHub
- URL: https://github.com/liwnn/skiplist
- Owner: liwnn
- Created: 2022-06-18T10:30:26.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-29T12:32:19.000Z (over 3 years ago)
- Last Synced: 2025-01-11T02:46:33.099Z (11 months ago)
- Topics: algorithm, data-structures, go, golang, skiplist
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Skip list implementation for Go
This Go package provides an implementation of skip list: [Skip Lists: A Probabilistic Alternative to Balanced Trees](https://www.cl.cam.ac.uk/teaching/2005/Algorithms/skiplists.pdf).
## Usage
All you have to do is to implement a comparison `function Less() bool` for your Item which will be store in the skip list, here are some examples.
A case for `int` items.
``` go
package main
import (
"github.com/liwnn/skiplist"
)
func main() {
sl := skiplist.New()
// Insert some values
for i := 0; i < 10; i++ {
sl.Insert(skiplist.Int(i))
}
// Get the value of the key
item := sl.Search(skiplist.Int(5))
if item != nil {
fmt.Println(item)
}
// Delete the key
if sl.Delete(skiplist.Int(4)) {
fmt.Println("Deleted", 4)
}
// Traverse the list
for it := sl.NewIterator(); it.Valid(); it.Next() {
fmt.Println(it.Value().(skiplist.Int))
}
}
```
A case for `struct` items:
``` go
package main
import (
"github.com/liwnn/skiplist"
)
type KV struct {
Key int
Value int
}
func (kv KV) Less(than skiplist.Item) bool {
return kv.Key < than.(KV).Key
}
func main() {
sl := skiplist.New()
// Insert some values
for i := 0; i < 10; i++ {
sl.Insert(KV{Key: i, Value: 100 + i})
}
// Get the value of the key
item := sl.Search(KV{Key: 1})
if item != nil {
fmt.Println(item.(KV))
}
// Delete the key
if sl.Delete(KV{Key: 4}) {
fmt.Println("Deleted", 4)
}
// Traverse the list
for it := sl.NewIterator(); it.Valid(); it.Next() {
fmt.Println(it.Value().(KV))
}
}
```