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 ¶
- type List
- func (l *List[T]) All() iter.Seq2[int, T]
- func (l *List[T]) At(i int) T
- func (l *List[T]) Clip() error
- func (l *List[T]) DeleteFunc(del func(T) bool) int
- func (l *List[T]) Free() error
- func (l *List[T]) IndexFunc(match func(T) bool) int
- func (l *List[T]) Len() int
- func (l *List[T]) PopBack() (T, bool)
- func (l *List[T]) PopFront() (T, bool)
- func (l *List[T]) Ptr(i int) *T
- func (l *List[T]) PushBack(value T) error
- func (l *List[T]) PushFront(value T) error
- func (l *List[T]) RemoveAt(i int) T
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 (*List[T]) All ¶
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 ¶
At returns element i, walking from the nearer end. It panics if i is out of range.
func (*List[T]) Clip ¶
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 ¶
DeleteFunc removes every element for which del returns true and returns how many it removed.
func (*List[T]) IndexFunc ¶
IndexFunc returns the index of the first element for which match returns true, or -1 if there is none.
func (*List[T]) PopBack ¶
PopBack removes and returns the last element, or reports false if l is empty.
func (*List[T]) PopFront ¶
PopFront removes and returns the first element, or reports false if l is empty.