Help us shape the future of Go! Take the 2026 Go Developer Survey

set

package
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 4 Imported by: 0

README

set

import "github.com/gechr/x/set"

Package set provides a generic set backed by a map.

Index

func Sorted

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

Sorted returns the items of s as a slice in ascending order.

Sorted is a function rather than a Set method because it requires T to be ordered, not just comparable.

Example

Sorted turns a Set's indeterminate iteration order into a stable one.

s := set.New("banana", "apple", "cherry")
fmt.Println(set.Sorted(s))

Output:

[apple banana cherry]

func SortedNatural

func SortedNatural[T ~string](s Set[T]) []T

SortedNatural returns the items of s as a slice in natural order, so embedded numbers compare by value (item2 before item10) rather than lexically. See strings.CompareNatural.

SortedNatural is a function rather than a Set method because it requires T to be string-like, not just comparable.

Example

SortedNatural orders embedded numbers by value, so "item2" sorts before "item10".

s := set.New("item10", "item2", "item1")
fmt.Println(set.Sorted(s))
fmt.Println(set.SortedNatural(s))

Output:

[item1 item10 item2]
[item1 item2 item10]

type Set

Set is a set of comparable items backed by a map. Pointer types and structs containing pointer fields are compared using shallow equality. The zero value is nil: read operations work, but Set.Add panics - use New or Collect to create a usable Set.

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

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

Collect returns a Set containing the values of seq.

Example

Collect builds a Set from any iter.Seq, such as slices.Values.

s := set.Collect(slices.Values([]int{3, 1, 3, 2}))
fmt.Println(set.Sorted(s))

Output:

[1 2 3]

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

New returns a Set containing items.

Example
s := set.New("a", "b", "a")
fmt.Println(s.Len())
fmt.Println(s.Contains("b"))
fmt.Println(s.Contains("c"))

Output:

2
true
false

func (Set[T]) Add
func (s Set[T]) Add(items ...T)

Add adds items to s.

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

All returns an iterator over the items of s, in indeterminate order.

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

Clone returns a copy of s.

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

Contains returns whether item is present in s.

func (Set[T]) Delete
func (s Set[T]) Delete(items ...T)

Delete removes items from s.

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

Difference returns a new Set containing the items of s not present in any of others.

Example
a := set.New(1, 2, 3)
b := set.New(2, 4)
fmt.Println(set.Sorted(a.Difference(b)))

Output:

[1 3]

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

Equal returns whether s and other contain the same items.

func (Set[T]) Intersect
func (s Set[T]) Intersect(others ...Set[T]) Set[T]

Intersect returns a new Set containing the items of s present in every one of others.

Example
a := set.New(1, 2, 3)
b := set.New(2, 3, 4)
fmt.Println(set.Sorted(a.Intersect(b)))

Output:

[2 3]

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

Len returns the number of items in s.

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

Slice returns the items of s as a slice, in indeterminate order.

func (Set[T]) SubsetOf
func (s Set[T]) SubsetOf(other Set[T]) bool

SubsetOf returns whether every item in s is present in other.

Example
a := set.New("x", "y")
b := set.New("x", "y", "z")
fmt.Println(a.SubsetOf(b))
fmt.Println(b.SubsetOf(a))

Output:

true
false

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

Union returns a new Set containing the items of s and all others.

Example
a := set.New(1, 2)
b := set.New(2, 3)
fmt.Println(set.Sorted(a.Union(b)))

Output:

[1 2 3]

type SortedSet

SortedSet is a set of ordered items, kept in ascending sorted order at all times: SortedSet.Add preserves sorted order, and combining sets (SortedSet.Union/SortedSet.Intersect/SortedSet.Difference) always yields a sorted result. Unlike Set, SortedSet.Slice and SortedSet.All iterate in deterministic ascending order rather than indeterminate map order.

The zero value is an empty, usable SortedSet.

type SortedSet[T cmp.Ordered] struct {
    // contains filtered or unexported fields
}

func CollectSorted
func CollectSorted[T cmp.Ordered](seq iter.Seq[T]) SortedSet[T]

CollectSorted returns a SortedSet containing the values of seq.

Example

CollectSorted builds a SortedSet from any iter.Seq, such as slices.Values.

s := set.CollectSorted(slices.Values([]int{3, 1, 3, 2}))
fmt.Println(s.Slice())

Output:

[1 2 3]

func NewSorted
func NewSorted[T cmp.Ordered](items ...T) SortedSet[T]

NewSorted returns a SortedSet containing items, sorted ascending with duplicates removed.

Example

SortedSet keeps its items in ascending order at all times, so iteration is deterministic.

s := set.NewSorted(3, 1, 2, 1)
s.Add(0)
s.Delete(2)
fmt.Println(s.Slice())

Output:

[0 1 3]

func (*SortedSet[T]) Add
func (s *SortedSet[T]) Add(items ...T)

