set

package
v0.0.0-...-7e5128f Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

README

set

Package set implements a container that holds a set of elements.

Usage

import "github.com/slewiskelly/x/container/set"
// Set
//
// Implements a container which holds a set of elements.
//
// Reading from a set is safe for concurrent use. However, modification of a set
// is not.
a, b := set.New(1, 2, 3, 4, 5, 6), set.New(4, 5, 6, 7, 8, 9)

// Difference
//
// Returns a new set containing all values in a that are not present in b.
difference := set.Difference(a, b)

// Intersection
//
// Returns a new set containing the intersection of a and b.
intersection := set.Intersection(a, b)

// Union
//
// Union returns a new set containing the union of a and b.
union := set.Union(a, b)

Documentation

Overview

Package set implements a container that holds a set of elements.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set implements a container which holds a set of elements.

Reading from a set is safe for concurrent use. However, modification of a set is not.

func Difference

func Difference[T comparable](a, b *Set[T]) *Set[T]

Difference returns a new set containing all elements in a that are not present in b.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/slewiskelly/x/container/set"
)

func main() {
	a, b := set.New(1, 2, 3, 4, 5, 6), set.New(4, 5, 6, 7, 8, 9)

	difference := set.Difference(a, b)

	fmt.Println(slices.Sorted(difference.Values()))
}
Output:

[1 2 3]

func Intersection

func Intersection[T comparable](a, b *Set[T]) *Set[T]

Intersection returns a new set containing the intersection of a and b.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/slewiskelly/x/container/set"
)

func main() {
	a, b := set.New(1, 2, 3, 4, 5, 6), set.New(4, 5, 6, 7, 8, 9)

	intersection := set.Intersection(a, b)

	fmt.Println(slices.Sorted(intersection.Values()))
}
Output:

[4 5 6]

func New

func New[T comparable](e ...T) *Set[T]

New returns a new set containing the given elements.

func Union

func Union[T comparable](a, b *Set[T]) *Set[T]

Union returns a new set containing the union of a and b.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/slewiskelly/x/container/set"
)

func main() {
	a, b := set.New(1, 2, 3, 4, 5, 6), set.New(4, 5, 6, 7, 8, 9)

	union := set.Union(a, b)

	fmt.Println(slices.Sorted(union.Values()))
}
Output:

[1 2 3 4 5 6 7 8 9]

func (*Set[T]) Add

func (s *Set[T]) Add(e ...T)

Add adds element(s) to the set.

func (*Set[T]) AddSet

func (s *Set[T]) AddSet(o *Set[T])

AddSet adds all elements of the given set to the set.

func (*Set[T]) All

func (s *Set[T]) All() iter.Seq2[int, T]

All returns an iterator over (index, value) pairs of the set.

Elements are yielded in an indeterminate order.

func (*Set[T]) Clear

func (s *Set[T]) Clear()

Clear clears all elements from the set.

func (*Set[T]) Clone

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

Clone returns a shallow copy of the set.

func (*Set[T]) Contains

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

Contains returns true if the given value is contained in the set; otherwise false.

func (*Set[T]) ContainsAll

func (s *Set[T]) ContainsAll(o *Set[T]) bool

ContainsAll returns true if the set contains all of the elements in given set; otherwise false.

func (*Set[T]) ContainsAny

func (s *Set[T]) ContainsAny(o *Set[T]) bool

ContainsAny returns true if the set contains any of the elements in the given set; otherwise false.

func (*Set[T]) Equal

func (s *Set[T]) Equal(o *Set[T]) bool

Equal returns true if the set is equal to the given set.

Sets are considered equal if they are of the same length and all elements are equal.

func (*Set[T]) Filter

func (s *Set[T]) Filter(keep func(T) bool)

Filter removes any elements from the set in which the given function returns false.

func (*Set[T]) ForEach

func (s *Set[T]) ForEach(fn func(T))

ForEach executes the given function for each element of the set.

func (*Set[T]) Len

func (s *Set[T]) Len() int

Len returns the number of elements in the set.

func (*Set[T]) Remove

func (s *Set[T]) Remove(e ...T)

Remove removes the given elements from the set.

func (*Set[T]) RemoveSet

func (s *Set[T]) RemoveSet(o *Set[T])

RemoveSet removes the elements of the given set from the set.

func (*Set[T]) String

func (s *Set[T]) String() string

String returns a string representation of the set.

func (*Set[T]) Values

func (s *Set[T]) Values() iter.Seq[T]

Values returns an iterator over elements of the set.

Elements are yielded in an indeterminate order.

Jump to

Keyboard shortcuts

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