hashmap

package
v1.0.25 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2024 License: MIT Imports: 6 Imported by: 2

Documentation

Index

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
Output:

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

func (hm *HashMap[K, V]) Clear()

Clear clears the map

func (*HashMap[K, V]) Contain

func (hm *HashMap[K, V]) Contain(k K) bool

Contain Test to see if the list contains the key k

func (*HashMap[K, V]) Contains

func (hm *HashMap[K, V]) Contains(ks ...K) bool

Contains looks for the given key, and returns true if the key exists in the map.

func (*HashMap[K, V]) Copy

func (hm *HashMap[K, V]) Copy(am cog.Map[K, V])

Copy copy items from another map am, override the existing items

func (*HashMap[K, V]) Each

func (hm *HashMap[K, V]) Each(f func(K, V) bool)

Each call f for each item(k,v) in the map

func (*HashMap[K, V]) Entries

func (hm *HashMap[K, V]) Entries() []cog.P[K, V]

Entries returns the key-value pair slice

func (*HashMap[K, V]) Get

func (hm *HashMap[K, V]) Get(key K) (v V, ok bool)

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

func (hm *HashMap[K, V]) IsEmpty() bool

IsEmpty returns true if the map has no items

func (*HashMap[K, V]) Keys

func (hm *HashMap[K, V]) Keys() []K

Keys returns the key slice

func (*HashMap[K, V]) Len

func (hm *HashMap[K, V]) Len() int

Len returns the length of the linked map.

func (*HashMap[K, V]) MarshalJSON

func (hm *HashMap[K, V]) MarshalJSON() ([]byte, error)

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, defaults ...V) V

MustGet looks for the given key, and returns the value associated with it. If not found, return defaults[0] or panic if defaults is not supplied.

func (*HashMap[K, V]) Remove

func (hm *HashMap[K, V]) Remove(k K) (ov V, ok bool)

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

func (hm *HashMap[K, V]) Removes(ks ...K)

Removes remove all items with key of ks.

func (*HashMap[K, V]) Set

func (hm *HashMap[K, V]) Set(key K, value V) (ov V, ok bool)

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

func (hm *HashMap[K, V]) SetEntries(pairs ...cog.P[K, V])

SetEntries set items from key-value items array, override the existing items

func (*HashMap[K, V]) SetIfAbsent

func (hm *HashMap[K, V]) SetIfAbsent(key K, value V) (ov V, ok bool)

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

func (hm *HashMap[K, V]) String() string

String print map to string

func (*HashMap[K, V]) UnmarshalJSON

func (hm *HashMap[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, lm)

func (*HashMap[K, V]) Values

func (hm *HashMap[K, V]) Values() []V

Values returns the value slice

Jump to

Keyboard shortcuts

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