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 ¶
- type ListHead
- func (l *ListHead[PT, T]) All() iter.Seq[PT]
- func (l *ListHead[PT, T]) AllReverse() iter.Seq[PT]
- func (l *ListHead[PT, T]) Back() PT
- func (l *ListHead[PT, T]) Front() PT
- func (l *ListHead[PT, T]) Initialize()
- func (l *ListHead[PT, T]) IsEmpty() bool
- func (l *ListHead[PT, T]) PushBack(v *ListLink[PT, T])
- func (l *ListHead[PT, T]) PushFront(v *ListLink[PT, T])
- type ListLink
- func (v *ListLink[PT, T]) Initialize(embedder PT)
- func (v *ListLink[PT, T]) InsertAfter(other *ListLink[PT, T])
- func (v *ListLink[PT, T]) InsertBefore(other *ListLink[PT, T])
- func (v *ListLink[PT, T]) IsEmpty() bool
- func (v *ListLink[PT, T]) Next() PT
- func (v *ListLink[PT, T]) Prev() PT
- func (v *ListLink[PT, T]) Remove()
- func (v *ListLink[PT, T]) Value() PT
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 ¶
All returns an iterator over the list's elements in forward order, yielding each element's containing struct pointer.
func (*ListHead[PT, T]) AllReverse ¶
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.
type ListLink ¶
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 ¶
InsertAfter inserts other after v, removing other from its current list first.
func (*ListLink[PT, T]) InsertBefore ¶
InsertBefore inserts other before v, removing other from its current list first.
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.