set

package
v1.117.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package set provides a generic set for comparable types.

Set's underlying type is map[T]struct{} -- the same representation the Go working group chose for the proposed container/set.Set (golang/go#69230) -- so it can be ranged over and used anywhere a map is accepted, and an existing map[T]struct{} (or a named type over it) converts to a Set at zero cost. The zero value supports read operations (Contains, Len, All, Equal, String); construct via Of, Collect, or make before calling a mutator, the same as an uninitialized Go map:

var z Set[string]
z.Contains("x") // false, no panic
z.Insert("x")   // panics: assignment to entry in nil map

Set is not safe for concurrent use.

Coming from deckarep/golang-set, the migration is largely a rename:

Add          -> Insert
Remove       -> Delete
Cardinality  -> Len
Iter/Each    -> All
Example (Algebra)
package main

import (
	"fmt"

	"github.com/getoutreach/gobox/pkg/set"
)

func main() {
	a := set.Of("x", "y")
	b := set.Of("y", "z")
	fmt.Println(set.Sorted(a.Union(b)))
	fmt.Println(set.Sorted(a.Intersection(b)))
	fmt.Println(set.Sorted(a.Difference(b)))
}
Output:
[x y z]
[y]
[x]
Example (Construct)
package main

import (
	"fmt"

	"github.com/getoutreach/gobox/pkg/set"
)

func main() {
	s := set.Of("x", "y", "z")
	fmt.Println(s.Len())
}
Output:
3
Example (Iterate)
package main

import (
	"fmt"

	"github.com/getoutreach/gobox/pkg/set"
)

func main() {
	s := set.Of("x")
	for v := range s.All() {
		fmt.Println(v)
	}
}
Output:
x
Example (Membership)
package main

import (
	"fmt"

	"github.com/getoutreach/gobox/pkg/set"
)

func main() {
	s := set.Of("x", "y")
	fmt.Println(s.Contains("x"))
	fmt.Println(s.Contains("z"))
}
Output:
true
false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sorted

func Sorted[T cmp.Ordered](s Set[T]) []T

Sorted returns the elements of s as an ascending sorted slice. It returns nil if s is empty.

func Subset

func Subset[T comparable](a, b Set[T]) bool

Subset reports whether every element of a is also in b.

func Superset

func Superset[T comparable](a, b Set[T]) bool

Superset reports whether every element of b is also in a.

Types

type Set

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

Set is a generic set of comparable elements, backed by a map[T]struct{}. See the package doc for the nil-safety rules: reads are nil-safe, but mutators that grow the set panic on a nil Set, just like an uninitialized map.

func Collect

func Collect[T comparable](seq iter.Seq[T]) Set[T]

Collect returns a new Set containing the values produced by seq, deduplicating them.

func Of

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

Of returns a new Set containing the given values.

func (Set[T]) All

func (s Set[T]) All() iter.Seq[T]

All returns an iterator over the elements of s, in unspecified order. All is nil-safe.

func (Set[T]) Clear

func (s Set[T]) Clear()

Clear removes all elements from s. Clear is a nil-safe no-op if s is nil.

func (Set[T]) Clone

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

Clone returns a shallow copy of s. Unlike maps.Clone, Clone always returns a non-nil set, even if s is nil.

func (Set[T]) Contains

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

Contains reports whether v is a member of s. Contains is nil-safe.

func (Set[T]) ContainsAll

func (s Set[T]) ContainsAll(seq iter.Seq[T]) bool

ContainsAll reports whether every value produced by seq is a member of s. ContainsAll is nil-safe.

func (Set[T]) ContainsAny

func (s Set[T]) ContainsAny(seq iter.Seq[T]) bool

ContainsAny reports whether any value produced by seq is a member of s. ContainsAny is nil-safe.

func (Set[T]) Delete

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

Delete removes v from s and reports whether it was present. Delete is a nil-safe no-op (not a panic) if s is nil, since it only ever removes.

func (Set[T]) DeleteAll

func (s Set[T]) DeleteAll(seq iter.Seq[T]) bool

DeleteAll removes every value produced by seq from s and reports whether the set shrank. DeleteAll is a nil-safe no-op if s is nil.

func (Set[T]) DeleteFunc

func (s Set[T]) DeleteFunc(f func(T) bool) bool

DeleteFunc removes every element for which f reports true and reports whether the set shrank. DeleteFunc is a nil-safe no-op if s is nil.

func (Set[T]) Difference

func (s Set[T]) Difference(o Set[T]) Set[T]

Difference returns a new set containing the elements of s that are not in o.

func (Set[T]) DifferenceWith

func (s Set[T]) DifferenceWith(o Set[T])

DifferenceWith removes every element of o from s. DifferenceWith is nil-safe since it only ever removes.

func (Set[T]) Equal

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

Equal reports whether s and o contain the same elements. Equal is nil-safe.

func (Set[T]) Insert

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

Insert adds v to s and reports whether the set grew (i.e. v was not already present). Insert panics if s is nil, the same as assigning into a nil map.

func (Set[T]) InsertAll

func (s Set[T]) InsertAll(seq iter.Seq[T]) bool

InsertAll adds every value produced by seq to s and reports whether the set grew. InsertAll panics if s is nil and seq produces at least one value.

func (Set[T]) Intersection

func (s Set[T]) Intersection(o Set[T]) Set[T]

Intersection returns a new set containing the elements present in both s and o.

func (Set[T]) IntersectionWith

func (s Set[T]) IntersectionWith(o Set[T])

IntersectionWith removes every element of s that is not also in o. IntersectionWith is nil-safe since it only ever removes.

func (Set[T]) Intersects

func (s Set[T]) Intersects(o Set[T]) bool

Intersects reports whether s and o share at least one element. Intersects is nil-safe.

func (Set[T]) Len

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

Len returns the number of elements in s. Len is nil-safe.

func (Set[T]) Slice

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

Slice returns the elements of s as a slice, in unspecified order. It returns nil if s is empty. Slice is nil-safe.

func (Set[T]) String

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

String returns a "{a, b, c}"-style representation of s. Elements are sorted by their fmt.Sprint representation, so the result is deterministic (though not necessarily in numeric order for numeric T). String is nil-safe.

func (Set[T]) SymmetricDifference

func (s Set[T]) SymmetricDifference(o Set[T]) Set[T]

SymmetricDifference returns a new set containing the elements that are in exactly one of s or o.

func (Set[T]) SymmetricDifferenceWith

func (s Set[T]) SymmetricDifferenceWith(o Set[T])

SymmetricDifferenceWith updates s in place to contain the elements that are in exactly one of s or o. SymmetricDifferenceWith panics if s is nil and o is non-empty.

func (Set[T]) Union

func (s Set[T]) Union(o Set[T]) Set[T]

Union returns a new set containing every element of s and o.

func (Set[T]) UnionWith

func (s Set[T]) UnionWith(o Set[T])

UnionWith adds every element of o to s in place. UnionWith panics if s is nil and o is non-empty.

Jump to

Keyboard shortcuts

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