listz

package
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Example
// Create a new list and put some numbers in it.
l := NewDoubly[int]()
e4 := l.PushBack(4)
e1 := l.PushFront(1)
l.InsertBefore(3, e4)
l.InsertAfter(2, e1)

// Iterate through list and print its contents.
for e := l.Front(); e != nil; e = e.Next() {
	fmt.Println(e.Value)
}
Output:

1
2
3
4

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DList

type DList[T any] struct {
	// contains filtered or unexported fields
}

DList represents a doubly linked list. The zero value for DList is an empty list ready to use.

func NewDoubly

func NewDoubly[T any]() *DList[T]

NewDoubly returns an initialized list.

func (*DList[T]) All

func (l *DList[T]) All() iter.Seq[T]

All returns an iterator that yields all element value in the doubly linked list.

func (*DList[T]) Back

func (l *DList[T]) Back() *DNode[T]

Back returns the last node of list l or nil if the list is empty.

func (*DList[T]) Front

func (l *DList[T]) Front() *DNode[T]

Front returns the first node of list l or nil if the list is empty.

func (*DList[T]) Init

func (l *DList[T]) Init() *DList[T]

Init initializes or clears list l.

func (*DList[T]) InsertAfter

func (l *DList[T]) InsertAfter(v T, mark *DNode[T]) *DNode[T]

InsertAfter inserts a new node e with value v immediately after mark and returns e. If mark is not a node of l, the list is not modified. The mark must not be nil.

func (*DList[T]) InsertBefore

func (l *DList[T]) InsertBefore(v T, mark *DNode[T]) *DNode[T]

InsertBefore inserts a new node e with value v immediately before mark and returns e. If mark is not a node of l, the list is not modified. The mark must not be nil.

func (*DList[T]) InsertNodeAfter

func (l *DList[T]) InsertNodeAfter(e, mark *DNode[T])

InsertNodeAfter inserts a new elenodeent e after mark and returns e.

func (*DList[T]) InsertNodeBefore

func (l *DList[T]) InsertNodeBefore(e, mark *DNode[T])

InsertNodeBefore inserts a new node e before mark and returns e.

func (*DList[T]) Len

func (l *DList[T]) Len() int

Len returns the number of nodes of list l. The complexity is O(1).

func (*DList[T]) MoveAfter

func (l *DList[T]) MoveAfter(e, mark *DNode[T])

MoveAfter moves node e to its new position after mark. If e or mark is not a node of l, or e == mark, the list is not modified. The node and mark must not be nil.

func (*DList[T]) MoveBefore

func (l *DList[T]) MoveBefore(e, mark *DNode[T])

MoveBefore moves node e to its new position before mark. If e or mark is not a node of l, or e == mark, the list is not modified. The node and mark must not be nil.

func (*DList[T]) MoveToBack

func (l *DList[T]) MoveToBack(e *DNode[T])

MoveToBack moves node e to the back of list l. If e is not an node of l, the list is not modified. The node must not be nil.

func (*DList[T]) MoveToFront

func (l *DList[T]) MoveToFront(e *DNode[T])

MoveToFront moves node e to the front of list l. If e is not an node of l, the list is not modified. The node must not be nil.

func (*DList[T]) PushBack

func (l *DList[T]) PushBack(v T) *DNode[T]

PushBack inserts a new node e with value v at the back of list l and returns e.

func (*DList[T]) PushBackDList

func (l *DList[T]) PushBackDList(other *DList[T])

PushBackDList inserts a copy of another list at the back of list l. The lists l and other may be the same. They must not be nil.

func (*DList[T]) PushBackNode

func (l *DList[T]) PushBackNode(e *DNode[T])

PushBackNode inserts a new node e at the back of list l.

func (*DList[T]) PushFront

func (l *DList[T]) PushFront(v T) *DNode[T]

PushFront inserts a new node e with value v at the front of list l and returns e.

func (*DList[T]) PushFrontDList

