Documentation
¶
Overview ¶
Package skip_list provides a Skip List data structure implementation. A Skip List is a probabilistic data structure that allows for efficient search, insertion, and deletion operations with average O(log n) time complexity. It maintains elements in sorted order and uses multiple levels for fast traversal.
Index ¶
- type Interface
- type SkipList
- func (sl *SkipList[K, V]) All() iter.Seq2[K, V]
- func (sl *SkipList[K, V]) AllBetween(start, end K) iter.Seq2[K, V]
- func (sl *SkipList[K, V]) AllFrom(start K) iter.Seq2[K, V]
- func (sl *SkipList[K, V]) Clear()
- func (sl *SkipList[K, V]) Delete(key K) bool
- func (sl *SkipList[K, V]) Get(key K) (V, bool)
- func (sl *SkipList[K, V]) GetMutable(key K) (*V, bool)
- func (sl *SkipList[K, V]) Has(key K) bool
- func (sl *SkipList[K, V]) Keys() []K
- func (sl *SkipList[K, V]) Len() int
- func (sl *SkipList[K, V]) Pairs() []pair.Pair[K, V]
- func (sl *SkipList[K, V]) Range(fn func(key K, value V) bool)
- func (sl *SkipList[K, V]) RangeBetween(start, end K, fn func(key K, value V) bool)
- func (sl *SkipList[K, V]) RangeFrom(start K, fn func(key K, value V) bool)
- func (sl *SkipList[K, V]) Set(key K, value V)
- func (sl *SkipList[K, V]) Values() []V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Interface ¶
type Interface[K comparable, V any] interface { // Len returns the number of key-value pairs stored in the skip list. Len() int // Get retrieves the value associated with the given key. // Returns the value and true if the key exists, zero value and false otherwise. Get(key K) (V, bool) // GetMutable returns a pointer to the value associated with the given key. // This allows for in-place modification of the value. // Returns a pointer to the value and true if the key exists, nil and false otherwise. GetMutable(key K) (*V, bool) // Set inserts or updates a key-value pair in the skip list. // If the key already exists, its value is updated. Set(key K, value V) // Delete removes the key-value pair with the given key from the skip list. // Returns true if the key was found and removed, false otherwise. Delete(key K) bool // Has checks whether the given key exists in the skip list. Has(key K) bool // Clear removes all key-value pairs from the skip list. Clear() // Keys returns a slice of all keys in the skip list in sorted order. Keys() []K // Values returns a slice of all values in the skip list in the order of their keys. Values() []V // Pairs returns a slice of all key-value pairs in the skip list in sorted order by key. Pairs() []pair.Pair[K, V] // Range calls the provided function for each key-value pair in the skip list // in sorted order by key. If the function returns false, the iteration stops. Range(fn func(key K, value V) bool) // RangeFrom calls the provided function for each key-value pair in the skip list // starting from the given key (inclusive) in sorted order by key. // If the function returns false, the iteration stops. RangeFrom(start K, fn func(key K, value V) bool) // RangeBetween calls the provided function for each key-value pair in the skip list // within the given key range [start, end] (both inclusive) in sorted order by key. // If the function returns false, the iteration stops. RangeBetween(start, end K, fn func(key K, value V) bool) // All returns an iterator over all key-value pairs in sorted order by key. All() iter.Seq2[K, V] // AllFrom returns an iterator over key-value pairs starting from the given key // (inclusive) in sorted order by key. AllFrom(start K) iter.Seq2[K, V] // AllBetween returns an iterator over key-value pairs within the given key range // [start, end] (both inclusive) in sorted order by key. AllBetween(start, end K) iter.Seq2[K, V] }
Interface defines the operations for a Skip List data structure. A Skip List maintains key-value pairs in sorted order by key and provides efficient operations through a probabilistic multi-level structure.
func NewOrderedSkipList ¶
NewOrderedSkipList creates a new skip list for ordered types (types that implement cmp.Ordered).
func NewSkipList ¶
func NewSkipList[K comparable, V any](compare func(a, b K) int) Interface[K, V]
NewSkipList creates and returns a new empty skip list.
type SkipList ¶
type SkipList[K comparable, V any] struct { // contains filtered or unexported fields }
SkipList is a concrete implementation of the Interface.
func (*SkipList[K, V]) All ¶
All returns an iterator over all key-value pairs in sorted order by key.
func (*SkipList[K, V]) AllBetween ¶
AllBetween returns an iterator over key-value pairs within the given range.
func (*SkipList[K, V]) AllFrom ¶
AllFrom returns an iterator over key-value pairs starting from the given key.
func (*SkipList[K, V]) Clear ¶
func (sl *SkipList[K, V]) Clear()
Clear removes all key-value pairs from the skip list.
func (*SkipList[K, V]) Delete ¶
Delete removes the key-value pair with the given key from the skip list.
func (*SkipList[K, V]) GetMutable ¶
GetMutable returns a pointer to the value associated with the given key.
func (*SkipList[K, V]) Keys ¶
func (sl *SkipList[K, V]) Keys() []K
Keys returns a slice of all keys in the skip list in sorted order.
func (*SkipList[K, V]) Pairs ¶
Pairs returns a slice of all key-value pairs in the skip list in sorted order by key.
func (*SkipList[K, V]) Range ¶
Range calls the provided function for each key-value pair in sorted order by key.
func (*SkipList[K, V]) RangeBetween ¶
RangeBetween calls the provided function for key-value pairs within the given range.
func (*SkipList[K, V]) RangeFrom ¶
RangeFrom calls the provided function for key-value pairs starting from the given key.