Documentation ¶
Overview ¶
Package skiplist implements skip list based maps and sets.
Skip lists are a data structure that can be used in place of balanced trees. Skip lists use probabilistic balancing rather than strictly enforced balancing and as a result the algorithms for insertion and deletion in skip lists are much simpler and significantly faster than equivalent algorithms for balanced trees.
Skip lists were first described in Pugh, William (June 1990). "Skip lists: a probabilistic alternative to balanced trees". Communications of the ACM 33 (6): 668–676
Index ¶
- Constants
- type Iterator
- type Ordered
- type Set
- func (s *Set) Add(key interface{})
- func (s *Set) Contains(key interface{}) bool
- func (s *Set) GetMaxLevel() int
- func (s *Set) Iterator() Iterator
- func (s *Set) Len() int
- func (s *Set) Range(from, to interface{}) Iterator
- func (s *Set) Remove(key interface{}) (ok bool)
- func (s *Set) SetMaxLevel(newMaxLevel int)
- type SkipList
- func (s *SkipList) Delete(key interface{}) (value interface{}, ok bool)
- func (s *SkipList) Get(key interface{}) (value interface{}, ok bool)
- func (s *SkipList) GetElementByRank(rank int) (interface{}, bool)
- func (s *SkipList) GetGreaterOrEqual(min interface{}) (actualKey, value interface{}, ok bool)
- func (s *SkipList) GetRank(key interface{}) int
- func (s *SkipList) Iterator() Iterator
- func (s *SkipList) Len() int
- func (s *SkipList) Range(from, to interface{}) Iterator
- func (s *SkipList) Seek(key interface{}) Iterator
- func (s *SkipList) SeekToFirst() Iterator
- func (s *SkipList) SeekToLast() Iterator
- func (s *SkipList) Set(key, value interface{})
Constants ¶
const DefaultMaxLevel = 32
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Iterator ¶
type Iterator interface { // Next returns true if the iterator contains subsequent elements // and advances its state to the next element if that is possible. Next() (ok bool) // Previous returns true if the iterator contains previous elements // and rewinds its state to the previous element if that is possible. Previous() (ok bool) // Key returns the current key. Key() interface{} // Value returns the current value. Value() interface{} // Seek reduces iterative seek costs for searching forward into the Skip List // by remarking the range of keys over which it has scanned before. If the // requested key occurs prior to the point, the Skip List will start searching // as a safeguard. It returns true if the key is within the known range of // the list. Seek(key interface{}) (ok bool) // Close this iterator to reap resources associated with it. While not // strictly required, it will provide extra hints for the garbage collector. Close() }
Iterator is an interface that you can use to iterate through the skip list (in its entirety or fragments). For an use example, see the documentation of SkipList.
Key and Value return the key and the value of the current node.
type Ordered ¶
Ordered is an interface which can be linearly ordered by the LessThan method, whereby this instance is deemed to be less than other. Additionally, Ordered instances should behave properly when compared using == and !=.
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set is an ordered set data structure.
Its elements must implement the Ordered interface. It uses a SkipList for storage, and it gives you similar performance guarantees.
To iterate over a set (where s is a *Set):
for i := s.Iterator(); i.Next(); { // do something with i.Key(). // i.Value() will be nil. }
func NewCustomSet ¶
NewCustomSet returns a new Set that will use lessThan as the comparison function. lessThan should define a linear order on elements you intend to use with the Set.
func NewStringSet ¶
func NewStringSet() *Set
NewStringSet returns a new Set that accepts string elements.
func (*Set) GetMaxLevel ¶
GetMaxLevel returns MaxLevel fo the underlying skip list.
func (*Set) Range ¶
Range returns an iterator that will go through all the elements of the set that are greater or equal than from, but less than to.
func (*Set) SetMaxLevel ¶
SetMaxLevel sets MaxLevel in the underlying skip list.
type SkipList ¶
type SkipList struct { // MaxLevel determines how many items the SkipList can store // efficiently (2^MaxLevel). // // It is safe to increase MaxLevel to accomodate more // elements. If you decrease MaxLevel and the skip list // already contains nodes on higer levels, the effective // MaxLevel will be the greater of the new MaxLevel and the // level of the highest node. // // A SkipList with MaxLevel equal to 0 is equivalent to a // standard linked list and will not have any of the nice // properties of skip lists (probably not what you want). MaxLevel int // contains filtered or unexported fields }
A SkipList is a map-like data structure that maintains an ordered collection of key-value pairs. Insertion, lookup, and deletion are all O(log n) operations. A SkipList can efficiently store up to 2^MaxLevel items.
To iterate over a skip list (where s is a *SkipList):
for i := s.Iterator(); i.Next(); { // do something with i.Key() and i.Value() }
func New ¶
func New() *SkipList
New returns a new SkipList.
Its keys must implement the Ordered interface.
func NewCustomMap ¶
NewCustomMap returns a new SkipList that will use lessThan as the comparison function. lessThan should define a linear order on keys you intend to use with the SkipList.
func NewStringMap ¶
func NewStringMap() *SkipList
NewStringMap returns a SkipList that accepts string keys.
func (*SkipList) Delete ¶
Delete removes the node with the given key.
It returns the old value and whether the node was present.
func (*SkipList) Get ¶
Get returns the value associated with key from s (nil if the key is not present in s). The second return value is true when the key is present.
func (*SkipList) GetElementByRank ¶
Finds an element by its rank. The rank argument needs to be 1-based.
func (*SkipList) GetGreaterOrEqual ¶
GetGreaterOrEqual finds the node whose key is greater than or equal to min. It returns its value, its actual key, and whether such a node is present in the skip list.
func (*SkipList) GetRank ¶
Find the rank for an element by both score and key.
Returns 0 when the element cannot be found, rank otherwise.
Note that the rank is 1-based due to the span of s.header to the first element.
func (*SkipList) Range ¶
Range returns an iterator that will go through all the elements of the skip list that are greater or equal than from, but less than to.
func (*SkipList) Seek ¶
Seek returns a bidirectional iterator starting with the first element whose key is greater or equal to key; otherwise, a nil iterator is returned.
func (*SkipList) SeekToFirst ¶
SeekToFirst returns a bidirectional iterator starting from the first element in the list if the list is populated; otherwise, a nil iterator is returned.
func (*SkipList) SeekToLast ¶
SeekToLast returns a bidirectional iterator starting from the last element in the list if the list is populated; otherwise, a nil iterator is returned.