types

package
v0.0.0-...-c0f44e6 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2024 License: MIT, MIT Imports: 6 Imported by: 7

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

func JsonToMap

func JsonToMap(j Json) map[string]any

Types

type El

type El[K cmp.Ordered, V any] struct {
	Key   K
	Value V
}

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.

type Json

type Json = *OrderedMap[string, any]

func JsonFromMap

func JsonFromMap(mp map[string]any) Json

func NewJson

func NewJson() Json

type OrderedMap

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

func NewOrderedMap

func NewOrderedMap[K cmp.Ordered, V any]() *OrderedMap[K, V]

func (*OrderedMap[K, V]) Back

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

Back will return the element that is the last (most recent Set element). If there are no elements this will return nil.

func (*OrderedMap[K, V]) Copy

func (m *OrderedMap[K, V]) Copy() *OrderedMap[K, V]

Copy returns a new OrderedMap with the same elements. Using Copy while there are concurrent writes may mangle the result.

func (*OrderedMap[K, V]) Delete

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

Delete will remove a key from the map. It will return true if the key was removed (the key did exist).

func (*OrderedMap[K, V]) ForEach

func (m *OrderedMap[K, V]) ForEach(f func(k K, v V))

func (*OrderedMap[K, V]) ForEachIndexed

func (m *OrderedMap[K, V]) ForEachIndexed(f func(i int, k K, v V))

func (*OrderedMap[K, V]) ForEachIndexedE

func (m *OrderedMap[K, V]) ForEachIndexedE(f func(i int, k K, v V) error) error

func (*OrderedMap[K, V]) Front

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

Front will return the element that is the first (oldest Set element). If there are no elements this will return nil.

func (*OrderedMap[K, V]) Get

func (m *OrderedMap[K, V]) Get(key K) (value V, ok 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]) GetN

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

GetN returns the value for a key. If the key does not exist, it returns the zero value for type V, such as 0 for int or an empty string for string.

func (*OrderedMap[K, V]) GetOrCreate

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

func (*OrderedMap[K, V]) GetOrCreateFunc

func (m *OrderedMap[K, V]) GetOrCreateFunc(key K, f func() V) V

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]) GetPathS

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

GetPathS returns the value at the specified path in the OrderedMap if that value is of string type The path is a dot-separated string representing a nested structure of keys. If the path does not exist or any intermediate key is not of type *OrderedMap[K, V], an empty string is returned.

func (*OrderedMap[K, V]) GetS

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

GetS returns the value for a key if it exists and has string type. Otherwise, it returns an empty string.

func (*OrderedMap[K, V]) Keys

func (m *OrderedMap[K, V]) Keys() (keys []K)

Keys returns all of the keys in the order they were inserted. If a key was replaced it will retain the same position. To ensure most recently set keys are always at the end you must always Delete before Set.

func (*OrderedMap[K, V]) Len

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

Len returns the number of elements in the map.

func (*OrderedMap[K, V]) Rename

func (m *OrderedMap[K, V]) Rename(key K, newkey K)

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]) SetAll

func (m *OrderedMap[K, V]) SetAll(s *OrderedMap[K, V])

func (*OrderedMap[K, V]) SetAllMap

func (m *OrderedMap[K, V]) SetAllMap(s map[K]V)

func (*OrderedMap[K, V]) SetIfAbsent

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

SetIfAbsent sets a value for a key only if the key does not already exist in the map. It returns true if the value was set successfully, or false if the key already exists.

func (*OrderedMap[K, V]) SetIfAbsentFunc

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

SetIfAbsentFunc sets a value for a key only if the key does not already exist in the map. It returns true if the value was set successfully, or false if the key already exists.

func (*OrderedMap[K, V]) String

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

func (*OrderedMap[K, V]) ToArray

func (m *OrderedMap[K, V]) ToArray() []El[K, V]

func (*OrderedMap[K, V]) ToMap

func (m *OrderedMap[K, V]) ToMap() map[K]V

type Set

type Set[K cmp.Ordered] map[K]struct{}

func NewSet

func NewSet[K cmp.Ordered](values ...K) Set[K]

func (Set[K]) Clear

func (s Set[K]) Clear()

func (Set[K]) Clone

func (s Set[K]) Clone() Set[K]

func (Set[K]) Contains

func (s Set[K]) Contains(key K) bool

func (Set[K]) Equals

func (s Set[K]) Equals(other Set[K]) bool

func (Set[K]) Hash

func (s Set[K]) Hash() string

func (Set[K]) Put

func (s Set[K]) Put(key K)

func (Set[K]) PutAll

func (s Set[K]) PutAll(keys []K)

func (Set[K]) PutAllKeys

func (s Set[K]) PutAllKeys(m map[K]any)

func (Set[K]) PutAllOrderedKeys

func (s Set[K]) PutAllOrderedKeys(m *OrderedMap[K, any])

func (Set[K]) PutSet

func (s Set[K]) PutSet(keys Set[K])

func (Set[K]) Remove

func (s Set[K]) Remove(key K)

func (Set[K]) Size

func (s Set[K]) Size() int

func (Set[K]) ToSlice

func (s Set[K]) ToSlice() []K

Jump to

Keyboard shortcuts

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