orderedmap

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package orderedmap implements an ordered map, that is; a map that remembers the order in which key, value pairs were inserted.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

Map is an ordered map.

func New

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

New creates and returns a new ordered map.

func WithCapacity added in v0.15.0

func WithCapacity[K comparable, V any](capacity int) *Map[K, V]

WithCapacity creates and returns a new ordered Map with the given capacity.

This can be a useful performance improvement when the expected maximum size of the map is known ahead of time as it eliminates the need for reallocation.

func (*Map[K, V]) All added in v0.18.0

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

All returns an iterator over the entries in the map in the order in which they were inserted.

func (*Map[K, V]) Contains added in v0.8.0

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

Contains reports whether the map contains the given key.

func (*Map[K, V]) Get

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

Get returns the value stored against the given key in the map and a boolean to indicate presence, like the standard Go map.

If the requested key wasn't in the map, the zero value for the item and false are returned. If the key was present, the item and true are returned.

func (*Map[K, V]) GetOrInsert

func (m *Map[K, V]) GetOrInsert(key K, value V) (val V, existed bool)

GetOrInsert fetches a value by it's key if it is present in the map, and if not inserts the passed in value against that key instead.

The returned boolean reports whether the key already existed.

func (*Map[K, V]) Insert

func (m *Map[K, V]) Insert(key K, value V) (val V, existed bool)

Insert inserts a new value into the map against the given key, returning the previous value and a boolean to indicate presence.

If the map did not have this key present before the call to Insert, it will return the value just inserted and false.

If the map did have this key, and this call to Insert is therefore an update of an existing value, then the old value and true are returned.

func (*Map[K, V]) Keys

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

Keys returns an iterator over the keys in the map in the order in which they were inserted.

func (*Map[K, V]) Newest

func (m *Map[K, V]) Newest() (key K, value V, ok bool)

Newest returns the newest key, value pair in the map, i.e. the pair that was inserted last. Note that in place modifications do not update the order.

func (*Map[K, V]) Oldest

func (m *Map[K, V]) Oldest() (key K, value V, ok bool)

Oldest returns the oldest key, value pair in the map, i.e. the pair that was inserted first. Note that in place modifications do not update the order.

func (*Map[K, V]) Remove

func (m *Map[K, V]) Remove(key K) (value V, existed bool)

Remove removes a key from the map, returning the stored value and a boolean to indicate whether it was in the map to begin with.

If the value was in the map, the removed value and true are returned, if not the zero value for the value type and false are returned.

func (*Map[K, V]) Size

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

Size returns the number of items currently stored in the map. This operation is O(1).

func (*Map[K, V]) Values

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

Values returns an iterator over the values in the map in the order in which they were inserted.

Jump to

Keyboard shortcuts

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