func (l *DList[T]) PushFrontDList(other *DList[T])

PushFrontDList inserts a copy of another list at the front of list l. The lists l and other may be the same. They must not be nil.

func (*DList[T]) PushFrontNode

func (l *DList[T]) PushFrontNode(e *DNode[T])

PushFrontNode inserts a new node e at the front of list l.

func (*DList[T]) Remove

func (l *DList[T]) Remove(e *DNode[T]) T

Remove removes e from l if e is an node of list l. It returns the node value e.Value. The node must not be nil.

type DNode

type DNode[T any] struct {

	// The value stored with this node.
	Value T
	// contains filtered or unexported fields
}

DNode is a node of a doubly linked list.

func (*DNode[T]) Next

func (e *DNode[T]) Next() *DNode[T]

Next returns the next list node or nil.

func (*DNode[T]) Prev

func (e *DNode[T]) Prev() *DNode[T]

Prev returns the previous list node or nil.

type SList

type SList[T any] struct {
	// contains filtered or unexported fields
}

SList represents a singly linked list.

func NewSingly

func NewSingly[T any]() *SList[T]

NewSingly returns an initialized singly linked list.

func (*SList[T]) All

func (l *SList[T]) All() iter.Seq[T]

All returns an iterator that yields all element value in the singly linked list.

func (*SList[T]) Back

func (l *SList[T]) Back() *SNode[T]

Back returns the last node of list l or nil if the list is empty.

func (*SList[T]) Front

func (l *SList[T]) Front() *SNode[T]

Front returns the first node of list l or nil if the list is empty.

func (*SList[T]) Get

func (l *SList[T]) Get(i int) *SNode[T]

Get returns the node at index i.

func (*SList[T]) InsertAt

func (l *SList[T]) InsertAt(i int, v T)

InsertAt inserts a new node with value v at index i. The old node at index i will be pushed to the next index.

func (*SList[T]) InsertNodeAt

func (l *SList[T]) InsertNodeAt(i int, e *SNode[T])

InsertNodeAt inserts a new node at index i. The old node at index i will be pushed to the next index.

func (*SList[T]) Len

func (l *SList[T]) Len() int

Len returns the number of nodes of list l.

func (*SList[T]) PushBack

func (l *SList[T]) PushBack(v T)

PushBack inserts a new node with value v at the back of list l.

func (*SList[T]) PushBackNode

func (l *SList[T]) PushBackNode(e *SNode[T])

PushBackNode inserts a new node at the back of the list.

func (*SList[T]) PushFront

func (l *SList[T]) PushFront(v T)

PushFront inserts a new node with value v at the front of list l.

func (*SList[T]) PushFrontNode

func (l *SList[T]) PushFrontNode(e *SNode[T])

PushFrontNode inserts a new node at the front of the list.

func (*SList[T]) Remove

func (l *SList[T]) Remove(i int) *SNode[T]

Remove removes the node at index i from the list. This operation is O(n) where n is the len of the list.

func (*SList[T]) RemoveFront

func (l *SList[T]) RemoveFront() *SNode[T]

RemoveFront removes the first node from the list and returns it.

func (*SList[T]) Swap

func (l *SList[T]) Swap(i, j int)

Swap swaps the values of two nodes at indexes i and j.

type SNode

type SNode[T any] struct {
	Value T
	// contains filtered or unexported fields
}

SNode is a node of a singly linked list.

func (*SNode[T]) Next

func (e *SNode[T]) Next() *SNode[T]

Next returns the next list node or nil.

type SkipList added in v0.0.20

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

func NewSkipList added in v0.0.20

func NewSkipList[K typez.Ordered, V any]() *SkipList[K, V]

NewSkipList returns an initialized skip list.

func (*SkipList[K, V]) All added in v0.0.20

func (s *SkipList[K, V]) All() iter.Seq2[K, V]

All returns an iterator that yields all element value in the skip list.

