A generic ordered map for Go 1.24+ that preserves insertion order while providing O(1) lookups. All methods are protected by an RWMutex, and SortedPairs accepts a comparator to return a sorted snapshot without side effects.
- Preserves insertion order while providing O(1) lookups like a built-in map.
- Keys are always unique; calling Set again does not change the order.
- SortedPairs accepts a comparator and returns a sorted snapshot (sorting happens outside the lock).
- SortedPairs uses a stable sort: keys considered equal keep their insertion (snapshot) order.
- All APIs are goroutine-safe (RWMutex + snapshot returns).
- See
docs/ordered_map_design.mdfor design details.
// Constructor
m := NewOrderedMap[string, int](WithComparator(func(a, b string) bool { return a < b }))
// Basic operations
m.Set("b", 20)
m.Set("a", 10)
v, ok := m.Get("a")
_ = ok
// Insertion-order access
pairs := m.Pairs() // [{Key:"b",Value:20}, {Key:"a",Value:10}]
// Sorted access (side-effect-free)
sorted, err := m.SortedPairs(nil) // if nil, uses the default less registered via WithComparator
_ = err
_ = sorted- This repository contains the design documentation and an implementation.
- The legacy
ExtendedMapimplementation is being replaced byordered_map.go.
- O(1):
Set/Get/Has/Len - O(n):
Delete/InsertBefore/InsertAfter(search/compaction/shift on the order slice) - O(n log n):
SortedPairs - Intended size: small-to-medium ordered data where order is part of the spec (roughly tens to hundreds of entries). Not suitable for huge datasets or hot-path caches.
- Clear policy:
Clear()keeps internal capacity for reuse. To aggressively release memory, create a new map withNewOrderedMap.
SortedPairscomparator: invoked outside the lock after snapshotting. A heavy comparator will make the call slower, but it does not block writers.Upsertupdate functionfn: invoked under the write lock to provide atomic updates.fnmust be fast and side-effect-free, and MUST NOT re-enter the sameOrderedMap(callingGet/Set/Delete/...will deadlock).
- Managing evaluation order for configs/rules
- Enumerating feature flag definitions
- LLM prompt pipelines where order is part of the spec
docs/ordered_map_design.md