Add adds items to s, preserving sorted order and ignoring duplicates.

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

All returns an iterator over the items of s, in ascending order.

Example
s := set.NewSorted("banana", "apple", "cherry")
for item := range s.All() {
    fmt.Println(item)
}

Output:

apple
banana
cherry

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

Clone returns a copy of s.

func (SortedSet[T]) Contains
func (s SortedSet[T]) Contains(item T) bool

Contains returns whether item is present in s.

func (*SortedSet[T]) Delete
func (s *SortedSet[T]) Delete(items ...T)

Delete removes items from s.

func (SortedSet[T]) Difference
func (s SortedSet[T]) Difference(others ...SortedSet[T]) SortedSet[T]

Difference returns a new SortedSet containing the items of s not present in any of others.

func (SortedSet[T]) Equal
func (s SortedSet[T]) Equal(other SortedSet[T]) bool

Equal returns whether s and other contain the same items.

func (SortedSet[T]) Intersect
func (s SortedSet[T]) Intersect(others ...SortedSet[T]) SortedSet[T]

Intersect returns a new SortedSet containing the items of s present in every one of others.

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

Len returns the number of items in s.

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

Slice returns the items of s as a slice, in ascending order.

func (SortedSet[T]) SubsetOf
func (s SortedSet[T]) SubsetOf(other SortedSet[T]) bool

SubsetOf returns whether every item in s is present in other.

func (SortedSet[T]) Union
func (s SortedSet[T]) Union(others ...SortedSet[T]) SortedSet[T]

Union returns a new SortedSet containing the items of s and all others.

Documentation

Overview

Package set provides a generic set backed by a map.

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 items of `s` as a slice in ascending order.

Sorted is a function rather than a Set method because it requires `T` to be ordered, not just comparable.

Example

Sorted turns a Set's indeterminate iteration order into a stable one.

package main

import (
	"fmt"

	"github.com/gechr/x/set"
)

func main() {
	s := set.New("banana", "apple", "cherry")
	fmt.Println(set.Sorted(s))
}
Output:
[apple banana cherry]

func SortedNatural

func SortedNatural[T ~string](s Set[T]) []T

SortedNatural returns the items of `s` as a slice in natural order, so embedded numbers compare by value (`item2` before `item10`) rather than lexically. See github.com/gechr/x/strings.CompareNatural.

SortedNatural is a function rather than a Set method because it requires `T` to be string-like, not just comparable.

Example

SortedNatural orders embedded numbers by value, so "item2" sorts before "item10".

package main

import (
	"fmt"

	"github.com/gechr/x/set"
)

func main() {
	s := set.New("item10", "item2", "item1")
	fmt.Println(set.Sorted(s))
	fmt.Println(set.SortedNatural(s))
}
Output:
[item1 item10 item2]
[item1 item2 item10]

Types

type Set

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

Set is a set of comparable items backed by a map. Pointer types and structs containing pointer fields are compared using shallow equality. The zero value is nil: read operations work, but Set.Add panics - use New or Collect to create a usable Set.

func Collect

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

Collect returns a Set containing the values of `seq`.

Example

Collect builds a Set from any iter.Seq, such as slices.Values.

package main

import (
	"fmt"
	"slices"

	"github.com/gechr/x/set"
)

func main() {
	s := set.Collect(slices.Values([]int{3, 1, 3, 2}))
	fmt.Println(set.Sorted(s))
}
Output:
[1 2 3]

func New

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

New returns a Set containing `items`.

Example
package main

import (
	"fmt"

	"github.com/gechr/x/set"
)

func main() {
	s := set.New("a", "b", "a")
	fmt.Println(s.Len())
	fmt.Println(s.Contains("b"))
	fmt.Println(s.Contains("c"))
}
Output:
2
true
false

func (Set[T]) Add

func (s Set[T]) Add(items ...T)

Add adds `items` to `s`.

func (Set[T]) All

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

All returns an iterator over the items of `s`, in indeterminate order.

func (Set[T]) Clone

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

Clone returns a copy of `s`.

func (Set[T]) Contains

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

Contains returns whether `item` is present in `s`.

func (Set[T]) Delete

func (s Set[T]) Delete(items ...T)

Delete removes `items` from `s`.

func (Set[T]) Difference

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

Difference returns a new Set containing the items of `s` not present in any of `others`.

Example
package main

import (
	"fmt"

	"github.com/gechr/x/set"
)

func main() {
	a := set.New(1, 2, 3)
	b := set.New(2, 4)
	fmt.Println(set.Sorted(a.Difference(b)))
}
Output:
[1 3]

func (Set[T]) Equal

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

Equal returns whether `s` and `other` contain the same items.

func (Set[T]) Intersect

func (s Set[T]) Intersect(others ...Set[T]) Set[T]

