linkedhashmap

package
v1.0.22 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2024 License: MIT Imports: 6 Imported by: 6

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LinkedHashMap

type LinkedHashMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LinkedHashMap implements an linked map that keeps track of the order in which keys were inserted. The zero value for LinkedHashMap is an empty map ready to use.

To iterate over a linked map (where lm is a *LinkedHashMap):

it := lm.Iterator()
for it.Next() {
	// do something with it.Key(), it.Value()
}
Example
m := NewLinkedHashMap[int, string]()
m.Set(2, "b")   // 2->b
m.Set(1, "x")   // 2->b, 1->x (insertion-order)
m.Set(1, "a")   // 2->b, 1->a (insertion-order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values()  // []interface {}{"b", "a"} (insertion-order)
_ = m.Keys()    // []interface {}{2, 1} (insertion-order)
m.Remove(1)     // 2->b
m.Clear()       // empty
m.IsEmpty()     // true
m.Len()         // 0
Output:

func NewLinkedHashMap

func NewLinkedHashMap[K comparable, V any](kvs ...cog.P[K, V]) *LinkedHashMap[K, V]

NewLinkedHashMap creates a new LinkedHashMap.

Example
// initialize from a list of key-value pairs
lm := NewLinkedHashMap([]cog.P[string, any]{
	{Key: "country", Value: "United States"},
	{Key: "countryCode", Value: "US"},
	{Key: "region", Value: "CA"},
	{Key: "regionName", Value: "California"},
	{Key: "city", Value: "Mountain View"},
	{Key: "zip", Value: "94043"},
	{Key: "lat", Value: 37.4192},
	{Key: "lon", Value: -122.0574},
	{Key: "timezone", Value: "America/Los_Angeles"},
	{Key: "isp", Value: "Google Cloud"},
	{Key: "org", Value: "Google Cloud"},
	{Key: "as", Value: "AS15169 Google Inc."},
	{Key: "mobile", Value: true},
	{Key: "proxy", Value: false},
	{Key: "query", Value: "35.192.xx.xxx"},
}...)

for it := lm.Iterator(); it.Next(); {
	fmt.Printf("%-12s: %v\n", it.Key(), it.Value())
}
Output:

country     : United States
countryCode : US
region      : CA
regionName  : California
city        : Mountain View
zip         : 94043
lat         : 37.4192
lon         : -122.0574
timezone    : America/Los_Angeles
isp         : Google Cloud
org         : Google Cloud
as          : AS15169 Google Inc.
mobile      : true
proxy       : false
query       : 35.192.xx.xxx

func (*LinkedHashMap[K, V]) Clear

func (lm *LinkedHashMap[K, V]) Clear()

Clear clears the map

func (*LinkedHashMap[K, V]) Contain

func (lm *LinkedHashMap[K, V]) Contain(k K) bool

Contain Test to see if the list contains the key k

func (*LinkedHashMap[K, V]) Contains

func (lm *LinkedHashMap[K, V]) Contains(ks ...K) bool

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

func (*LinkedHashMap[K, V]) Copy

func (lm *LinkedHashMap[K, V]) Copy(am cog.Map[K, V])

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

func (*LinkedHashMap[K, V]) Each

func (lm *LinkedHashMap[K, V]) Each(f func(K, V) bool)

Each call f for each item in the map

func (*LinkedHashMap[K, V]) Entries

func (lm *LinkedHashMap[K, V]) Entries() []cog.P[K, V]

Entries returns the key-value pair slice

func (*LinkedHashMap[K, V]) Get

