set

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

README

go-set

Generic map-compatible Set implementation in Golang.

Introduction

A SetType interface in go-set is defined as any type whose underlying type is a map:

type SetType[K comparable, V any] interface {
	~map[K]V
}

Additionally, go-set defines a Set struct type which is implemented as map[K]struct{}:

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

Which can be used as a set type in Golang.

Examples

You can find the following examples ./examples

Functions

Any map-based type can be used with the functions defined in go-set:

package main

import (
	"fmt"
	"slices"

	set "github.com/paskozdilar/go-set"
)

func main() {
	m := make(map[int]string)

	set.Insert(m, 420)
	fmt.Println(set.Contains(m, 420)) // true
	set.Remove(m, 420)

	m1 := make(map[int]string)
	m2 := make(map[int]string)

	for i := 0; i < 6; i++ {
		set.Insert(m1, i)
	}
	for i := 3; i < 9; i++ {
		set.Insert(m2, i)
	}

	mu := set.Union(m1, m2)
	eu := set.Elements(mu) // slice containing elements of set
	slices.Sort(eu)
	// 0, 1, 2, 3, 4, 5, 6, 7, 8
	for _, e := range eu {
		fmt.Println(e)
	}

	mi := set.Intersection(m1, m2)
	ei := set.Elements(mi)
	slices.Sort(ei)
	// 3, 4, 5
	for _, e := range ei {
		fmt.Println(e)
	}

	msd := set.SymmetricDifference(m1, m2)
	esd := set.Elements(msd)
	slices.Sort(esd)
	// 0, 1, 2, 6, 7, 8
	for _, e := range esd {
		fmt.Println(e)
	}
}
Set type

A named type implementing the set methods is also defined in go-set

package main

import (
	"fmt"
	"slices"

	set "github.com/paskozdilar/go-set"
)

func main() {
	m := make(map[int]string)

	set.Insert(m, 420)
	fmt.Println(set.Contains(m, 420)) // true
	set.Remove(m, 420)

	m1 := make(map[int]string)
	m2 := make(map[int]string)

	for i := 0; i < 6; i++ {
		set.Insert(m1, i)
	}
	for i := 3; i < 9; i++ {
		set.Insert(m2, i)
	}

	mu := set.Union(m1, m2)
	eu := set.Elements(mu) // slice containing elements of set
	slices.Sort(eu)
	// 0, 1, 2, 3, 4, 5, 6, 7, 8
	for _, e := range eu {
		fmt.Println(e)
	}

	mi := set.Intersection(m1, m2)
	ei := set.Elements(mi)
	slices.Sort(ei)
	// 3, 4, 5
	for _, e := range ei {
		fmt.Println(e)
	}

	msd := set.SymmetricDifference(m1, m2)
	esd := set.Elements(msd)
	slices.Sort(esd)
	// 0, 1, 2, 6, 7, 8
	for _, e := range esd {
		fmt.Println(e)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[S SetType[K, V], K comparable, V any](s S, k K) bool

func Difference

func Difference[S SetType[K, V], K comparable, V any](sets ...S) S

func Elements

func Elements[S SetType[K, V], K comparable, V any](set S) []K

func From2

func From2[S SetType[K, V], K comparable, V any](it iter.Seq2[K, V]) S

func Insert

func Insert[S SetType[K, V], K comparable, V any](s S, k K)

func Intersection

func Intersection[S SetType[K, V], K comparable, V any](sets ...S) S

func Remove

func Remove[S SetType[K, V], K comparable, V any](s S, k K)

func SymmetricDifference

func SymmetricDifference[S SetType[K, V], K comparable, V any](sets ...S) S

func Union

func Union[S SetType[K, V], K comparable, V any](sets ...S) S

Types

type Set

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

func From

func From[E comparable](it iter.Seq[E]) Set[E]

func New

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

func (Set[E]) Contains

func (s Set[E]) Contains(e E) bool

func (Set[T]) Difference

func (s Set[T]) Difference(sets ...Set[T]) Set[T]

func (Set[T]) Elements

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

func (Set[E]) Insert

func (s Set[E]) Insert(e E)

func (Set[T]) Intersection

func (s Set[T]) Intersection(sets ...Set[T]) Set[T]

func (Set[E]) Remove

func (s Set[E]) Remove(e E)

func (Set[T]) SymmetricDifference

func (s Set[T]) SymmetricDifference(sets ...Set[T]) Set[T]

func (Set[T]) Union

func (s Set[T]) Union(sets ...Set[T]) Set[T]

type SetType

type SetType[K comparable, V any] interface {
	~map[K]V
}

Directories

Path Synopsis
examples
map command
set command

Jump to

Keyboard shortcuts

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