Documentation
¶
Overview ¶
Package skiplist is a sorted []byte key/value store.
Put, Get, and Pop are O(log n) on average. Keys sort with bytes.Compare. Not safe for concurrent use.
s := NewSkipList(0, 0) // defaults: max level 32, p 0.5
s.Put([]byte("a"), []byte("1"))
v, _ := s.Get([]byte("a"))
s.Pop([]byte("a"))
for k, v := range s.All() { _ = k; _ = v }
Example ¶
Example demonstrates basic usage of SkipList.
s := NewSkipList(16, 0.5)
s.Put([]byte("cat"), []byte("meow"))
s.Put([]byte("dog"), []byte("woof"))
s.Put([]byte("cat"), []byte("purr")) // update
if v, err := s.Get([]byte("cat")); err == nil {
fmt.Println("cat ->", string(v))
}
fmt.Println("All entries:")
for k, v := range s.All() {
fmt.Printf(" %s: %s\n", k, v)
}
s.Pop([]byte("dog"))
fmt.Println("After pop dog, len via iteration:")
count := 0
for range s.All() {
count++
}
fmt.Println("count:", count)
Output: cat -> purr All entries: cat: purr dog: woof After pop dog, len via iteration: count: 1
Index ¶
- Variables
- func DefaultValues() (int, float64)
- type Element
- type SkipList
- func (s *SkipList) All() iter.Seq2[[]byte, []byte]
- func (s *SkipList) ForEach(do func(key, value []byte) bool)
- func (s *SkipList) Get(key []byte) ([]byte, error)
- func (s *SkipList) Len() int
- func (s *SkipList) NewElement(key, val []byte, l int) *Element
- func (s *SkipList) Pop(key []byte) error
- func (s *SkipList) Put(key, val []byte) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func DefaultValues ¶ added in v0.0.2
DefaultValues returns the default max level (32) and probability (0.5).
Types ¶
type SkipList ¶
type SkipList struct {
Header *Element
MaxLevel int
P float64
// MaxLen is the max number of entries. 0 means unlimited.
MaxLen int
// contains filtered or unexported fields
}
SkipList stores sorted []byte key/value pairs.
func NewSkipList ¶
NewSkipList creates a list. Pass 0 for either arg to use defaults (32, 0.5).
func (*SkipList) All ¶
All iterates key/value pairs in sorted order. Don't modify the list while ranging.
func (*SkipList) ForEach ¶ added in v0.0.2
ForEach walks all pairs in sorted order. Return false from do to stop early.
func (*SkipList) Get ¶
Get returns the value for key. ErrKeyNotFound if missing, ErrNilKey if empty.
func (*SkipList) NewElement ¶
NewElement builds a node with level l. Use Put for normal inserts.