Skip to content

proposal: container/set: new package to provide a generic set type #69230

@lucas-deangelis

Description

@lucas-deangelis

This proposal is entirely based on the initial proposal and following discussion at #47331 and https://go.dev/blog/range-functions. There was a proposal to reopen this discussion 2 weeks ago (#69033) but from what I understand proposals must include what's actually proposed. I apologize if this is not the proper way to do things.

Proposal details

// Package set defines a Set type that holds a set of elements.
package set

// A Set is a set of elements of some comparable type.
// The zero value of a Set is an empty set ready to use.
type Set[Elem comparable] struct {
	// contains filtered or unexported fields
}

// Add adds elements to a set.
func (s *Set[Elem]) Add(v ...Elem)

// Remove removes elements from a set.
// Elements that are not present are ignored.
func (s *Set[Elem]) Remove(v ...Elem)

// Contains reports whether v is in the set.
func (s *Set[Elem]) Contains(v Elem) bool

// Len returns the number of elements in s.
func (s *Set[Elem]) Len() int

// All is an iterator over the elements of s.
func (s *Set[Elem]) All() iter.Seq[Elem]

// Union constructs a new set containing the union of s1 and s2.
func Union[Elem comparable](s1, s2 Set[Elem]) Set[Elem]

// Intersection constructs a new set containing the intersection of s1 and s2.
func Intersection[Elem comparable](s1, s2 Set[Elem]) Set[Elem]

// Difference constructs a new set containing the elements of s1 that
// are not present in s2.
func Difference[Elem comparable](s1, s2 Set[Elem]) Set[Elem]

This is a partial copy of the API proposed at #47331, with the doc comment modified following #47331 (comment), and a new All function that comes from https://go.dev/blog/range-functions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions