Documentation ¶
Overview ¶
Package skiplist is an implementation of a skiplist to store elements in increasing order. It allows finding, insertion and deletion operations in approximately O(n log(n)). Additionally, there are methods for retrieving the next and previous element as well as changing the actual value without the need for re-insertion (as long as the key stays the same!) Skiplist is a fast alternative to a balanced tree.
Index ¶
- type ListElement
- type SkipList
- func (t *SkipList) ChangeValue(e *SkipListElement, newValue ListElement) (ok bool)
- func (t *SkipList) Delete(e ListElement)
- func (t *SkipList) Find(e ListElement) (elem *SkipListElement, ok bool)
- func (t *SkipList) FindGreaterOrEqual(e ListElement) (elem *SkipListElement, ok bool)
- func (t *SkipList) GetLargestNode() *SkipListElement
- func (t *SkipList) GetNodeCount() int
- func (t *SkipList) GetSmallestNode() *SkipListElement
- func (t *SkipList) Insert(e ListElement)
- func (t *SkipList) IsEmpty() bool
- func (t *SkipList) Next(e *SkipListElement) *SkipListElement
- func (t *SkipList) Prev(e *SkipListElement) *SkipListElement
- func (t *SkipList) String() string
- type SkipListElement
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ListElement ¶
type ListElement interface { // ExtractKey() returns a float64 representation of the key that is used for insertion/deletion/find. It needs to establish an order over all elements ExtractKey() float64 // A string representation of the element. Can be used for pretty-printing the list. Otherwise just return an empty string. String() string }
ListElement is the interface to implement for elements that are inserted into the skiplist.
type SkipList ¶
type SkipList struct {
// contains filtered or unexported fields
}
SkipList is the actual skiplist representation. It saves all nodes accessible from the start and end and keeps track of element count, eps and levels.
func NewEps ¶
NewEps returns a new empty, initialized Skiplist. Eps is used to compare keys given by the ExtractKey() function on equality.
func NewSeed ¶
NewSeed returns a new empty, initialized Skiplist. Given a seed, a deterministic height/list behaviour can be achieved.
func NewSeedEps ¶
NewSeedEps returns a new empty, initialized Skiplist. Given a seed, a deterministic height/list behaviour can be achieved. Eps is used to compare keys given by the ExtractKey() function on equality.
func (*SkipList) ChangeValue ¶
func (t *SkipList) ChangeValue(e *SkipListElement, newValue ListElement) (ok bool)
ChangeValue can be used to change the actual value of a node in the skiplist without the need of Deleting and reinserting the node again. Be advised, that ChangeValue only works, if the actual key from ExtractKey() will stay the same! ok is an indicator, wether the value is actually changed.
func (*SkipList) Delete ¶
func (t *SkipList) Delete(e ListElement)
Delete removes an element equal to e from the skiplist, if there is one. If there are multiple entries with the same value, Delete will remove one of them (Which one will change based on the actual skiplist layout) Delete runs in approx. O(log(n))
func (*SkipList) Find ¶
func (t *SkipList) Find(e ListElement) (elem *SkipListElement, ok bool)
Find tries to find an element in the skiplist based on the key from the given ListElement. elem can be used, if ok is true. Find runs in approx. O(log(n))
func (*SkipList) FindGreaterOrEqual ¶
func (t *SkipList) FindGreaterOrEqual(e ListElement) (elem *SkipListElement, ok bool)
FindGreaterOrEqual finds the first element, that is greater or equal to the given ListElement e. The comparison is done on the keys (So on ExtractKey()). FindGreaterOrEqual runs in approx. O(log(n))
func (*SkipList) GetLargestNode ¶
func (t *SkipList) GetLargestNode() *SkipListElement
GetLargestNode returns the very last/largest node in the skiplist. GetLargestNode runs in O(1)
func (*SkipList) GetNodeCount ¶
GetNodeCount returns the number of nodes currently in the skiplist.
func (*SkipList) GetSmallestNode ¶
func (t *SkipList) GetSmallestNode() *SkipListElement
GetSmallestNode returns the very first/smallest node in the skiplist. GetSmallestNode runs in O(1)
func (*SkipList) Insert ¶
func (t *SkipList) Insert(e ListElement)
Insert inserts the given ListElement into the skiplist. Insert runs in approx. O(log(n))
func (*SkipList) Next ¶
func (t *SkipList) Next(e *SkipListElement) *SkipListElement
Next returns the next element based on the given node. Next will loop around to the first node, if you call it on the last!
func (*SkipList) Prev ¶
func (t *SkipList) Prev(e *SkipListElement) *SkipListElement
Prev returns the previous element based on the given node. Prev will loop around to the last node, if you call it on the first!
type SkipListElement ¶
type SkipListElement struct {
// contains filtered or unexported fields
}
SkipListElement represents one actual Node in the skiplist structure. It saves the actual element, pointers to the next nodes and a pointer to one previous node.
func (*SkipListElement) GetValue ¶
func (e *SkipListElement) GetValue() ListElement
GetValue extracts the ListElement value from a skiplist node.