sequencedmap

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package sequencedmap provides a map implementation that maintains the order of keys as they are added.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Len added in v1.0.0

func Len[K comparable, V any](m *Map[K, V]) int

Len returns the number of elements in the map. nil safe.

Types

type Element

type Element[K comparable, V any] struct {
	Key   K
	Value V
}

Element is a key-value pair that is stored in a sequenced map.

func NewElem

func NewElem[K comparable, V any](key K, value V) *Element[K, V]

NewElem creates a new element with the specified key and value.

func (*Element[K, V]) GetKey added in v1.0.0

func (e *Element[K, V]) GetKey() K

GetKey returns the key of the element. If the element is nil, the zero value of the key type is returned.

func (*Element[K, V]) GetValue added in v1.0.0

func (e *Element[K, V]) GetValue() V

GetValue returns the value of the element. If the element is nil, the zero value of the value type is returned.

type Map

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

Map is a map implementation that maintains the order of keys as they are added.

func From added in v1.0.0

func From[K comparable, V any](seq iter.Seq2[K, V]) *Map[K, V]

From creates a new map from the given sequence.

func New

func New[K comparable, V any](elements ...*Element[K, V]) *Map[K, V]

New creates a new map with the specified elements.

func NewWithCapacity

func NewWithCapacity[K comparable, V any](capacity int, elements ...*Element[K, V]) *Map[K, V]

NewWithCapacity creates a new map with the specified capacity and elements.

func (*Map[K, V]) Add added in v1.0.0

func (m *Map[K, V]) Add(key K, value V)

Add adds the specified key-value pair to the map. If the key already exists, it is moved to the end of the list.

func (*Map[K, V]) AddAny added in v1.0.0

func (m *Map[K, V]) AddAny(key, value any)

AddAny Add with any type

func (*Map[K, V]) All

func (m *Map[K, V]) All() iter.Seq2[K, V]

All returns an iterator that iterates over all elements in the map, in the order they were added. The iterator is safe for concurrent mutations - it creates a snapshot at the start of iteration, so elements added or removed during iteration will not affect the current iteration.

func (*Map[K, V]) AllOrdered added in v1.0.0

func (m *Map[K, V]) AllOrdered(order OrderType) iter.Seq2[K, V]

AllOrdered returns an iterator that iterates over all elements in the map in the specified order. The iterator is safe for concurrent mutations - it creates a snapshot at the start of iteration, so elements added or removed during iteration will not affect the current iteration.

func (*Map[K, V]) AllUntyped

func (m *Map[K, V]) AllUntyped() iter.Seq2[any, any]

AllUntyped returns an iterator that iterates over all elements in the map with untyped key and value. This allows for using the map in generic code. The iterator is safe for concurrent mutations - it creates a snapshot at the start of iteration.

func (*Map[K, V]) At added in v1.0.0

func (m *Map[K, V]) At(index int) *Element[K, V]

At returns the element at the specified index.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K)

Delete removes the element with the specified key from the map.

func (*Map[K, V]) DeleteAny added in v0.2.2

func (m *Map[K, V]) DeleteAny(key any)

DeleteAny Delete with any type

func (*Map[K, V]) First added in v1.0.0

func (m *Map[K, V]) First() *Element[K, V]

First returns the first element in the map.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K) (V, bool)

Get returns the value for the specified key and a boolean indicating whether the key was found.

func (*Map[K, V]) GetAny added in v0.2.2

func (m *Map[K, V]) GetAny(key any) (any, bool)

GetAny Get with any type

func (*Map[K, V]) GetKeyType

func (m *Map[K, V]) GetKeyType() reflect.Type

GetKeyType returns the type of the keys in the map.

func (*Map[K, V]) GetOrZero

func (m *Map[K, V]) GetOrZero(key K) V

GetOrZero returns the value for the specified key or the zero value if the key is not found.

func (*Map[K, V]) GetUntyped

func (m *Map[K, V]) GetUntyped(key any) (any, bool)

GetUntyped returns the untyped value for the specified key with untyped key and a boolean indicating whether the key was found. This allows for using the map in generic code. If they key is not of the correct type, the zero value is returned.

