xmaps

package
v0.15.3 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Package xmaps contains utilities for working with maps.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Difference

func Difference[S ~map[T]struct{}, T comparable](a, b S) S

Difference returns all items of a that do not appear in b.

Example
package main

import (
	"fmt"

	"github.com/bradenaw/juniper/xmaps"
)

func main() {
	a := xmaps.Set[int]{
		1: {},
		4: {},
		5: {},
	}
	b := xmaps.Set[int]{
		3: {},
		4: {},
	}

	difference := xmaps.Difference(a, b)

	fmt.Println(difference)

}
Output:

map[1:{} 5:{}]

func FromKeysAndValues

func FromKeysAndValues[K comparable, V any](keys []K, values []V) (map[K]V, bool)

FromKeysAndValues returns a map from keys[i] to values[i]. If there are any duplicate keys, the resulting map has an arbitrary choice of the associated values and the second return is false. It panics if len(keys)!=len(values).

func Intersection

func Intersection[S ~map[T]struct{}, T comparable](sets ...S) S

Intersection returns a set of the items that all input sets have in common.

Example
package main

import (
	"fmt"

	"github.com/bradenaw/juniper/xmaps"
)

func main() {
	a := xmaps.Set[int]{
		1: {},
		2: {},
		4: {},
	}
	b := xmaps.Set[int]{
		1: {},
		3: {},
		4: {},
	}
	c := map[int]struct{}{
		1: {},
		4: {},
		5: {},
	}

	intersection := xmaps.Intersection(a, b, c)

	fmt.Println(intersection)

}
Output:

map[1:{} 4:{}]

func Intersects

func Intersects[S ~map[T]struct{}, T comparable](sets ...S) bool

Intersects returns true if the input sets have any element in common.

Example
package main

import (
	"fmt"

	"github.com/bradenaw/juniper/xmaps"
)

func main() {
	a := xmaps.Set[int]{
		1: {},
		2: {},
	}
	b := xmaps.Set[int]{
		1: {},
		3: {},
	}
	c := xmaps.Set[int]{
		3: {},
		4: {},
	}

	fmt.Println(xmaps.Intersects(a, b))
	fmt.Println(xmaps.Intersects(b, c))
	fmt.Println(xmaps.Intersects(a, c))

}
Output:

true
true
false

func Reverse

func Reverse[M ~map[K]V, K comparable, V comparable](m M) map[V][]K

Reverse returns a map from m's values to each of the keys that mapped to it in arbitrary order.

Example
package main

import (
	"fmt"

	"github.com/bradenaw/juniper/xmaps"
)

func main() {
	a := map[string]int{
		"foo": 2,
		"bar": 1,
		"baz": 2,
	}

	reversed := xmaps.Reverse(a)

	fmt.Println(1, reversed[1][0])
	fmt.Println(2, reversed[2][0])
	fmt.Println(2, reversed[2][1])

}
Output:

1 bar
2 foo
2 baz

func ReverseSingle

func ReverseSingle[M ~map[K]V, K comparable, V comparable](m M) (map[V]K, bool)

ReverseSingle returns a map of m's values to m's keys. If there are any duplicate values, the resulting map has an arbitrary choice of the associated keys and the second return is false.

Example
package main

import (
	"fmt"

	"github.com/bradenaw/juniper/xmaps"
)

func main() {
	a := map[string]int{
		"foo": 1,
		"bar": 2,
		"baz": 3,
	}

	reversed, ok := xmaps.ReverseSingle(a)
	fmt.Println(ok)
	fmt.Println(reversed)

}
Output:

true
map[1:foo 2:bar 3:baz]

func ToIndex

func ToIndex[K comparable](keys []K) map[K]int

ToIndex returns a map from keys[i] to i.

Example
package main

import (
	"fmt"

	"github.com/bradenaw/juniper/xmaps"
)

func main() {
	m := []string{"foo", "bar", "baz"}

	fmt.Println(xmaps.ToIndex(m))

}
Output:

map[bar:1 baz:2 foo:0]

func Union

func Union[S ~map[T]struct{}, T comparable](sets ...S) S

Union returns a set containing all elements of all input sets.

Example
package main

import (
	"fmt"

	"github.com/bradenaw/juniper/xmaps"
)

func main() {
	a := xmaps.Set[int]{
		1: {},
		4: {},
	}
	b := xmaps.Set[int]{
		3: {},
		4: {},
	}
	c := xmaps.Set[int]{
		1: {},
		5: {},
	}

	union := xmaps.Union(a, b, c)

	fmt.Println(union)

}
Output:

map[1:{} 3:{} 4:{} 5:{}]

Types

type Set

type Set[T comparable] map[T]struct{}

Set[T] is shorthand for map[T]struct{} with convenience methods.

func SetFromSlice

func SetFromSlice[T comparable](items []T) Set[T]

SetFromSlice returns a Set whose elements are items.

func (Set[T]) Add

func (s Set[T]) Add(item T)

Add adds item to the set.

func (Set[T]) Contains

func (s Set[T]) Contains(item T) bool

Contains returns true if item is in the set.

func (Set[T]) Remove

func (s Set[T]) Remove(item T)

Remove removes item from the set.

Jump to

Keyboard shortcuts

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