Documentation
¶
Overview ¶
Package orderedmap provides Map type which is a map presering the order of key insertions.
Usage ¶
To create an ordered map is as follows:
om := orderedmap.New[string, string]()
To add a map entry is as follows:
om.Store("foo", "hoge") prev, swapped := om.Swap("bar", "fuga") actual, loaded := om.LoadOrStore("baz", "fuga")
To get a value for a key is as follows:
om.Load("foo")
To delete a map entry is as follows:
om.Delete("bar") v,, deleted := om.LoadAndDelete("baz")
To delete a map entry logically is as follows:
om.Ldelete("bar") v,, deleted := om.LoadAndLdelete("baz")
To iterate map entries is as folLows. The order is same with key insertions:
om.Range(func(k, v) bool { ... }) for ent := om.Front(); ent != nil; ent = ent.Next() { k := ent.Key(); v : = ent.Value(); ... } for ent := om.Back(); ent != nil; ent = ent.Prev() { k := ent.Key(); v : = ent.Value(); ... }
Index ¶
- type Entry
- type Map
- func (om *Map[K, V]) Back() *Entry[K, V]
- func (om *Map[K, V]) Delete(key K)
- func (om *Map[K, V]) Front() *Entry[K, V]
- func (om *Map[K, V]) Ldelete(key K)
- func (om *Map[K, V]) Len() int
- func (om *Map[K, V]) Load(key K) (value V, ok bool)
- func (om *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)
- func (om *Map[K, V]) LoadAndLdelete(key K) (value V, loaded bool)
- func (om *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (om *Map[K, V]) LoadOrStoreFunc(key K, fn func() (V, error)) (actual V, loaded bool, err error)
- func (om *Map[K, V]) Range(fn func(key K, value V) bool)
- func (om *Map[K, V]) Store(key K, value V)
- func (om *Map[K, V]) Swap(key K, value V) (previous V, loaded bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry[K comparable, V any] struct { // contains filtered or unexported fields }
Entry is a struct which is a map element and holds a pair of key and value. This struct also has methods: Next and Prev which moves next or previous entties sequencially.
func (*Entry[K, V]) Key ¶
func (ent *Entry[K, V]) Key() K
Key is a method which returns the key of this entry.
func (*Entry[K, V]) Next ¶
Next is a method which returns the next entry of this entry. If this entry is a last entry of an ordered map, the returned value is nil.
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a struct which represents a map similar with Go standard map, or sync.Map, but preserves the order in which keys were inserted.
This map has same methods with sync.Map except CompareAndDelete and CompareAndSwap. (But not support concurrent use.) Its Range method processes a key and a value of each map entry, and the processing order is same with the order of key insertions. And this map also has methods: Front and Back, which iterate this map entries in the order of key insertions and in that reverse order.
func New ¶
func New[K comparable, V any]() Map[K, V]
New is a function which creates a new ordered map, which is ampty.
func (*Map[K, V]) Delete ¶
func (om *Map[K, V]) Delete(key K)
Delete is a method which deletes a value for a key.
func (*Map[K, V]) Ldelete ¶
func (om *Map[K, V]) Ldelete(key K)
Ldelete is a method which logically deletes a value for a key.
func (*Map[K, V]) Load ¶
Load is a method which returns a value stored in this map for a key. If no value was found for a key, the ok result is false.
func (*Map[K, V]) LoadAndDelete ¶
LoadAndDelete is a method which deletes a value for a key, and returns the previous value if any. The loaded flag is true if the key was present.
func (*Map[K, V]) LoadAndLdelete ¶
LoadAndLdelete is a method which logically deletes a value for a key, and returns the previous value if any. The loaded flag is true if the key was present.
func (*Map[K, V]) LoadOrStore ¶
LoadOrStore is a method which returns a value for a key if presents, otherwise stores and returns a given value. The loaded flag is true if the value was loaded, false if stored.
func (*Map[K, V]) LoadOrStoreFunc ¶ added in v0.2.0
func (om *Map[K, V]) LoadOrStoreFunc( key K, fn func() (V, error), ) (actual V, loaded bool, err error)
LoadOrStoreFunc is a method which returns a value for a key if presents, otherwise executes a give function, then stores and returns the result value. The loaded flag is true if the value was loaded, false if stored.
func (*Map[K, V]) Range ¶
Range is a method which calls the specified function: fn sequentially for each key and value in this map. If fn returns false, this method stops the iteration.