func (lm *LinkedHashMap[K, V]) Get(key K) (V, 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 (*LinkedHashMap[K, V]) Head

func (lm *LinkedHashMap[K, V]) Head() *LinkedMapNode[K, V]

Head returns the oldest key/value item.

func (*LinkedHashMap[K, V]) IsEmpty

func (lm *LinkedHashMap[K, V]) IsEmpty() bool

IsEmpty returns true if the map has no items

func (*LinkedHashMap[K, V]) Items

func (lm *LinkedHashMap[K, V]) Items() []*LinkedMapNode[K, V]

Items returns the map item slice

func (*LinkedHashMap[K, V]) Iterator

func (lm *LinkedHashMap[K, V]) Iterator() cog.Iterator2[K, V]

Iterator returns a iterator for the map

func (*LinkedHashMap[K, V]) IteratorOf

func (lm *LinkedHashMap[K, V]) IteratorOf(k K) cog.Iterator2[K, V]

IteratorOf returns a iterator at the specified key Returns nil if the map does not contains the key

func (*LinkedHashMap[K, V]) Keys

func (lm *LinkedHashMap[K, V]) Keys() []K

Keys returns the key slice

func (*LinkedHashMap[K, V]) Len

func (lm *LinkedHashMap[K, V]) Len() int

Len returns the length of the linked map.

func (*LinkedHashMap[K, V]) MarshalJSON

func (lm *LinkedHashMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(lm)

func (*LinkedHashMap[K, V]) MustGet

func (lm *LinkedHashMap[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 (*LinkedHashMap[K, V]) PollHead

func (lm *LinkedHashMap[K, V]) PollHead() *LinkedMapNode[K, V]

PollHead remove the first item of map.

func (*LinkedHashMap[K, V]) PollTail

func (lm *LinkedHashMap[K, V]) PollTail() *LinkedMapNode[K, V]

PollTail remove the last item of map.

func (*LinkedHashMap[K, V]) Remove

func (lm *LinkedHashMap[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 (*LinkedHashMap[K, V]) Removes

func (lm *LinkedHashMap[K, V]) Removes(ks ...K)

Remove remove all items with key of ks.

func (*LinkedHashMap[K, V]) ReverseEach

func (lm *LinkedHashMap[K, V]) ReverseEach(f func(K, V) bool)

ReverseEach call f for each item in the map with reverse order

func (*LinkedHashMap[K, V]) Set

func (lm *LinkedHashMap[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 (*LinkedHashMap[K, V]) SetEntries

func (lm *LinkedHashMap[K, V]) SetEntries(pairs ...cog.P[K, V])

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

func (*LinkedHashMap[K, V]) SetIfAbsent

func (lm *LinkedHashMap[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`. Example: lm.SetIfAbsent("k1", "v1", "k2", "v2")

func (*LinkedHashMap[K, V]) String

func (lm *LinkedHashMap[K, V]) String() string

String print map to string

func (*LinkedHashMap[K, V]) Tail

func (lm *LinkedHashMap[K, V]) Tail() *LinkedMapNode[K, V]

Tail returns the newest key/value item.

func (*LinkedHashMap[K, V]) UnmarshalJSON

func (lm *LinkedHashMap[K, V]) UnmarshalJSON(data []byte) error

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

Example
const jsonStream = `{
  "country"     : "United States",
  "countryCode" : "US",
  "region"      : "CA",
  "regionName"  : "California",
  "city"        : "Mountain View",
  "zip"         : "94043",
  "lat"         : "37.4192",
  "lon"         : "-122.0574",
  "timezone"    : "America/Los_Angeles",
  "isp"         : "Google Cloud",
  "org"         : "Google Cloud",
  "as"          : "AS15169 Google Inc.",
  "mobile"      : "true",
  "proxy"       : "false",
  "query"       : "35.192.xx.xxx"
}`

// compare with if using a regular generic map, the unmarshalled result
//  is a map with unpredictable order of keys
var m map[string]any
err := json.Unmarshal([]byte(jsonStream), &m)
if err != nil {
	fmt.Println("error:", err)
}
for key := range m {
	// fmt.Printf("%-12s: %v\n", key, m[key])
	_ = key
}

// use the LinkedHashMap to Unmarshal from JSON object
lm := NewLinkedHashMap[string, string]()
err = json.Unmarshal([]byte(jsonStream), lm)
if err != nil {
	fmt.Println("error:", err)
}

// loop over all key-value pairs,
// it is ok to call Set append-modify new key-value pairs,
// but not safe to call Delete during iteration.
for it := lm.Iterator(); it.Next(); {
	fmt.Printf("%-12s: %v\n", it.Key(), it.Value())
	if it.Key() == "city" {
		lm.Set("mobile", "false")
		lm.Set("extra", "42")
	}
}
Output:

country     : United States
countryCode : US
region      : CA
regionName  : California
city        : Mountain View
zip         : 94043
lat         : 37.4192
lon         : -122.0574
timezone    : America/Los_Angeles
isp         : Google Cloud
org         : Google Cloud
as          : AS15169 Google Inc.
mobile      : false
proxy       : false
query       : 35.192.xx.xxx
extra       : 42

func (*LinkedHashMap[K, V]) Values

func (lm *LinkedHashMap[K, V]) Values() []V

Values returns the value slice

type LinkedMapNode

type LinkedMapNode[K any, V any] struct {
	// contains filtered or unexported fields
}

LinkedMapNode is a node of a linked hash map.

func (*LinkedMapNode[K, V]) Key

func (ln *LinkedMapNode[K, V]) Key() K

Key returns the key

func (*LinkedMapNode[K, V]) SetValue

func (ln *LinkedMapNode[K, V]) SetValue(v V)

SetValue sets the value

func (*LinkedMapNode[K, V]) String

func (ln *LinkedMapNode[K, V]) String() string

String print the list item to string

func (*LinkedMapNode[K, V]) Value

func (ln *LinkedMapNode[K, V]) Value() V

Value returns the key

Jump to

Keyboard shortcuts

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