orderedmap

package
v1.31.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package orderedmap provides a generic ordered map that preserves insertion order and supports YAML and JSON marshaling/unmarshaling.

Index

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 is a generic ordered map that preserves insertion order and supports YAML and JSON marshaling/unmarshaling. The zero value is not usable; use New to create instances.

func New

func New[K comparable, V any](capacity ...int) *OrderedMap[K, V]

New creates a new OrderedMap with optional capacity hint. If capacity is provided, it pre-allocates space for that many elements.

func (*OrderedMap[K, V]) All

func (om *OrderedMap[K, V]) All() iter.Seq2[K, V]

All returns an iterator over all key-value pairs in insertion order. This enables the modern Go range-over-func syntax:

for key, value := range om.All() {
    // use key and value
}

func (*OrderedMap[K, V]) Delete

func (om *OrderedMap[K, V]) Delete(key K) (V, bool)

Delete removes a key-value pair. Returns the old value and true if the key existed, or zero value and false if not.

func (*OrderedMap[K, V]) Get

func (om *OrderedMap[K, V]) Get(key K) (V, bool)

Get retrieves a value by key. Returns the value and true if found, or the zero value and false if not found.

func (*OrderedMap[K, V]) Has

func (om *OrderedMap[K, V]) Has(key K) bool

Has checks if a key exists in the map.

func (*OrderedMap[K, V]) KeySlice

func (om *OrderedMap[K, V]) KeySlice() []K

KeySlice returns an ordered slice of all keys.

func (*OrderedMap[K, V]) Keys

func (om *OrderedMap[K, V]) Keys() iter.Seq[K]

Keys returns an iterator over all keys in insertion order. This enables iterating over just keys:

for key := range om.Keys() {
    // use key
}

func (*OrderedMap[K, V]) Len

func (om *OrderedMap[K, V]) Len() int

Len returns the number of key-value pairs in the map.

func (*OrderedMap[K, V]) MarshalJSON

func (om *OrderedMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. The map is serialized in insertion order.

func (*OrderedMap[K, V]) MarshalYAML

func (om *OrderedMap[K, V]) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler. The map is serialized in insertion order.

func (*OrderedMap[K, V]) Newest

func (om *OrderedMap[K, V]) Newest() *Pair[K, V]

Newest returns the last (newest) pair for backward iteration. Returns nil if the map is empty. Use this for manual iteration: for pair := om.Newest(); pair != nil; pair = pair.Prev().

func (*OrderedMap[K, V]) Oldest

func (om *OrderedMap[K, V]) Oldest() *Pair[K, V]

Oldest returns the first (oldest) pair for forward iteration. Returns nil if the map is empty. Use this for manual iteration: for pair := om.Oldest(); pair != nil; pair = pair.Next().

func (*OrderedMap[K, V]) Set

func (om *OrderedMap[K, V]) Set(key K, value V)

Set adds or updates a key-value pair. If the key already exists, its value is updated without changing its position.

func (*OrderedMap[K, V]) UnmarshalJSON

func (om *OrderedMap[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. The insertion order from the JSON document is preserved.

func (*OrderedMap[K, V]) UnmarshalYAML

func (om *OrderedMap[K, V]) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler. The insertion order from the YAML document is preserved.

func (*OrderedMap[K, V]) ValueSlice

func (om *OrderedMap[K, V]) ValueSlice() []V

ValueSlice returns an ordered slice of all values.

func (*OrderedMap[K, V]) Values

func (om *OrderedMap[K, V]) Values() iter.Seq[V]

Values returns an iterator over all values in insertion order. This enables iterating over just values:

for value := range om.Values() {
    // use value
}

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
	// contains filtered or unexported fields
}

Pair represents a key-value pair in the ordered map. It can be used to iterate through the map in insertion order.

func (*Pair[K, V]) Next

func (p *Pair[K, V]) Next() *Pair[K, V]

Next returns the next pair in insertion order, or nil if this is the last.

func (*Pair[K, V]) Prev

func (p *Pair[K, V]) Prev() *Pair[K, V]

Prev returns the previous pair in insertion order, or nil if this is the first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL