linkedlist

package
v1.0.0 Latest Latest
Warning

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

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

README

LinkedList

This is double linked list implementation with hash-map index Useful in cases of memory optimization and efficient go through many linked elements. Thread unsafe.

Documentation

Overview

Package linkedlist provides a generic doubly linked list with ID-based indexing.

NOT safe for concurrent use. Callers must synchronize access externally.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element[V any] struct {
	// The value stored with this element.
	Value V
	// contains filtered or unexported fields
}

Element is a node in the doubly linked list, holding a value and pointers to adjacent elements.

func (*Element[V]) ID

func (e *Element[V]) ID() string

ID returns the unique identifier assigned to this element.

func (*Element[V]) IsEmpty

func (e *Element[V]) IsEmpty() bool

IsEmpty reports whether e is an empty (sentinel) element with no assigned ID.

func (*Element[V]) Next

func (e *Element[V]) Next() *Element[V]

Next returns the next list element or an empty Element if e is at the back.

func (*Element[V]) Prev

func (e *Element[V]) Prev() *Element[V]

Prev returns the previous list element or an empty Element if e is at the front.

func (*Element[V]) Root

func (e *Element[V]) Root() *Element[V]

Root returns the first element of the list that e belongs to.

type Entity

type Entity interface {
	ID() string
}

Entity is implemented by values that provide their own identity. When a value stored in the list satisfies Entity, its ID is used as the element key instead of generating a random one.

type List

type List[V any] struct {
	// contains filtered or unexported fields
}

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

func New

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

New creates and initializes a new empty List.

func (*List[V]) Append

func (l *List[V]) Append(elements ...V)

Append pushes one or more values to the back of the list.

func (*List[V]) Back

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

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

func (*List[V]) ByID

func (l *List[V]) ByID(id string) *Element[V]

ByID returns the element with the given ID, or nil if not found.

func (*List[V]) Clear

func (l *List[V]) Clear()

Clear removes all elements from the list.

func (*List[V]) Copy

func (l *List[V]) Copy() *List[V]

Copy returns a deep copy of the list. The new list has independent elements but shares the same value references. Element order is preserved.

func (*List[V]) Front

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

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

func (*List[V]) Init

func (l *List[V]) Init() *List[V]

Init initializes or clears the list and returns it.

func (*List[V]) InsertAfter

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

InsertAfter inserts a new element with value v immediately after mark and returns it. If mark does not belong to l, the list is not modified and nil is returned.

func (*List[V]) InsertBefore

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

InsertBefore inserts a new element with value v immediately before mark and returns it. If mark does not belong to l, the list is not modified and nil is returned.

func (*List[V]) Len

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

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

func (*List[V]) List

func (l *List[V]) List() (result []V)

List returns all element values in front-to-back order as a slice.

func (*List[V]) MoveAfter

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

MoveAfter moves element e to its new position after mark.

func (*List[V]) MoveBefore

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

MoveBefore moves element e to its new position before mark.

func (*List[V]) MoveToBack

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

MoveToBack moves element e to the back of list l.

func (*List[V]) MoveToFront

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

MoveToFront moves element e to the front of list l.

func (*List[V]) NextID

func (l *List[V]) NextID() string

NextID generates a new unique identifier for an element.

func (*List[V]) PushBack

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

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

func (*List[V]) PushBackList

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

PushBackList inserts a copy of another list at the back of list l. The other list may be the same as l.

func (*List[V]) PushFront

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

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

func (*List[V]) PushFrontList

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

PushFrontList inserts a copy of another list at the front of list l. The other list may be the same as l.

func (*List[V]) Remove

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

Remove removes e from l if e belongs to the list, and returns its value.

func (*List[V]) ValueToAny

func (l *List[V]) ValueToAny(v V) any

ValueToAny converts a value of type V to the any interface for type assertion.

Jump to

Keyboard shortcuts

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