set

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

set is giving Set structure.

Example (AddRemove)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/set"
)

func main() {
	a := set.Of(1, 3, 5, 7, 9)
	a.Add(2)
	a.Add(4)
	a.Remove(3)
	a.Remove(5)
	a.Foreach(func(i int) {
		fmt.Print(i, " ")
	})
	fmt.Println()
}
Output:

1 2 4 7 9
Example (Contains)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/set"
)

func main() {
	a := set.Of(1, 3, 5, 7, 9)
	fmt.Println(a.Contains(4))
}
Output:

false
Example (Intersect)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/set"
)

func main() {
	a := set.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
	b := set.Of(2, 4, 6, 8, 10)
	c := a.Intersect(b)
	c.Foreach(func(i int) {
		fmt.Print(i, " ")
	})
	fmt.Println()
}
Output:

2 4 6 8 10
Example (Subtract)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/set"
)

func main() {
	a := set.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
	b := set.Of(2, 4, 6, 8, 10)
	c := a.Subtract(b)
	c.Foreach(func(i int) {
		fmt.Print(i, " ")
	})
	fmt.Println()
}
Output:

1 3 5 7 9
Example (Union)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/set"
)

func main() {
	a := set.Of(1, 3, 5, 7, 9)
	b := set.Of(2, 4, 6, 8, 10)
	c := a.Union(b)
	c.Foreach(func(i int) {
		fmt.Print(i, " ")
	})
	fmt.Println()
}
Output:

1 2 3 4 5 6 7 8 9 10

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToSlice

func ToSlice[T comparable](s Set[T]) []T

ToSlice returns a slice with the elements of the Set.

Types

type Set

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

Set is a map[T]struct{} with additional methods.

func FromMapKey

func FromMapKey[K comparable, V any](m map[K]V) Set[K]

FromMapKey returns a new Set with keys from a map.

func FromMapValue

func FromMapValue[K comparable, V comparable](m map[K]V) Set[V]

FromMapValue returns a new Set with values from a map.

func FromSlice

func FromSlice[T comparable](s []T) Set[T]

FromSlice returns a new Set from a slice.

func New

func New[T comparable]() Set[T]

New returns a new Set.

func Of

func Of[T comparable](values ...T) Set[T]

Of returns a new Set with the given elements.

func (Set[T]) Add

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

Add adds the given value to the Set.

func (Set[T]) Clone

func (s Set[T]) Clone() Set[T]

Clone returns a new Set with the same contents as this one.

func (Set[T]) Contains

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

Contains returns true if the given value is in the Set.

func (Set[T]) Equal

func (s1 Set[T]) Equal(s2 Set[T]) bool

Equal returns true if this Set and the given Set are equal.

func (Set[T]) Foreach

func (s1 Set[T]) Foreach(f func(T))

Foreach calls the given function for each element in the Set.

func (Set[T]) Intersect

func (s1 Set[T]) Intersect(s2 Set[T]) Set[T]

Intersect returns a new Set with the intersection of this Set and the given Set.

func (Set[T]) IsDisjoint

func (s1 Set[T]) IsDisjoint(s2 Set[T]) bool

IsDisjoint returns true if this Set and the given Set have no elements in common.

func (Set[T]) IsSubset

func (s1 Set[T]) IsSubset(s2 Set[T]) bool

IsSubset returns true if this Set is a subset of the given Set.

func (Set[T]) IsSuperset

func (s1 Set[T]) IsSuperset(s2 Set[T]) bool

IsSuperset returns true if this Set is a superset of the given Set.

func (Set[T]) Remove

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

Remove removes the given value from the Set.

func (Set[T]) Subtract

func (s1 Set[T]) Subtract(s2 Set[T]) Set[T]

Subtract returns a new Set with the elements of this Set that are not in the given Set.

func (Set[T]) Union

func (s1 Set[T]) Union(s2 Set[T]) Set[T]

Union returns a new Set with the union of this Set and the given Set.

Jump to

Keyboard shortcuts

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