skiplist

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2024 License: GPL-3.0 Imports: 7 Imported by: 0

README

Skip List

Description

This package implements a skip list, a probabilistic data structure often used as an alternative to balanced trees, like AVL or red-black. Skip lists have an average-case time complexity of O(log N) and worst-case of O(N) for searching for, inserting, and deleting elements, where N is the number of elements in the list.

Documentation

Index

Constants

View Source
const AbsoluteMaxLevel = 64
View Source
const DefaultMaxLevel = 32

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator[K, V any] interface {
	// Next returns true if there are further nodes over which to iterate and
	// advances the iterator if there are
	Next() bool

	// Prev returns true if there are previous elements over which to iterate
	// and rewinds to the previous node if possible
	Prev() bool

	// Key returns the current key
	Key() K

	// Value returns the current value
	Value() V
}

Iterator is a bidirectional iterator over the skip list.

type Pair added in v0.1.4

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

Pair is a key-value pair, or element, in the skip list.

func NewPair added in v0.1.4

func NewPair[K, V any](key K, val V) Pair[K, V]

NewPair returns a new key-value pair.

func (Pair[K, V]) Key added in v0.1.4

func (p Pair[K, V]) Key() K

Key returns the key of this key-value pair.

func (Pair[K, V]) String added in v0.1.4

func (p Pair[K, V]) String() string

String returns a string representation of the key-value pair.

func (Pair[K, V]) Value added in v0.1.4

func (p Pair[K, V]) Value() V

Value returns the value of this key-value pair.

type SkipList

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

func Merge

func Merge[K, V any](sl1, sl2 *SkipList[K, V]) *SkipList[K, V]

Merge returns a new skip list with the elements from both lists. For any keys that are in both of the lists, the result will use the value from the second list. The maxLevel of the result will be the greater maxLevel of the inputs.

func NewCustomSkipList

func NewCustomSkipList[K, V any](lessThan func(K, K) bool, items ...Pair[K, V]) *SkipList[K, V]

NewCustomSkipList initializes a skip list using a custom key type, which means there must be a function that defines a linear ordering of keys, i.e. for two keys X & Y the function must define how X is less than Y. Optionally include items with which to initialize the list. Uses default max level of 32.

func NewSkipList

func NewSkipList[K cmp.Ordered, V any](items ...Pair[K, V]) *SkipList[K, V]

NewSkipList initializes a skip list using a cmp.Ordered key type and with a default max level of 32. Optionally include items with which to initialize the list.

func (*SkipList[K, V]) Clear

func (sl *SkipList[K, V]) Clear()

Clear resets the state of the skip list, removing all elements from the skip list.

func (*SkipList[K, V]) Delete

func (sl *SkipList[K, V]) Delete(key K) (V, bool)

Delete removes the element with given key from the skip list. Returns the deleted value if it existed and a bool indicating if it did. Time complexity: O(logN), where N is the number of elements in the skip list.

func (*SkipList[K, V]) DeleteAll

func (sl *SkipList[K, V]) DeleteAll(keys ...K)

DeleteAll elements with the given keys. Time complexity: O(MlogN), where M is the number of keys, and N is the number of elements of the skip list.

func (*SkipList[K, V]) First added in v0.1.3

func (sl *SkipList[K, V]) First() *Pair[K, V]

First returns the first element, or the element with the minimum key, of the skip list, or nil if the list is empty. Time complexity: O(1).

func (*SkipList[K, V]) Get

func (sl *SkipList[K, V]) Get(key K) (V, bool)

Get returns the value associated with the key if the key exists and a bool indicating if it does. Time complexity: O(logN), where N is the number of elements in the skip list.

func (*SkipList[K, V]) IsEmpty

func (sl *SkipList[K, V]) IsEmpty() bool

IsEmpty returns true if the skip list has no elements.

func (*SkipList[K, V]) Iterator

func (sl *SkipList[K, V]) Iterator() Iterator[K, V]

Iterator returns a bidirectional iterator starting from the first node of the skip list, or nil if the list is empty.

func (*SkipList[K, V]) IteratorFrom

func (sl *SkipList[K, V]) IteratorFrom(start K) Iterator[K, V]

IteratorFrom returns a bidirectional iterator starting from the first node with key equal to or greater than start, or nil if the list is empty.

func (*SkipList[K, V]) IteratorFromEnd

func (sl *SkipList[K, V]) IteratorFromEnd() Iterator[K, V]

IteratorFromEnd returns a bidirectional iterator starting from the last node of the skip list, or nil if the list is empty.

func (*SkipList[K, V]) Last added in v0.1.3

func (sl *SkipList[K, V]) Last() *Pair[K, V]

Last returns the last element, or the element with the maximum key, of the skip list, or nil if the list is empty. Time complexity: O(1).

func (*SkipList[K, V]) Len

func (sl *SkipList[K, V]) Len() int

Len returns the number of elements in the skip list.

func (*SkipList[K, V]) MaxLevel

func (sl *SkipList[K, V]) MaxLevel() int

MaxLevel returns the maximum number of levels any node in the skip list can be on.

func (*SkipList[K, V]) Range

func (sl *SkipList[K, V]) Range(start, end K) Iterator[K, V]

Range returns a bidirectional iterator beginning at the first node with key greater than or equal to start (inclusive) to the node with key end (exclusive), or nil if the list is empty.

func (*SkipList[K, V]) Set

func (sl *SkipList[K, V]) Set(key K, val V) (bool, V)

Set sets a key to a value in the skip list. Returns true if this is pair was newly inserted. If this updated an existing key, returns the old value and false. Time complexity: O(logN), where N is the number of elements in the skip list.

func (*SkipList[K, V]) SetAll

func (sl *SkipList[K, V]) SetAll(items []Pair[K, V])

SetAll inserts each key-value pair in an array of pairs into the skip list.

func (*SkipList[K, V]) SetMaxLevel

func (sl *SkipList[K, V]) SetMaxLevel(newMaxLevel int)

SetMaxLevel sets the max level of the skip list, up to 64. Inputs greater than 64 are clamped to 64. If the new max level is less than the level of the highest node in the list, the new max level will instead be that node's level.

func (*SkipList[K, V]) String

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

String returns a string representing a visualization of the skip list.

Jump to

Keyboard shortcuts

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