listutil

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllOfList

func AllOfList[T any](l *list.List) []T

AllOfList returns all elements of the given list as a slice of type T.

The caller must ensure that every element stored in the list is of type T. If an element has a different type, this function will panic at runtime.

If the list is nil or empty, an empty slice is returned.

func ListOf

func ListOf[T any](items ...T) *list.List

ListOf creates a new list.List and populates it with the provided items.

All elements are inserted in the order they appear in the argument list.

func SliceOf added in v0.0.8

func SliceOf[T any](items ...T) []T

SliceOf returns a slice containing the provided items.

func WriteStrings added in v0.0.8

func WriteStrings(w io.Writer, values ...string) error

WriteStrings writes the provided strings to w in order. It returns the first error encountered, if any.

Types

type Element added in v0.0.11

type Element[T any] struct {
	*list.Element
}

Element is an element of a linked list.

func (Element[T]) Next added in v0.0.11

func (e Element[T]) Next() Element[T]

Next returns the next element of list l or nil if e is the last element.

func (Element[T]) Prev added in v0.0.11

func (e Element[T]) Prev() Element[T]

Prev returns the previous element of list l or nil if e is the first element.

func (Element[T]) Valid added in v0.0.11

func (e Element[T]) Valid() bool

Valid returns true if e is a valid element of list l.

func (Element[T]) Value added in v0.0.11

func (e Element[T]) Value() T

Value returns the value of element e.

type List added in v0.0.11

type List[T any] struct {
	*list.List
}

List is a doubly linked list.

func New added in v0.0.11

func New[T any]() *List[T]

New returns an empty list.

Example
l := New[int]()
l.PushBack(1)
l.PushBack(2)
l.PushBack(3)

for e := l.Front(); e.Valid(); e = e.Next() {
	_ = e.Value()
}

func (*List[T]) Back added in v0.0.11

func (l *List[T]) Back() Element[T]

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

func (*List[T]) Front added in v0.0.11

func (l *List[T]) Front() Element[T]

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

func (*List[T]) InsertAfter added in v0.0.11

func (l *List[T]) InsertAfter(v T, mark Element[T]) Element[T]

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

func (*List[T]) InsertBefore added in v0.0.11

func (l *List[T]) InsertBefore(v T, mark Element[T]) Element[T]

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

Example
l := New[int]()
e1 := l.PushBack(1)
e3 := l.PushBack(3)

l.InsertBefore(2, e3)

for e := l.Front(); e.Valid(); e = e.Next() {
	_ = e.Value()
}
_ = e1

func (*List[T]) Len added in v0.0.11

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

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

func (*List[T]) MoveAfter added in v0.0.11

func (l *List[T]) MoveAfter(e, mark Element[T])

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

func (*List[T]) MoveBefore added in v0.0.11

func (l *List[T]) MoveBefore(e, mark Element[T])

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

func (*List[T]) MoveToBack added in v0.0.11

func (l *List[T]) MoveToBack(e Element[T])

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

func (*List[T]) MoveToFront added in v0.0.11

func (l *List[T]) MoveToFront(e Element[T])

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

Example
l := New[int]()
l.PushBack(1)
l.PushBack(2)
l.PushBack(3)

e := l.Back()
l.MoveToFront(e)

_ = l.Front().Value()

func (*List[T]) PushBack added in v0.0.11

func (l *List[T]) PushBack(v T) Element[T]

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

Example
l := New[string]()
l.PushBack("apple")
l.PushBack("banana")
l.PushBack("cherry")

front := l.Front().Value()
back := l.Back().Value()
_ = front
_ = back

func (*List[T]) PushBackList added in v0.0.11

func (l *List[T]) PushBackList(other *List[T])

PushBackList 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.

Example
l1 := New[int]()
l1.PushBack(1)
l1.PushBack(2)

l2 := New[int]()
l2.PushBack(3)
l2.PushBack(4)

l1.PushBackList(l2)

for e := l1.Front(); e.Valid(); e = e.Next() {
	_ = e.Value()
}

func (*List[T]) PushFront added in v0.0.11

func (l *List[T]) PushFront(v T) Element[T]

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

func (*List[T]) PushFrontList added in v0.0.11

func (l *List[T]) PushFrontList(other *List[T])

PushFrontList 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 (*List[T]) Remove added in v0.0.11

func (l *List[T]) Remove(e Element[T]) T

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

Example
l := New[int]()
e1 := l.PushBack(10)
e2 := l.PushBack(20)
e3 := l.PushBack(30)

l.Remove(e2)

for e := l.Front(); e.Valid(); e = e.Next() {
	_ = e.Value()
}
_ = e1
_ = e3

Jump to

Keyboard shortcuts

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