skiplist

package
v0.0.0-...-a479826 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

package skiplist implements a skiplist data structure.

In computer science, a skip list (or skiplist) is a probabilistic data structure that allows O(log n) average complexity for search as well as O(log n) average complexity for insertion within an ordered sequence of n elements. Thus it can get the best features of a sorted array (for searching) while maintaining a linked list-like structure that allows insertion, which is not possible with a static array. Fast search is made possible by maintaining a linked hierarchy of subsequences, with each successive subsequence skipping over fewer elements than the previous one. Searching starts in the sparsest subsequence until two consecutive elements have been found, one smaller and one larger than or equal to the element searched for. Via the linked hierarchy, these two elements link to elements of the next sparsest subsequence, where searching is continued until finally searching in the full sequence. The elements that are skipped over may be chosen probabilistically or deterministically, with the former being more common.

Reference: https://en.wikipedia.org/wiki/Skip_list

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element[K comparable, V any] struct {

	// The value stored with this element.
	Value V
	// contains filtered or unexported fields
}

Element represents a key-value pair of a node.

func (*Element[K, V]) Key

func (e *Element[K, V]) Key() K

Key returns the key of element.

type Node

type Node[K comparable, V any] struct {
	// The element stored with this node.
	Element *Element[K, V]
	// contains filtered or unexported fields
}

Node is a node of a skiplist.

func (*Node[K, V]) Next

func (n *Node[K, V]) Next() *Node[K, V]

Next returns the next list node or nil.

func (*Node[K, V]) Prev

func (n *Node[K, V]) Prev() *Node[K, V]

Prev returns the previous list node or nil.

type Skiplist

