multiset

package
v0.0.0-...-810742f Latest Latest
Warning

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

Go to latest
Published: May 13, 2023 License: BSD-2-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package multiset uses the primitives in mapset to implement a simple multiset.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrOverflow = errors.New("overflow")

ErrOverflow is used whenever addition of two uint64 would overflow. This is an unlikely thing that few will ever need to consider so it is always reported via

panic(ErrOverflow)

Functions

This section is empty.

Types

type Of

type Of[K comparable] map[K]uint64

multiset.Of[K] is a set of K where keys can be stored more than once. The number of times a key is stored, m[k], is called the multiplicity of the key.

A multiplicity of 0 is the same as not being in the set.

func (Of[K]) Add

func (m Of[K]) Add(o Of[K]) Of[K]

Add chooses the sum of multiplicities of both multisets. It panics if any sum overflows.

r[k] = m[k] + o[k]
Example
package main

import (
	"fmt"

	"github.com/jimmyfrasche/mapset/multiset"
)

func main() {
	x := multiset.Of[string]{"a": 1, "b": 0, "c": 5}
	y := multiset.Of[string]{"c": 3, "d": 4}

	fmt.Println(x.Add(y))

}
Output:

map[a:1 c:8 d:4]

func (Of[K]) Cardinality

func (m Of[K]) Cardinality() uint64

Cardinality is the sum of the multiplicities of all items. It panics if the sum overflows.

Example
package main

import (
	"fmt"

	"github.com/jimmyfrasche/mapset/multiset"
)

func main() {
	x := multiset.Of[string]{"a": 1, "b": 0, "c": 5}
	fmt.Println(x.Cardinality())

}
Output:

6

func (Of[K]) Clone

func (m Of[K]) Clone() Of[K]

func (Of[K]) Contains

func (m Of[K]) Contains(k K) bool

Contains k if m[k] > 0.

func (Of[K]) Dec

func (m Of[K]) Dec(k K, x uint64) uint64

Dec properly subtracts x from m[k].

func (Of[K]) Equal

func (m Of[K]) Equal(o Of[K]) bool

true if, for all k:

m[k] == o[k]
Example
package main

import (
	"fmt"

	"github.com/jimmyfrasche/mapset/multiset"
)

func main() {
	x := multiset.Of[string]{"a": 1, "b": 2, "c": 3}
	y := multiset.Of[string]{"a": 1, "b": 3, "c": 4}
	z := multiset.Of[string]{"a": 1, "b": 2, "c": 3, "d": 1}

	fmt.Println("x = y?", x.Equal(y))
	fmt.Println("y = x?", y.Equal(x))
	fmt.Println("x = z?", x.Equal(z))
	fmt.Println("x = x?", x.Equal(x))

}
Output:

x = y? false
y = x? false
x = z? false
x = x? true

func (Of[K]) Inc

func (m Of[K]) Inc(k K, x uint64) uint64

Inc adds x to m[k]. This panics if the addition overflows.

func (Of[K]) IncDec

func (m Of[K]) IncDec(k K, n int) uint64

If n >= 0, call Inc(k, uint(n)); otherwise Dec(k, uint64(-n)).

func (Of[K]) Included

func (m Of[K]) Included(o Of[K]) bool

m Included in o if the multiplicity of each item in m is <= the corresponding multiplicity in o.

true if, for all k:

m[k] <= o[k]
Example
package main

import (
	"fmt"

	"github.com/jimmyfrasche/mapset/multiset"
)

func main() {
	x := multiset.Of[string]{"a": 1, "b": 2, "c": 3}
	y := multiset.Of[string]{"a": 1, "b": 3, "c": 4}
	z := multiset.Of[string]{"d": 0, "e": 9, "f": 2}

	fmt.Println("x ≤ y?", x.Included(y))
	fmt.Println("x ≥ y?", y.Included(x))
	fmt.Println("x ≤ z?", x.Included(z))

}
Output:

x ≤ y? true
x ≥ y? false
x ≤ z? false

func (Of[K]) Intersect

func (m Of[K]) Intersect(o Of[K]) Of[K]

Intersect chooses the minimum of multiplicities of both multisets.

r[k] = min(m[k], o[k])
Example
package main

import (
	"fmt"

	"github.com/jimmyfrasche/mapset/multiset"
)

func main() {
	x := multiset.Of[string]{"a": 1, "b": 0, "c": 5, "e": 1}
	y := multiset.Of[string]{"c": 3, "d": 4, "e": 9}

	fmt.Println(x.Intersect(y))

}
Output:

map[a:1 c:3 d:4 e:1]

func (Of[K]) Keys

func (m Of[K]) Keys() []K

Keys returns the support of m as a slice.

func (Of[K]) ProperIncluded

func (m Of[K]) ProperIncluded(o Of[K]) bool

m is ProperIncluded in o if the multiplicity of each item in m is < the corresponding multiplicity in o.

true if, for all k:

m[k] < o[k]
Example
package main

import (
	"fmt"

	"github.com/jimmyfrasche/mapset/multiset"
)

func main() {
	x := multiset.Of[string]{"a": 1, "b": 2, "c": 3}
	y := multiset.Of[string]{"a": 5, "b": 3, "c": 4}
	z := multiset.Of[string]{"d": 0, "e": 9, "f": 2}

	fmt.Println("x < y?", x.ProperIncluded(y))
	fmt.Println("x > y?", y.ProperIncluded(x))
	fmt.Println("x < z?", x.ProperIncluded(z))

}
Output:

x < y? true
x > y? false
x < z? false

func (Of[K]) Purge

func (m Of[K]) Purge()

Purge removes all keys of multiplicity 0.

func (Of[K]) Sub

func (m Of[K]) Sub(o Of[K]) Of[K]

Sub chooses the proper subtraction of both multisets.

r[k] = max(m[k] - o[k], 0)
Example
package main

import (
	"fmt"

	"github.com/jimmyfrasche/mapset/multiset"
)

func main() {
	x := multiset.Of[string]{"a": 1, "b": 0, "c": 5}
	y := multiset.Of[string]{"c": 3, "d": 4}

	fmt.Println(x.Sub(y))
	fmt.Println(y.Sub(x))
}
Output:

map[a:1 c:2 d:4]
map[a:1 d:4]

func (Of[K]) Union

func (m Of[K]) Union(o Of[K]) Of[K]

Union chooses the maximum of multiplicities of both multisets.

r[k] = max(m[k], o[k])
Example
package main

import (
	"fmt"

	"github.com/jimmyfrasche/mapset/multiset"
)

func main() {
	x := multiset.Of[string]{"a": 1, "b": 0, "c": 5, "e": 1}
	y := multiset.Of[string]{"c": 3, "d": 4, "e": 9}

	fmt.Println(x.Union(y))

}
Output:

map[a:1 c:5 d:4 e:9]

Jump to

Keyboard shortcuts

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