Intersect returns a new Set containing the items of `s` present in every one of `others`.

Example
package main

import (
	"fmt"

	"github.com/gechr/x/set"
)

func main() {
	a := set.New(1, 2, 3)
	b := set.New(2, 3, 4)
	fmt.Println(set.Sorted(a.Intersect(b)))
}
Output:
[2 3]

func (Set[T]) Len

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

Len returns the number of items in `s`.

func (Set[T]) Slice

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

Slice returns the items of `s` as a slice, in indeterminate order.

func (Set[T]) SubsetOf

func (s Set[T]) SubsetOf(other Set[T]) bool

SubsetOf returns whether every item in `s` is present in `other`.

Example
package main

import (
	"fmt"

	"github.com/gechr/x/set"
)

func main() {
	a := set.New("x", "y")
	b := set.New("x", "y", "z")
	fmt.Println(a.SubsetOf(b))
	fmt.Println(b.SubsetOf(a))
}
Output:
true
false

func (Set[T]) Union

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

Union returns a new Set containing the items of `s` and all `others`.

Example
package main

import (
	"fmt"

	"github.com/gechr/x/set"
)

func main() {
	a := set.New(1, 2)
	b := set.New(2, 3)
	fmt.Println(set.Sorted(a.Union(b)))
}
Output:
[1 2 3]

type SortedSet

type SortedSet[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

SortedSet is a set of ordered items, kept in ascending sorted order at all times: SortedSet.Add preserves sorted order, and combining sets (SortedSet.Union/SortedSet.Intersect/SortedSet.Difference) always yields a sorted result. Unlike Set, SortedSet.Slice and SortedSet.All iterate in deterministic ascending order rather than indeterminate map order.

The zero value is an empty, usable SortedSet.

func CollectSorted

func CollectSorted[T cmp.Ordered](seq iter.Seq[T]) SortedSet[T]

CollectSorted returns a SortedSet containing the values of `seq`.

Example

CollectSorted builds a SortedSet from any iter.Seq, such as slices.Values.

package main

import (
	"fmt"
	"slices"

	"github.com/gechr/x/set"
)

func main() {
	s := set.CollectSorted(slices.Values([]int{3, 1, 3, 2}))
	fmt.Println(s.Slice())
}
Output:
[1 2 3]

func NewSorted

func NewSorted[T cmp.Ordered](items ...T) SortedSet[T]

NewSorted returns a SortedSet containing `items`, sorted ascending with duplicates removed.

Example

SortedSet keeps its items in ascending order at all times, so iteration is deterministic.

package main

import (
	"fmt"

	"github.com/gechr/x/set"
)

func main() {
	s := set.NewSorted(3, 1, 2, 1)
	s.Add(0)
	s.Delete(2)
	fmt.Println(s.Slice())
}
Output:
[0 1 3]

func (*SortedSet[T]) Add

func (s *SortedSet[T]) Add(items ...T)

Add adds `items` to `s`, preserving sorted order and ignoring duplicates.

func (SortedSet[T]) All

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

All returns an iterator over the items of `s`, in ascending order.

Example
package main

import (
	"fmt"

	"github.com/gechr/x/set"
)

func main() {
	s := set.NewSorted("banana", "apple", "cherry")
	for item := range s.All() {
		fmt.Println(item)
	}
}
Output:
apple
banana
cherry

func (SortedSet[T]) Clone

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

Clone returns a copy of `s`.

func (SortedSet[T]) Contains

func (s SortedSet[T]) Contains(item T) bool

Contains returns whether `item` is present in `s`.

func (*SortedSet[T]) Delete

func (s *SortedSet[T]) Delete(items ...T)

Delete removes `items` from `s`.

func (SortedSet[T]) Difference

func (s SortedSet[T]) Difference(others ...SortedSet[T]) SortedSet[T]

Difference returns a new SortedSet containing the items of `s` not present in any of `others`.

func (SortedSet[T]) Equal

func (s SortedSet[T]) Equal(other SortedSet[T]) bool

Equal returns whether `s` and `other` contain the same items.

func (SortedSet[T]) Intersect

func (s SortedSet[T]) Intersect(others ...SortedSet[T]) SortedSet[T]

Intersect returns a new SortedSet containing the items of `s` present in every one of `others`.

func (SortedSet[T]) Len

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

Len returns the number of items in `s`.

func (SortedSet[T]) Slice

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

Slice returns the items of `s` as a slice, in ascending order.

func (SortedSet[T]) SubsetOf

func (s SortedSet[T]) SubsetOf(other SortedSet[T]) bool

SubsetOf returns whether every item in `s` is present in `other`.

func (SortedSet[T]) Union

func (s SortedSet[T]) Union(others ...SortedSet[T]) SortedSet[T]

Union returns a new SortedSet containing the items of `s` and all `others`.

Jump to

Keyboard shortcuts

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