skiplist

package
v0.0.0-...-b1f4cf3 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const DefaultMaxLevel = 64
View Source
const DefaultProbability = 0.5
View Source
const InvalidPos = -1

InvalidPos is returned, when an element is not found within the skip list.

View Source
const MaxLevel = 512

Variables

This section is empty.

Functions

func WithLevelFunc

func WithLevelFunc[K cmp.Ordered, V any](levelFunc LevelFunc) skipListOption[K, V]

WithLevelFunc adds a custom function for generating the level of each inserted element in the list.

func WithMaxLevel

func WithMaxLevel[K cmp.Ordered, V any](maxLevel int) skipListOption[K, V]

WithMaxLevel overrides the DefaultMaxLevel.

func WithProbability

func WithProbability[K cmp.Ordered, V any](prob float64) skipListOption[K, V]

WithProbability overrides the DefaultProbability.

Types

type LevelFunc

type LevelFunc func(p float64, maxLevel int) int

type Node

type Node[K cmp.Ordered, V any] struct {
	Value V // Value is the payload within an element node.
	// contains filtered or unexported fields
}

Node holds an element within the SkipList with a unique key `key`.

func (*Node[K, V]) Key

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

func (*Node[K, V]) Level

func (n *Node[K, V]) Level() int

func (*Node[K, V]) Next

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

Next returns the adjacent element within the skip list. If there is no such element, nil is returned.

func (*Node[K, V]) String

func (n *Node[K, V]) String() string

type SkipList

type SkipList[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

SkipList is a structure implementing the skip list of William Pugh. It allows in addition to the standard key operations SkipList.Set(), SkipList.Get(), and SkipList.Remove() the indexed linear list operations SkipList.GetByPos() and SkipList.RemoveByPos(). There are two generic parameters K is the key, which must be cmp.Ordered policy, and the value V can be of any type.

func NewSkipList

func NewSkipList[K cmp.Ordered, V any](options ...skipListOption[K, V]) *SkipList[K, V]

NewSkipList creates a new empty SkipList object.

Example
package main

import (
	"fmt"

	"github.com/andremueller/goskiplist/pkg/skiplist"
)

func main() {
	// creates a skip list with key type `int` and value type `string`
	s := skiplist.NewSkipList[int, string]()

	s.Set(1, "cat")
	s.Set(2, "dog")

	x, _ := s.Get(1)
	fmt.Printf("Value: %s", x.Value)
	// Output  "Value: cat"
}
Output:

func (*SkipList[K, V]) First

func (s *SkipList[K, V]) First() *Node[K, V]

First returns the first node of a skip list or nil if the list is empty. With the Node.Next() function the list can be iterated.

func (*SkipList[K, V]) Get

func (s *SkipList[K, V]) Get(key K) (*Node[K, V], int)

Get returns the node matching the searched key or nil if it was not found. The second return argument is the position 0...n-1 of the key or InvalidPos if the element was not found.

func (*SkipList[K, V]) GetByPos

func (s *SkipList[K, V]) GetByPos(k int) *Node[K, V]

GetByPos returns the kth element of the skip list where k must be in the interval [0, Size()). This operation is performed in O(log(n)) steps in the average due to the maintenance of the distance vectors within each element. Returns a node pointer to the element.

func (*SkipList[K, V]) Level

func (s *SkipList[K, V]) Level() int

func (*SkipList[K, V]) Remove

func (s *SkipList[K, V]) Remove(key K) (*Node[K, V], int)

Remove removes an element with key `key` from the skip list. Returns a reference to the removed element and its position 0...n-1 before it was removed.

func (*SkipList[K, V]) RemoveByPos

func (s *SkipList[K, V]) RemoveByPos(k int) *Node[K, V]

Remove removes an element at position k [0, Size()) from the skip list. Returns a reference to the removed element.

func (*SkipList[K, V]) Set

func (s *SkipList[K, V]) Set(key K, value V) (*Node[K, V], int, bool)

Set sets the value `value` of a key `key` within the skip list. Replaces the value if the key was already added to the set or inserts the key if not. Returns a reference to the node and its current position 0...n-1 within the skip list. The bool value is true, if a new node was created and false if the value was overridden.

Example
package main

import (
	"fmt"

	"github.com/andremueller/goskiplist/pkg/skiplist"
)

func main() {
	// creates a skip list with key type `int` and value type `string`
	s := skiplist.NewSkipList[int, string]()

	// sets keys
	s.Set(1, "cat")
	s.Set(2, "dog")

	// overrides the first key
	s.Set(1, "worm")

	x, _ := s.Get(1)
	fmt.Printf("Value: %s", x.Value)
	// Output  "Value: worm"
}
Output:

func (*SkipList[K, V]) Size

func (s *SkipList[K, V]) Size() int

Size returns the number of elements within the skip list.

func (*SkipList[K, V]) String

func (s *SkipList[K, V]) String() string

Jump to

Keyboard shortcuts

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