Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

fifo source pure

Package fifo implements a fixed-size FIFO (First-In-First-Out) list data structure using a singly-linked list. The im...

Overview

Package fifo implements a fixed-size FIFO (First-In-First-Out) list data structure using a singly-linked list. The implementation prioritizes storage efficiency by minimizing storage operations - each add/remove operation only updates 1-2 pointers, regardless of list size.

Key features: - Fixed-size with automatic removal of oldest entries when full - Support for both prepend (add at start) and append (add at end) operations - Constant storage usage through automatic pruning - O(1) append operations and latest element access - Iterator support for sequential access - Dynamic size adjustment via SetMaxSize

This implementation is optimized for frequent updates, as insertions and deletions only require updating 1-2 pointers. However, random access operations are O(n) as they require traversing the list. For use cases where writes are rare, a slice-based implementation might be more suitable.

The linked list structure is equally efficient for storing both small values (like pointers) and larger data structures, as each node maintains a single next-pointer regardless of the stored value's size.

Example usage:

Example
1list := fifo.New(3)        // Create a new list with max size 3
2list.Append("a")           // List: [a]
3list.Append("b")           // List: [a b]
4list.Append("c")           // List: [a b c]
5list.Append("d")           // List: [b c d] (oldest element "a" was removed)
6latest := list.Latest()    // Returns "d"
7all := list.Entries()      // Returns ["b", "c", "d"]

Functions 1

func New

1func New(maxSize int) *List
source

New creates a new FIFO list with the specified maximum size

Types 1

type List

struct
1type List struct {
2	head    *node
3	tail    *node
4	size    int
5	maxSize int
6}
source

List represents a fixed-size FIFO list

Methods on List

func Append

method on List
1func (l *List) Append(entry any)
source

Append adds a new entry at the end of the list. If the list exceeds maxSize, the first entry is automatically removed.

func Delete

method on List
1func (l *List) Delete(index int) bool
source

Delete removes the element at the specified index. Returns true if an element was removed, false if the index was invalid.

func Entries

method on List
1func (l *List) Entries() []any
source

Entries returns all current entries as a slice

func Get

method on List
1func (l *List) Get(index int) any
source

Get returns the entry at the specified index. Index 0 is the oldest entry, Size()-1 is the newest.

func Iterator

method on List
1func (l *List) Iterator() func() any
source

Iterator returns a function that can be used to iterate over the entries from oldest to newest. Returns nil when there are no more entries.

func Latest

method on List
1func (l *List) Latest() any
source

Latest returns the most recent entry. Returns nil if the list is empty.

func MaxSize

method on List
1func (l *List) MaxSize() int
source

MaxSize returns the maximum size configured for this list

func Prepend

method on List
1func (l *List) Prepend(entry any)
source

Prepend adds a new entry at the start of the list. If the list exceeds maxSize, the last entry is automatically removed.

func SetMaxSize

method on List
1func (l *List) SetMaxSize(maxSize int)
source

SetMaxSize updates the maximum size of the list. If the new maxSize is smaller than the current size, the oldest entries are removed to fit the new size.

func Size

method on List
1func (l *List) Size() int
source

Size returns the current number of entries in the list

Source Files 3