func (*SkipList[K, V]) Clear added in v0.0.20

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

Clear removes all nodes from the skip list.

func (*SkipList[K, V]) Get added in v0.0.20

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

Get returns the value associated with the key.

func (*SkipList[K, V]) GetNode added in v0.0.20

func (s *SkipList[K, V]) GetNode(key K) *SkipNode[K, V]

GetNode returns the node associated with the key.

func (*SkipList[K, V]) Head added in v0.0.20

func (s *SkipList[K, V]) Head() *SkipNode[K, V]

Head returns the first node of the skip list.

func (*SkipList[K, V]) Init added in v0.0.20

func (s *SkipList[K, V]) Init()

Init initializes the skip list.

func (*SkipList[K, V]) Keys added in v0.0.20

func (s *SkipList[K, V]) Keys() []K

Keys returns all keys in the skip list.

func (*SkipList[K, V]) Len added in v0.0.20

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

Len returns the number of nodes of the skip list.

func (*SkipList[K, V]) Range added in v0.0.20

func (s *SkipList[K, V]) Range(f func(key K, val V) bool)

Range traverses the skip list in ascending order.

func (*SkipList[K, V]) RangeWithRange added in v0.0.20

func (s *SkipList[K, V]) RangeWithRange(start, end K, f func(key K, val V) bool)

RangeWithRange traverses the skip list in ascending order starting from the start key and ending before the end key. The zone is [start, end)

func (*SkipList[K, V]) RangeWithStart added in v0.0.20

func (s *SkipList[K, V]) RangeWithStart(start K, f func(key K, val V) bool)

RangeWithStart traverses the skip list in ascending order starting from the start key. The zone is [start, +∞)

func (*SkipList[K, V]) Remove added in v0.0.20

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

Remove deletes the value associated with the key.

func (*SkipList[K, V]) Set added in v0.0.20

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

Set sets the value associated with the key.

func (*SkipList[K, V]) SetNx added in v0.0.20

func (s *SkipList[K, V]) SetNx(key K, val V) bool

SetNx sets the value associated with the key if the key does not exist.

func (*SkipList[K, V]) SetX added in v0.0.20

func (s *SkipList[K, V]) SetX(key K, val V) bool

SetX sets the value associated with the key if the key exists.

func (*SkipList[K, V]) Values added in v0.0.20

func (s *SkipList[K, V]) Values() []V

Values returns all values in the skip list.

type SkipListWithCmp added in v0.0.20

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

func NewSkipListWithCmp added in v0.0.20

func NewSkipListWithCmp[K any, V any](keyCmp func(K, K) int) *SkipListWithCmp[K, V]

NewSkipListWithCmp returns an initialized skip list with custom comparator.

func (*SkipListWithCmp[K, V]) All added in v0.0.20

func (s *SkipListWithCmp[K, V]) All() iter.Seq2[K, V]

All returns an iterator that yields all element value in the skip list with custom comparator.

func (*SkipListWithCmp[K, V]) Clear added in v0.0.20

func (s *SkipListWithCmp[K, V]) Clear()

Clear removes all nodes from the skip list.

func (*SkipListWithCmp[K, V]) Get added in v0.0.20

func (s *SkipListWithCmp[K, V]) Get(key K) (V, bool)

Get returns the value associated with the key.

func (*SkipListWithCmp[K, V]) GetNode added in v0.0.20

func (s *SkipListWithCmp[K, V]) GetNode(key K) *SkipNodeCmp[K, V]

GetNode returns the node associated with the key.

func (*SkipListWithCmp[K, V]) Head added in v0.0.20

func (s *SkipListWithCmp[K, V]) Head() *SkipNodeCmp[K, V]

Head returns the first node of the skip list.

func (*SkipListWithCmp[K, V]) Init added in v0.0.20

func (s *SkipListWithCmp[K, V]) Init(keyCmp func(K, K) int)

Init initializes the skip list with custom comparator.