func (*Map[K, V]) GetValueType

func (m *Map[K, V]) GetValueType() reflect.Type

GetValueType returns the type of the values in the map.

func (*Map[K, V]) Has

func (m *Map[K, V]) Has(key K) bool

Has returns a boolean indicating whether the map contains the specified key.

func (*Map[K, V]) Init

func (m *Map[K, V]) Init()

Init initializes the underlying resources of the map.

func (*Map[K, V]) IsEqual added in v1.0.0

func (m *Map[K, V]) IsEqual(other *Map[K, V]) bool

IsEqual compares two Map instances for equality. It compares both the keys and values, and requires them to be in the same order. Treats both empty and nil maps as equal.

func (*Map[K, V]) IsEqualFunc added in v1.0.0

func (m *Map[K, V]) IsEqualFunc(other *Map[K, V], equalFunc func(V, V) bool) bool

IsEqualFunc compares two Map instances for equality using a custom comparison function. This is useful when you need custom comparison logic for the values. Treats both empty and nil maps as equal.

func (*Map[K, V]) IsInitialized added in v1.0.0

func (m *Map[K, V]) IsInitialized() bool

IsInitialized returns true if the map has been initialized.

func (*Map[K, V]) Keys

func (m *Map[K, V]) Keys() iter.Seq[K]

Keys returns an iterator that iterates over all keys in the map, in the order they were added. The iterator is safe for concurrent mutations - it creates a snapshot at the start of iteration.

func (*Map[K, V]) KeysAny added in v0.2.2

func (m *Map[K, V]) KeysAny() iter.Seq[any]

KeysAny Keys with any type The iterator is safe for concurrent mutations - it creates a snapshot at the start of iteration.

func (*Map[K, V]) Last added in v1.0.0

func (m *Map[K, V]) Last() *Element[K, V]

Last returns the last element in the map.

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

Len returns the number of elements in the map. nil safe.

func (*Map[K, V]) MarshalJSON

func (m *Map[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON representation of the map.

func (*Map[K, V]) MarshalYAML added in v1.0.1

func (m *Map[K, V]) MarshalYAML() (interface{}, error)

MarshalYAML implements the yaml.Marshaler interface for the Map type. It marshals the sequenced map to YAML, preserving the order of keys.

func (*Map[K, V]) NavigateWithKey

func (m *Map[K, V]) NavigateWithKey(key string) (any, error)

NavigateWithKey returns the value for the specified key with the key as a string. This is an implementation of the jsonpointer.KeyNavigable interface.

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(key K, value V)

Set sets the value for the specified key. If the key does not exist, it is added to the end of the list.

func (*Map[K, V]) SetAny added in v0.2.2

func (m *Map[K, V]) SetAny(key, value any)

SetAny Set with any type

func (*Map[K, V]) SetUntyped

func (m *Map[K, V]) SetUntyped(key, value any) error

SetUntyped sets the value for the specified key with untyped key and value. This allows for using the map in generic code. An error is returned if the key or value is not of the correct type.

func (*Map[K, V]) UnmarshalYAML added in v1.0.1

func (m *Map[K, V]) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements the yaml.Unmarshaler interface for the Map type. It unmarshals YAML data into the sequenced map, preserving the order of keys.

func (*Map[K, V]) Values

func (m *Map[K, V]) Values() iter.Seq[V]

Values returns an iterator that iterates over all values in the map, in the order they were added. The iterator is safe for concurrent mutations - it creates a snapshot at the start of iteration.

type OrderType added in v1.0.0

type OrderType int

OrderType represents the different ways to order iteration through the map

const (
	// OrderAdded iterates in the order items were added (default behavior)
	OrderAdded OrderType = iota
	// OrderAddedReverse iterates in reverse order of when items were added
	OrderAddedReverse
	// OrderKeyAsc iterates with keys in alphabetical ascending order
	OrderKeyAsc
	// OrderKeyDesc iterates with keys in alphabetical descending order
	OrderKeyDesc
)

Jump to

Keyboard shortcuts

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