avmap

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: MIT Imports: 1 Imported by: 0

README

What is Autovivification?

Autovivification can be found in many interpreted languages, and some compiled ones.

It's the creation of the intermediary layers in nested structures.

The article in Wikipedia has some good examples, here's one for Perl:

%h = ();          # %h is an empty hash
$h{A}{B}{C}{D}=1  # creates %h = (A => {B => {C => {D => 1}}})

What is this package?

avmap adds autovivification to the maps only. It arose from a task where several nested maps were needed to keep intermediary results of a calculation.

Given the declaration

type calcs struct {
    someAttrs  map[string]int
    otherAttrs map[string]int
    ...
}
perStateMap := map[string]*calcs{}

It allows you to switch from this

for ... {
    state := ...
    someAttr := ...
    otherAttr := ...
    if _, ok := perStateMap[state]; !ok {
        perStateMap[state] = &calcs{
            someAttrs:  map[string]int{},
            otherAttrs: map[string]int{},
            ...
        }
    }
    if _, ok := perStateMap[state].someAttrs[someAttr]; !ok {
        perStateMap[state].someAttrs[someAttr] = 0
    }
    perStateMap[state].someAttrs[someAttr]++
    if _, ok := perStateMap[state].otherAttrs[otherAttr]; !ok {
        perStateMap[state].otherAttrs[otherAttr] = 0
    }
    perStateMap[state].otherAttrs[otherAttr]++
}

to this

for name := range ... {
    state := ...
    someAttr := ...
    otherAttr := ...
    avmap.SetIfMissing(perStateMap, state, &calcs{
            someAttrs:  map[string]int{},
            otherAttrs: map[string]int{},
            ...
        }
    )
    avmap.Inc(perStateMap[state].someAttrs, someAttr)
    avmap.Inc(perStateMap[state].otherAttrs, otherAttr)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add[K comparable, V Addable](m map[K]V, key K, value V)

Add the value to the value at the key, create the key if not exists

func Append

func Append[K comparable, V Values](m map[K][]V, key K, value V)

Append to the []V key, create the key if not exists

func GetOrCreateRef added in v0.0.2

func GetOrCreateRef[K comparable, VP *V, V any](m map[K]VP, key K) VP

GetOrCreateRef can be used to set a default value of a reference type, and return it for further use

func Inc

func Inc[K comparable, V Ints](m map[K]V, key K)

Inc the value at the key, create the key if not exists. Integer values only

func SetIfMissing

func SetIfMissing[K comparable, V any](m map[K]V, key K, value V)

Types

type Addable added in v0.0.2

type Addable interface {
	constraints.Integer | constraints.Float | constraints.Complex | ~string
}

type Ints

type Ints interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Values

type Values interface {
	constraints.Integer | constraints.Float | constraints.Complex | ~string | ~bool
}

Jump to

Keyboard shortcuts

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