Documentation
¶
Overview ¶
Package orderedmap provides a thread-safe, generic ordered map that maintains insertion order while providing O(1) lookups. Unlike Go's built-in map, OrderedMap iterates in the order items were added, preventing UI flickering and enabling predictable, deterministic behavior.
Index ¶
- type OrderedMap
- func (om *OrderedMap[K, V]) At(i int) (K, V, bool)
- func (om *OrderedMap[K, V]) Back() (K, V, bool)
- func (om *OrderedMap[K, V]) Clear()
- func (om *OrderedMap[K, V]) Delete(key K) bool
- func (om *OrderedMap[K, V]) DeleteWithValue(key K) (V, bool)
- func (om *OrderedMap[K, V]) Front() (K, V, bool)
- func (om *OrderedMap[K, V]) Get(key K) (V, bool)
- func (om *OrderedMap[K, V]) GetOrSet(key K, mk func() V) (V, bool)
- func (om *OrderedMap[K, V]) Has(key K) bool
- func (om *OrderedMap[K, V]) Keys() []K
- func (om *OrderedMap[K, V]) Len() int
- func (om *OrderedMap[K, V]) MoveToEnd(key K) bool
- func (om *OrderedMap[K, V]) PopBack() (K, V, bool)
- func (om *OrderedMap[K, V]) PopFront() (K, V, bool)
- func (om *OrderedMap[K, V]) Range(fn func(key K, value V))
- func (om *OrderedMap[K, V]) RangeBreak(fn func(key K, value V) bool)
- func (om *OrderedMap[K, V]) RangeBreakLocked(fn func(key K, value V) bool)
- func (om *OrderedMap[K, V]) RangeLocked(fn func(key K, value V))
- func (om *OrderedMap[K, V]) Reset()
- func (om *OrderedMap[K, V]) Set(key K, value V)
- func (om *OrderedMap[K, V]) Values() []V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type OrderedMap ¶
type OrderedMap[K comparable, V any] struct { // contains filtered or unexported fields }
OrderedMap maintains insertion order while providing O(1) lookups, deletes, and moves This prevents UI flickering from Go's random map iteration order
Thread-safe: All operations use internal locking Zero value is usable: var om OrderedMap[K,V] works without initialization All operations are O(1) except Range which is O(n) Range/RangeBreak: Iterate over snapshot at call time, not live data (safe for re-entrant calls)
func NewOrderedMap ¶
func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]
NewOrderedMap creates a new ordered map Note: The zero value is also usable without calling this constructor
func (*OrderedMap[K, V]) At ¶
func (om *OrderedMap[K, V]) At(i int) (K, V, bool)
At returns the key-value pair at the given index, or zero values if out of bounds (O(n))
func (*OrderedMap[K, V]) Back ¶
func (om *OrderedMap[K, V]) Back() (K, V, bool)
Back returns the last key-value pair, or zero values if empty
func (*OrderedMap[K, V]) Clear ¶
func (om *OrderedMap[K, V]) Clear()
Clear removes all entries while preserving allocated capacity
func (*OrderedMap[K, V]) Delete ¶
func (om *OrderedMap[K, V]) Delete(key K) bool
Delete removes a key, preserving insertion order (O(1) complexity) Returns true if the key existed and was deleted
func (*OrderedMap[K, V]) DeleteWithValue ¶
func (om *OrderedMap[K, V]) DeleteWithValue(key K) (V, bool)
DeleteWithValue removes a key and returns its value (O(1) complexity) Returns the value and true if the key existed, or zero value and false otherwise
func (*OrderedMap[K, V]) Front ¶
func (om *OrderedMap[K, V]) Front() (K, V, bool)
Front returns the first key-value pair, or zero values if empty
func (*OrderedMap[K, V]) Get ¶
func (om *OrderedMap[K, V]) Get(key K) (V, bool)
Get retrieves a value by key (O(1) lookup)
func (*OrderedMap[K, V]) GetOrSet ¶
func (om *OrderedMap[K, V]) GetOrSet(key K, mk func() V) (V, bool)
GetOrSet returns the existing value if key exists, otherwise calls mk to create a new value, stores it, and returns it. The second return value indicates whether the value already existed (true) or was newly created (false).
WARNING: mk is called while holding the write lock. Keep it fast and simple. This avoids double lookups when implementing caching patterns.
func (*OrderedMap[K, V]) Has ¶
func (om *OrderedMap[K, V]) Has(key K) bool
Has checks if a key exists (O(1) lookup)
func (*OrderedMap[K, V]) Keys ¶
func (om *OrderedMap[K, V]) Keys() []K
Keys returns a copy of all keys in insertion order
func (*OrderedMap[K, V]) Len ¶
func (om *OrderedMap[K, V]) Len() int
Len returns the number of entries (O(1))
func (*OrderedMap[K, V]) MoveToEnd ¶
func (om *OrderedMap[K, V]) MoveToEnd(key K) bool
MoveToEnd moves the given key to the end of the insertion order (O(1) complexity) Returns true if the key existed and was moved, false otherwise Useful for implementing LRU-like behavior
func (*OrderedMap[K, V]) PopBack ¶
func (om *OrderedMap[K, V]) PopBack() (K, V, bool)
PopBack removes and returns the last key-value pair (O(1) complexity) Returns zero values and false if the map is empty
func (*OrderedMap[K, V]) PopFront ¶
func (om *OrderedMap[K, V]) PopFront() (K, V, bool)
PopFront removes and returns the first key-value pair (O(1) complexity) Returns zero values and false if the map is empty
func (*OrderedMap[K, V]) Range ¶
func (om *OrderedMap[K, V]) Range(fn func(key K, value V))
Range iterates over all entries IN INSERTION ORDER at the time of call Takes a snapshot before iterating, so fn can safely call other methods without deadlock
Note: Snapshots both keys and values. Mutations to the value V after the snapshot are not reflected in the iteration. For pointer or reference types, changes to the underlying data will be visible.
func (*OrderedMap[K, V]) RangeBreak ¶
func (om *OrderedMap[K, V]) RangeBreak(fn func(key K, value V) bool)
RangeBreak iterates in insertion order and stops when fn returns false Takes a snapshot before iterating, so fn can safely call other methods without deadlock
Note: Snapshots both keys and values. Mutations to the value V after the snapshot are not reflected in the iteration. For pointer or reference types, changes to the underlying data will be visible.
func (*OrderedMap[K, V]) RangeBreakLocked ¶
func (om *OrderedMap[K, V]) RangeBreakLocked(fn func(key K, value V) bool)
RangeBreakLocked iterates while holding the RLock and stops when fn returns false WARNING: fn MUST NOT call any OrderedMap methods or deadlock will occur Use RangeBreak() if you need to modify the map during iteration
func (*OrderedMap[K, V]) RangeLocked ¶
func (om *OrderedMap[K, V]) RangeLocked(fn func(key K, value V))
RangeLocked iterates while holding the RLock (no snapshot allocation) WARNING: fn MUST NOT call any OrderedMap methods or deadlock will occur Use Range() if you need to modify the map during iteration
func (*OrderedMap[K, V]) Reset ¶
func (om *OrderedMap[K, V]) Reset()
Reset removes all entries and frees allocated memory
func (*OrderedMap[K, V]) Set ¶
func (om *OrderedMap[K, V]) Set(key K, value V)
Set adds or updates a key-value pair (O(1)) If key exists, updates the value in-place (preserves order) If key is new, appends to the end
func (*OrderedMap[K, V]) Values ¶
func (om *OrderedMap[K, V]) Values() []V
Values returns a copy of all values in insertion order