Documentation
¶
Overview ¶
Package setz provides generic set data structures and operations. It offers both a Set type with methods for fluent API usage and standalone functions for interoperability with other packages in the henry library.
Index ¶
- func Contains[T comparable](s Set[T], element T) bool
- func IsDisjoint[T comparable](a, b Set[T]) bool
- func IsSubset[T comparable](a, b Set[T]) bool
- func ToSlice[T comparable](s Set[T]) []T
- type Set
- func Difference[T comparable](first Set[T], others ...Set[T]) Set[T]
- func FromMap[T comparable](m Set[T]) Set[T]
- func FromSlice[T comparable](slice []T) Set[T]
- func Intersection[T comparable](sets ...Set[T]) Set[T]
- func Map[T comparable, U comparable](s Set[T], mapper func(T) U) Set[U]
- func New[T comparable](elements ...T) Set[T]
- func Union[T comparable](sets ...Set[T]) Set[T]
- func (s Set[T]) Add(elements ...T) Set[T]
- func (s Set[T]) Clear()
- func (s Set[T]) Contains(element T) bool
- func (s Set[T]) ContainsAll(elements ...T) bool
- func (s Set[T]) Copy() Set[T]
- func (s Set[T]) Difference(other Set[T]) Set[T]
- func (s Set[T]) Filter(predicate func(T) bool) Set[T]
- func (s Set[T]) Intersection(other Set[T]) Set[T]
- func (s Set[T]) IsDisjoint(other Set[T]) bool
- func (s Set[T]) IsEmpty() bool
- func (s Set[T]) IsEqual(other Set[T]) bool
- func (s Set[T]) IsProperSubset(other Set[T]) bool
- func (s Set[T]) IsProperSuperset(other Set[T]) bool
- func (s Set[T]) IsSubset(other Set[T]) bool
- func (s Set[T]) IsSuperset(other Set[T]) bool
- func (s Set[T]) Len() int
- func (s Set[T]) Pop() (T, bool)
- func (s Set[T]) Remove(elements ...T) Set[T]
- func (s Set[T]) String() string
- func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]
- func (s Set[T]) ToSlice() []T
- func (s Set[T]) Union(other Set[T]) Set[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](s Set[T], element T) bool
Contains returns true if the element is in the set. Standalone function version.
func IsDisjoint ¶
func IsDisjoint[T comparable](a, b Set[T]) bool
IsDisjoint returns true if a and b have no elements in common. Standalone function version.
func IsSubset ¶
func IsSubset[T comparable](a, b Set[T]) bool
IsSubset returns true if all elements of a are in b. Standalone function version.
func ToSlice ¶
func ToSlice[T comparable](s Set[T]) []T
ToSlice returns all elements from a set as a slice. Standalone function version.
Types ¶
type Set ¶
type Set[T comparable] map[T]struct{}
Set represents a mathematical set of unique elements. It is implemented as a type alias of map[T]struct{} for O(1) operations. The Set is mutable - methods like Add, Remove, and Clear modify the set. Operations like Union, Intersection return new sets (immutable results).
func Difference ¶
func Difference[T comparable](first Set[T], others ...Set[T]) Set[T]
Difference returns a new set with elements in the first set but not in others. Standalone function version.
func FromMap ¶
func FromMap[T comparable](m Set[T]) Set[T]
FromMap creates a Set from an existing map[T]struct{}. This allows interoperability with slicez.Set() output.
Example:
m := map[string]struct{}{"a": {}, "b": {}}
s := setz.FromMap(m) // Set with "a", "b"
func FromSlice ¶
func FromSlice[T comparable](slice []T) Set[T]
FromSlice creates a Set from a slice, removing duplicates.
Example:
s := setz.FromSlice([]int{1, 2, 2, 3, 3, 3})
// s contains {1, 2, 3}
func Intersection ¶
func Intersection[T comparable](sets ...Set[T]) Set[T]
Intersection returns a new set with elements common to all sets. Standalone function version.
func Map ¶
func Map[T comparable, U comparable](s Set[T], mapper func(T) U) Set[U]
Map returns a new set by applying a function to each element.
func New ¶
func New[T comparable](elements ...T) Set[T]
New creates a new empty Set with optional initial elements.
Example:
s := setz.New[int]() // Empty set
s := setz.New(1, 2, 3) // Set with elements 1, 2, 3
s := setz.New("a", "b", "c") // Set of strings
func Union ¶
func Union[T comparable](sets ...Set[T]) Set[T]
Union returns a new set with all elements from the given sets. Standalone function version.
func (Set[T]) Add ¶
Add adds elements to the set. Returns the set for chaining.
Example:
s := setz.New[int]().Add(1, 2, 3).Add(4)
func (Set[T]) Contains ¶
Contains returns true if the element is in the set.
Example:
s := setz.New(1, 2, 3) s.Contains(2) // true s.Contains(5) // false
func (Set[T]) ContainsAll ¶
ContainsAll returns true if all elements are in the set.
func (Set[T]) Difference ¶
Difference returns a new set with elements in s but not in other.
Example:
s1 := setz.New(1, 2, 3, 4)
s2 := setz.New(2, 4)
s1.Difference(s2) // {1, 3}
func (Set[T]) Intersection ¶
Intersection returns a new set with elements common to both sets.
Example:
s1 := setz.New(1, 2, 3, 4)
s2 := setz.New(2, 3, 4, 5)
s1.Intersection(s2) // {2, 3, 4}
func (Set[T]) IsDisjoint ¶
IsDisjoint returns true if s and other have no elements in common.
Example:
s1 := setz.New(1, 2) s2 := setz.New(3, 4) s1.IsDisjoint(s2) // true
func (Set[T]) IsProperSubset ¶
IsProperSubset returns true if s is a subset of other and s ≠ other.
func (Set[T]) IsProperSuperset ¶
IsProperSuperset returns true if s is a superset of other and s ≠ other.
func (Set[T]) IsSubset ¶
IsSubset returns true if all elements of s are in other.
Example:
s1 := setz.New(1, 2) s2 := setz.New(1, 2, 3) s1.IsSubset(s2) // true s2.IsSubset(s1) // false
func (Set[T]) IsSuperset ¶
IsSuperset returns true if s contains all elements of other.
Example:
s1 := setz.New(1, 2, 3) s2 := setz.New(1, 2) s1.IsSuperset(s2) // true
func (Set[T]) Pop ¶
Pop removes and returns an arbitrary element from the set. Returns the zero value and false if the set is empty.
func (Set[T]) Remove ¶
Remove removes elements from the set. Returns the set for chaining.
Example:
s := setz.New(1, 2, 3, 4).Remove(2, 3) // s contains {1, 4}
func (Set[T]) String ¶
String returns a string representation of the set. Note: Element order is not guaranteed.
func (Set[T]) SymmetricDifference ¶
SymmetricDifference returns a new set with elements in either set but not both. This is equivalent to (s ∪ other) - (s ∩ other).
Example:
s1 := setz.New(1, 2, 3)
s2 := setz.New(2, 3, 4)
s1.SymmetricDifference(s2) // {1, 4}