Documentation
¶
Overview ¶
Package skiplist implements a probabilistic skip list, a sorted key-value store with expected O(log n) search, insert, and delete performance.
Skip lists maintain multiple levels of linked lists. Lower levels contain all elements; higher levels contain fewer elements and serve as shortcuts during search. The height of each inserted element is chosen randomly using a geometric distribution controlled by the probability parameter P.
Keys ([]byte) are ordered using bytes.Compare. Values are also []byte.
The implementation is not concurrent-safe.
Example:
s := NewSkipList(32, 0.5)
s.Push([]byte("alpha"), []byte("first"))
s.Push([]byte("beta"), []byte("second"))
v := s.Get([]byte("alpha")) // []byte("first")
for k, v := range s.All() {
// iterates in sorted key order
}
s.Pop([]byte("beta"))
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
Return default values for maxlevel and p
Types ¶
type Element ¶
type Element struct {
// Key and Value hold the stored data. Keys are compared with bytes.Compare.
Key, Value []byte
// Level is the highest level this element participates in.
Level int
// contains filtered or unexported fields
}
Element represents one node in the skip list containing a key/value pair.
Level indicates the highest level (0-based) at which this element appears. Elements at level L are also linked into all levels 0 through L.
type SkipList ¶
type SkipList struct {
// Header is the entry point for searches at every level.
Header *Element
MaxLevel int
// P controls the probability of promoting an element to a higher level.
P float64
// contains filtered or unexported fields
}
SkipList is a sorted collection of []byte key/value pairs implemented as a probabilistic skip list.
Header is a sentinel node. Its nexts slice contains the head pointers for each level of the skip list.
MaxLevel is the maximum number of levels in the skip list.
P is the probability used when choosing random level for new elements (see NewSkipList).
func NewSkipList ¶
NewSkipList creates and returns a new SkipList.
maxlevel is the maximum height of the skip list. If <= 0, it defaults to 32.
p is the probability that an inserted element will be promoted to the next higher level (geometric distribution). The classic value is 0.5. If p <= 0, it defaults to 0.5.
Higher p values produce taller structures on average; lower values produce flatter ones. 0.5 is generally a good balance.
func (*SkipList) All ¶
All returns an iterator that yields all key/value pairs in ascending key order.
It satisfies the iter.Seq2 interface and can be used directly with range:
for key, value := range list.All() {
...
}
The iterator is valid only for the current state of the list. Modifying the list during iteration may produce unexpected results.
func (*SkipList) Get ¶
Get returns the value associated with key.
It returns nil if the key does not exist in the skip list. Note: because values are []byte, it is not possible to distinguish between a missing key and a key whose value is nil or empty using Get alone.
func (*SkipList) NewElement ¶
NewElement creates a new Element with the provided key, value and level. The created element will have forward pointers allocated up to s.MaxLevel.
This is primarily intended for advanced use cases. Most callers should use [SkipList.Push] instead.