linkedlist

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package linkedlist provides a doubly linked list whose nodes live off the Go heap in one contiguous pool and link to each other by 32-bit index, so links cost eight bytes per node. Removed nodes are reused by later insertions.

Example
package main

import (
	"fmt"

	memory "github.com/stubbedev/memory-go"
	"github.com/stubbedev/memory-go/linkedlist"
)

func main() {
	var a memory.Local
	defer a.Close()

	l, err := linkedlist.New[rune](&a)
	if err != nil {
		panic(err)
	}
	defer l.Free()
	for _, r := range "bcd" {
		if err := l.PushBack(r); err != nil {
			panic(err)
		}
	}
	if err := l.PushFront('a'); err != nil {
		panic(err)
	}
	l.RemoveAt(2)
	for _, r := range l.All() {
		fmt.Print(string(r))
	}
	fmt.Println()
}
Output:
abd

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

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

List is a doubly linked list of off-heap Ts. It is not safe for concurrent use. Pointers into it are valid until the next insertion or Clip.

func New

func New[T any](a memory.Allocator) (*List[T], error)

New returns an empty List.

func (*List[T]) All

func (l *List[T]) All() iter.Seq2[int, T]

All yields each index and element in order. Inserting or removing elements, or Clip, during the loop panics; changing values is allowed.

func (*List[T]) At

func (l *List[T]) At(i int) T

At returns element i, walking from the nearer end. It panics if i is out of range.

func (*List[T]) Clip

func (l *List[T]) Clip() error

Clip rebuilds the node pool in list order with no spare nodes, releasing the memory of removed elements and making traversal sequential in memory.

func (*List[T]) DeleteFunc

func (l *List[T]) DeleteFunc(del func(T) bool) int

DeleteFunc removes every element for which del returns true and returns how many it removed.

func (*List[T]) Free

func (l *List[T]) Free() error

Free releases every node and leaves l empty.

func (*List[T]) IndexFunc

func (l *List[T]) IndexFunc(match func(T) bool) int

IndexFunc returns the index of the first element for which match returns true, or -1 if there is none.

func (*List[T]) Len

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

Len returns the number of elements.

func (*List[T]) PopBack

func (l *List[T]) PopBack() (T, bool)

PopBack removes and returns the last element, or reports false if l is empty.

func (*List[T]) PopFront

func (l *List[T]) PopFront() (T, bool)

PopFront removes and returns the first element, or reports false if l is empty.

func (*List[T]) Ptr

func (l *List[T]) Ptr(i int) *T

Ptr returns a pointer to element i. It panics if i is out of range.

func (*List[T]) PushBack

func (l *List[T]) PushBack(value T) error

PushBack appends value.

func (*List[T]) PushFront

func (l *List[T]) PushFront(value T) error

PushFront prepends value.

func (*List[T]) RemoveAt

func (l *List[T]) RemoveAt(i int) T

RemoveAt removes element i and returns it. It panics if i is out of range.

Jump to

Keyboard shortcuts

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