skiplist

package
v0.0.0-...-b0c7fd6 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	IntComparator     = orderedComparator[int]
	Int8Comparator    = orderedComparator[int8]
	Int16Comparator   = orderedComparator[int16]
	Int32Comparator   = orderedComparator[int32]
	Int64Comparator   = orderedComparator[int64]
	UintComparator    = orderedComparator[uint]
	Uint8Comparator   = orderedComparator[uint8]
	Uint16Comparator  = orderedComparator[uint16]
	Uint32Comparator  = orderedComparator[uint32]
	Uint64Comparator  = orderedComparator[uint64]
	Float32Comparator = orderedComparator[float32]
	Float64Comparator = orderedComparator[float64]
	StringComparator  = orderedComparator[string]
)

Functions

This section is empty.

Types

type Comparator

type Comparator[K any] func(a, b K) int

Comparator is a function that compares two keys. It returns a negative number if a < b, 0 if a == b, and a positive number if a > b.

func InverseComparator

func InverseComparator[T constraints.Ordered](comparator Comparator[T]) Comparator[T]

type Iterator

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

Iterator is an iterator over a Skiplist.

func (*Iterator[K, V]) HasNext

func (it *Iterator[K, V]) HasNext() bool

HasNext returns true if there are more items in the iterator.

func (*Iterator[K, V]) Next

func (it *Iterator[K, V]) Next() (key K, value V)

Next returns the next key-value pair in the iterator. It panics if there are no more items, so HasNext should always be called before calling Next.

type Skiplist

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

Skiplist is a generic skiplist implementation. It is thread safe and supports concurrent reads and writes. It allows to multiple readers to access the list simultaneously, but only a single writer. The writer does not block the readers, and the readers do not block the writer.

func New

func New[K any, V any](comparator Comparator[K]) *Skiplist[K, V]

New returns a new Skiplist. The comparator is used to compare keys.

func (*Skiplist[K, V]) Contains

func (l *Skiplist[K, V]) Contains(key K) bool

Contains returns true if the list contains the given key.

func (*Skiplist[K, V]) Get

func (l *Skiplist[K, V]) Get(key K) (ret V, found bool)

Get returns the value for the given key. If the key is not found, ErrNotFound is returned.

func (*Skiplist[K, V]) Height

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

Height returns the height of the list.

func (*Skiplist[K, V]) Insert

func (l *Skiplist[K, V]) Insert(key K, value V)

Insert inserts a new key-value pair into the list.

func (*Skiplist[K, V]) LessOrEqual

func (l *Skiplist[K, V]) LessOrEqual(key K) (retk K, retv V, found bool)

LessOrEqual returns the value for the key that is less than the given key.

func (*Skiplist[K, V]) Remove

func (l *Skiplist[K, V]) Remove(key K) bool

Remove removes the key-value pair with the given key from the list. It returns true if the key was found.

func (*Skiplist[K, V]) Scan

func (l *Skiplist[K, V]) Scan() *Iterator[K, V]

Scan returns an iterator that scans the list from the beginning. Note that the list may change while the iterator is in use.

func (*Skiplist[K, V]) ScanFrom

func (l *Skiplist[K, V]) ScanFrom(key K) *Iterator[K, V]

ScanFrom returns an iterator that scans the list from the given key. Note that the list may change while the iterator is in use.

func (*Skiplist[K, V]) ScanRange

func (l *Skiplist[K, V]) ScanRange(start, end K) *Iterator[K, V]

ScanRange returns an iterator that scans the list from the given start key to the given end key. Note that the list may change while the iterator is in use.

func (*Skiplist[K, V]) Size

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

Size returns the number of key-value pairs in the list.

Jump to

Keyboard shortcuts

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