Documentation
¶
Index ¶
- type Map
- func (m *Map[K, V]) Delete(key K) bool
- func (m *Map[K, V]) Get(key K) (V, bool)
- func (m *Map[K, V]) GetAndDelete(key K) (V, bool)
- func (m *Map[K, V]) GetOrPut(key K, value V) (V, bool)
- func (m *Map[k, V]) Length() int
- func (m *Map[K, V]) OrderedRange(f func(key K, value V))
- func (m *Map[K, V]) Put(key K, val V)
- func (m *Map[K, V]) UnorderedRange(f func(key K, value V))
Examples ¶
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 a thread safe and ordered implementation of standard map. K is the type of key and V is the type of value.
func New ¶
func New[K comparable, V any]() *Map[K, V]
New returns an initialized Map[K, V].
Example ¶
package main import ( "fmt" ordered_sync_map "github.com/m-murad/ordered-sync-map" ) func main() { mp := ordered_sync_map.New[string, string]() mp.Put("k1", "v1") v, ok := mp.Get("k1") fmt.Println(v, ok) ok = mp.Delete("k2") fmt.Println(ok) mp.UnorderedRange(func(key, value string) { fmt.Println(key, value) }) mp.OrderedRange(func(key, value string) { fmt.Println(key, value) }) len := mp.Length() fmt.Println(len) v, ok = mp.GetOrPut("k1", "v2") fmt.Println(v, ok) v, ok = mp.GetAndDelete("k1") fmt.Println(v, ok) }
func (*Map[K, V]) Delete ¶
Delete deletes the value for a key. It returns a boolean indicating weather the key existed and it was deleted.
func (*Map[K, V]) Get ¶
Get returns the value stored in the map for a key. If the key is not found in the Map it return the zero value of type V. The bool indicates whether value was found in the map.
func (*Map[K, V]) GetAndDelete ¶
GetAndDelete will get the value saved against the given key. deleted will be true if the key existed previously otherwise it will be false.
func (*Map[K, V]) GetOrPut ¶
GetOrPut will return the existing value if the key exists in the Map. If the key did not exist previously it will be added to the Map. updated will be true if the key existed previously otherwise it will be false if the key did not exist and was added to the Map.
func (*Map[K, V]) OrderedRange ¶
func (m *Map[K, V]) OrderedRange(f func(key K, value V))
OrderedRange will range over the map in ab ordered sequence. Parameter func f should not call any method of the Map, eg Get, Put, Delete, UnorderedRange, OrderedRange etc It will cause a deadlock.
func (*Map[K, V]) Put ¶
func (m *Map[K, V]) Put(key K, val V)
Put sets the value for the given key. It will replace the value if the key already exists in the map even if the values are same.
func (*Map[K, V]) UnorderedRange ¶
func (m *Map[K, V]) UnorderedRange(f func(key K, value V))
UnorderedRange will range over the map in an unordered sequence. This is same as ranging over a map using the "for range" syntax. Parameter func f should not call any method of the Map, eg Get, Put, Delete, UnorderedRange, OrderedRange etc It will cause a deadlock.