list

package module
v0.0.0-...-291f210 Latest Latest
Warning

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

Go to latest
Published: May 12, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package list provides a generic, intrusive doubly-linked list implementation. List elements are structs with embeded ListLink fields, allowing a single struct to belong to multiple lists without duplicating data. This reduces memory allocations and supports efficient insertion, removal, and bidirectional traversal.

WARNING: ListLink must be embedded in the containing struct type T and each ListLink field must be initialized with Initialize(&struct) before use. ListHead must be initialized with Initialize(). Failure to initialize a ListLink causes a panic.

List implementation is not thread-safe; users must ensure synchronization for concurrent access.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ListHead

type ListHead[PT *T, T any] struct {
	// contains filtered or unexported fields
}

ListHead represents a doubly-linked list, connecting elements via embedded ListLink fields within the list elements. ListHead values *must* be initialized with Initialize() before use.

func (*ListHead[PT, T]) All

func (l *ListHead[PT, T]) All() iter.Seq[PT]

All returns an iterator over the list's elements in forward order, yielding each element's containing struct pointer.

func (*ListHead[PT, T]) AllReverse

func (l *ListHead[PT, T]) AllReverse() iter.Seq[PT]

AllReverse returns an iterator over the list's elements in reverse order, yielding each element's containing struct pointer.

func (*ListHead[PT, T]) Back

func (l *ListHead[PT, T]) Back() PT

Back returns the last element in the list. Returns nil if list is empty.

func (*ListHead[PT, T]) Front

func (l *ListHead[PT, T]) Front() PT

Front returns the first element in the list. Returns nil if list is empty.

func (*ListHead[PT, T]) Initialize

func (l *ListHead[PT, T]) Initialize()

Initialize initializes the ListHead as an empty list.

func (*ListHead[PT, T]) IsEmpty

func (l *ListHead[PT, T]) IsEmpty() bool

IsEmpty returns true if the list is empty.

func (*ListHead[PT, T]) PushBack

func (l *ListHead[PT, T]) PushBack(v *ListLink[PT, T])

PushBack inserts v at the back of the list, removing it from its current list.

func (*ListHead[PT, T]) PushFront

func (l *ListHead[PT, T]) PushFront(v *ListLink[PT, T])

PushFront inserts v at the front of the list, removing it from its current list.

type ListLink[PT *T, T any] struct {
	// contains filtered or unexported fields
}

ListLink represents a single doubly-linked list element. It must be embedded within the list element (struct of type T).

ListLink values must be initialized with the containing struct object with Initialize(&struct).

Embedding multiple ListLink fields allows a struct object to participate in multiple lists without memory allocations.

func (*ListLink[PT, T]) Initialize

func (v *ListLink[PT, T]) Initialize(embedder PT)

Initialize initializes the ListLink as a list element with the address of its containing struct. Panics if embedder is nil or if link is already initialized.

func (*ListLink[PT, T]) InsertAfter

func (v *ListLink[PT, T]) InsertAfter(other *ListLink[PT, T])

InsertAfter inserts other after v, removing other from its current list first.

func (*ListLink[PT, T]) InsertBefore

func (v *ListLink[PT, T]) InsertBefore(other *ListLink[PT, T])

InsertBefore inserts other before v, removing other from its current list first.

func (*ListLink[PT, T]) IsEmpty

func (v *ListLink[PT, T]) IsEmpty() bool

IsEmpty returns true if the element is not in a list.

func (*ListLink[PT, T]) Next

func (v *ListLink[PT, T]) Next() PT

Next returns element after the receiver. Returns nil if receiver is not part of a list or is at the end of the list.

func (*ListLink[PT, T]) Prev

func (v *ListLink[PT, T]) Prev() PT

Prev returns element before the receiver. Returns nil if receiver is not part of a list or is at the beginning of the list.

func (*ListLink[PT, T]) Remove

func (v *ListLink[PT, T]) Remove()

Remove removes the element from its list. Safe to call on an already removed element.

func (*ListLink[PT, T]) Value

func (v *ListLink[PT, T]) Value() PT

Value returns the list element.

Jump to

Keyboard shortcuts

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