func New
New creates a new FIFO list with the specified maximum size
Package fifo implements a fixed-size FIFO (First-In-First-Out) list data structure using a singly-linked list. The im...
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:
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"]
List represents a fixed-size FIFO list
Append adds a new entry at the end of the list. If the list exceeds maxSize, the first entry is automatically removed.
Delete removes the element at the specified index. Returns true if an element was removed, false if the index was invalid.
Entries returns all current entries as a slice
Get returns the entry at the specified index. Index 0 is the oldest entry, Size()-1 is the newest.
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.
Latest returns the most recent entry. Returns nil if the list is empty.
MaxSize returns the maximum size configured for this list
Prepend adds a new entry at the start of the list. If the list exceeds maxSize, the last entry is automatically removed.
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.
Size returns the current number of entries in the list