type Skiplist[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Skiplist represents a skiplist.

Example
package main

import (
	"fmt"

	"github.com/docodex/gopkg/container/skiplist"
)

func main() {
	strs := []string{"Hello", "World", "Golang", "Python", "Rust", "C", "JavaScript", "Haskell", "Pascal", "ZZ"}
	ints := []int{3, 5, 4, 1, 8, 6, 5, 7, 9, 0}
	t1 := skiplist.New[string, int]()
	t2 := skiplist.New[int, string]()
	for i := range len(strs) {
		t1.Insert(strs[i], ints[i])
		t2.Insert(ints[i], strs[i])
	}
	k1 := t1.Keys()
	v1 := t1.Values()
	for i := range k1 {
		fmt.Printf("%s:%d\n", k1[i], v1[i])
	}
	k2 := t2.Keys()
	v2 := t2.Values()
	for i := range k2 {
		fmt.Printf("%d:%s\n", k2[i], v2[i])
	}

}
Output:

C:6
Golang:4
Haskell:7
Hello:3
JavaScript:5
Pascal:9
Python:1
Rust:8
World:5
ZZ:0
0:ZZ
1:Python
3:Hello
4:Golang
5:JavaScript
6:C
7:Haskell
8:Rust
9:Pascal

func New

func New[K cmp.Ordered, V any]() *Skiplist[K, V]

New returns an initialized skiplist with cmp.Compare as the cmp function.

func NewFunc

func NewFunc[K comparable, V any](cmp container.Compare[K]) *Skiplist[K, V]

NewFunc returns an initialized skiplist with the given function cmp as the cmp function.

func (*Skiplist[K, V]) Clear

func (l *Skiplist[K, V]) Clear()

Clear removes all nodes in skiplist.

func (*Skiplist[K, V]) Get

func (l *Skiplist[K, V]) Get(k K) (*Element[K, V], int)

Get returns the element which key equals to the given key k. Get also returns the rank of the returned element in skiplist.

func (*Skiplist[K, V]) GetByRank

func (l *Skiplist[K, V]) GetByRank(rank int) *Element[K, V]

GetByRank returns the element which rank equals to the given rank.

func (*Skiplist[K, V]) GetRange

func (l *Skiplist[K, V]) GetRange(k1, k2 K) []*Element[K, V]

GetRange returns the elements which keys is within the given range [k1, k2) in which k1 is inclusive and k2 is exclusive.

func (*Skiplist[K, V]) GetRangeByRank

func (l *Skiplist[K, V]) GetRangeByRank(rank1, rank2 int) []*Element[K, V]

GetRangeByRank removes the nodes which rank is within the given range [rank1, rank2) which rank1 is inclusive and rank2 is exclusive.

func (*Skiplist[K, V]) Insert

func (l *Skiplist[K, V]) Insert(k K, v V)

Insert inserts a new node with the given key-value pair (k, v) to skiplist, or update the key and value if the given key k already exists in skiplist.

func (*Skiplist[K, V]) Keys

func (l *Skiplist[K, V]) Keys() []K

Values returns all keys in skiplist.

func (*Skiplist[K, V]) Len

func (l *Skiplist[K, V]) Len() int

Len returns the number of nodes of skiplist t. The complexity is O(1).

func (*Skiplist[K, V]) MarshalJSON

func (l *Skiplist[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON marshals skiplist into valid JSON. Ref: std json.Marshaler.

func (*Skiplist[K, V]) Max

func (l *Skiplist[K, V]) Max() *Element[K, V]

Max returns the element which key is the maximum key of skiplist.

func (*Skiplist[K, V]) MaxInRange

func (l *Skiplist[K, V]) MaxInRange(k1, k2 K) *Element[K, V]

MaxInRange returns the element which key is the maximum key within the given range [k1, k2) in which k1 is inclusive and k2 is exclusive.

func (*Skiplist[K, V]) MaxNode

func (l *Skiplist[K, V]) MaxNode() *Node[K, V]

MaxNode returns the node which key is the maximum key of skiplist.

func (*Skiplist[K, V]) MaxNodeInRange

func (l *Skiplist[K, V]) MaxNodeInRange(k1, k2 K) *Node[K, V]

MaxNodeInRange returns the node which key is the maximum key within the given range [k1, k2) in which k1 is inclusive and k2 is exclusive.

func (*Skiplist[K, V]) Min

func (l *Skiplist[K, V]) Min() *Element[K, V]

Min returns the element which key is the minimum key of skiplist.

func (*Skiplist[K, V]) MinInRange

func (l *Skiplist[K, V]) MinInRange(k1, k2 K) *Element[K, V]

MinInRange returns the element which key is the minimum key within the given range [k1, k2) in which k1 is inclusive and k2 is exclusive.

func (*Skiplist[K, V]) MinNode

func (l *Skiplist[K, V]) MinNode() *Node[K, V]

MinNode returns the node which key is the minimum key of skiplist.

func (*Skiplist[K, V]) MinNodeInRange

func (l *Skiplist[K, V]) MinNodeInRange(k1, k2 K) *Node[K, V]

MinNodeInRange returns the node which key is the minimum key within the given range [k1, k2) in which k1 is inclusive and k2 is exclusive.

func (*Skiplist[K, V]) Range

func (l *Skiplist[K, V]) Range(f func(k K, v V) bool)

Range calls f sequentially for each key-value pair (k, v) present in skiplist. If f returns false, range stops the iteration.

func (*Skiplist[K, V]) Remove

func (l *Skiplist[K, V]) Remove(k K) *Element[K, V]

Remove removes the node which key equals to the given key k from skiplist and returns the element of that node.

func (*Skiplist[K, V]) RemoveByRank

func (l *Skiplist[K, V]) RemoveByRank(rank int) *Element[K, V]

RemoveByRank removes the node which rank equals to the given rank from skiplist and returns the element of that node.

func (*Skiplist[K, V]) RemoveRange

func (l *Skiplist[K, V]) RemoveRange(k1, k2 K) []*Element[K, V]

RemoveRange removes the nodes which keys is within the given range [k1, k2) in which k1 is inclusive and k2 is exclusive from skiplist and returns the elements of those nodes.

func (*Skiplist[K, V]) RemoveRangeByRank

func (l *Skiplist[K, V]) RemoveRangeByRank(rank1, rank2 int) []*Element[K, V]

RemoveRangeByRank removes the nodes which rank is within the given range [rank1, rank2) which rank1 is inclusive and rank2 is exclusive from skiplist and returns the elements of those nodes.

func (*Skiplist[K, V]) String

func (l *Skiplist[K, V]) String() string

String returns the string representation of skiplist. Ref: std fmt.Stringer.

func (*Skiplist[K, V]) UnmarshalJSON

func (l *Skiplist[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals a JSON description of skiplist. The input can be assumed to be a valid encoding of a JSON value. UnmarshalJSON must copy the JSON data if it wishes to retain the data after returning. Ref: std json.Unmarshaler.

func (*Skiplist[K, V]) Values

func (l *Skiplist[K, V]) Values() []V

Values returns all values in skiplist.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL