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 ¶
- func Sorted[T cmp.Ordered](s Set[T]) []T
- func Subset[T comparable](a, b Set[T]) bool
- func Superset[T comparable](a, b Set[T]) bool
- type Set
- func (s Set[T]) All() iter.Seq[T]
- func (s Set[T]) Clear()
- func (s Set[T]) Clone() Set[T]
- func (s Set[T]) Contains(v T) bool
- func (s Set[T]) ContainsAll(seq iter.Seq[T]) bool
- func (s Set[T]) ContainsAny(seq iter.Seq[T]) bool
- func (s Set[T]) Delete(v T) bool
- func (s Set[T]) DeleteAll(seq iter.Seq[T]) bool
- func (s Set[T]) DeleteFunc(f func(T) bool) bool
- func (s Set[T]) Difference(o Set[T]) Set[T]
- func (s Set[T]) DifferenceWith(o Set[T])
- func (s Set[T]) Equal(o Set[T]) bool
- func (s Set[T]) Insert(v T) bool
- func (s Set[T]) InsertAll(seq iter.Seq[T]) bool
- func (s Set[T]) Intersection(o Set[T]) Set[T]
- func (s Set[T]) IntersectionWith(o Set[T])
- func (s Set[T]) Intersects(o Set[T]) bool
- func (s Set[T]) Len() int
- func (s Set[T]) Slice() []T
- func (s Set[T]) String() string
- func (s Set[T]) SymmetricDifference(o Set[T]) Set[T]
- func (s Set[T]) SymmetricDifferenceWith(o Set[T])
- func (s Set[T]) Union(o Set[T]) Set[T]
- func (s Set[T]) UnionWith(o Set[T])
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Sorted ¶
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 (Set[T]) All ¶
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 ¶
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]) ContainsAll ¶
ContainsAll reports whether every value produced by seq is a member of s. ContainsAll is nil-safe.
func (Set[T]) ContainsAny ¶
ContainsAny reports whether any value produced by seq is a member of s. ContainsAny is nil-safe.
func (Set[T]) Delete ¶
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 ¶
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 ¶
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 ¶
Difference returns a new set containing the elements of s that are not in o.
func (Set[T]) DifferenceWith ¶
DifferenceWith removes every element of o from s. DifferenceWith is nil-safe since it only ever removes.
func (Set[T]) Insert ¶
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 ¶
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 ¶
Intersection returns a new set containing the elements present in both s and o.
func (Set[T]) IntersectionWith ¶
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 ¶
Intersects reports whether s and o share at least one element. Intersects 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 ¶
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 ¶
SymmetricDifference returns a new set containing the elements that are in exactly one of s or o.
func (Set[T]) SymmetricDifferenceWith ¶
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.