Documentation
¶
Index ¶
- type HashMap
- func (hm *HashMap[K, V]) Clear()
- func (hm *HashMap[K, V]) Contains(k K) bool
- func (hm *HashMap[K, V]) ContainsAll(ks ...K) bool
- func (hm *HashMap[K, V]) Copy(am cog.Map[K, V])
- func (hm *HashMap[K, V]) Each(f func(K, V) bool)
- func (hm *HashMap[K, V]) Entries() []cog.P[K, V]
- func (hm *HashMap[K, V]) Get(key K) (v V, ok bool)
- func (hm *HashMap[K, V]) HashMap() map[K]V
- func (hm *HashMap[K, V]) IsEmpty() bool
- func (hm *HashMap[K, V]) Keys() []K
- func (hm *HashMap[K, V]) Len() int
- func (hm *HashMap[K, V]) MarshalJSON() ([]byte, error)
- func (hm *HashMap[K, V]) MustGet(key K) V
- func (hm *HashMap[K, V]) Remove(k K) (ov V, ok bool)
- func (hm *HashMap[K, V]) RemoveAll(ks ...K)
- func (hm *HashMap[K, V]) SafeGet(key K, defaults ...V) V
- func (hm *HashMap[K, V]) Seq() iter.Seq2[K, V]
- func (hm *HashMap[K, V]) Set(key K, value V) (ov V, ok bool)
- func (hm *HashMap[K, V]) SetEntries(pairs ...cog.P[K, V])
- func (hm *HashMap[K, V]) SetIfAbsent(key K, value V) (ov V, ok bool)
- func (hm *HashMap[K, V]) String() string
- func (hm *HashMap[K, V]) UnmarshalJSON(data []byte) error
- func (hm *HashMap[K, V]) Values() []V
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HashMap ¶
type HashMap[K comparable, V any] struct { // contains filtered or unexported fields }
HashMap hash map type
Example ¶
m := NewHashMap[int, string]() m.Set(1, "x") // 1->x m.Set(2, "b") // 2->b, 1->x (random order) m.Set(1, "a") // 2->b, 1->a (random order) _, _ = m.Get(2) // b, true _, _ = m.Get(3) // nil, false _ = m.Values() // []interface {}{"b", "a"} (random order) _ = m.Keys() // []interface {}{1, 2} (random order) m.Remove(1) // 2->b m.Clear() // empty m.IsEmpty() // true m.Len() // 0
func AsHashMap ¶
func AsHashMap[K comparable, V any](m map[K]V) *HashMap[K, V]
AsHashMap creates a new HashMap from a map. Example: AsHashMap(map[K]V{"k1": "v1", "k2": "v2"})
func NewHashMap ¶
func NewHashMap[K comparable, V any](kvs ...cog.P[K, V]) *HashMap[K, V]
NewHashMap creates a new HashMap.
func (*HashMap[K, V]) ContainsAll ¶ added in v1.0.27
ContainsAll Test to see if the map contains all keys of ks
func (*HashMap[K, V]) Get ¶
Get looks for the given key, and returns the value associated with it, or nil if not found. The boolean it returns says whether the key is ok in the map.
func (*HashMap[K, V]) HashMap ¶
func (hm *HashMap[K, V]) HashMap() map[K]V
HashMap returns underlying hash map
func (*HashMap[K, V]) MarshalJSON ¶
MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(lm)
func (*HashMap[K, V]) MustGet ¶
func (hm *HashMap[K, V]) MustGet(key K) V
MustGet looks for the given key, and returns the value associated with it. Panic if not found.
func (*HashMap[K, V]) Remove ¶
Remove remove the item with key k, and returns what `Get` would have returned on that key prior to the call to `Set`.
func (*HashMap[K, V]) RemoveAll ¶ added in v1.0.27
func (hm *HashMap[K, V]) RemoveAll(ks ...K)
RemoveAll remove all items with key of ks.
func (*HashMap[K, V]) SafeGet ¶ added in v1.0.27
func (hm *HashMap[K, V]) SafeGet(key K, defaults ...V) V
SafeGet looks for the given key, and returns the value associated with it. If not found, return defaults[0] or zero V.
func (*HashMap[K, V]) Set ¶
Set sets the paired key-value items, and returns what `Get` would have returned on that key prior to the call to `Set`.
func (*HashMap[K, V]) SetEntries ¶
SetEntries set items from key-value items array, override the existing items
func (*HashMap[K, V]) SetIfAbsent ¶
SetIfAbsent sets the key-value item if the key does not exists in the map, and returns what `Get` would have returned on that key prior to the call to `Set`.
func (*HashMap[K, V]) UnmarshalJSON ¶
UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, lm)