orderedmap

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2023 License: MIT Imports: 7 Imported by: 0

README

🔃 github.com/elliotchance/orderedmap/v2 GoDoc

Basic Usage

An *OrderedMap is a high performance ordered map that maintains amortized O(1) for Set, Get, Delete and Len:

import "github.com/elliotchance/orderedmap/v2"

func main() {
	m := orderedmap.NewOrderedMap[string, any]()

	m.Set("foo", "bar")
	m.Set("qux", 1.23)
	m.Set("123", true)

	m.Delete("qux")
}

Note: v2 requires Go v1.18 for generics. If you need to support Go 1.17 or below, you can use v1.

Internally an *OrderedMap uses the composite type map combined with a trimmed down linked list to maintain the order.

Iterating

Be careful using Keys() as it will create a copy of all of the keys so it's only suitable for a small number of items:

for _, key := range m.Keys() {
	value, _:= m.Get(key)
	fmt.Println(key, value)
}

For larger maps you should use Front() or Back() to iterate per element:

// Iterate through all elements from oldest to newest:
for el := m.Front(); el != nil; el = el.Next() {
    fmt.Println(el.Key, el.Value)
}

// You can also use Back and Prev to iterate in reverse:
for el := m.Back(); el != nil; el = el.Prev() {
    fmt.Println(el.Key, el.Value)
}

The iterator is safe to use bidirectionally, and will return nil once it goes beyond the first or last item.

If the map is changing while the iteration is in-flight it may produce unexpected behavior.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element[K comparable, V any] struct {

	// The key that corresponds to this element in the ordered map.
	Key K

	// The value stored with this element.
	Value V
	// contains filtered or unexported fields
}

Element is an element of a null terminated (non circular) intrusive doubly linked list that contains the key of the correspondent element in the ordered map too.

func (*Element[K, V]) Next

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

Next returns the next list element or nil.

func (*Element[K, V]) Prev

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

Prev returns the previous list element or nil.

func (*Element[K, V]) String

func (e *Element[K, V]) String() string

MapItem as a string, for debugging.

func (*Element[K, V]) UnmarshalJSON

func (e *Element[K, V]) UnmarshalJSON(ctx context.Context, data []byte) error

UnmarshalJSON for orderedmap element.

type OrderedMap

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

MapSlice is a type structure that acts as a map and as a slice (keeping the order of the elements).

func New

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

func (*OrderedMap[K, V]) Get

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

Get returns the value for a key. If the key does not exist, the second return parameter will be false and the value will be nil.

func (*OrderedMap[K, V]) GetElement

func (m *OrderedMap[K, V]) GetElement(key K) *Element[K, V]

GetElement returns the element for a key. If the key does not exist, the pointer will be nil.

func (*OrderedMap[K, V]) GetOrDefault

func (m *OrderedMap[K, V]) GetOrDefault(key K, defaultValue V) V

GetOrDefault returns the value for a key. If the key does not exist, returns the default value instead.

func (*OrderedMap[K, V]) Len

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

func (OrderedMap[K, V]) MarshalJSON

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

MarshalJSON for map slice.

func (*OrderedMap[K, V]) Set

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

Set will set (or replace) a value for a key. If the key was new, then true will be returned. The returned value will be false if the value was replaced (even if the value was the same).

func (*OrderedMap[K, V]) String

func (m *OrderedMap[K, V]) String() string

MapItem as a string, for debugging.

func (*OrderedMap[K, V]) UnmarshalJSON

func (m *OrderedMap[K, V]) UnmarshalJSON(b []byte) error

UnmarshalJSON for map slice.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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