func (*SkipListWithCmp[K, V]) Keys added in v0.0.20

func (s *SkipListWithCmp[K, V]) Keys() []K

Keys returns all keys in the skip list.

func (*SkipListWithCmp[K, V]) Len added in v0.0.20

func (s *SkipListWithCmp[K, V]) Len() int

Len returns the number of nodes of the skip list.

func (*SkipListWithCmp[K, V]) Range added in v0.0.20

func (s *SkipListWithCmp[K, V]) Range(f func(K, V) bool)

Range calls f sequentially for each key and value present in the skip list.

func (*SkipListWithCmp[K, V]) RangeWithRange added in v0.0.20

func (s *SkipListWithCmp[K, V]) RangeWithRange(start, end K, f func(K, V) bool)

RangeWithRange calls f sequentially for each key and value present in the skip list within the range [start, end).

func (*SkipListWithCmp[K, V]) RangeWithStart added in v0.0.20

func (s *SkipListWithCmp[K, V]) RangeWithStart(start K, f func(K, V) bool)

RangeWithStart calls f sequentially for each key and value present in the skip list starting from the key. The zone is [start, +∞)

func (*SkipListWithCmp[K, V]) Remove added in v0.0.20

func (s *SkipListWithCmp[K, V]) Remove(key K) (V, bool)

Remove deletes the value associated with the key.

func (*SkipListWithCmp[K, V]) Set added in v0.0.20

func (s *SkipListWithCmp[K, V]) Set(key K, val V)

Set sets the value associated with the key.

func (*SkipListWithCmp[K, V]) SetNx added in v0.0.20

func (s *SkipListWithCmp[K, V]) SetNx(key K, val V) bool

SetNx sets the value associated with the key if the key does not exist.

func (*SkipListWithCmp[K, V]) SetX added in v0.0.20

func (s *SkipListWithCmp[K, V]) SetX(key K, val V) bool

SetX sets the value associated with the key if the key exists.

func (*SkipListWithCmp[K, V]) Values added in v0.0.20

func (s *SkipListWithCmp[K, V]) Values() []V

Values returns all values in the skip list.

type SkipNode added in v0.0.20

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

func (*SkipNode[K, V]) Key added in v0.0.20

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

func (*SkipNode[K, V]) Next added in v0.0.20

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

func (*SkipNode[K, V]) SetValue added in v0.0.20

func (n *SkipNode[K, V]) SetValue(val V)

func (*SkipNode[K, V]) Value added in v0.0.20

func (n *SkipNode[K, V]) Value() V

type SkipNodeCmp added in v0.0.20

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

func (*SkipNodeCmp[K, V]) Key added in v0.0.20

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

func (*SkipNodeCmp[K, V]) Next added in v0.0.20

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

func (*SkipNodeCmp[K, V]) SetValue added in v0.0.20

func (n *SkipNodeCmp[K, V]) SetValue(val V)

func (*SkipNodeCmp[K, V]) Value added in v0.0.20

func (n *SkipNodeCmp[K, V]) Value() V

type SyncList

type SyncList[T any] struct {
	// contains filtered or unexported fields
}

func NewSync

func NewSync[T any]() *SyncList[T]

NewSync creates a new SyncList.

func (*SyncList[T]) Len

func (l *SyncList[T]) Len() int

Len returns the number of elements in the list.

func (*SyncList[T]) Pop

func (l *SyncList[T]) Pop() (T, bool)

Pop removes and returns the value at the front of the list. If the list is empty or concurrent Pop is in progress, it returns false.

func (*SyncList[T]) PopWait

func (l *SyncList[T]) PopWait(maxWait time.Duration) (T, bool)

PopWait removes and returns the value at the front of the list. If maxWait is negative, it will block until the value is popped.

func (*SyncList[T]) Push

func (l *SyncList[T]) Push(value T)

Push adds a value to the end of the list.

Jump to

Keyboard shortcuts

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